OCWebDAVClient.m 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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 setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  402. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  403. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  404. [operation resume];
  405. }
  406. - (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{
  407. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil timeout:k_timeout_webdav];
  408. //If is not nil is a redirection so we keep the original url server
  409. if (!self.originalUrlServer) {
  410. self.originalUrlServer = [request.URL absoluteString];
  411. }
  412. //We add the cookies of that URL
  413. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  414. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  415. downloadProgress(progress);
  416. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  417. [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:localDestination] error:nil];
  418. return [NSURL fileURLWithPath:localDestination];
  419. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  420. if (error) {
  421. failure(response, error);
  422. } else {
  423. success(response,filePath);
  424. }
  425. }];
  426. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  427. if (defaultPriority) {
  428. [downloadTask resume];
  429. }
  430. return downloadTask;
  431. }
  432. - (void)checkServer:(NSString *)path onCommunication:
  433. (OCCommunication *)sharedOCCommunication
  434. success:(void(^)(NSHTTPURLResponse *, id))success
  435. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  436. _requestMethod = @"HEAD";
  437. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  438. request.HTTPShouldHandleCookies = false;
  439. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  440. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  441. [operation resume];
  442. }
  443. - (void)makeCollection:(NSString *)path onCommunication:
  444. (OCCommunication *)sharedOCCommunication
  445. success:(void(^)(NSHTTPURLResponse *, id))success
  446. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  447. _requestMethod = @"MKCOL";
  448. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  449. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  450. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  451. [operation resume];
  452. }
  453. - (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 {
  454. NSFileManager *fileManager = [NSFileManager defaultManager];
  455. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  456. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  457. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  458. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  459. failureBeforeRequest(error);
  460. return nil;
  461. } else {
  462. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil timeout:k_timeout_webdav];
  463. [request setTimeoutInterval:k_timeout_upload];
  464. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  465. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  466. //If is not nil is a redirection so we keep the original url server
  467. if (!self.originalUrlServer) {
  468. self.originalUrlServer = [request.URL absoluteString];
  469. }
  470. if (sharedOCCommunication.isCookiesAvailable) {
  471. //We add the cookies of that URL
  472. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  473. } else {
  474. [UtilsFramework deleteAllCookies];
  475. }
  476. NSURL *file = [NSURL fileURLWithPath:localSource];
  477. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  478. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  479. uploadProgress(progress);
  480. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  481. if (error) {
  482. failure(response, responseObject, error);
  483. } else {
  484. success(response,responseObject);
  485. }
  486. }];
  487. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  488. [uploadTask resume];
  489. return uploadTask;
  490. }
  491. }
  492. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  493. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  494. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  495. NSString *apiUserUrl = nil;
  496. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  497. NSLog(@"api user name call: %@", apiUserUrl);
  498. _requestMethod = @"GET";
  499. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil timeout:k_timeout_webdav];
  500. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  501. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  502. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  503. [operation resume];
  504. }
  505. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  506. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  507. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  508. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  509. _requestMethod = @"GET";
  510. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil timeout:k_timeout_webdav];
  511. request.HTTPShouldHandleCookies = false;
  512. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  513. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  514. [operation resume];
  515. }
  516. - (void)listSharedByServer:(NSString *)serverPath
  517. onCommunication:(OCCommunication *)sharedOCCommunication
  518. success:(void(^)(NSHTTPURLResponse *, id))success
  519. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  520. NSParameterAssert(success);
  521. _requestMethod = @"GET";
  522. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  523. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  524. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  525. [operation resume];
  526. }
  527. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  528. onCommunication:(OCCommunication *)sharedOCCommunication
  529. success:(void(^)(NSHTTPURLResponse *, id))success
  530. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  531. NSParameterAssert(success);
  532. NSString *postString = [NSString stringWithFormat: @"?path=%@&reshares=true",path];
  533. serverPath = [serverPath stringByAppendingString:postString];
  534. _requestMethod = @"GET";
  535. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  536. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  537. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  538. [operation resume];
  539. }
  540. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  541. onCommunication:(OCCommunication *)sharedOCCommunication
  542. success:(void(^)(NSHTTPURLResponse *, id))success
  543. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  544. NSParameterAssert(success);
  545. _requestMethod = @"POST";
  546. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  547. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&permissions=%ld&password=%@&hidedownload=%i", filePath, (long)permission, password, hideDownload];
  548. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  549. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  550. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  551. [operation resume];
  552. }
  553. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  554. onCommunication:(OCCommunication *)sharedOCCommunication
  555. success:(void(^)(NSHTTPURLResponse *, id))success
  556. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  557. NSParameterAssert(success);
  558. _requestMethod = @"POST";
  559. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  560. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  561. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  562. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  563. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  564. [operation resume];
  565. }
  566. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  567. success:(void(^)(NSHTTPURLResponse *, id))success
  568. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  569. NSParameterAssert(success);
  570. _requestMethod = @"POST";
  571. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  572. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  573. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  574. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  575. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  576. [operation resume];
  577. }
  578. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  579. onCommunication:(OCCommunication *)sharedOCCommunication
  580. success:(void(^)(NSHTTPURLResponse *, id))success
  581. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  582. NSParameterAssert(success);
  583. _requestMethod = @"DELETE";
  584. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  585. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  586. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  587. [operation resume];
  588. }
  589. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  590. onCommunication:(OCCommunication *)sharedOCCommunication
  591. success:(void(^)(NSHTTPURLResponse *, id))success
  592. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  593. NSParameterAssert(success);
  594. _requestMethod = @"GET";
  595. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  596. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  597. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  598. [operation resume];
  599. }
  600. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andNote:(NSString *)note andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  601. onCommunication:(OCCommunication *)sharedOCCommunication
  602. success:(void(^)(NSHTTPURLResponse *, id response))success
  603. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  604. NSParameterAssert(success);
  605. _requestMethod = @"PUT";
  606. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  607. if (password) {
  608. self.postStringForShare = [NSString stringWithFormat:@"password=%@&",password];
  609. } else if (note) {
  610. self.postStringForShare = [NSString stringWithFormat:@"note=%@",note];
  611. } else if (expirationTime) {
  612. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  613. } else if (permissions > 0) {
  614. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  615. } else {
  616. if (hideDownload) self.postStringForShare = [NSString stringWithFormat:@"hideDownload=true"];
  617. else self.postStringForShare = [NSString stringWithFormat:@"hideDownload=false"];
  618. }
  619. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  620. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  621. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  622. [operation resume];
  623. }
  624. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  625. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  626. NSParameterAssert(success);
  627. _requestMethod = @"GET";
  628. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  629. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  630. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  631. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  632. serverPath = [serverPath stringByAppendingString:jsonQuery];
  633. serverPath = [serverPath stringByAppendingString:queryType];
  634. serverPath = [serverPath stringByAppendingString:searchQuery];
  635. serverPath = [serverPath stringByAppendingString:pagination];
  636. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  637. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  638. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  639. [operation resume];
  640. }
  641. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  642. _requestMethod = @"GET";
  643. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  644. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  645. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  646. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  647. [operation resume];
  648. }
  649. #pragma mark - Remote thumbnails
  650. - (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{
  651. _requestMethod = @"GET";
  652. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files/api/v1/thumbnail/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  653. serverPath = [serverPath stringByAppendingString:query];
  654. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  655. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  656. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  657. return operation;
  658. }
  659. - (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 {
  660. _requestMethod = @"GET";
  661. if (path.length > 0) {
  662. serverPath = path;
  663. } else {
  664. 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];
  665. serverPath = [serverPath stringByAppendingString:query];
  666. }
  667. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  668. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  669. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  670. return operation;
  671. }
  672. - (OCHTTPRequestOperation *) getRemotePreviewTrashByServer:(NSString *)serverPath ofFileId:(NSString*)fileId size:(NSString *)size onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  673. _requestMethod = @"GET";
  674. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files_trashbin/preview?fileId=%@&x=%@&y=%@", fileId, size, size];
  675. serverPath = [serverPath stringByAppendingString:query];
  676. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  677. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  678. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  679. return operation;
  680. }
  681. #pragma mark - Get Notification
  682. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  683. _requestMethod = @"GET";
  684. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  685. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  686. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  687. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  688. [operation resume];
  689. }
  690. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  691. NSParameterAssert(success);
  692. _requestMethod = type;
  693. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  694. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  695. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  696. [operation resume];
  697. }
  698. - (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 {
  699. NSParameterAssert(success);
  700. _requestMethod = @"POST";
  701. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  702. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushTokenHash=%@",pushTokenHash]];
  703. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey]];
  704. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath]];
  705. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  706. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  707. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  708. [operation resume];
  709. }
  710. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  711. NSParameterAssert(success);
  712. _requestMethod = @"DELETE";
  713. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  714. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  715. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  716. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  717. [operation resume];
  718. }
  719. - (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 {
  720. NSParameterAssert(success);
  721. _requestMethod = @"POST";
  722. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  723. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushToken=%@",pushToken]];
  724. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  725. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  726. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  727. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  728. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  729. NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
  730. //[request setValue:[userAgent stringByAppendingString:@" (SilentPush)"] forHTTPHeaderField:@"User-Agent"];
  731. [request setValue:[userAgent stringByAppendingString:@" (Strict VoIP)"] forHTTPHeaderField:@"User-Agent"];
  732. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  733. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  734. [operation resume];
  735. }
  736. - (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 {
  737. NSParameterAssert(success);
  738. _requestMethod = @"DELETE";
  739. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  740. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  741. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  742. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  743. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  744. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  745. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  746. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  747. [operation resume];
  748. }
  749. #pragma mark - Get Activity
  750. - (void) getActivityServer:(NSString*)serverPath since:(NSInteger)since limit:(NSInteger)limit objectId:(NSString *)objectId objectType:(NSString *)objectType 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{
  751. _requestMethod = @"GET";
  752. if (objectId.length == 0) {
  753. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"/all?format=json&since=%ld&limit=%ld", (long)since, (long)limit]];
  754. } else {
  755. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"/filter?format=json&since=%ld&limit=%ld&object_id=%@&object_type=%@",(long)since, (long)limit, objectId, objectType]];
  756. }
  757. if (previews) {
  758. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&previews=true"]];
  759. }
  760. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  761. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  762. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  763. [operation resume];
  764. }
  765. #pragma mark - Get External sites
  766. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  767. _requestMethod = @"GET";
  768. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  769. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  770. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  771. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  772. [operation resume];
  773. }
  774. #pragma mark - Get User Profile
  775. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  776. _requestMethod = @"GET";
  777. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  778. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  779. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  780. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  781. [operation resume];
  782. }
  783. #pragma mark - End-to-End Encryption
  784. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  785. _requestMethod = @"GET";
  786. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  787. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  788. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  789. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  790. [operation resume];
  791. }
  792. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  793. _requestMethod = @"GET";
  794. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  795. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  796. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  797. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  798. [operation resume];
  799. }
  800. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  801. _requestMethod = @"GET";
  802. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  803. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  804. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  805. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  806. [operation resume];
  807. }
  808. - (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{
  809. NSParameterAssert(success);
  810. _requestMethod = @"POST";
  811. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  812. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  813. _postStringKey = [NSString stringWithFormat: @"csr=%@",key];
  814. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  815. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  816. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  817. [operation resume];
  818. }
  819. - (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{
  820. NSParameterAssert(success);
  821. _requestMethod = @"POST";
  822. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  823. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  824. _postStringKey = [NSString stringWithFormat: @"privateKey=%@",key];
  825. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  826. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  827. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  828. [operation resume];
  829. }
  830. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  831. _requestMethod = @"DELETE";
  832. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  833. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  834. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  835. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  836. [operation resume];
  837. }
  838. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  839. _requestMethod = @"DELETE";
  840. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  841. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  842. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  843. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  844. [operation resume];
  845. }
  846. - (void)markEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  847. _requestMethod = @"PUT";
  848. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  849. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  850. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  851. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  852. [operation resume];
  853. }
  854. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath e2eToken:(NSString *)e2eToken onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  855. _requestMethod = @"DELETE";
  856. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  857. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  858. [request setValue:e2eToken forHTTPHeaderField:@"e2e-token"];
  859. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  860. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  861. [operation resume];
  862. }
  863. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath e2eToken:(NSString *)e2eToken onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  864. _requestMethod = @"POST";
  865. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  866. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  867. [request setValue:e2eToken forHTTPHeaderField:@"e2e-token"];
  868. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  869. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  870. [operation resume];
  871. }
  872. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath e2eToken:(NSString *)e2eToken onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  873. _requestMethod = @"DELETE";
  874. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  875. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  876. [request setValue:e2eToken forHTTPHeaderField:@"e2e-token"];
  877. NSLog(@"%@", [request allHTTPHeaderFields]);
  878. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  879. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  880. [operation resume];
  881. }
  882. - (void)getEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  883. NSParameterAssert(success);
  884. _requestMethod = @"GET";
  885. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  886. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  887. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  888. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  889. [operation resume];
  890. }
  891. - (void)storeEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata e2eToken:(NSString *)e2eToken onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  892. NSParameterAssert(success);
  893. _requestMethod = @"POST";
  894. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  895. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  896. [request setValue:e2eToken forHTTPHeaderField:@"e2e-token"];
  897. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  898. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  899. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  900. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  901. [operation resume];
  902. }
  903. - (void)updateEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata e2eToken:(NSString *)e2eToken onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  904. NSParameterAssert(success);
  905. _requestMethod = @"PUT";
  906. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  907. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  908. [request setValue:e2eToken forHTTPHeaderField:@"e2e-token"];
  909. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  910. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  911. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  912. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  913. [operation resume];
  914. }
  915. - (void)deleteEndToEndMetadata:(NSString*)serverPath e2eToken:(NSString *)e2eToken onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  916. NSParameterAssert(success);
  917. _requestMethod = @"DELETE";
  918. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  919. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  920. [request setValue:e2eToken forHTTPHeaderField:@"e2e-token"];
  921. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  922. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  923. [operation resume];
  924. }
  925. #pragma mark - Manage Mobile Editor OCS API
  926. - (void)createLinkRichdocuments:(NSString *)serverPath fileId:(NSString *)fileId onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  927. NSParameterAssert(success);
  928. _requestMethod = @"POST";
  929. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  930. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  931. [request setHTTPBody:[[NSString stringWithFormat: @"fileId=%@",fileId] dataUsingEncoding:NSUTF8StringEncoding]];
  932. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  933. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  934. [operation resume];
  935. }
  936. - (void)getTemplatesRichdocuments:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  937. NSParameterAssert(success);
  938. _requestMethod = @"GET";
  939. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  940. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  941. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  942. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  943. [operation resume];
  944. }
  945. - (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 {
  946. NSParameterAssert(success);
  947. _requestMethod = @"POST";
  948. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  949. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  950. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@&template=%@", path, templateID] dataUsingEncoding:NSUTF8StringEncoding]];
  951. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  952. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  953. [operation resume];
  954. }
  955. - (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 {
  956. NSParameterAssert(success);
  957. _requestMethod = @"POST";
  958. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  959. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  960. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@",path] dataUsingEncoding:NSUTF8StringEncoding]];
  961. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  962. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  963. [operation resume];
  964. }
  965. #pragma mark - Fulltextsearch
  966. - (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 {
  967. NSParameterAssert(success);
  968. _requestMethod = @"GET";
  969. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  970. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  971. //[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
  972. [request setHTTPBody:[[NSString stringWithFormat:@"request=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  973. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  974. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  975. [operation resume];
  976. }
  977. #pragma mark - Remore wipe
  978. - (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 {
  979. NSParameterAssert(success);
  980. _requestMethod = @"POST";
  981. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  982. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  983. [request setHTTPBody:[[NSString stringWithFormat:@"token=%@",token] dataUsingEncoding:NSUTF8StringEncoding]];
  984. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  985. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  986. [operation resume];
  987. }
  988. #pragma mark - Trash
  989. - (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
  990. {
  991. NSParameterAssert(success);
  992. _requestMethod = @"PROPFIND";
  993. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  994. [request setValue: depth forHTTPHeaderField: @"Depth"];
  995. 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>";
  996. body = [body stringByAppendingString:NCResource];
  997. body = [body stringByAppendingString:@"</d:prop></d:propfind>"];
  998. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  999. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1000. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1001. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1002. [operation resume];
  1003. }
  1004. - (void)emptyTrash:(NSString*)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1005. NSParameterAssert(success);
  1006. _requestMethod = @"DELETE";
  1007. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1008. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1009. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1010. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1011. [operation resume];
  1012. }
  1013. #pragma mark - Messages
  1014. - (void)getComments:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1015. NSParameterAssert(success);
  1016. _requestMethod = @"PROPFIND";
  1017. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1018. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  1019. "<d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1020. "<d:prop>"
  1021. "<oc:id/>"
  1022. "<oc:verb/>"
  1023. "<oc:actorType/>"
  1024. "<oc:actorId/>"
  1025. "<oc:creationDateTime/>"
  1026. "<oc:objectType/>"
  1027. "<oc:objectId/>"
  1028. "<oc:isUnread/>"
  1029. "<oc:message/>"
  1030. "<oc:actorDisplayName/>"
  1031. "</d:prop>"
  1032. "</d:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  1033. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1034. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1035. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1036. [operation resume];
  1037. }
  1038. - (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 {
  1039. NSParameterAssert(success);
  1040. _requestMethod = @"POST";
  1041. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1042. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  1043. [request setHTTPBody:[[NSString stringWithFormat: @"{\"actorType\":\"users\",\"verb\":\"comment\",\"message\":\"%@\"}",message] dataUsingEncoding:NSUTF8StringEncoding]];
  1044. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1045. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1046. [operation resume];
  1047. }
  1048. - (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 {
  1049. NSParameterAssert(success);
  1050. _requestMethod = @"PROPPATCH";
  1051. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1052. NSString *body;
  1053. body = [NSString stringWithFormat: @""
  1054. "<?xml version=\"1.0\"?>"
  1055. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1056. "<d:set>"
  1057. "<d:prop>"
  1058. "<oc:message>%@</oc:message>"
  1059. "</d:prop>"
  1060. "</d:set>"
  1061. "</d:propertyupdate>", message];
  1062. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  1063. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1064. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1065. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1066. [operation resume];
  1067. }
  1068. - (void)readMarkComments:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1069. NSParameterAssert(success);
  1070. _requestMethod = @"PROPPATCH";
  1071. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1072. NSString *body;
  1073. body = [NSString stringWithFormat: @""
  1074. "<?xml version=\"1.0\"?>"
  1075. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1076. "<d:set>"
  1077. "<d:prop>"
  1078. "<readMarker xmlns=\"http://owncloud.org/ns\"/>"
  1079. "</d:prop>"
  1080. "</d:set>"
  1081. "</d:propertyupdate>"];
  1082. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  1083. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  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. - (void)deleteComments:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1089. NSParameterAssert(success);
  1090. _requestMethod = @"DELETE";
  1091. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1092. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1093. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1094. [operation resume];
  1095. }
  1096. #pragma mark - Third Parts
  1097. - (void)getHCUserProfile:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1098. _requestMethod = @"GET";
  1099. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1100. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1101. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1102. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1103. [operation resume];
  1104. }
  1105. - (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 {
  1106. NSParameterAssert(success);
  1107. _requestMethod = @"POST";
  1108. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1109. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1110. [request setHTTPBody:[[NSString stringWithFormat: @"data=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  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. - (void)getHCFeatures:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1116. _requestMethod = @"GET";
  1117. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1118. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1119. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1120. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1121. [operation resume];
  1122. }
  1123. #pragma mark - Manage Redirections
  1124. - (void)setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  1125. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  1126. if (response == nil) {
  1127. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  1128. return request;
  1129. }
  1130. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  1131. NSDictionary *dict = [httpResponse allHeaderFields];
  1132. //Server path of redirected server
  1133. NSString *responseURLString = [dict objectForKey:@"Location"];
  1134. if (responseURLString) {
  1135. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  1136. //We set the redirectedServer in case SAML or is a permanent redirection
  1137. self.redirectedServer = responseURLString;
  1138. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  1139. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  1140. return nil;
  1141. }
  1142. }
  1143. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  1144. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  1145. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  1146. requestRedirect.HTTPMethod = _requestMethod;
  1147. if (_postStringForShare) {
  1148. //It is a request to share a file by link
  1149. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil timeout:k_timeout_webdav];
  1150. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  1151. }
  1152. return requestRedirect;
  1153. } else {
  1154. // no location to redirect -> nil to NO FOLLOW
  1155. return nil;
  1156. }
  1157. }];
  1158. }
  1159. @end