OCWebDAVClient.m 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. //
  2. // OCWebDAVClient.m
  3. // OCWebDAVClient
  4. //
  5. // This class is based in https://github.com/zwaldowski/DZWebDAVClient. Copyright (c) 2012 Zachary Waldowski, Troy Brant, Marcus Rohrmoser, and Sam Soffes.
  6. //
  7. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. //
  26. // Add : getNotificationServer & setNotificationServer
  27. // Add : getUserProfileServer
  28. // Add : Support for Favorite
  29. // Add : getActivityServer
  30. //
  31. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  32. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  33. //
  34. #import "OCWebDAVClient.h"
  35. #import "OCFrameworkConstants.h"
  36. #import "OCCommunication.h"
  37. #import "UtilsFramework.h"
  38. #import "NSString+Encode.h"
  39. #import "OCConstants.h"
  40. #define k_api_user_url_xml @"index.php/ocs/cloud/user"
  41. #define k_api_user_url_json @"index.php/ocs/cloud/user?format=json"
  42. #define k_server_information_json @"status.php"
  43. #define k_api_header_request @"OCS-APIREQUEST"
  44. #define k_group_sharee_type 1
  45. NSString const *OCWebDAVContentTypeKey = @"getcontenttype";
  46. NSString const *OCWebDAVETagKey = @"getetag";
  47. NSString const *OCWebDAVCTagKey = @"getctag";
  48. NSString const *OCWebDAVCreationDateKey = @"creationdate";
  49. NSString const *OCWebDAVModificationDateKey = @"modificationdate";
  50. NSString *const NCResource = @"<d:displayname/>"
  51. "<d:getcontenttype/>"
  52. "<d:resourcetype/>"
  53. "<d:getcontentlength/>"
  54. "<d:getlastmodified/>"
  55. "<d:creationdate/>"
  56. "<d:getetag/>"
  57. "<d:quota-used-bytes/>"
  58. "<d:quota-available-bytes/>"
  59. "<permissions xmlns=\"http://owncloud.org/ns\"/>"
  60. "<id xmlns=\"http://owncloud.org/ns\"/>"
  61. "<fileid xmlns=\"http://owncloud.org/ns\"/>"
  62. "<size xmlns=\"http://owncloud.org/ns\"/>"
  63. "<favorite xmlns=\"http://owncloud.org/ns\"/>"
  64. "<is-encrypted xmlns=\"http://nextcloud.org/ns\"/>"
  65. "<mount-type xmlns=\"http://nextcloud.org/ns\"/>"
  66. "<owner-id xmlns=\"http://owncloud.org/ns\"/>"
  67. "<owner-display-name xmlns=\"http://owncloud.org/ns\"/>"
  68. "<comments-unread xmlns=\"http://owncloud.org/ns\"/>"
  69. "<has-preview xmlns=\"http://nextcloud.org/ns\"/>"
  70. "<trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>"
  71. "<trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>"
  72. "<trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>";
  73. @interface OCWebDAVClient()
  74. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth onCommunication:
  75. (OCCommunication *)sharedOCCommunication
  76. success:(void(^)(NSHTTPURLResponse *, id))success
  77. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure;
  78. @end
  79. @implementation OCWebDAVClient
  80. - (id) init {
  81. self = [super init];
  82. if (self != nil) {
  83. self.defaultHeaders = [NSMutableDictionary new];
  84. }
  85. return self;
  86. }
  87. - (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
  88. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
  89. [self.defaultHeaders setObject:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString: basicAuthCredentials]] forKey:@"Authorization"];
  90. }
  91. - (void)setAuthorizationHeaderWithCookie:(NSString *) cookieString {
  92. [self.defaultHeaders setObject:cookieString forKey:@"Cookie"];
  93. }
  94. - (void)setAuthorizationHeaderWithToken:(NSString *)token {
  95. [self.defaultHeaders setObject:token forKey:@"Authorization"];
  96. }
  97. - (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
  98. [self.defaultHeaders setObject:value forKey:header];
  99. }
  100. - (void)setUserAgent:(NSString *)userAgent{
  101. [self.defaultHeaders setObject:userAgent forKey:@"User-Agent"];
  102. }
  103. - (OCHTTPRequestOperation *)mr_operationWithRequest:(NSMutableURLRequest *)request onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString*)token success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error, NSString *token))failure {
  104. //If is not nil is a redirection so we keep the original url server
  105. if (!self.originalUrlServer) {
  106. self.originalUrlServer = [request.URL absoluteString];
  107. }
  108. if (sharedOCCommunication.isCookiesAvailable) {
  109. //We add the cookies of that URL
  110. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  111. } else {
  112. [UtilsFramework deleteAllCookies];
  113. }
  114. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  115. if (!error) {
  116. success((NSHTTPURLResponse*)response,responseObject, token);
  117. } else {
  118. failure((NSHTTPURLResponse*)response, responseObject, error, token);
  119. }
  120. }];
  121. return operation;
  122. }
  123. - (OCHTTPRequestOperation *)mr_operationWithRequest:(NSMutableURLRequest *)request onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  124. //If is not nil is a redirection so we keep the original url server
  125. if (!self.originalUrlServer) {
  126. self.originalUrlServer = [request.URL absoluteString];
  127. }
  128. if (sharedOCCommunication.isCookiesAvailable) {
  129. //We add the cookies of that URL
  130. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  131. } else {
  132. [UtilsFramework deleteAllCookies];
  133. }
  134. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  135. if (!error) {
  136. success((NSHTTPURLResponse*)response,responseObject);
  137. } else {
  138. failure((NSHTTPURLResponse*)response, responseObject, error);
  139. }
  140. }];
  141. return operation;
  142. }
  143. - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters timeout:(NSTimeInterval)timeout {
  144. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  145. [request setAllHTTPHeaderFields:self.defaultHeaders];
  146. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  147. [request setTimeoutInterval: timeout];
  148. return request;
  149. }
  150. - (NSMutableURLRequest *)sharedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters timeout:(NSTimeInterval)timeout {
  151. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  152. [request setAllHTTPHeaderFields:self.defaultHeaders];
  153. //NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  154. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  155. [request setTimeoutInterval: k_timeout_webdav];
  156. //Header for use the OC API CALL
  157. NSString *ocs_apiquests = @"true";
  158. [request setValue:ocs_apiquests forHTTPHeaderField:k_api_header_request];
  159. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  160. [request addValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];
  161. return request;
  162. }
  163. - (void)movePath:(NSString *)source toPath:(NSString *)destination
  164. onCommunication:(OCCommunication *)sharedOCCommunication
  165. success:(void(^)(NSHTTPURLResponse *, id))success
  166. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  167. _requestMethod = @"MOVE";
  168. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:source parameters:nil timeout:k_timeout_webdav];
  169. [request setValue:destination forHTTPHeaderField:@"Destination"];
  170. [request setValue:@"T" forHTTPHeaderField:@"Overwrite"];
  171. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  172. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  173. [operation resume];
  174. }
  175. - (void)deletePath:(NSString *)path
  176. onCommunication:(OCCommunication *)sharedOCCommunication
  177. success:(void(^)(NSHTTPURLResponse *, id))success
  178. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  179. _requestMethod = @"DELETE";
  180. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  181. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  182. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  183. [operation resume];
  184. }
  185. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth onCommunication:
  186. (OCCommunication *)sharedOCCommunication
  187. success:(void(^)(NSHTTPURLResponse *, id))success
  188. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  189. NSParameterAssert(success);
  190. _requestMethod = @"PROPFIND";
  191. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  192. [request setValue: depth forHTTPHeaderField: @"Depth"];
  193. NSString *body = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><d:prop>";
  194. body = [body stringByAppendingString:NCResource];
  195. body = [body stringByAppendingString:@"</d:prop></d:propfind>"];
  196. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  197. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  198. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  199. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  200. [operation resume];
  201. }
  202. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth withUserSessionToken:(NSString*)token onCommunication:
  203. (OCCommunication *)sharedOCCommunication
  204. success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success
  205. failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *, NSString *token))failure {
  206. NSParameterAssert(success);
  207. _requestMethod = @"PROPFIND";
  208. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  209. [request setValue: depth forHTTPHeaderField: @"Depth"];
  210. NSString *body = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><d:prop>";
  211. body = [body stringByAppendingString:NCResource];
  212. body = [body stringByAppendingString:@"</d:prop></d:propfind>"];
  213. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  214. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  215. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  216. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  217. [operation resume];
  218. }
  219. - (void)propertiesOfPath:(NSString *)path
  220. onCommunication: (OCCommunication *)sharedOCCommunication
  221. success:(void(^)(NSHTTPURLResponse *, id ))success
  222. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  223. [self mr_listPath:path depth:@"0" onCommunication:sharedOCCommunication success:success failure:failure];
  224. }
  225. - (void)listPath:(NSString *)path depth:(NSString *)depth
  226. onCommunication:(OCCommunication *)sharedOCCommunication
  227. success:(void(^)(NSHTTPURLResponse *, id))success
  228. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  229. [self mr_listPath:path depth:depth onCommunication:sharedOCCommunication success:success failure:failure];
  230. }
  231. - (void)listPath:(NSString *)path depth:(NSString *)depth
  232. onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token
  233. success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success
  234. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  235. [self mr_listPath:path depth:depth withUserSessionToken:token onCommunication:sharedOCCommunication success:success failure:failure];
  236. }
  237. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName depth:(NSString *)depth lteDateLastModified:(NSString *)lteDateLastModified gteDateLastModified:(NSString *)gteDateLastModified contentType:(NSArray *)contentType user:(NSString *)user userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  238. NSString *body = @"";
  239. NSString *whereType = @"";
  240. NSString *whereDate = @"";
  241. NSParameterAssert(success);
  242. _requestMethod = @"SEARCH";
  243. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_search];
  244. if (contentType && lteDateLastModified && gteDateLastModified) {
  245. body = [NSString stringWithFormat: @""
  246. "<?xml version=\"1.0\"?>"
  247. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  248. "<d:basicsearch>"
  249. "<d:select>"
  250. "<d:prop>"
  251. "%@"
  252. "</d:prop>"
  253. "</d:select>"
  254. "<d:from>"
  255. "<d:scope>"
  256. "<d:href>/files/%@%@</d:href>"
  257. "<d:depth>infinity</d:depth>"
  258. "</d:scope>"
  259. "</d:from>"
  260. "<d:orderby>"
  261. "<d:order>"
  262. "<d:prop><d:getlastmodified/></d:prop>"
  263. "<d:descending/>"
  264. "</d:order>"
  265. "<d:order>"
  266. "<d:prop><d:displayname/></d:prop>"
  267. "<d:descending/>"
  268. "</d:order>"
  269. "</d:orderby>"
  270. "<d:where><d:and><d:or>", NCResource, userID, folder];
  271. for (NSString *type in contentType) {
  272. whereType = [NSString stringWithFormat: @"%@<d:like><d:prop><d:getcontenttype/></d:prop><d:literal>%@</d:literal></d:like>", whereType, type];
  273. }
  274. body = [NSString stringWithFormat: @"%@%@</d:or><d:or>", body, whereType];
  275. whereDate = [NSString stringWithFormat: @"%@<d:and><d:lte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:lte><d:gte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:gte></d:and>", whereDate, lteDateLastModified, gteDateLastModified];
  276. /*
  277. if (gteDateLastModified != nil && lteDateLastModified == nil) {
  278. whereDate = [NSString stringWithFormat: @"%@<d:gte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:gte>", whereDate, gteDateLastModified];
  279. } else if (gteDateLastModified != nil && lteDateLastModified != nil) {
  280. whereDate = [NSString stringWithFormat: @"%@<d:and><d:lte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:lte><d:gte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:gte></d:and>", whereDate, lteDateLastModified, gteDateLastModified];
  281. }
  282. */
  283. body = [NSString stringWithFormat: @"%@%@</d:or></d:and></d:where></d:basicsearch></d:searchrequest>", body, whereDate];
  284. } else {
  285. body = [NSString stringWithFormat: @""
  286. "<?xml version=\"1.0\"?>"
  287. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  288. "<d:basicsearch>"
  289. "<d:select>"
  290. "<d:prop>"
  291. "%@"
  292. "</d:prop>"
  293. "</d:select>"
  294. "<d:from>"
  295. "<d:scope>"
  296. "<d:href>/files/%@%@</d:href>"
  297. "<d:depth>infinity</d:depth>"
  298. "</d:scope>"
  299. "</d:from>"
  300. "<d:where>"
  301. "<d:like>"
  302. "<d:prop><d:displayname/></d:prop>"
  303. "<d:literal>%@</d:literal>"
  304. "</d:like>"
  305. "</d:where>"
  306. "</d:basicsearch>"
  307. "</d:searchrequest>", NCResource, userID, folder, fileName];
  308. }
  309. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  310. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  311. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  312. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  313. [operation resume];
  314. }
  315. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName dateLastModified:(NSString *)dateLastModified numberOfItem:(NSInteger)numberOfItem userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  316. NSString *body = @"";
  317. NSParameterAssert(success);
  318. _requestMethod = @"SEARCH";
  319. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_search];
  320. body = [NSString stringWithFormat: @""
  321. "<?xml version=\"1.0\"?>"
  322. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  323. "<d:basicsearch>"
  324. "<d:select>"
  325. "<d:prop>"
  326. "%@"
  327. "</d:prop>"
  328. "</d:select>"
  329. "<d:from>"
  330. "<d:scope>"
  331. "<d:href>/files/%@%@</d:href>"
  332. "<d:depth>1</d:depth>"
  333. "</d:scope>"
  334. "</d:from>"
  335. /*
  336. "<d:orderby>"
  337. "<d:order>"
  338. "<d:prop><d:displayname/></d:prop>"
  339. "<d:descending/>"
  340. "</d:order>"
  341. "<d:order>"
  342. "<d:prop><d:getlastmodified/></d:prop>"
  343. "<d:descending/>"
  344. "</d:order>"
  345. "</d:orderby>"
  346. */
  347. "<d:where>"
  348. "<d:like>"
  349. "<d:prop><d:displayname/></d:prop>"
  350. "<d:literal>%@</d:literal>"
  351. "</d:like>"
  352. "</d:where>"
  353. /*
  354. "<d:where><d:and><d:or>"
  355. "<d:gte>"
  356. "<d:prop><d:getlastmodified/></d:prop>"
  357. "<d:literal>%@</d:literal>"
  358. "</d:gte>"
  359. "</d:or></d:and></d:where>"
  360. "<d:limit>"
  361. "<d:nresults>%@</d:nresults>"
  362. "</d:limit>"
  363. */
  364. "</d:basicsearch>"
  365. "</d:searchrequest>", NCResource, userID, folder, fileName]; //, [@(numberOfItem) stringValue]];
  366. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  367. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  368. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  369. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  370. [operation resume];
  371. }
  372. - (void)settingFavorite:(NSString * _Nonnull)path favorite:(BOOL)favorite onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  373. NSParameterAssert(success);
  374. _requestMethod = @"PROPPATCH";
  375. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  376. NSString *body;
  377. body = [NSString stringWithFormat: @""
  378. "<?xml version=\"1.0\"?>"
  379. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  380. "<d:set>"
  381. "<d:prop>"
  382. "<oc:favorite>%i</oc:favorite>"
  383. "</d:prop>"
  384. "</d:set>"
  385. "</d:propertyupdate>", (favorite ? 1 : 0)];
  386. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  387. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  388. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  389. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  390. [operation resume];
  391. }
  392. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder user:(NSString *)user userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  393. NSParameterAssert(success);
  394. _requestMethod = @"REPORT";
  395. userID = [userID stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  396. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:[NSString stringWithFormat:@"%@/files/%@%@", path, userID, folder] parameters:nil timeout:k_timeout_webdav];
  397. NSString *body = @"<?xml version=\"1.0\"?><oc:filter-files xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><d:prop>";
  398. body = [body stringByAppendingString:NCResource];
  399. body = [body stringByAppendingString:@"</d:prop><oc:filter-rules><oc:favorite>1</oc:favorite></oc:filter-rules></oc:filter-files>"];
  400. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  401. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  402. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  403. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  404. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  405. [operation resume];
  406. }
  407. - (NSURLSessionDownloadTask *)downloadWithSessionPath:(NSString *)remoteSource toPath:(NSString *)localDestination defaultPriority:(BOOL)defaultPriority onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))downloadProgress success:(void(^)(NSURLResponse *response, NSURL *filePath))success failure:(void(^)(NSURLResponse *response, NSError *error))failure{
  408. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil timeout:k_timeout_webdav];
  409. //If is not nil is a redirection so we keep the original url server
  410. if (!self.originalUrlServer) {
  411. self.originalUrlServer = [request.URL absoluteString];
  412. }
  413. //We add the cookies of that URL
  414. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  415. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  416. downloadProgress(progress);
  417. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  418. [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:localDestination] error:nil];
  419. return [NSURL fileURLWithPath:localDestination];
  420. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  421. if (error) {
  422. failure(response, error);
  423. } else {
  424. success(response,filePath);
  425. }
  426. }];
  427. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  428. if (defaultPriority) {
  429. [downloadTask resume];
  430. }
  431. return downloadTask;
  432. }
  433. - (void)checkServer:(NSString *)path onCommunication:
  434. (OCCommunication *)sharedOCCommunication
  435. success:(void(^)(NSHTTPURLResponse *, id))success
  436. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  437. _requestMethod = @"HEAD";
  438. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  439. request.HTTPShouldHandleCookies = false;
  440. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  441. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  442. [operation resume];
  443. }
  444. - (void)makeCollection:(NSString *)path onCommunication:
  445. (OCCommunication *)sharedOCCommunication
  446. success:(void(^)(NSHTTPURLResponse *, id))success
  447. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  448. _requestMethod = @"MKCOL";
  449. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  450. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  451. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  452. [operation resume];
  453. }
  454. - (NSURLSessionUploadTask *)putWithSessionLocalPath:(NSString *)localSource atRemotePath:(NSString *)remoteDestination onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))uploadProgress success:(void(^)(NSURLResponse *, NSString *))success failure:(void(^)(NSURLResponse *, id, NSError *))failure failureBeforeRequest:(void(^)(NSError *)) failureBeforeRequest {
  455. NSFileManager *fileManager = [NSFileManager defaultManager];
  456. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  457. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  458. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  459. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  460. failureBeforeRequest(error);
  461. return nil;
  462. } else {
  463. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil timeout:k_timeout_webdav];
  464. [request setTimeoutInterval:k_timeout_upload];
  465. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  466. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  467. //If is not nil is a redirection so we keep the original url server
  468. if (!self.originalUrlServer) {
  469. self.originalUrlServer = [request.URL absoluteString];
  470. }
  471. if (sharedOCCommunication.isCookiesAvailable) {
  472. //We add the cookies of that URL
  473. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  474. } else {
  475. [UtilsFramework deleteAllCookies];
  476. }
  477. NSURL *file = [NSURL fileURLWithPath:localSource];
  478. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  479. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  480. uploadProgress(progress);
  481. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  482. if (error) {
  483. failure(response, responseObject, error);
  484. } else {
  485. success(response,responseObject);
  486. }
  487. }];
  488. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  489. [uploadTask resume];
  490. return uploadTask;
  491. }
  492. }
  493. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  494. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  495. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  496. NSString *apiUserUrl = nil;
  497. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  498. NSLog(@"api user name call: %@", apiUserUrl);
  499. _requestMethod = @"GET";
  500. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil timeout:k_timeout_webdav];
  501. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  502. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  503. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  504. [operation resume];
  505. }
  506. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  507. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  508. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  509. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  510. _requestMethod = @"GET";
  511. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil timeout:k_timeout_webdav];
  512. request.HTTPShouldHandleCookies = false;
  513. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  514. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  515. [operation resume];
  516. }
  517. - (void)listSharedByServer:(NSString *)serverPath
  518. onCommunication:(OCCommunication *)sharedOCCommunication
  519. success:(void(^)(NSHTTPURLResponse *, id))success
  520. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  521. NSParameterAssert(success);
  522. _requestMethod = @"GET";
  523. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  524. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  525. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  526. [operation resume];
  527. }
  528. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  529. onCommunication:(OCCommunication *)sharedOCCommunication
  530. success:(void(^)(NSHTTPURLResponse *, id))success
  531. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  532. NSParameterAssert(success);
  533. NSString *postString = [NSString stringWithFormat: @"?path=%@&reshares=true",path];
  534. serverPath = [serverPath stringByAppendingString:postString];
  535. _requestMethod = @"GET";
  536. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  537. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  538. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  539. [operation resume];
  540. }
  541. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  542. onCommunication:(OCCommunication *)sharedOCCommunication
  543. success:(void(^)(NSHTTPURLResponse *, id))success
  544. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  545. NSParameterAssert(success);
  546. _requestMethod = @"POST";
  547. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  548. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&permissions=%ld&password=%@&hidedownload=%i", filePath, (long)permission, password, hideDownload];
  549. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  550. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  551. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  552. [operation resume];
  553. }
  554. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  555. onCommunication:(OCCommunication *)sharedOCCommunication
  556. success:(void(^)(NSHTTPURLResponse *, id))success
  557. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  558. NSParameterAssert(success);
  559. _requestMethod = @"POST";
  560. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  561. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  562. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  563. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  564. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  565. [operation resume];
  566. }
  567. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  568. success:(void(^)(NSHTTPURLResponse *, id))success
  569. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  570. NSParameterAssert(success);
  571. _requestMethod = @"POST";
  572. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  573. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  574. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  575. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  576. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  577. [operation resume];
  578. }
  579. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  580. onCommunication:(OCCommunication *)sharedOCCommunication
  581. success:(void(^)(NSHTTPURLResponse *, id))success
  582. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  583. NSParameterAssert(success);
  584. _requestMethod = @"DELETE";
  585. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  586. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  587. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  588. [operation resume];
  589. }
  590. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  591. onCommunication:(OCCommunication *)sharedOCCommunication
  592. success:(void(^)(NSHTTPURLResponse *, id))success
  593. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  594. NSParameterAssert(success);
  595. _requestMethod = @"GET";
  596. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  597. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  598. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  599. [operation resume];
  600. }
  601. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andNote:(NSString *)note andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  602. onCommunication:(OCCommunication *)sharedOCCommunication
  603. success:(void(^)(NSHTTPURLResponse *, id response))success
  604. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  605. NSParameterAssert(success);
  606. _requestMethod = @"PUT";
  607. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  608. if (password) {
  609. self.postStringForShare = [NSString stringWithFormat:@"password=%@&",password];
  610. } else if (note) {
  611. self.postStringForShare = [NSString stringWithFormat:@"note=%@",note];
  612. } else if (expirationTime) {
  613. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  614. } else if (permissions > 0) {
  615. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  616. } else {
  617. if (hideDownload) self.postStringForShare = [NSString stringWithFormat:@"hideDownload=true"];
  618. else self.postStringForShare = [NSString stringWithFormat:@"hideDownload=false"];
  619. }
  620. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  621. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  622. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  623. [operation resume];
  624. }
  625. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  626. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  627. NSParameterAssert(success);
  628. _requestMethod = @"GET";
  629. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  630. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  631. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  632. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  633. serverPath = [serverPath stringByAppendingString:jsonQuery];
  634. serverPath = [serverPath stringByAppendingString:queryType];
  635. serverPath = [serverPath stringByAppendingString:searchQuery];
  636. serverPath = [serverPath stringByAppendingString:pagination];
  637. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  638. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  639. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  640. [operation resume];
  641. }
  642. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  643. _requestMethod = @"GET";
  644. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  645. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  646. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  647. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  648. [operation resume];
  649. }
  650. #pragma mark - Remote thumbnails
  651. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  652. _requestMethod = @"GET";
  653. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files/api/v1/thumbnail/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  654. serverPath = [serverPath stringByAppendingString:query];
  655. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  656. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  657. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  658. return operation;
  659. }
  660. - (OCHTTPRequestOperation *) getRemotePreviewByServer:(NSString *)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight andA:(NSInteger)a andMode:(NSString *)mode path:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  661. _requestMethod = @"GET";
  662. if (path.length > 0) {
  663. serverPath = path;
  664. } else {
  665. NSString *query = [NSString stringWithFormat:@"/index.php/core/preview.png?file=%@&x=%d&y=%d&a=%d&mode=%@", filePath, (int)fileWidth, (int)fileHeight, (int)a, mode];
  666. serverPath = [serverPath stringByAppendingString:query];
  667. }
  668. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  669. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  670. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  671. return operation;
  672. }
  673. - (OCHTTPRequestOperation *) getRemotePreviewTrashByServer:(NSString *)serverPath ofFileId:(NSString*)fileId onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  674. _requestMethod = @"GET";
  675. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files_trashbin/preview?fileId=%@&x=128&y=128", fileId];
  676. serverPath = [serverPath stringByAppendingString:query];
  677. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  678. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  679. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  680. return operation;
  681. }
  682. #pragma mark - Get Notification
  683. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  684. _requestMethod = @"GET";
  685. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  686. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  687. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  688. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  689. [operation resume];
  690. }
  691. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  692. NSParameterAssert(success);
  693. _requestMethod = type;
  694. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  695. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  696. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  697. [operation resume];
  698. }
  699. - (void)subscribingNextcloudServerPush:(NSString *)serverPath pushTokenHash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey proxyServerPath:(NSString *)proxyServerPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  700. NSParameterAssert(success);
  701. _requestMethod = @"POST";
  702. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  703. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushTokenHash=%@",pushTokenHash]];
  704. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey]];
  705. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath]];
  706. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  707. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  708. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  709. [operation resume];
  710. }
  711. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  712. NSParameterAssert(success);
  713. _requestMethod = @"DELETE";
  714. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  715. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  716. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  717. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  718. [operation resume];
  719. }
  720. - (void)subscribingPushProxy:(NSString *)serverPath pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  721. NSParameterAssert(success);
  722. _requestMethod = @"POST";
  723. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  724. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushToken=%@",pushToken]];
  725. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  726. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  727. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  728. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  729. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  730. NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
  731. //[request setValue:[userAgent stringByAppendingString:@" (SilentPush)"] forHTTPHeaderField:@"User-Agent"];
  732. [request setValue:[userAgent stringByAppendingString:@" (PushKit)"] forHTTPHeaderField:@"User-Agent"];
  733. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  734. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  735. [operation resume];
  736. }
  737. - (void)unsubscribingPushProxy:(NSString *)serverPath deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  738. NSParameterAssert(success);
  739. _requestMethod = @"DELETE";
  740. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  741. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  742. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  743. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  744. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  745. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  746. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  747. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  748. [operation resume];
  749. }
  750. #pragma mark - Get Activity
  751. - (void) getActivityServer:(NSString*)serverPath since:(NSInteger)since limit:(NSInteger)limit previews:(BOOL)previews link:(NSString *)link onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  752. _requestMethod = @"GET";
  753. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json&since=%ld&limit=%ld", (long)since, (long)limit]];
  754. if (previews) {
  755. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&previews=true"]];
  756. }
  757. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  758. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  759. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  760. [operation resume];
  761. }
  762. #pragma mark - Get External sites
  763. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  764. _requestMethod = @"GET";
  765. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  766. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  767. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  768. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  769. [operation resume];
  770. }
  771. #pragma mark - Get User Profile
  772. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  773. _requestMethod = @"GET";
  774. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  775. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  776. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  777. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  778. [operation resume];
  779. }
  780. #pragma mark - End-to-End Encryption
  781. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  782. _requestMethod = @"GET";
  783. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  784. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  785. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  786. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  787. [operation resume];
  788. }
  789. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  790. _requestMethod = @"GET";
  791. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  792. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  793. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  794. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  795. [operation resume];
  796. }
  797. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  798. _requestMethod = @"GET";
  799. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  800. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  801. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  802. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  803. [operation resume];
  804. }
  805. - (void)signEndToEndPublicKey:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  806. NSParameterAssert(success);
  807. _requestMethod = @"POST";
  808. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  809. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  810. _postStringKey = [NSString stringWithFormat: @"csr=%@",key];
  811. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  812. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  813. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  814. [operation resume];
  815. }
  816. - (void)storeEndToEndPrivateKeyCipher:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  817. NSParameterAssert(success);
  818. _requestMethod = @"POST";
  819. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  820. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  821. _postStringKey = [NSString stringWithFormat: @"privateKey=%@",key];
  822. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  823. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  824. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  825. [operation resume];
  826. }
  827. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  828. _requestMethod = @"DELETE";
  829. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  830. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  831. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  832. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  833. [operation resume];
  834. }
  835. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  836. _requestMethod = @"DELETE";
  837. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  838. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  839. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  840. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  841. [operation resume];
  842. }
  843. - (void)markEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  844. _requestMethod = @"PUT";
  845. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  846. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  847. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  848. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  849. [operation resume];
  850. }
  851. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  852. _requestMethod = @"DELETE";
  853. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  854. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  855. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  856. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  857. [operation resume];
  858. }
  859. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  860. _requestMethod = @"POST";
  861. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  862. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  863. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  864. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  865. [operation resume];
  866. }
  867. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath token:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  868. _requestMethod = @"DELETE";
  869. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  870. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  871. // Add token
  872. [request setValue:token forHTTPHeaderField:@"token"];
  873. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  874. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  875. [operation resume];
  876. }
  877. - (void)getEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  878. NSParameterAssert(success);
  879. _requestMethod = @"GET";
  880. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  881. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  882. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  883. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  884. [operation resume];
  885. }
  886. - (void)storeEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  887. NSParameterAssert(success);
  888. _requestMethod = @"POST";
  889. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  890. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  891. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  892. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  893. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  894. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  895. [operation resume];
  896. }
  897. - (void)updateEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  898. NSParameterAssert(success);
  899. _requestMethod = @"PUT";
  900. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  901. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  902. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  903. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  904. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  905. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  906. [operation resume];
  907. }
  908. - (void)deleteEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  909. NSParameterAssert(success);
  910. _requestMethod = @"DELETE";
  911. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  912. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  913. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  914. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  915. [operation resume];
  916. }
  917. #pragma mark - Manage Mobile Editor OCS API
  918. - (void)createLinkRichdocuments:(NSString *)serverPath ocId:(NSString *)ocId onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  919. NSParameterAssert(success);
  920. _requestMethod = @"POST";
  921. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  922. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  923. [request setHTTPBody:[[NSString stringWithFormat: @"ocId=%@",ocId] dataUsingEncoding:NSUTF8StringEncoding]];
  924. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  925. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  926. [operation resume];
  927. }
  928. - (void)getTemplatesRichdocuments:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  929. NSParameterAssert(success);
  930. _requestMethod = @"GET";
  931. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  932. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  933. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  934. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  935. [operation resume];
  936. }
  937. - (void)createNewRichdocuments:(NSString *)serverPath path:(NSString *)path templateID:(NSString *)templateID onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  938. NSParameterAssert(success);
  939. _requestMethod = @"POST";
  940. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  941. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  942. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@&template=%@", path, templateID] dataUsingEncoding:NSUTF8StringEncoding]];
  943. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  944. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  945. [operation resume];
  946. }
  947. - (void)createAssetRichdocuments:(NSString *)serverPath path:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  948. NSParameterAssert(success);
  949. _requestMethod = @"POST";
  950. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  951. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  952. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@",path] dataUsingEncoding:NSUTF8StringEncoding]];
  953. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  954. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  955. [operation resume];
  956. }
  957. #pragma mark - Fulltextsearch
  958. - (void)fullTextSearch:(NSString *)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  959. NSParameterAssert(success);
  960. _requestMethod = @"GET";
  961. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  962. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  963. //[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
  964. [request setHTTPBody:[[NSString stringWithFormat:@"request=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  965. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  966. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  967. [operation resume];
  968. }
  969. #pragma mark - Remore wipe
  970. - (void)getSetRemoteWipe:(NSString *)serverPath token:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  971. NSParameterAssert(success);
  972. _requestMethod = @"POST";
  973. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  974. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  975. [request setHTTPBody:[[NSString stringWithFormat:@"token=%@",token] dataUsingEncoding:NSUTF8StringEncoding]];
  976. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  977. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  978. [operation resume];
  979. }
  980. #pragma mark - Trash
  981. - (void)listTrash:(NSString *)path depth:(NSString *)depth onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *error))failure
  982. {
  983. NSParameterAssert(success);
  984. _requestMethod = @"PROPFIND";
  985. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  986. [request setValue: depth forHTTPHeaderField: @"Depth"];
  987. NSString *body = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><d:prop>";
  988. body = [body stringByAppendingString:NCResource];
  989. body = [body stringByAppendingString:@"</d:prop></d:propfind>"];
  990. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  991. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  992. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  993. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  994. [operation resume];
  995. }
  996. - (void)emptyTrash:(NSString*)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  997. NSParameterAssert(success);
  998. _requestMethod = @"DELETE";
  999. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1000. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1001. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1002. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1003. [operation resume];
  1004. }
  1005. #pragma mark - Messages
  1006. - (void)getComments:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1007. NSParameterAssert(success);
  1008. _requestMethod = @"PROPFIND";
  1009. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1010. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  1011. "<d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1012. "<d:prop>"
  1013. "<oc:id/>"
  1014. "<oc:verb/>"
  1015. "<oc:actorType/>"
  1016. "<oc:actorId/>"
  1017. "<oc:creationDateTime/>"
  1018. "<oc:objectType/>"
  1019. "<oc:objectId/>"
  1020. "<oc:isUnread/>"
  1021. "<oc:message/>"
  1022. "<oc:actorDisplayName/>"
  1023. "</d:prop>"
  1024. "</d:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  1025. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1026. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1027. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1028. [operation resume];
  1029. }
  1030. - (void)putComments:(NSString*)serverPath message:(NSString *)message onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1031. NSParameterAssert(success);
  1032. _requestMethod = @"POST";
  1033. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1034. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  1035. [request setHTTPBody:[[NSString stringWithFormat: @"{\"actorType\":\"users\",\"verb\":\"comment\",\"message\":\"%@\"}",message] dataUsingEncoding:NSUTF8StringEncoding]];
  1036. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1037. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1038. [operation resume];
  1039. }
  1040. - (void)updateComments:(NSString*)serverPath message:(NSString *)message onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1041. NSParameterAssert(success);
  1042. _requestMethod = @"PROPPATCH";
  1043. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1044. NSString *body;
  1045. body = [NSString stringWithFormat: @""
  1046. "<?xml version=\"1.0\"?>"
  1047. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1048. "<d:set>"
  1049. "<d:prop>"
  1050. "<oc:message>%@</oc:message>"
  1051. "</d:prop>"
  1052. "</d:set>"
  1053. "</d:propertyupdate>", message];
  1054. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  1055. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1056. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1057. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1058. [operation resume];
  1059. }
  1060. - (void)readMarkComments:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1061. NSParameterAssert(success);
  1062. _requestMethod = @"PROPPATCH";
  1063. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1064. NSString *body;
  1065. body = [NSString stringWithFormat: @""
  1066. "<?xml version=\"1.0\"?>"
  1067. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1068. "<d:set>"
  1069. "<d:prop>"
  1070. "<readMarker xmlns=\"http://owncloud.org/ns\"/>"
  1071. "</d:prop>"
  1072. "</d:set>"
  1073. "</d:propertyupdate>"];
  1074. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  1075. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1076. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1077. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1078. [operation resume];
  1079. }
  1080. - (void)deleteComments:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1081. NSParameterAssert(success);
  1082. _requestMethod = @"DELETE";
  1083. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1084. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1085. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1086. [operation resume];
  1087. }
  1088. #pragma mark - Third Parts
  1089. - (void)getHCUserProfile:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1090. _requestMethod = @"GET";
  1091. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1092. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1093. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1094. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1095. [operation resume];
  1096. }
  1097. - (void)putHCUserProfile:(NSString*)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1098. NSParameterAssert(success);
  1099. _requestMethod = @"POST";
  1100. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1101. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1102. [request setHTTPBody:[[NSString stringWithFormat: @"data=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  1103. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1104. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1105. [operation resume];
  1106. }
  1107. - (void)getHCFeatures:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1108. _requestMethod = @"GET";
  1109. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1110. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1111. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1112. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1113. [operation resume];
  1114. }
  1115. #pragma mark - Manage Redirections
  1116. - (void)setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  1117. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  1118. if (response == nil) {
  1119. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  1120. return request;
  1121. }
  1122. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  1123. NSDictionary *dict = [httpResponse allHeaderFields];
  1124. //Server path of redirected server
  1125. NSString *responseURLString = [dict objectForKey:@"Location"];
  1126. if (responseURLString) {
  1127. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  1128. //We set the redirectedServer in case SAML or is a permanent redirection
  1129. self.redirectedServer = responseURLString;
  1130. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  1131. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  1132. return nil;
  1133. }
  1134. }
  1135. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  1136. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  1137. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  1138. requestRedirect.HTTPMethod = _requestMethod;
  1139. if (_postStringForShare) {
  1140. //It is a request to share a file by link
  1141. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil timeout:k_timeout_webdav];
  1142. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  1143. }
  1144. return requestRedirect;
  1145. } else {
  1146. // no location to redirect -> nil to NO FOLLOW
  1147. return nil;
  1148. }
  1149. }];
  1150. }
  1151. @end