@ -1,14 +1,16 @@
import requests
import datetime
import xml.etree.ElementTree as ET
import sys
import sys
import time
import time
import datetime
import xml.etree.ElementTree as ET
import requests
# Prepare and send the propfind request return xml string
# Prepare and send the propfind request return xml string
def propfind ( path , auth ) :
def propfind ( path , auth ) :
headers = { " Depth " : " 1 " }
headers = { " Depth " : " 1 " }
# This body returns only the timelastmodified and the fileid variable
# This body returns only the timelastmodified and the fileid variable
data = " <d:propfind xmlns:d= \" DAV: \" xmlns:oc= \" http://owncloud.org/ns \" xmlns:nc= \" http://nextcloud.org/ns \" > " + " <d:prop> " + " <d:getlastmodified /> " + " <oc:fileid /> " + " </d:prop> " + " </d:propfind> "
data = " <d:propfind xmlns:d= \" DAV: \" xmlns:oc= \" http://owncloud.org/ns \" xmlns:nc= \" http://nextcloud.org/ns \" > " + " <d:prop> " + " <d:getlastmodified /> " + " <oc:fileid /> " + " </d:prop> " + " </d:propfind> "
req = r = requests . Request ( " PROPFIND " , path , headers = headers , auth = auth , data = data )
req = r = requests . Request ( " PROPFIND " , path , headers = headers , auth = auth , data = data )
prepped = req . prepare ( )
prepped = req . prepare ( )
@ -16,6 +18,7 @@ def propfind(path,auth):
resp = s . send ( prepped )
resp = s . send ( prepped )
return resp . text
return resp . text
# Function iterates through the given xml which contains all files and folders in the provided path
# Function iterates through the given xml which contains all files and folders in the provided path
def search_folder ( requestreturn ) :
def search_folder ( requestreturn ) :
# List to collect path of folders stored in path
# List to collect path of folders stored in path
@ -32,10 +35,10 @@ def search_folder(requestreturn):
# Here we can get the filpath out of href and get further information in prop (lastimemodified, typeoffile, etc.)
# Here we can get the filpath out of href and get further information in prop (lastimemodified, typeoffile, etc.)
for p in resp :
for p in resp :
# In case p.text is not none, it contains the filepath
# In case p.text is not none, it contains the filepath
if not ( p . text is None ) :
if not ( p . text is None ) :
if ( p . text [ - 1 ] == ' / ' ) :
if ( p . text [ - 1 ] == ' / ' ) :
# If the current object is a folder, check it its not first folder
# If the current object is a folder, check it its not first folder
if not ( firstfolder ) :
if not ( firstfolder ) :
innerfolders . append ( p . text )
innerfolders . append ( p . text )
else :
else :
firstfolder = False
firstfolder = False
@ -43,14 +46,16 @@ def search_folder(requestreturn):
# In case p.text is none, it contains the further information
# In case p.text is none, it contains the further information
else :
else :
for t in p . findall ( ' .//{DAV:}getlastmodified ' ) :
for t in p . findall ( ' .//{DAV:}getlastmodified ' ) :
#this function converts the given date to unix timestamp
# this function converts the given date to unix timestamp
lastmodified = time . mktime ( datetime . datetime . strptime ( t . text , " % a, %d % b % Y % H: % M: % S GMT " ) . timetuple ( ) )
lastmodified = time . mktime (
#631148400 is the unix timestamp of 01.01.1990 00:00:00, because we know there is no file older than this in our nextcloud system
datetime . datetime . strptime ( t . text , " % a, %d % b % Y % H: % M: % S GMT " ) . timetuple ( ) )
# 631148400 is the unix timestamp of 01.01.1990 00:00:00, because we know there is no file older than this in our nextcloud system
if lastmodified < 631148400 :
if lastmodified < 631148400 :
for fileid in p . findall ( ' .//{http://owncloud.org/ns}fileid ' ) :
for fileid in p . findall ( ' .//{http://owncloud.org/ns}fileid ' ) :
linkswrongtime . append ( fileid . text )
linkswrongtime . append ( fileid . text )
return [ innerfolders , linkswrongtime ]
return [ innerfolders , linkswrongtime ]
# This function returns the fileid of the version of a given fileid with the most current timestamp or None if there are no versions with a timestamp younger than 01.01.1990
# This function returns the fileid of the version of a given fileid with the most current timestamp or None if there are no versions with a timestamp younger than 01.01.1990
def version_check ( xmlfile ) :
def version_check ( xmlfile ) :
tree = ET . ElementTree ( ET . fromstring ( xmlfile ) )
tree = ET . ElementTree ( ET . fromstring ( xmlfile ) )
@ -63,13 +68,14 @@ def version_check(xmlfile):
for resp in tree . findall ( ' .//{DAV:}response ' ) :
for resp in tree . findall ( ' .//{DAV:}response ' ) :
for p in resp :
for p in resp :
# In case p.text is not none, it contains the filepath
# In case p.text is not none, it contains the filepath
if not ( p . text is None ) :
if not ( p . text is None ) :
temp = p . text
temp = p . text
else :
else :
for t in p . findall ( ' .//{DAV:}getlastmodified ' ) :
for t in p . findall ( ' .//{DAV:}getlastmodified ' ) :
if not ( t . text is None ) :
if not ( t . text is None ) :
#this function converts the given date to unix timestamp
# this function converts the given date to unix timestamp
lastmodified = time . mktime ( datetime . datetime . strptime ( t . text , " % a, %d % b % Y % H: % M: % S GMT " ) . timetuple ( ) )
lastmodified = time . mktime (
datetime . datetime . strptime ( t . text , " % a, %d % b % Y % H: % M: % S GMT " ) . timetuple ( ) )
#
#
if lastmodified > most_current_timestamp :
if lastmodified > most_current_timestamp :
most_current_timestamp = lastmodified
most_current_timestamp = lastmodified
@ -83,14 +89,15 @@ def version_check(xmlfile):
else :
else :
return None
return None
def main ( ) :
if __name__ == " __main__ " :
# Enter username and password to enter nextcloud via webdav
# Enter username and password to enter nextcloud via webdav
user = sys . argv [ 1 ]
user = sys . argv [ 1 ]
passw = sys . argv [ 2 ]
passw = sys . argv [ 2 ]
auth = requests . auth . HTTPBasicAuth ( user , passw )
auth = requests . auth . HTTPBasicAuth ( user , passw )
# Prepare the path we want to use
# Prepare the path we want to use
prefix_path = " https://kingsx.cs.uni-saarland.de "
prefix_path = " https://kingsx.cs.uni-saarland.de "
mainpath = " /remote.php/dav/files/ " + user + " /Testrequests/ "
mainpath = " /remote.php/dav/files/ " + user + " /Testrequests/ "
# List of all folderpaths we need to enter
# List of all folderpaths we need to enter
folders = [ mainpath ]
folders = [ mainpath ]
# List of all fileids with wrong time
# List of all fileids with wrong time
@ -100,7 +107,7 @@ def main():
while folders :
while folders :
path_suffix = folders . pop ( 0 )
path_suffix = folders . pop ( 0 )
path = prefix_path + str ( path_suffix )
path = prefix_path + str ( path_suffix )
r = propfind ( path , auth )
r = propfind ( path , auth )
res = search_folder ( r )
res = search_folder ( r )
# Append all found folders and files with wrong timestamps to global list
# Append all found folders and files with wrong timestamps to global list
folders = folders + res [ 0 ]
folders = folders + res [ 0 ]
@ -110,7 +117,5 @@ def main():
fileid = wrongtime . pop ( 0 )
fileid = wrongtime . pop ( 0 )
version_suffix = " /remote.php/dav/versions/ " + user + " /versions/ " + fileid
version_suffix = " /remote.php/dav/versions/ " + user + " /versions/ " + fileid
version_path = prefix_path + version_suffix
version_path = prefix_path + version_suffix
versions = propfind ( version_path , auth )
versions = propfind ( version_path , auth )
print ( version_check ( versions ) )
print ( version_check ( versions ) )
main ( )
xxxxxxxxxx