OCCommunication.m 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  1. //
  2. // OCCommunication.m
  3. // Owncloud iOs Client
  4. //
  5. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. // Add : getNotificationServer & setNotificationServer
  25. // Add : getUserProfileServer
  26. // Add : Support for Favorite
  27. // Add : getActivityServer
  28. //
  29. // Author Marino Faggiana <m.faggiana@twsweb.it>
  30. // Copyright (c) 2017 TWS. All rights reserved.
  31. //
  32. #import "OCCommunication.h"
  33. #import "OCHTTPRequestOperation.h"
  34. #import "UtilsFramework.h"
  35. #import "OCXMLParser.h"
  36. #import "OCXMLSharedParser.h"
  37. #import "OCXMLServerErrorsParser.h"
  38. #import "OCXMLListParser.h"
  39. #import "NSString+Encode.h"
  40. #import "OCFrameworkConstants.h"
  41. #import "OCWebDAVClient.h"
  42. #import "OCXMLShareByLinkParser.h"
  43. #import "OCErrorMsg.h"
  44. #import "AFURLSessionManager.h"
  45. #import "OCShareUser.h"
  46. #import "OCActivity.h"
  47. #import "OCExternalSites.h"
  48. #import "OCCapabilities.h"
  49. #import "OCNotifications.h"
  50. #import "OCNotificationsAction.h"
  51. #import "OCRichObjectStrings.h"
  52. #import "OCUserProfile.h"
  53. @interface OCCommunication ()
  54. @property (nonatomic, strong) NSString *currentServerVersion;
  55. @end
  56. @implementation OCCommunication
  57. -(id) init {
  58. self = [super init];
  59. if (self) {
  60. //Init the Donwload queue array
  61. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  62. //Credentials not set yet
  63. self.kindOfCredential = credentialNotSet;
  64. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  65. self.isCookiesAvailable = YES;
  66. self.isForbiddenCharactersAvailable = NO;
  67. #ifdef UNIT_TEST
  68. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  69. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  70. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  71. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  72. #else
  73. //Network Upload queue for NSURLSession (iOS 7)
  74. NSURLSessionConfiguration *uploadConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_session_name];
  75. uploadConfiguration.HTTPShouldUsePipelining = YES;
  76. uploadConfiguration.HTTPMaximumConnectionsPerHost = 1;
  77. uploadConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  78. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:uploadConfiguration];
  79. [self.uploadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  80. //Network Download queue for NSURLSession (iOS 7)
  81. NSURLSessionConfiguration *downConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_download_session_name];
  82. downConfiguration.HTTPShouldUsePipelining = YES;
  83. downConfiguration.HTTPMaximumConnectionsPerHost = 1;
  84. downConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  85. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:downConfiguration];
  86. [self.downloadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  87. //Network Download queue for NSURLSession (iOS 7)
  88. NSURLSessionConfiguration *networkConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  89. networkConfiguration.HTTPShouldUsePipelining = YES;
  90. networkConfiguration.HTTPMaximumConnectionsPerHost = 1;
  91. networkConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  92. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:networkConfiguration];
  93. [self.networkSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  94. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  95. #endif
  96. }
  97. return self;
  98. }
  99. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager {
  100. self = [super init];
  101. if (self) {
  102. //Init the Donwload queue array
  103. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  104. self.isCookiesAvailable = YES;
  105. self.isForbiddenCharactersAvailable = NO;
  106. //Credentials not set yet
  107. self.kindOfCredential = credentialNotSet;
  108. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  109. self.uploadSessionManager = uploadSessionManager;
  110. }
  111. return self;
  112. }
  113. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager andDownloadSessionManager:(AFURLSessionManager *) downloadSessionManager andNetworkSessionManager:(AFURLSessionManager *) networkSessionManager {
  114. self = [super init];
  115. if (self) {
  116. //Init the Donwload queue array
  117. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  118. //Credentials not set yet
  119. self.kindOfCredential = credentialNotSet;
  120. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  121. self.uploadSessionManager = uploadSessionManager;
  122. self.downloadSessionManager = downloadSessionManager;
  123. self.networkSessionManager = networkSessionManager;
  124. }
  125. return self;
  126. }
  127. - (AFSecurityPolicy *) createSecurityPolicy {
  128. return [AFSecurityPolicy defaultPolicy];
  129. }
  130. - (void)setSecurityPolicyManagers:(AFSecurityPolicy *)securityPolicy {
  131. self.securityPolicy = securityPolicy;
  132. self.uploadSessionManager.securityPolicy = securityPolicy;
  133. self.downloadSessionManager.securityPolicy = securityPolicy;
  134. }
  135. #pragma mark - Setting Credentials
  136. - (void) setCredentialsWithUser:(NSString*) user andUserID:(NSString *) userID andPassword:(NSString*) password {
  137. self.kindOfCredential = credentialNormal;
  138. self.user = user;
  139. self.userID = userID;
  140. self.password = password;
  141. }
  142. - (void) setCredentialsWithCookie:(NSString*) cookie {
  143. self.kindOfCredential = credentialCookie;
  144. self.password = cookie;
  145. }
  146. - (void) setCredentialsOauthWithToken:(NSString*) token {
  147. self.kindOfCredential = credentialOauth;
  148. self.password = token;
  149. }
  150. ///-----------------------------------
  151. /// @name getRequestWithCredentials
  152. ///-----------------------------------
  153. /**
  154. * Method to return the request with the right credential
  155. *
  156. * @param OCWebDAVClient like a dinamic typed
  157. *
  158. * @return OCWebDAVClient like a dinamic typed
  159. *
  160. */
  161. - (id) getRequestWithCredentials:(id) request {
  162. if ([request isKindOfClass:[NSMutableURLRequest class]]) {
  163. NSMutableURLRequest *myRequest = (NSMutableURLRequest *)request;
  164. switch (self.kindOfCredential) {
  165. case credentialNotSet:
  166. //Without credentials
  167. break;
  168. case credentialNormal:
  169. {
  170. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.user, self.password];
  171. [myRequest addValue:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString:basicAuthCredentials]] forHTTPHeaderField:@"Authorization"];
  172. break;
  173. }
  174. case credentialCookie:
  175. NSLog(@"Cookie: %@", self.password);
  176. [myRequest addValue:self.password forHTTPHeaderField:@"Cookie"];
  177. break;
  178. case credentialOauth:
  179. [myRequest addValue:[NSString stringWithFormat:@"Bearer %@", self.password] forHTTPHeaderField:@"Authorization"];
  180. break;
  181. default:
  182. break;
  183. }
  184. if (self.userAgent) {
  185. [myRequest addValue:self.userAgent forHTTPHeaderField:@"User-Agent"];
  186. }
  187. return myRequest;
  188. } else if([request isKindOfClass:[OCWebDAVClient class]]) {
  189. OCWebDAVClient *myRequest = (OCWebDAVClient *)request;
  190. switch (self.kindOfCredential) {
  191. case credentialNotSet:
  192. //Without credentials
  193. break;
  194. case credentialNormal:
  195. [myRequest setAuthorizationHeaderWithUsername:self.user password:self.password];
  196. break;
  197. case credentialCookie:
  198. [myRequest setAuthorizationHeaderWithCookie:self.password];
  199. break;
  200. case credentialOauth:
  201. [myRequest setAuthorizationHeaderWithToken:[NSString stringWithFormat:@"Bearer %@", self.password]];
  202. break;
  203. default:
  204. break;
  205. }
  206. if (self.userAgent) {
  207. [myRequest setUserAgent:self.userAgent];
  208. }
  209. return request;
  210. } else {
  211. NSLog(@"We do not know witch kind of object is");
  212. return request;
  213. }
  214. }
  215. #pragma mark - WebDav network Operations
  216. ///-----------------------------------
  217. /// @name Check Server
  218. ///-----------------------------------
  219. - (void) checkServer: (NSString *) path
  220. onCommunication:(OCCommunication *)sharedOCCommunication
  221. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  222. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  223. OCWebDAVClient *request = [OCWebDAVClient new];
  224. request = [self getRequestWithCredentials:request];
  225. if (self.userAgent) {
  226. [request setUserAgent:self.userAgent];
  227. }
  228. path = [path encodeString:NSUTF8StringEncoding];
  229. [request checkServer:path onCommunication:sharedOCCommunication
  230. success:^(NSHTTPURLResponse *response, id responseObject) {
  231. if (successRequest) {
  232. successRequest(response, request.redirectedServer);
  233. }
  234. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  235. failureRequest(response, error, request.redirectedServer);
  236. }];
  237. }
  238. ///-----------------------------------
  239. /// @name Create a folder
  240. ///-----------------------------------
  241. - (void) createFolder: (NSString *) path
  242. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  243. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  244. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  245. errorBeforeRequest:(void(^)(NSError *error)) errorBeforeRequest {
  246. if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:path] withForbiddenCharactersSupported:isFCSupported]) {
  247. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorForbidenCharacters];
  248. errorBeforeRequest(error);
  249. } else {
  250. OCWebDAVClient *request = [OCWebDAVClient new];
  251. request = [self getRequestWithCredentials:request];
  252. path = [path encodeString:NSUTF8StringEncoding];
  253. [request makeCollection:path onCommunication:sharedOCCommunication
  254. success:^(NSHTTPURLResponse *response, id responseObject) {
  255. if (successRequest) {
  256. successRequest(response, request.redirectedServer);
  257. }
  258. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  259. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  260. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  261. if (err) {
  262. failureRequest(response, err, request.redirectedServer);
  263. }else{
  264. failureRequest(response, error, request.redirectedServer);
  265. }
  266. }];
  267. }];
  268. }
  269. }
  270. ///-----------------------------------
  271. /// @name Move a file or a folder
  272. ///-----------------------------------
  273. - (void) moveFileOrFolder:(NSString *)sourcePath
  274. toDestiny:(NSString *)destinyPath
  275. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  276. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectServer))successRequest
  277. failureRequest:(void (^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest
  278. errorBeforeRequest:(void (^)(NSError *error))errorBeforeRequest {
  279. if ([UtilsFramework isTheSameFileOrFolderByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  280. //We check that we are not trying to move the file to the same place
  281. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingTheDestinyAndOriginAreTheSame];
  282. errorBeforeRequest(error);
  283. } else if ([UtilsFramework isAFolderUnderItByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  284. //We check we are not trying to move a folder inside himself
  285. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingFolderInsideHimself];
  286. errorBeforeRequest(error);
  287. } else if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:destinyPath] withForbiddenCharactersSupported:isFCSupported]) {
  288. //We check that we are making a move not a rename to prevent special characters problems
  289. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingDestinyNameHaveForbiddenCharacters];
  290. errorBeforeRequest(error);
  291. } else {
  292. sourcePath = [sourcePath encodeString:NSUTF8StringEncoding];
  293. destinyPath = [destinyPath encodeString:NSUTF8StringEncoding];
  294. OCWebDAVClient *request = [OCWebDAVClient new];
  295. request = [self getRequestWithCredentials:request];
  296. [request movePath:sourcePath toPath:destinyPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  297. if (successRequest) {
  298. successRequest(response, request.redirectedServer);
  299. }
  300. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  301. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  302. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  303. if (err) {
  304. failureRequest(response, err, request.redirectedServer);
  305. }else{
  306. failureRequest(response, error, request.redirectedServer);
  307. }
  308. }];
  309. }];
  310. }
  311. }
  312. ///-----------------------------------
  313. /// @name Delete a file or a folder
  314. ///-----------------------------------
  315. - (void) deleteFileOrFolder:(NSString *)path
  316. onCommunication:(OCCommunication *)sharedOCCommunication
  317. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  318. failureRquest:(void (^)(NSHTTPURLResponse *resposne, NSError *error, NSString *redirectedServer))failureRequest {
  319. path = [path encodeString:NSUTF8StringEncoding];
  320. OCWebDAVClient *request = [OCWebDAVClient new];
  321. request = [self getRequestWithCredentials:request];
  322. [request deletePath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  323. if (successRequest) {
  324. successRequest(response, request.redirectedServer);
  325. }
  326. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  327. failureRequest(response, error, request.redirectedServer);
  328. }];
  329. }
  330. ///-----------------------------------
  331. /// @name Read folder
  332. ///-----------------------------------
  333. - (void) readFolder: (NSString *) path depth:(NSString *)depth withUserSessionToken:(NSString *)token
  334. onCommunication:(OCCommunication *)sharedOCCommunication
  335. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest
  336. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  337. if (!token){
  338. token = @"no token";
  339. }
  340. path = [path encodeString:NSUTF8StringEncoding];
  341. OCWebDAVClient *request = [OCWebDAVClient new];
  342. request = [self getRequestWithCredentials:request];
  343. [request listPath:path depth:depth onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  344. if (successRequest) {
  345. NSData *responseData = (NSData*) responseObject;
  346. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  347. // NSLog(@"newStrReadFolder: %@", newStr);
  348. OCXMLParser *parser = [[OCXMLParser alloc]init];
  349. [parser initParserWithData:responseData];
  350. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  351. //Return success
  352. successRequest(response, directoryList, request.redirectedServer, token);
  353. }
  354. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  355. NSLog(@"Failure");
  356. failureRequest(response, error, token, request.redirectedServer);
  357. }];
  358. }
  359. ///-----------------------------------
  360. /// @name Download File Session
  361. ///-----------------------------------
  362. - (NSURLSessionDownloadTask *) downloadFileSession:(NSString *)remotePath toDestiny:(NSString *)localPath defaultPriority:(BOOL)defaultPriority onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))downloadProgress successRequest:(void(^)(NSURLResponse *response, NSURL *filePath)) successRequest failureRequest:(void(^)(NSURLResponse *response, NSError *error)) failureRequest {
  363. OCWebDAVClient *request = [OCWebDAVClient new];
  364. request = [self getRequestWithCredentials:request];
  365. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  366. NSURLSessionDownloadTask *downloadTask = [request downloadWithSessionPath:remotePath toPath:localPath defaultPriority:defaultPriority onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  367. downloadProgress(progress);
  368. } success:^(NSURLResponse *response, NSURL *filePath) {
  369. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  370. successRequest(response,filePath);
  371. } failure:^(NSURLResponse *response, NSError *error) {
  372. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  373. failureRequest(response,error);
  374. }];
  375. return downloadTask;
  376. }
  377. ///-----------------------------------
  378. /// @name Set Download Task Complete Block
  379. ///-----------------------------------
  380. - (void)setDownloadTaskComleteBlock: (NSURL * (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block{
  381. [self.downloadSessionManager setDownloadTaskDidFinishDownloadingBlock:block];
  382. }
  383. ///-----------------------------------
  384. /// @name Set Download Task Did Get Body Data Block
  385. ///-----------------------------------
  386. - (void) setDownloadTaskDidGetBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)) block{
  387. [self.downloadSessionManager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  388. block(session,downloadTask,bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);
  389. }];
  390. }
  391. ///-----------------------------------
  392. /// @name Upload File Session
  393. ///-----------------------------------
  394. - (NSURLSessionUploadTask *) uploadFileSession:(NSString *) localPath toDestiny:(NSString *) remotePath onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))uploadProgress successRequest:(void(^)(NSURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSURLResponse *response, NSString *redirectedServer, NSError *error)) failureRequest failureBeforeRequest:(void(^)(NSError *error)) failureBeforeRequest {
  395. OCWebDAVClient *request = [OCWebDAVClient new];
  396. request = [self getRequestWithCredentials:request];
  397. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  398. NSURLSessionUploadTask *uploadTask = [request putWithSessionLocalPath:localPath atRemotePath:remotePath onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  399. uploadProgress(progress);
  400. } success:^(NSURLResponse *response, id responseObjec){
  401. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  402. //TODO: The second parameter is the redirected server
  403. successRequest(response, @"");
  404. } failure:^(NSURLResponse *response, id responseObject, NSError *error) {
  405. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  406. //TODO: The second parameter is the redirected server
  407. NSData *responseData = (NSData*) responseObject;
  408. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  409. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  410. if (err) {
  411. failureRequest(response, @"", err);
  412. }else{
  413. failureRequest(response, @"", error);
  414. }
  415. }];
  416. } failureBeforeRequest:^(NSError *error) {
  417. failureBeforeRequest(error);
  418. }];
  419. return uploadTask;
  420. }
  421. ///-----------------------------------
  422. /// @name Set Task Did Complete Block
  423. ///-----------------------------------
  424. - (void) setTaskDidCompleteBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, NSError *error)) block{
  425. [self.uploadSessionManager setTaskDidCompleteBlock:^(NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  426. block(session, task, error);
  427. }];
  428. }
  429. ///-----------------------------------
  430. /// @name Set Task Did Send Body Data Block
  431. ///-----------------------------------
  432. - (void) setTaskDidSendBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend)) block{
  433. [self.uploadSessionManager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
  434. block(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend);
  435. }];
  436. }
  437. ///-----------------------------------
  438. /// @name Read File
  439. ///-----------------------------------
  440. - (void) readFile: (NSString *) path
  441. onCommunication:(OCCommunication *)sharedOCCommunication
  442. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer)) successRequest
  443. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  444. path = [path encodeString:NSUTF8StringEncoding];
  445. OCWebDAVClient *request = [OCWebDAVClient new];
  446. request = [self getRequestWithCredentials:request];
  447. [request propertiesOfPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  448. if (successRequest) {
  449. NSData *responseData = (NSData*) responseObject;
  450. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  451. // NSLog(@"newStrReadFile: %@", newStr);
  452. OCXMLParser *parser = [[OCXMLParser alloc]init];
  453. [parser initParserWithData:responseData];
  454. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  455. //Return success
  456. successRequest(response, directoryList, request.redirectedServer);
  457. }
  458. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  459. failureRequest(response, error, request.redirectedServer);
  460. }];
  461. }
  462. ///-----------------------------------
  463. /// @name search
  464. ///-----------------------------------
  465. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName depth:(NSString *)depth dateLastModified:(NSString *)dateLastModified contentType:(NSString *)contentType withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  466. if (!token){
  467. token = @"no token";
  468. }
  469. path = [path encodeString:NSUTF8StringEncoding];
  470. OCWebDAVClient *request = [OCWebDAVClient new];
  471. request = [self getRequestWithCredentials:request];
  472. [request search:path folder:folder fileName:fileName depth:depth dateLastModified:dateLastModified contentType:contentType user:_user userID:_userID onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  473. if (successRequest) {
  474. NSData *responseData = (NSData*) responseObject;
  475. OCXMLListParser *parser = [OCXMLListParser new];
  476. [parser initParserWithData:responseData];
  477. NSMutableArray *searchList = [parser.searchList mutableCopy];
  478. //Return success
  479. successRequest(response, searchList, request.redirectedServer, token);
  480. }
  481. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  482. failureRequest(response, error, token, request.redirectedServer);
  483. }];
  484. }
  485. ///-----------------------------------
  486. /// @name Setting favorite
  487. ///-----------------------------------
  488. - (void)settingFavoriteServer:(NSString *)path andFileOrFolderPath:(NSString *)filePath favorite:(BOOL)favorite withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest {
  489. if (!token){
  490. token = @"no token";
  491. }
  492. path = [NSString stringWithFormat:@"%@/files/%@/%@", path, _userID, filePath];
  493. path = [path encodeString:NSUTF8StringEncoding];
  494. OCWebDAVClient *request = [OCWebDAVClient new];
  495. request = [self getRequestWithCredentials:request];
  496. [request settingFavorite:path favorite:favorite onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  497. if (successRequest) {
  498. //Return success
  499. successRequest(response, request.redirectedServer, token);
  500. }
  501. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  502. NSLog(@"Failure");
  503. failureRequest(response, error, token, request.redirectedServer);
  504. }];
  505. }
  506. ///-----------------------------------
  507. /// @name Listing favorites
  508. ///-----------------------------------
  509. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  510. if (!token){
  511. token = @"no token";
  512. }
  513. path = [path encodeString:NSUTF8StringEncoding];
  514. OCWebDAVClient *request = [OCWebDAVClient new];
  515. request = [self getRequestWithCredentials:request];
  516. [request listingFavorites:path folder:folder user:_user userID:_userID onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  517. if (successRequest) {
  518. NSData *responseData = (NSData*) responseObject;
  519. OCXMLListParser *parser = [OCXMLListParser new];
  520. [parser initParserWithData:responseData];
  521. NSMutableArray *searchList = [parser.searchList mutableCopy];
  522. //Return success
  523. successRequest(response, searchList, request.redirectedServer, token);
  524. }
  525. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  526. failureRequest(response, error, token, request.redirectedServer);
  527. }];
  528. }
  529. #pragma mark - OC API Calls
  530. - (NSString *) getCurrentServerVersion {
  531. return self.currentServerVersion;
  532. }
  533. - (void) getServerVersionWithPath:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  534. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *serverVersion, NSString *redirectedServer)) success
  535. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  536. OCWebDAVClient *request = [OCWebDAVClient new];
  537. if (self.userAgent) {
  538. [request setUserAgent:self.userAgent];
  539. }
  540. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  541. NSData *data = (NSData*) responseObject;
  542. NSString *versionString = [NSString new];
  543. NSError* error=nil;
  544. if (data) {
  545. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
  546. if(error) {
  547. NSLog(@"Error parsing JSON: %@", error);
  548. } else {
  549. //Obtain the server version from the version field
  550. versionString = [jsonArray valueForKey:@"version"];
  551. self.currentServerVersion = versionString;
  552. }
  553. } else {
  554. NSLog(@"Error parsing JSON: data is null");
  555. }
  556. success(response, versionString, request.redirectedServer);
  557. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  558. failure(response, error, request.redirectedServer);
  559. }];
  560. }
  561. ///-----------------------------------
  562. /// @name Get UserName by cookie
  563. ///-----------------------------------
  564. - (void) getUserNameByCookie:(NSString *) cookieString ofServerPath:(NSString *)path onCommunication:
  565. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *response, NSData *responseData, NSString *redirectedServer))success
  566. failure:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failure{
  567. OCWebDAVClient *request = [OCWebDAVClient new];
  568. request = [self getRequestWithCredentials:request];
  569. [request requestUserNameOfServer: path byCookie:cookieString onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  570. success(response, responseObject, request.redirectedServer);
  571. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  572. failure(response, error, request.redirectedServer);
  573. }];
  574. }
  575. - (void) getFeaturesSupportedByServer:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  576. successRequest:(void(^)(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer)) success
  577. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  578. OCWebDAVClient *request = [OCWebDAVClient new];
  579. if (self.userAgent) {
  580. [request setUserAgent:self.userAgent];
  581. }
  582. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  583. if (responseObject) {
  584. NSError* error = nil;
  585. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: (NSData*) responseObject options: NSJSONReadingMutableContainers error: &error];
  586. if(error) {
  587. // NSLog(@"Error parsing JSON: %@", error);
  588. failure(response, error, request.redirectedServer);
  589. }else{
  590. self.currentServerVersion = [jsonArray valueForKey:@"version"];
  591. BOOL hasShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_shared];
  592. BOOL hasShareeSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_sharee_api];
  593. BOOL hasCookiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_cookies];
  594. BOOL hasForbiddenCharactersSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_forbidden_characters];
  595. BOOL hasCapabilitiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_capabilities];
  596. BOOL hasFedSharesOptionShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_share_option_fed_share];
  597. success(response, hasShareSupport, hasShareeSupport, hasCookiesSupport, hasForbiddenCharactersSupport, hasCapabilitiesSupport, hasFedSharesOptionShareSupport, request.redirectedServer);
  598. }
  599. } else {
  600. // NSLog(@"Error parsing JSON: data is null");
  601. failure(response, nil, request.redirectedServer);
  602. }
  603. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  604. failure(response, error, request.redirectedServer);
  605. }];
  606. }
  607. - (void) readSharedByServer: (NSString *) path
  608. onCommunication:(OCCommunication *)sharedOCCommunication
  609. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  610. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  611. path = [path encodeString:NSUTF8StringEncoding];
  612. path = [path stringByAppendingString:k_url_acces_shared_api];
  613. OCWebDAVClient *request = [OCWebDAVClient new];
  614. request = [self getRequestWithCredentials:request];
  615. [request listSharedByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  616. if (successRequest) {
  617. NSData *responseData = (NSData*) responseObject;
  618. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  619. //NSLog(@"response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
  620. [parser initParserWithData:responseData];
  621. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  622. //Return success
  623. successRequest(response, sharedList, request.redirectedServer);
  624. }
  625. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  626. failureRequest(response, error, request.redirectedServer);
  627. }];
  628. }
  629. - (void) readSharedByServer: (NSString *) serverPath andPath: (NSString *) path
  630. onCommunication:(OCCommunication *)sharedOCCommunication
  631. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  632. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  633. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  634. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  635. path = [path encodeString:NSUTF8StringEncoding];
  636. OCWebDAVClient *request = [OCWebDAVClient new];
  637. request = [self getRequestWithCredentials:request];
  638. [request listSharedByServer:serverPath andPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  639. if (successRequest) {
  640. NSData *responseData = (NSData*) responseObject;
  641. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  642. // NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  643. // NSLog(@"responseDataReadSharedByServer:andPath: %@", str);
  644. // NSLog(@"pathFolders: %@", path);
  645. // NSLog(@"serverPath: %@", serverPath);
  646. [parser initParserWithData:responseData];
  647. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  648. //Return success
  649. successRequest(response, sharedList, request.redirectedServer);
  650. }
  651. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  652. failureRequest(response, error, request.redirectedServer);
  653. }];
  654. }
  655. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath andPassword:(NSString *)password
  656. onCommunication:(OCCommunication *)sharedOCCommunication
  657. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer)) successRequest
  658. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  659. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  660. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  661. OCWebDAVClient *request = [OCWebDAVClient new];
  662. request = [self getRequestWithCredentials:request];
  663. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath andPassword:password onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  664. NSData *responseData = (NSData*) responseObject;
  665. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  666. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  667. [parser initParserWithData:responseData];
  668. switch (parser.statusCode) {
  669. case kOCSharedAPISuccessful:
  670. {
  671. NSString *url = parser.url;
  672. NSString *token = parser.token;
  673. if (url != nil) {
  674. successRequest(response, url, request.redirectedServer);
  675. }else if (token != nil){
  676. //We remove the \n and the empty spaces " "
  677. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  678. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  679. successRequest(response, token, request.redirectedServer);
  680. }else{
  681. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  682. failureRequest(response, error, request.redirectedServer);
  683. }
  684. break;
  685. }
  686. default:
  687. {
  688. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  689. failureRequest(response, error, request.redirectedServer);
  690. }
  691. }
  692. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  693. failureRequest(response, error, request.redirectedServer);
  694. }];
  695. }
  696. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath
  697. onCommunication:(OCCommunication *)sharedOCCommunication
  698. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *shareLink, NSString *redirectedServer)) successRequest
  699. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  700. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  701. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  702. OCWebDAVClient *request = [OCWebDAVClient new];
  703. request = [self getRequestWithCredentials:request];
  704. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  705. NSData *responseData = (NSData*) responseObject;
  706. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  707. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  708. [parser initParserWithData:responseData];
  709. switch (parser.statusCode) {
  710. case kOCSharedAPISuccessful:
  711. {
  712. NSString *url = parser.url;
  713. NSString *token = parser.token;
  714. if (url != nil) {
  715. successRequest(response, url, request.redirectedServer);
  716. }else if (token != nil){
  717. //We remove the \n and the empty spaces " "
  718. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  719. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  720. successRequest(response, token, request.redirectedServer);
  721. }else{
  722. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  723. failureRequest(response, error, request.redirectedServer);
  724. }
  725. break;
  726. }
  727. default:
  728. {
  729. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  730. failureRequest(response, error, request.redirectedServer);
  731. }
  732. }
  733. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  734. failureRequest(response, error, request.redirectedServer);
  735. }];
  736. }
  737. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andFileOrFolderPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  738. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  739. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest{
  740. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  741. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  742. userOrGroup = [userOrGroup encodeString:NSUTF8StringEncoding];
  743. OCWebDAVClient *request = [OCWebDAVClient new];
  744. request = [self getRequestWithCredentials:request];
  745. [request shareWith:userOrGroup shareeType:shareeType inServer:serverPath andPath:filePath andPermissions:permissions onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  746. NSData *responseData = (NSData*) responseObject;
  747. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  748. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  749. [parser initParserWithData:responseData];
  750. switch (parser.statusCode) {
  751. case kOCSharedAPISuccessful:
  752. {
  753. successRequest(response, request.redirectedServer);
  754. break;
  755. }
  756. default:
  757. {
  758. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  759. failureRequest(response, error, request.redirectedServer);
  760. }
  761. }
  762. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  763. failureRequest(response, error, request.redirectedServer);
  764. }];
  765. }
  766. - (void) unShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  767. onCommunication:(OCCommunication *)sharedOCCommunication
  768. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  769. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  770. path = [path encodeString:NSUTF8StringEncoding];
  771. path = [path stringByAppendingString:k_url_acces_shared_api];
  772. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  773. OCWebDAVClient *request = [OCWebDAVClient new];
  774. request = [self getRequestWithCredentials:request];
  775. [request unShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  776. if (successRequest) {
  777. //Return success
  778. successRequest(response, request.redirectedServer);
  779. }
  780. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  781. failureRequest(response, error, request.redirectedServer);
  782. }];
  783. }
  784. - (void) isShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  785. onCommunication:(OCCommunication *)sharedOCCommunication
  786. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, BOOL isShared, id shareDto)) successRequest
  787. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  788. path = [path encodeString:NSUTF8StringEncoding];
  789. path = [path stringByAppendingString:k_url_acces_shared_api];
  790. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  791. OCWebDAVClient *request = [OCWebDAVClient new];
  792. request = [self getRequestWithCredentials:request];
  793. [request isShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  794. if (successRequest) {
  795. NSData *responseData = (NSData*) responseObject;
  796. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  797. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  798. [parser initParserWithData:responseData];
  799. BOOL isShared = NO;
  800. OCSharedDto *shareDto = nil;
  801. if (parser.shareList) {
  802. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  803. if ([sharedList count] > 0) {
  804. isShared = YES;
  805. shareDto = [sharedList objectAtIndex:0];
  806. }
  807. }
  808. //Return success
  809. successRequest(response, request.redirectedServer, isShared, shareDto);
  810. }
  811. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  812. failureRequest(response, error, request.redirectedServer);
  813. }];
  814. }
  815. - (void) updateShare:(NSInteger)shareId ofServerPath:(NSString *)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions
  816. onCommunication:(OCCommunication *)sharedOCCommunication
  817. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  818. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  819. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  820. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  821. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)shareId]];
  822. OCWebDAVClient *request = [OCWebDAVClient new];
  823. request = [self getRequestWithCredentials:request];
  824. [request updateShareItem:shareId ofServerPath:serverPath withPasswordProtect:password andExpirationTime:expirationTime andPermissions:permissions onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  825. NSData *responseData = (NSData*) responseObject;
  826. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  827. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  828. [parser initParserWithData:responseData];
  829. switch (parser.statusCode) {
  830. case kOCSharedAPISuccessful:
  831. {
  832. successRequest(response, request.redirectedServer);
  833. break;
  834. }
  835. default:
  836. {
  837. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  838. failureRequest(response, error, request.redirectedServer);
  839. }
  840. }
  841. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  842. failureRequest(response, error, request.redirectedServer);
  843. }];
  844. }
  845. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  846. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  847. serverPath = [serverPath stringByAppendingString:k_url_access_sharee_api];
  848. searchString = [searchString encodeString:NSUTF8StringEncoding];
  849. OCWebDAVClient *request = [OCWebDAVClient new];
  850. request = [self getRequestWithCredentials:request];
  851. [request searchUsersAndGroupsWith:searchString forPage:page with:resultsPerPage ofServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  852. NSData *responseData = (NSData*) responseObject;
  853. NSMutableArray *itemList = [NSMutableArray new];
  854. //Parse
  855. NSError *error;
  856. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  857. if (error == nil) {
  858. NSDictionary *ocsDict = [jsongParsed valueForKey:@"ocs"];
  859. NSDictionary *metaDict = [ocsDict valueForKey:@"meta"];
  860. NSInteger statusCode = [[metaDict valueForKey:@"statuscode"] integerValue];
  861. if (statusCode == kOCShareeAPISuccessful || statusCode == kOCSharedAPISuccessful) {
  862. NSDictionary *dataDict = [ocsDict valueForKey:@"data"];
  863. NSArray *exactDict = [dataDict valueForKey:@"exact"];
  864. NSArray *usersFounded = [dataDict valueForKey:@"users"];
  865. NSArray *groupsFounded = [dataDict valueForKey:@"groups"];
  866. NSArray *usersRemote = [dataDict valueForKey:@"remotes"];
  867. NSArray *usersExact = [exactDict valueForKey:@"users"];
  868. NSArray *groupsExact = [exactDict valueForKey:@"groups"];
  869. NSArray *remotesExact = [exactDict valueForKey:@"remotes"];
  870. [self addUserItemOfType:shareTypeUser fromArray:usersFounded ToList:itemList];
  871. [self addUserItemOfType:shareTypeUser fromArray:usersExact ToList:itemList];
  872. [self addUserItemOfType:shareTypeRemote fromArray:usersRemote ToList:itemList];
  873. [self addUserItemOfType:shareTypeRemote fromArray:remotesExact ToList:itemList];
  874. [self addGroupItemFromArray:groupsFounded ToList:itemList];
  875. [self addGroupItemFromArray:groupsExact ToList:itemList];
  876. }else{
  877. NSString *message = (NSString*)[metaDict objectForKey:@"message"];
  878. if ([message isKindOfClass:[NSNull class]]) {
  879. message = @"";
  880. }
  881. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  882. failureRequest(response, error, request.redirectedServer);
  883. }
  884. //Return success
  885. successRequest(response, itemList, request.redirectedServer);
  886. }
  887. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  888. failureRequest(response, error, request.redirectedServer);
  889. }];
  890. }
  891. - (void)getSharePermissionsFile:(NSString *)fileName onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  892. fileName = [fileName encodeString:NSUTF8StringEncoding];
  893. OCWebDAVClient *request = [OCWebDAVClient new];
  894. request = [self getRequestWithCredentials:request];
  895. [request getSharePermissionsFile:fileName onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  896. if (successRequest) {
  897. NSString *permissions;
  898. NSData *responseData = (NSData *)responseObject;
  899. NSString *newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  900. NSLog(@"newStrReadFolder: %@", newStr);
  901. OCXMLParser *parser = [[OCXMLParser alloc]init];
  902. [parser initParserWithData:responseData];
  903. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  904. if ([directoryList count] == 1) {
  905. OCFileDto *file = [directoryList objectAtIndex:0];
  906. permissions = file.permissions;
  907. }
  908. //Return success
  909. successRequest(response, permissions, request.redirectedServer);
  910. }
  911. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  912. failureRequest(response, error, request.redirectedServer);
  913. }];
  914. }
  915. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  916. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  917. serverPath = [serverPath stringByAppendingString:k_url_capabilities];
  918. OCWebDAVClient *request = [OCWebDAVClient new];
  919. request = [self getRequestWithCredentials:request];
  920. [request getCapabilitiesOfServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  921. NSData *responseData = (NSData*) responseObject;
  922. //Parse
  923. NSError *error;
  924. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  925. NSLog(@"dic: %@",jsongParsed);
  926. OCCapabilities *capabilities = [OCCapabilities new];
  927. if (jsongParsed.allKeys > 0) {
  928. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  929. NSDictionary *data = [ocs valueForKey:@"data"];
  930. NSDictionary *version = [data valueForKey:@"version"];
  931. if (ocs.count > 0 && data.count > 0 && version.count > 0) {
  932. //VERSION
  933. NSNumber *versionMajorNumber = (NSNumber*) [version valueForKey:@"major"];
  934. NSNumber *versionMinorNumber = (NSNumber*) [version valueForKey:@"minor"];
  935. NSNumber *versionMicroNumber = (NSNumber*) [version valueForKey:@"micro"];
  936. capabilities.versionMajor = versionMajorNumber.integerValue;
  937. capabilities.versionMinor = versionMinorNumber.integerValue;
  938. capabilities.versionMicro = versionMicroNumber.integerValue;
  939. capabilities.versionString = (NSString*)[version valueForKey:@"string"];
  940. capabilities.versionEdition = (NSString*)[version valueForKey:@"edition"];
  941. NSDictionary *capabilitiesDict = [data valueForKey:@"capabilities"];
  942. NSDictionary *core = [capabilitiesDict valueForKey:@"core"];
  943. //CORE
  944. NSNumber *corePollIntervalNumber = (NSNumber*)[core valueForKey:@"pollinterval"];
  945. capabilities.corePollInterval = corePollIntervalNumber.integerValue;
  946. NSDictionary *fileSharing = [capabilitiesDict valueForKey:@"files_sharing"];
  947. //FILE SHARING
  948. NSNumber *fileSharingAPIEnabledNumber = (NSNumber*)[fileSharing valueForKey:@"api_enabled"];
  949. NSNumber *filesSharingReSharingEnabledNumber = (NSNumber*)[fileSharing valueForKey:@"resharing"];
  950. capabilities.isFilesSharingAPIEnabled = fileSharingAPIEnabledNumber.boolValue;
  951. capabilities.isFilesSharingReSharingEnabled = filesSharingReSharingEnabledNumber.boolValue;
  952. NSDictionary *fileSharingPublic = [fileSharing valueForKey:@"public"];
  953. NSNumber *filesSharingShareLinkEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"enabled"];
  954. NSNumber *filesSharingAllowPublicUploadsEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"upload"];
  955. NSNumber *filesSharingAllowUserSendMailNotificationAboutShareLinkEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"send_mail"];
  956. capabilities.isFilesSharingShareLinkEnabled = filesSharingShareLinkEnabledNumber.boolValue;
  957. capabilities.isFilesSharingAllowPublicUploadsEnabled = filesSharingAllowPublicUploadsEnabledNumber.boolValue;
  958. capabilities.isFilesSharingAllowUserSendMailNotificationAboutShareLinkEnabled = filesSharingAllowUserSendMailNotificationAboutShareLinkEnabledNumber.boolValue;
  959. NSDictionary *fileSharingPublicExpireDate = [fileSharingPublic valueForKey:@"expire_date"];
  960. NSNumber *filesSharingExpireDateByDefaultEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enabled"];
  961. NSNumber *filesSharingExpireDateEnforceEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enforced"];
  962. NSNumber *filesSharingExpireDateDaysNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"days"];
  963. capabilities.isFilesSharingExpireDateByDefaultEnabled = filesSharingExpireDateByDefaultEnabledNumber.boolValue;
  964. capabilities.isFilesSharingExpireDateEnforceEnabled = filesSharingExpireDateEnforceEnabledNumber.boolValue;
  965. capabilities.filesSharingExpireDateDaysNumber = filesSharingExpireDateDaysNumber.integerValue;
  966. NSDictionary *fileSharingPublicPassword = [fileSharingPublic valueForKey:@"password"];
  967. NSNumber *filesSharingPasswordEnforcedEnabledNumber = (NSNumber*)[fileSharingPublicPassword valueForKey:@"enforced"];
  968. capabilities.isFilesSharingPasswordEnforcedEnabled = filesSharingPasswordEnforcedEnabledNumber.boolValue;;
  969. NSDictionary *fileSharingUser = [fileSharing valueForKey:@"user"];
  970. NSNumber *filesSharingAllowUserSendMailNotificationAboutOtherUsersEnabledNumber = (NSNumber*)[fileSharingUser valueForKey:@"send_mail"];
  971. capabilities.isFilesSharingAllowUserSendMailNotificationAboutOtherUsersEnabled = filesSharingAllowUserSendMailNotificationAboutOtherUsersEnabledNumber.boolValue;
  972. //FEDERATION
  973. NSDictionary *fileSharingFederation = [fileSharing valueForKey:@"federation"];
  974. NSNumber *filesSharingAllowUserSendSharesToOtherServersEnabledNumber = (NSNumber*)[fileSharingFederation valueForKey:@"outgoing"];
  975. NSNumber *filesSharingAllowUserReceiveSharesToOtherServersEnabledNumber = (NSNumber*)[fileSharingFederation valueForKey:@"incoming"];
  976. capabilities.isFilesSharingAllowUserSendSharesToOtherServersEnabled = filesSharingAllowUserSendSharesToOtherServersEnabledNumber.boolValue;
  977. capabilities.isFilesSharingAllowUserReceiveSharesToOtherServersEnabled = filesSharingAllowUserReceiveSharesToOtherServersEnabledNumber.boolValue;
  978. // EXTERNAL SITES
  979. NSDictionary *externalSitesDic = [capabilitiesDict valueForKey:@"external"];
  980. if (externalSitesDic) {
  981. NSArray *externalSitesArray = [externalSitesDic valueForKey:@"v1"];
  982. if (externalSitesArray)
  983. if ([[externalSitesArray objectAtIndex:0] isEqualToString:@"sites"])
  984. capabilities.isExternalSitesServerEnabled = YES;
  985. }
  986. //FILES
  987. NSDictionary *files = [capabilitiesDict valueForKey:@"files"];
  988. NSNumber *fileBigFileChunkingEnabledNumber = (NSNumber*)[files valueForKey:@"bigfilechunking"];
  989. NSNumber *fileUndeleteEnabledNumber = (NSNumber*)[files valueForKey:@"undelete"];
  990. NSNumber *fileVersioningEnabledNumber = (NSNumber*)[files valueForKey:@"versioning"];
  991. capabilities.isFileBigFileChunkingEnabled = fileBigFileChunkingEnabledNumber.boolValue;
  992. capabilities.isFileUndeleteEnabled = fileUndeleteEnabledNumber.boolValue;
  993. capabilities.isFileVersioningEnabled = fileVersioningEnabledNumber.boolValue;
  994. //THEMING
  995. NSDictionary *theming = [capabilitiesDict valueForKey:@"theming"];
  996. if ([theming count] > 0) {
  997. if ([theming valueForKey:@"background"] && ![[theming valueForKey:@"background"] isEqual:[NSNull null]])
  998. capabilities.themingBackground = [theming valueForKey:@"background"];
  999. if ([theming valueForKey:@"background-default"] && ![[theming valueForKey:@"background-default"] isEqual:[NSNull null]]) {
  1000. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-default"];
  1001. capabilities.themingBackgroundDefault = result.boolValue;
  1002. }
  1003. if ([theming valueForKey:@"background-plain"] && ![[theming valueForKey:@"background-plain"] isEqual:[NSNull null]]) {
  1004. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-plain"];
  1005. capabilities.themingBackgroundPlain = result.boolValue;
  1006. }
  1007. if ([theming valueForKey:@"color"] && ![[theming valueForKey:@"color"] isEqual:[NSNull null]])
  1008. capabilities.themingColor = [theming valueForKey:@"color"];
  1009. if ([theming valueForKey:@"color-element"] && ![[theming valueForKey:@"color-element"] isEqual:[NSNull null]])
  1010. capabilities.themingColorElement = [theming valueForKey:@"color-element"];
  1011. if ([theming valueForKey:@"color-text"] && ![[theming valueForKey:@"color-text"] isEqual:[NSNull null]])
  1012. capabilities.themingColorText = [theming valueForKey:@"color-text"];
  1013. if ([theming valueForKey:@"logo"] && ![[theming valueForKey:@"logo"] isEqual:[NSNull null]])
  1014. capabilities.themingLogo = [theming valueForKey:@"logo"];
  1015. if ([theming valueForKey:@"name"] && ![[theming valueForKey:@"name"] isEqual:[NSNull null]])
  1016. capabilities.themingName = [theming valueForKey:@"name"];
  1017. if ([theming valueForKey:@"slogan"] && ![[theming valueForKey:@"slogan"] isEqual:[NSNull null]])
  1018. capabilities.themingSlogan = [theming valueForKey:@"slogan"];
  1019. if ([theming valueForKey:@"url"] && ![[theming valueForKey:@"url"] isEqual:[NSNull null]])
  1020. capabilities.themingUrl = [theming valueForKey:@"url"];
  1021. }
  1022. //END TO END Encryption
  1023. NSDictionary *endToEndEncryption = [capabilitiesDict valueForKey:@"end-to-end-encryption"];
  1024. if ([endToEndEncryption count] > 0) {
  1025. NSNumber *endToEndEncryptionEnabled = (NSNumber*)[endToEndEncryption valueForKey:@"enabled"];
  1026. capabilities.isEndToEndEncryptionEnabled = endToEndEncryptionEnabled.boolValue;
  1027. if ([endToEndEncryption valueForKey:@"api-version"] && ![[endToEndEncryption valueForKey:@"api-version"] isEqual:[NSNull null]])
  1028. capabilities.endToEndEncryptionVersion = [endToEndEncryption valueForKey:@"api-version"];
  1029. }
  1030. }
  1031. successRequest(response, capabilities, request.redirectedServer);
  1032. } else {
  1033. failureRequest(response, error, request.redirectedServer);
  1034. }
  1035. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1036. failureRequest(response, error, request.redirectedServer);
  1037. }];
  1038. }
  1039. #pragma mark - Remote thumbnails
  1040. - (NSURLSessionTask *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCComunication
  1041. successRequest:(void(^)(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer)) successRequest
  1042. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1043. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1044. serverPath = [serverPath stringByAppendingString:k_url_thumbnails];
  1045. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1046. OCWebDAVClient *request = [OCWebDAVClient new];
  1047. request = [self getRequestWithCredentials:request];
  1048. OCHTTPRequestOperation *operation = [request getRemoteThumbnailByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight onCommunication:sharedOCComunication
  1049. success:^(NSHTTPURLResponse *response, id responseObject) {
  1050. NSData *responseData = (NSData*) responseObject;
  1051. successRequest(response, responseData, request.redirectedServer);
  1052. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1053. failureRequest(response, error, request.redirectedServer);
  1054. }];
  1055. [operation resume];
  1056. return operation;
  1057. }
  1058. #pragma mark - Notification Server
  1059. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1060. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1061. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_notification_api];
  1062. OCWebDAVClient *request = [OCWebDAVClient new];
  1063. request = [self getRequestWithCredentials:request];
  1064. [request getNotificationServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1065. NSData *responseData = (NSData*) responseObject;
  1066. //Parse
  1067. NSError *error;
  1068. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1069. NSLog(@"[LOG] Notifications : %@",jsongParsed);
  1070. NSMutableArray *listOfNotifications = [NSMutableArray new];
  1071. if (jsongParsed.allKeys > 0) {
  1072. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1073. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1074. NSDictionary *datas = [ocs valueForKey:@"data"];
  1075. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1076. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1077. for (NSDictionary *data in datas) {
  1078. OCNotifications *notification = [OCNotifications new];
  1079. if ([data valueForKey:@"notification_id"] && ![[data valueForKey:@"notification_id"] isEqual:[NSNull null]])
  1080. notification.idNotification = [[data valueForKey:@"notification_id"] integerValue];
  1081. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1082. notification.application = [data valueForKey:@"app"];
  1083. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1084. notification.user = [data valueForKey:@"user"];
  1085. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1086. NSString *dateString = [data valueForKey:@"datetime"];
  1087. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1088. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1089. [dateFormatter setLocale:enUSPOSIXLocale];
  1090. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1091. notification.date = [dateFormatter dateFromString:dateString];
  1092. }
  1093. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1094. notification.typeObject = [data valueForKey:@"object_type"];
  1095. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1096. notification.idObject = [data valueForKey:@"object_id"];
  1097. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1098. notification.subject = [data valueForKey:@"subject"];
  1099. if ([data valueForKey:@"subjectRich"] && ![[data valueForKey:@"subjectRich"] isEqual:[NSNull null]])
  1100. notification.subjectRich = [data valueForKey:@"subjectRich"];
  1101. if ([data valueForKey:@"subjectRichParameters"] && ![[data valueForKey:@"subjectRichParameters"] isEqual:[NSNull null]])
  1102. notification.subjectRichParameters = [data valueForKey:@"subjectRichParameters"];
  1103. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1104. notification.message = [data valueForKey:@"message"];
  1105. if ([data valueForKey:@"messageRich"] && ![[data valueForKey:@"messageRich"] isEqual:[NSNull null]])
  1106. notification.messageRich = [data valueForKey:@"messageRich"];
  1107. if ([data valueForKey:@"messageRichParameters"] && ![[data valueForKey:@"messageRichParameters"] isEqual:[NSNull null]])
  1108. notification.messageRichParameters = [data valueForKey:@"messageRichParameters"];
  1109. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1110. notification.link = [data valueForKey:@"link"];
  1111. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1112. notification.icon = [data valueForKey:@"icon"];
  1113. /* ACTION */
  1114. NSMutableArray *actionsArr = [NSMutableArray new];
  1115. NSDictionary *actions = [data valueForKey:@"actions"];
  1116. for (NSDictionary *action in actions) {
  1117. OCNotificationsAction *notificationAction = [OCNotificationsAction new];
  1118. if ([action valueForKey:@"label"] && ![[action valueForKey:@"label"] isEqual:[NSNull null]])
  1119. notificationAction.label = [action valueForKey:@"label"];
  1120. if ([action valueForKey:@"link"] && ![[action valueForKey:@"link"] isEqual:[NSNull null]])
  1121. notificationAction.link = [action valueForKey:@"link"];
  1122. if ([action valueForKey:@"primary"] && ![[action valueForKey:@"primary"] isEqual:[NSNull null]])
  1123. notificationAction.primary = [[action valueForKey:@"primary"] boolValue];
  1124. if ([action valueForKey:@"type"] && ![[action valueForKey:@"type"] isEqual:[NSNull null]])
  1125. notificationAction.type = [action valueForKey:@"type"];
  1126. [actionsArr addObject:notificationAction];
  1127. }
  1128. notification.actions = [[NSArray alloc] initWithArray:actionsArr];
  1129. [listOfNotifications addObject:notification];
  1130. }
  1131. } else {
  1132. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1133. if ([message isKindOfClass:[NSNull class]]) {
  1134. message = @"";
  1135. }
  1136. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1137. failureRequest(response, error, request.redirectedServer);
  1138. }
  1139. }
  1140. //Return success
  1141. successRequest(response, listOfNotifications, request.redirectedServer);
  1142. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1143. failureRequest(response, error, request.redirectedServer);
  1144. }];
  1145. }
  1146. - (void)setNotificationServer:(NSString*)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1147. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1148. OCWebDAVClient *request = [OCWebDAVClient new];
  1149. request = [self getRequestWithCredentials:request];
  1150. __weak OCWebDAVClient *wrequest = request;
  1151. [request setNotificationServer:serverPath type:type onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1152. successRequest(response, wrequest.redirectedServer);
  1153. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1154. failureRequest(response, error, wrequest.redirectedServer);
  1155. }];
  1156. }
  1157. - (void)subscribingNextcloudServerPush:(NSString *)serverPath pushTokenHash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey proxyServerPath:(NSString *)proxyServerPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer)) successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1158. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1159. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1160. OCWebDAVClient *request = [OCWebDAVClient new];
  1161. request = [self getRequestWithCredentials:request];
  1162. [request subscribingNextcloudServerPush:serverPath authorizationToken:_password pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath:proxyServerPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1163. NSData *responseData = (NSData*) responseObject;
  1164. //Parse
  1165. NSError *error;
  1166. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1167. NSLog(@"[LOG] Subscribing at the Nextcloud server : %@",jsongParsed);
  1168. NSString *publicKey = [jsongParsed objectForKey:@"publicKey"];
  1169. NSString *deviceIdentifier = [jsongParsed objectForKey:@"deviceIdentifier"];
  1170. NSString *signature = [jsongParsed objectForKey:@"signature"];
  1171. successRequest(response, publicKey, deviceIdentifier, signature, request.redirectedServer);
  1172. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1173. failureRequest(response, error, request.redirectedServer);
  1174. }];
  1175. }
  1176. - (void)subscribingPushProxy:(NSString *)serverPath pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature userPublicKey:(NSString *)userPublicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *redirectedServer))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1177. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1178. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1179. OCWebDAVClient *request = [OCWebDAVClient new];
  1180. request = [self getRequestWithCredentials:request];
  1181. [request subscribingPushProxy:serverPath authorizationToken:_password pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature userPublicKey:userPublicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1182. if (successRequest) {
  1183. //Return success
  1184. successRequest(response, request.redirectedServer);
  1185. }
  1186. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1187. failureRequest(response, error, request.redirectedServer);
  1188. }];
  1189. }
  1190. #pragma mark - Activity
  1191. - (void) getActivityServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1192. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1193. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_activity_api];
  1194. OCWebDAVClient *request = [OCWebDAVClient new];
  1195. request = [self getRequestWithCredentials:request];
  1196. [request getActivityServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1197. NSData *responseData = (NSData*) responseObject;
  1198. //Parse
  1199. NSError *error;
  1200. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1201. NSLog(@"[LOG] Activity : %@",jsongParsed);
  1202. NSMutableArray *listOfActivity = [NSMutableArray new];
  1203. if (jsongParsed.allKeys > 0) {
  1204. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1205. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1206. NSDictionary *datas = [ocs valueForKey:@"data"];
  1207. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1208. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1209. for (NSDictionary *data in datas) {
  1210. OCActivity *activity = [OCActivity new];
  1211. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isEqual:[NSNull null]])
  1212. activity.idActivity = [[data valueForKey:@"id"] integerValue];
  1213. if ([data valueForKey:@"date"] && ![[data valueForKey:@"date"] isEqual:[NSNull null]]) {
  1214. NSString *dateString = [data valueForKey:@"date"];
  1215. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1216. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1217. [dateFormatter setLocale:enUSPOSIXLocale];
  1218. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1219. activity.date = [dateFormatter dateFromString:dateString];
  1220. }
  1221. if ([data valueForKey:@"file"] && ![[data valueForKey:@"file"] isEqual:[NSNull null]])
  1222. activity.file = [data valueForKey:@"file"];
  1223. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1224. activity.link = [data valueForKey:@"link"];
  1225. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1226. activity.message = [data valueForKey:@"message"];
  1227. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1228. activity.subject = [data valueForKey:@"subject"];
  1229. [listOfActivity addObject:activity];
  1230. }
  1231. } else {
  1232. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1233. if ([message isKindOfClass:[NSNull class]]) {
  1234. message = @"";
  1235. }
  1236. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1237. failureRequest(response, error, request.redirectedServer);
  1238. }
  1239. }
  1240. //Return success
  1241. successRequest(response, listOfActivity, request.redirectedServer);
  1242. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1243. failureRequest(response, error, request.redirectedServer);
  1244. }];
  1245. }
  1246. #pragma mark - External sites
  1247. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1248. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1249. serverPath = [serverPath stringByAppendingString:k_url_acces_external_sites_api];
  1250. OCWebDAVClient *request = [OCWebDAVClient new];
  1251. request = [self getRequestWithCredentials:request];
  1252. [request getExternalSitesServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1253. NSData *responseData = (NSData*) responseObject;
  1254. //Parse
  1255. NSError *error;
  1256. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1257. NSLog(@"[LOG] External Sites : %@",jsongParsed);
  1258. NSMutableArray *listOfExternalSites = [NSMutableArray new];
  1259. if (jsongParsed.allKeys > 0) {
  1260. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1261. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1262. NSDictionary *datas = [ocs valueForKey:@"data"];
  1263. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1264. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1265. for (NSDictionary *data in datas) {
  1266. OCExternalSites *externalSites = [OCExternalSites new];
  1267. externalSites.idExternalSite = [[data valueForKey:@"id"] integerValue];
  1268. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1269. externalSites.icon = [data valueForKey:@"icon"];
  1270. if ([data valueForKey:@"lang"] && ![[data valueForKey:@"lang"] isEqual:[NSNull null]])
  1271. externalSites.lang = [data valueForKey:@"lang"];
  1272. if ([data valueForKey:@"name"] && ![[data valueForKey:@"name"] isEqual:[NSNull null]])
  1273. externalSites.name = [data valueForKey:@"name"];
  1274. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isEqual:[NSNull null]])
  1275. externalSites.url = [data valueForKey:@"url"];
  1276. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1277. externalSites.type = [data valueForKey:@"type"];
  1278. [listOfExternalSites addObject:externalSites];
  1279. }
  1280. } else {
  1281. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1282. if ([message isKindOfClass:[NSNull class]]) {
  1283. message = @"";
  1284. }
  1285. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1286. failureRequest(response, error, request.redirectedServer);
  1287. }
  1288. }
  1289. //Return success
  1290. successRequest(response, listOfExternalSites, request.redirectedServer);
  1291. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1292. failureRequest(response, error, request.redirectedServer);
  1293. }];
  1294. }
  1295. #pragma mark - User Profile
  1296. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1297. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_userprofile_api];
  1298. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1299. OCWebDAVClient *request = [OCWebDAVClient new];
  1300. request = [self getRequestWithCredentials:request];
  1301. [request getUserProfileServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1302. NSData *responseData = (NSData*) responseObject;
  1303. //Parse
  1304. NSError *error;
  1305. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1306. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  1307. OCUserProfile *userProfile = [OCUserProfile new];
  1308. if (jsongParsed.allKeys > 0) {
  1309. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1310. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1311. NSDictionary *data = [ocs valueForKey:@"data"];
  1312. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1313. if (statusCode == kOCUserProfileAPISuccessful) {
  1314. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  1315. userProfile.address = [data valueForKey:@"address"];
  1316. if ([data valueForKey:@"display-name"] && ![[data valueForKey:@"display-name"] isKindOfClass:[NSNull class]])
  1317. userProfile.displayName = [data valueForKey:@"display-name"];
  1318. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  1319. userProfile.email = [data valueForKey:@"email"];
  1320. if ([data valueForKey:@"enabled"] && ![[data valueForKey:@"enabled"] isKindOfClass:[NSNull class]])
  1321. userProfile.enabled = [[data valueForKey:@"enabled"] boolValue];
  1322. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isKindOfClass:[NSNull class]])
  1323. userProfile.id = [data valueForKey:@"id"];
  1324. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  1325. userProfile.phone = [data valueForKey:@"phone"];
  1326. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  1327. userProfile.twitter = [data valueForKey:@"twitter"];
  1328. if ([data valueForKey:@"webpage"] && ![[data valueForKey:@"webpage"] isKindOfClass:[NSNull class]])
  1329. userProfile.webpage = [data valueForKey:@"webpage"];
  1330. /* QUOTA */
  1331. NSDictionary *quota = [data valueForKey:@"quota"];
  1332. if ([quota count] > 0) {
  1333. if ([quota valueForKey:@"free"] && ![[quota valueForKey:@"free"] isKindOfClass:[NSNull class]])
  1334. userProfile.quotaFree = [[quota valueForKey:@"free"] doubleValue];
  1335. if ([quota valueForKey:@"quota"] && ![[quota valueForKey:@"quota"] isKindOfClass:[NSNull class]])
  1336. userProfile.quota = [[quota valueForKey:@"quota"] doubleValue];
  1337. if ([quota valueForKey:@"relative"] && ![[quota valueForKey:@"relative"] isKindOfClass:[NSNull class]])
  1338. userProfile.quotaRelative = [[quota valueForKey:@"relative"] doubleValue];
  1339. if ([quota valueForKey:@"total"] && ![[quota valueForKey:@"total"] isKindOfClass:[NSNull class]])
  1340. userProfile.quotaTotal = [[quota valueForKey:@"total"] doubleValue];
  1341. if ([quota valueForKey:@"used"] && ![[quota valueForKey:@"used"] isKindOfClass:[NSNull class]])
  1342. userProfile.quotaUsed = [[quota valueForKey:@"used"] doubleValue];
  1343. }
  1344. } else {
  1345. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1346. if ([message isKindOfClass:[NSNull class]]) {
  1347. message = @"";
  1348. }
  1349. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1350. failureRequest(response, error, request.redirectedServer);
  1351. }
  1352. }
  1353. //Return success
  1354. successRequest(response, userProfile, request.redirectedServer);
  1355. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1356. failureRequest(response, error, request.redirectedServer);
  1357. }];
  1358. }
  1359. #pragma mark - End-to-End Encryption
  1360. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1361. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1362. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1363. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1364. OCWebDAVClient *request = [OCWebDAVClient new];
  1365. request = [self getRequestWithCredentials:request];
  1366. [request getEndToEndPublicKeys:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1367. NSData *responseData = (NSData*) responseObject;
  1368. NSString *publicKey;
  1369. //Parse
  1370. NSError *error;
  1371. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1372. NSLog(@"[LOG] E2E Get PublicKey : %@",jsongParsed);
  1373. if (jsongParsed.allKeys > 0) {
  1374. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1375. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1376. NSDictionary *data = [ocs valueForKey:@"data"];
  1377. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1378. if (statusCode == kOCUserProfileAPISuccessful) {
  1379. if ([data valueForKey:@"public-keys"] && ![[data valueForKey:@"public-keys"] isKindOfClass:[NSNull class]]) {
  1380. NSDictionary *publickeys = [data valueForKey:@"public-keys"];
  1381. publicKey = [publickeys valueForKey:self.userID];
  1382. }
  1383. } else {
  1384. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1385. if ([message isKindOfClass:[NSNull class]]) {
  1386. message = @"";
  1387. }
  1388. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1389. failureRequest(response, error, request.redirectedServer);
  1390. }
  1391. }
  1392. //Return success
  1393. successRequest(response, publicKey, request.redirectedServer);
  1394. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1395. //Return error
  1396. failureRequest(response, error, request.redirectedServer);
  1397. }];
  1398. }
  1399. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1400. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1401. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1402. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1403. OCWebDAVClient *request = [OCWebDAVClient new];
  1404. request = [self getRequestWithCredentials:request];
  1405. [request getEndToEndPrivateKeyCipher:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1406. NSData *responseData = (NSData*) responseObject;
  1407. NSString *privateKeyChiper;
  1408. //Parse
  1409. NSError *error;
  1410. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1411. NSLog(@"[LOG] E2E Get PrivateKey : %@",jsongParsed);
  1412. if (jsongParsed.allKeys > 0) {
  1413. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1414. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1415. NSDictionary *data = [ocs valueForKey:@"data"];
  1416. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1417. if (statusCode == kOCUserProfileAPISuccessful) {
  1418. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]])
  1419. privateKeyChiper = [data valueForKey:@"private-key"];
  1420. } else {
  1421. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1422. if ([message isKindOfClass:[NSNull class]]) {
  1423. message = @"";
  1424. }
  1425. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1426. failureRequest(response, error, request.redirectedServer);
  1427. }
  1428. }
  1429. //Return success
  1430. successRequest(response, privateKeyChiper, request.redirectedServer);
  1431. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1432. //Return error
  1433. failureRequest(response, error, request.redirectedServer);
  1434. }];
  1435. }
  1436. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1437. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1438. serverPath = [serverPath stringByAppendingString:@"/server-key"];
  1439. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1440. OCWebDAVClient *request = [OCWebDAVClient new];
  1441. request = [self getRequestWithCredentials:request];
  1442. [request getEndToEndServerPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1443. NSData *responseData = (NSData*) responseObject;
  1444. NSString *publicKey;
  1445. //Parse
  1446. NSError *error;
  1447. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1448. NSLog(@"[LOG] E2E Get Server PublicKey : %@",jsongParsed);
  1449. if (jsongParsed.allKeys > 0) {
  1450. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1451. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1452. NSDictionary *data = [ocs valueForKey:@"data"];
  1453. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1454. if (statusCode == kOCUserProfileAPISuccessful) {
  1455. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1456. publicKey = [data valueForKey:@"public-key"];
  1457. }
  1458. } else {
  1459. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1460. if ([message isKindOfClass:[NSNull class]]) {
  1461. message = @"";
  1462. }
  1463. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1464. failureRequest(response, error, request.redirectedServer);
  1465. }
  1466. }
  1467. //Return success
  1468. successRequest(response, publicKey, request.redirectedServer);
  1469. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1470. //Return error
  1471. failureRequest(response, error, request.redirectedServer);
  1472. }];
  1473. }
  1474. - (void)signEndToEndPublicKey:(NSString*)serverPath publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey,NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1475. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1476. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1477. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1478. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1479. request = [self getRequestWithCredentials:request];
  1480. [request signEndToEndPublicKey:serverPath key:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1481. NSData *responseData = (NSData*) responseObject;
  1482. NSString *publicKey;
  1483. //Parse
  1484. NSError *error;
  1485. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1486. NSLog(@"[LOG] E2E Sign PublicKey : %@",jsongParsed);
  1487. if (jsongParsed.allKeys > 0) {
  1488. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1489. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1490. NSDictionary *data = [ocs valueForKey:@"data"];
  1491. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1492. if (statusCode == kOCUserProfileAPISuccessful) {
  1493. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1494. publicKey = [data valueForKey:@"public-key"];
  1495. }
  1496. } else {
  1497. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1498. if ([message isKindOfClass:[NSNull class]]) {
  1499. message = @"";
  1500. }
  1501. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1502. failureRequest(response, error, request.redirectedServer);
  1503. }
  1504. }
  1505. //Return success
  1506. successRequest(response, publicKey, request.redirectedServer);
  1507. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1508. //Return error
  1509. failureRequest(response, error, request.redirectedServer);
  1510. }];
  1511. }
  1512. - (void)storeEndToEndPrivateKeyCipher:(NSString*)serverPath privateKeyChiper:(NSString *)privateKeyChiper onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1513. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1514. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1515. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1516. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1517. request = [self getRequestWithCredentials:request];
  1518. [request storeEndToEndPrivateKeyCipher:serverPath key:privateKeyChiper onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1519. NSData *responseData = (NSData*) responseObject;
  1520. NSString *privateKey;
  1521. //Parse
  1522. NSError *error;
  1523. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1524. NSLog(@"[LOG] E2E Store PrivateKey : %@",jsongParsed);
  1525. if (jsongParsed.allKeys > 0) {
  1526. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1527. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1528. NSDictionary *data = [ocs valueForKey:@"data"];
  1529. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1530. if (statusCode == kOCUserProfileAPISuccessful) {
  1531. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]]) {
  1532. privateKey = [data valueForKey:@"private-key"];
  1533. }
  1534. } else {
  1535. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1536. if ([message isKindOfClass:[NSNull class]]) {
  1537. message = @"";
  1538. }
  1539. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1540. failureRequest(response, error, request.redirectedServer);
  1541. }
  1542. }
  1543. //Return success
  1544. successRequest(response, privateKey, request.redirectedServer);
  1545. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1546. //Return error
  1547. failureRequest(response, error, request.redirectedServer);
  1548. }];
  1549. }
  1550. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1551. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1552. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1553. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1554. OCWebDAVClient *request = [OCWebDAVClient new];
  1555. request = [self getRequestWithCredentials:request];
  1556. [request deleteEndToEndPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1557. //Return success
  1558. successRequest(response, request.redirectedServer);
  1559. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1560. //Return error
  1561. failureRequest(response, error, request.redirectedServer);
  1562. }];
  1563. }
  1564. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1565. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1566. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1567. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1568. OCWebDAVClient *request = [OCWebDAVClient new];
  1569. request = [self getRequestWithCredentials:request];
  1570. [request deleteEndToEndPrivateKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1571. //Return success
  1572. successRequest(response, request.redirectedServer);
  1573. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1574. //Return error
  1575. failureRequest(response, error, request.redirectedServer);
  1576. }];
  1577. }
  1578. - (void)markEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1579. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1580. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1581. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1582. OCWebDAVClient *request = [OCWebDAVClient new];
  1583. request = [self getRequestWithCredentials:request];
  1584. [request markEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1585. //Return success
  1586. successRequest(response, request.redirectedServer);
  1587. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1588. //Return error
  1589. failureRequest(response, error, request.redirectedServer);
  1590. }];
  1591. }
  1592. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1593. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1594. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1595. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1596. OCWebDAVClient *request = [OCWebDAVClient new];
  1597. request = [self getRequestWithCredentials:request];
  1598. [request deletemarkEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1599. //Return success
  1600. successRequest(response, request.redirectedServer);
  1601. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1602. //Return error
  1603. failureRequest(response, error, request.redirectedServer);
  1604. }];
  1605. }
  1606. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID token:(NSString *)token onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1607. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1608. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1609. if (token) {
  1610. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1611. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1612. } else {
  1613. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1614. }
  1615. OCWebDAVClient *request = [OCWebDAVClient new];
  1616. request = [self getRequestWithCredentials:request];
  1617. [request lockEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1618. NSData *responseData = (NSData*) responseObject;
  1619. NSString *token;
  1620. //Parse
  1621. NSError *error;
  1622. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1623. NSLog(@"[LOG] E2E Lock File : %@",jsongParsed);
  1624. if (jsongParsed.allKeys > 0) {
  1625. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1626. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1627. NSDictionary *data = [ocs valueForKey:@"data"];
  1628. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1629. if (statusCode == kOCUserProfileAPISuccessful) {
  1630. if ([data valueForKey:@"token"] && ![[data valueForKey:@"token"] isKindOfClass:[NSNull class]]) {
  1631. token = [data valueForKey:@"token"];
  1632. }
  1633. } else {
  1634. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1635. if ([message isKindOfClass:[NSNull class]]) {
  1636. message = @"";
  1637. }
  1638. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1639. failureRequest(response, error, request.redirectedServer);
  1640. }
  1641. }
  1642. //Return success
  1643. successRequest(response, token, request.redirectedServer);
  1644. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1645. //Return error
  1646. failureRequest(response, error, request.redirectedServer);
  1647. }];
  1648. }
  1649. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID token:(NSString *)token onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1650. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1651. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1652. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1653. OCWebDAVClient *request = [OCWebDAVClient new];
  1654. request = [self getRequestWithCredentials:request];
  1655. [request unlockEndToEndFolderEncrypted:serverPath token:token onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1656. //Return success
  1657. successRequest(response, request.redirectedServer);
  1658. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1659. //Return error
  1660. failureRequest(response, error, request.redirectedServer);
  1661. }];
  1662. }
  1663. - (void)getEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1664. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1665. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1666. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1667. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1668. request = [self getRequestWithCredentials:request];
  1669. [request getEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1670. NSData *responseData = (NSData*) responseObject;
  1671. NSString *encryptedMetadata;
  1672. //Parse
  1673. NSError *error;
  1674. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1675. NSLog(@"[LOG] E2E Get Metadata : %@",jsongParsed);
  1676. if (jsongParsed.allKeys > 0) {
  1677. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1678. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1679. NSDictionary *data = [ocs valueForKey:@"data"];
  1680. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1681. if (statusCode == kOCUserProfileAPISuccessful) {
  1682. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1683. encryptedMetadata = [data valueForKey:@"meta-data"];
  1684. }
  1685. } else {
  1686. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1687. if ([message isKindOfClass:[NSNull class]]) {
  1688. message = @"";
  1689. }
  1690. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1691. failureRequest(response, error, request.redirectedServer);
  1692. }
  1693. }
  1694. //Return success
  1695. successRequest(response, encryptedMetadata, request.redirectedServer);
  1696. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1697. //Return error
  1698. failureRequest(response, error, request.redirectedServer);
  1699. }];
  1700. }
  1701. - (void)storeEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID encryptedMetadata:(NSString *)encryptedMetadata onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1702. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1703. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1704. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1705. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1706. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1707. request = [self getRequestWithCredentials:request];
  1708. [request storeEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1709. NSData *responseData = (NSData*) responseObject;
  1710. NSString *encryptedMetadata;
  1711. //Parse
  1712. NSError *error;
  1713. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1714. NSLog(@"[LOG] E2E Store Metadata : %@",jsongParsed);
  1715. if (jsongParsed.allKeys > 0) {
  1716. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1717. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1718. NSDictionary *data = [ocs valueForKey:@"data"];
  1719. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1720. if (statusCode == kOCUserProfileAPISuccessful) {
  1721. if ([data valueForKey:@"encrypted-meta-data"] && ![[data valueForKey:@"encrypted-meta-data"] isKindOfClass:[NSNull class]]) {
  1722. encryptedMetadata = [data valueForKey:@"encrypted-meta-data"];
  1723. }
  1724. } else {
  1725. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1726. if ([message isKindOfClass:[NSNull class]]) {
  1727. message = @"";
  1728. }
  1729. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1730. failureRequest(response, error, request.redirectedServer);
  1731. }
  1732. }
  1733. //Return success
  1734. successRequest(response, encryptedMetadata, request.redirectedServer);
  1735. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1736. //Return error
  1737. failureRequest(response, error, request.redirectedServer);
  1738. }];
  1739. }
  1740. - (void)updateEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID encryptedMetadata:(NSString *)encryptedMetadata token:(NSString *)token onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1741. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1742. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1743. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1744. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1745. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1746. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1747. request = [self getRequestWithCredentials:request];
  1748. [request updateEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1749. NSData *responseData = (NSData*) responseObject;
  1750. NSString *encryptedMetadata;
  1751. //Parse
  1752. NSError *error;
  1753. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1754. NSLog(@"[LOG] E2E Update Metadata : %@",jsongParsed);
  1755. if (jsongParsed.allKeys > 0) {
  1756. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1757. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1758. NSDictionary *data = [ocs valueForKey:@"data"];
  1759. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1760. if (statusCode == kOCUserProfileAPISuccessful) {
  1761. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1762. encryptedMetadata = [data valueForKey:@"meta-data"];
  1763. }
  1764. } else {
  1765. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1766. if ([message isKindOfClass:[NSNull class]]) {
  1767. message = @"";
  1768. }
  1769. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1770. failureRequest(response, error, request.redirectedServer);
  1771. }
  1772. }
  1773. //Return success
  1774. successRequest(response, encryptedMetadata, request.redirectedServer);
  1775. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1776. //Return error
  1777. failureRequest(response, error, request.redirectedServer);
  1778. }];
  1779. }
  1780. - (void)deleteEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1781. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1782. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1783. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1784. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1785. request = [self getRequestWithCredentials:request];
  1786. [request deleteEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1787. //Return success
  1788. successRequest(response, request.redirectedServer);
  1789. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1790. //Return error
  1791. failureRequest(response, error, request.redirectedServer);
  1792. }];
  1793. }
  1794. #pragma mark - Clear Cache
  1795. - (void)eraseURLCache
  1796. {
  1797. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  1798. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  1799. }
  1800. #pragma mark - Utils
  1801. - (void) addUserItemOfType:(NSInteger) shareeType fromArray:(NSArray*) usersArray ToList: (NSMutableArray *) itemList
  1802. {
  1803. for (NSDictionary *userFound in usersArray) {
  1804. OCShareUser *user = [OCShareUser new];
  1805. if ([[userFound valueForKey:@"label"] isKindOfClass:[NSNumber class]]) {
  1806. NSNumber *number = [userFound valueForKey:@"label"];
  1807. user.displayName = [NSString stringWithFormat:@"%ld", number.longValue];
  1808. }else{
  1809. user.displayName = [userFound valueForKey:@"label"];
  1810. }
  1811. NSDictionary *userValues = [userFound valueForKey:@"value"];
  1812. if ([[userValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  1813. NSNumber *number = [userValues valueForKey:@"shareWith"];
  1814. user.name = [NSString stringWithFormat:@"%ld", number.longValue];
  1815. }else{
  1816. user.name = [userValues valueForKey:@"shareWith"];
  1817. }
  1818. user.shareeType = shareeType;
  1819. user.server = [userValues valueForKey:@"server"];
  1820. [itemList addObject:user];
  1821. }
  1822. }
  1823. - (void) addGroupItemFromArray:(NSArray*) groupsArray ToList: (NSMutableArray *) itemList
  1824. {
  1825. for (NSDictionary *groupFound in groupsArray) {
  1826. OCShareUser *group = [OCShareUser new];
  1827. NSDictionary *groupValues = [groupFound valueForKey:@"value"];
  1828. if ([[groupValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  1829. NSNumber *number = [groupValues valueForKey:@"shareWith"];
  1830. group.name = [NSString stringWithFormat:@"%ld", number.longValue];
  1831. }else{
  1832. group.name = [groupValues valueForKey:@"shareWith"];
  1833. }
  1834. group.shareeType = shareTypeGroup;
  1835. [itemList addObject:group];
  1836. }
  1837. }
  1838. @end