OCCommunication.m 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243
  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 <marino.faggiana@nextcloud.com>
  30. // Copyright (c) 2017 Marino Faggiana. 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 "OCShareUser.h"
  45. #import "OCActivity.h"
  46. #import "OCExternalSites.h"
  47. #import "OCCapabilities.h"
  48. #import "OCNotifications.h"
  49. #import "OCNotificationsAction.h"
  50. #import "OCRichObjectStrings.h"
  51. #import "OCUserProfile.h"
  52. #import "NCRichDocumentTemplate.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 lteDateLastModified:(NSString *)lteDateLastModified gteDateLastModified:(NSString *)gteDateLastModified contentType:(NSArray *)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 lteDateLastModified:lteDateLastModified gteDateLastModified:gteDateLastModified contentType:contentType user:_user userID:_userID onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  473. if (successRequest) {
  474. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  475. NSData *responseData = (NSData*) responseObject;
  476. OCXMLListParser *parser = [OCXMLListParser new];
  477. [parser initParserWithData:responseData];
  478. NSMutableArray *searchList = [parser.searchList mutableCopy];
  479. //Return success
  480. dispatch_async(dispatch_get_main_queue(), ^{
  481. successRequest(response, searchList, request.redirectedServer, token);
  482. });
  483. });
  484. }
  485. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  486. failureRequest(response, error, token, request.redirectedServer);
  487. }];
  488. }
  489. ///-----------------------------------
  490. /// @name Setting favorite
  491. ///-----------------------------------
  492. - (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 {
  493. if (!token){
  494. token = @"no token";
  495. }
  496. path = [NSString stringWithFormat:@"%@/files/%@/%@", path, _userID, filePath];
  497. path = [path encodeString:NSUTF8StringEncoding];
  498. OCWebDAVClient *request = [OCWebDAVClient new];
  499. request = [self getRequestWithCredentials:request];
  500. [request settingFavorite:path favorite:favorite onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  501. if (successRequest) {
  502. //Return success
  503. successRequest(response, request.redirectedServer, token);
  504. }
  505. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  506. NSLog(@"Failure");
  507. failureRequest(response, error, token, request.redirectedServer);
  508. }];
  509. }
  510. ///-----------------------------------
  511. /// @name Listing favorites
  512. ///-----------------------------------
  513. - (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{
  514. if (!token){
  515. token = @"no token";
  516. }
  517. path = [path encodeString:NSUTF8StringEncoding];
  518. OCWebDAVClient *request = [OCWebDAVClient new];
  519. request = [self getRequestWithCredentials:request];
  520. [request listingFavorites:path folder:folder user:_user userID:_userID onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  521. if (successRequest) {
  522. NSData *responseData = (NSData*) responseObject;
  523. OCXMLListParser *parser = [OCXMLListParser new];
  524. [parser initParserWithData:responseData];
  525. NSMutableArray *searchList = [parser.searchList mutableCopy];
  526. //Return success
  527. successRequest(response, searchList, request.redirectedServer, token);
  528. }
  529. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  530. failureRequest(response, error, token, request.redirectedServer);
  531. }];
  532. }
  533. #pragma mark - OC/NC API Calls
  534. - (NSString *) getCurrentServerVersion {
  535. return self.currentServerVersion;
  536. }
  537. - (void) getServerVersionWithPath:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  538. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *serverVersion, NSString *redirectedServer)) success
  539. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  540. OCWebDAVClient *request = [OCWebDAVClient new];
  541. if (self.userAgent) {
  542. [request setUserAgent:self.userAgent];
  543. }
  544. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  545. NSData *data = (NSData*) responseObject;
  546. NSString *versionString = [NSString new];
  547. NSError* error=nil;
  548. if (data) {
  549. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
  550. if(error) {
  551. NSLog(@"Error parsing JSON: %@", error);
  552. } else {
  553. //Obtain the server version from the version field
  554. versionString = [jsonArray valueForKey:@"version"];
  555. self.currentServerVersion = versionString;
  556. }
  557. } else {
  558. NSLog(@"Error parsing JSON: data is null");
  559. }
  560. success(response, versionString, request.redirectedServer);
  561. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  562. failure(response, error, request.redirectedServer);
  563. }];
  564. }
  565. ///-----------------------------------
  566. /// @name Get UserName by cookie
  567. ///-----------------------------------
  568. - (void) getUserNameByCookie:(NSString *) cookieString ofServerPath:(NSString *)path onCommunication:
  569. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *response, NSData *responseData, NSString *redirectedServer))success
  570. failure:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failure{
  571. OCWebDAVClient *request = [OCWebDAVClient new];
  572. request = [self getRequestWithCredentials:request];
  573. [request requestUserNameOfServer: path byCookie:cookieString onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  574. success(response, responseObject, request.redirectedServer);
  575. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  576. failure(response, error, request.redirectedServer);
  577. }];
  578. }
  579. - (void) getFeaturesSupportedByServer:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  580. successRequest:(void(^)(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer)) success
  581. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  582. OCWebDAVClient *request = [OCWebDAVClient new];
  583. if (self.userAgent) {
  584. [request setUserAgent:self.userAgent];
  585. }
  586. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  587. if (responseObject) {
  588. NSError* error = nil;
  589. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: (NSData*) responseObject options: NSJSONReadingMutableContainers error: &error];
  590. if(error) {
  591. // NSLog(@"Error parsing JSON: %@", error);
  592. failure(response, error, request.redirectedServer);
  593. }else{
  594. self.currentServerVersion = [jsonArray valueForKey:@"version"];
  595. BOOL hasShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_shared];
  596. BOOL hasShareeSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_sharee_api];
  597. BOOL hasCookiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_cookies];
  598. BOOL hasForbiddenCharactersSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_forbidden_characters];
  599. BOOL hasCapabilitiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_capabilities];
  600. BOOL hasFedSharesOptionShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_share_option_fed_share];
  601. success(response, hasShareSupport, hasShareeSupport, hasCookiesSupport, hasForbiddenCharactersSupport, hasCapabilitiesSupport, hasFedSharesOptionShareSupport, request.redirectedServer);
  602. }
  603. } else {
  604. // NSLog(@"Error parsing JSON: data is null");
  605. failure(response, nil, request.redirectedServer);
  606. }
  607. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  608. failure(response, error, request.redirectedServer);
  609. }];
  610. }
  611. #pragma mark - Share
  612. - (void) readSharedByServer: (NSString *) path
  613. onCommunication:(OCCommunication *)sharedOCCommunication
  614. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  615. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  616. path = [path encodeString:NSUTF8StringEncoding];
  617. path = [path stringByAppendingString:k_url_acces_shared_api];
  618. OCWebDAVClient *request = [OCWebDAVClient new];
  619. request = [self getRequestWithCredentials:request];
  620. [request listSharedByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  621. if (successRequest) {
  622. NSData *responseData = (NSData*) responseObject;
  623. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  624. //NSLog(@"response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
  625. [parser initParserWithData:responseData];
  626. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  627. //Return success
  628. successRequest(response, sharedList, request.redirectedServer);
  629. }
  630. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  631. failureRequest(response, error, request.redirectedServer);
  632. }];
  633. }
  634. - (void) readSharedByServer: (NSString *) serverPath andPath: (NSString *) path
  635. onCommunication:(OCCommunication *)sharedOCCommunication
  636. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  637. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  638. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  639. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  640. path = [path encodeString:NSUTF8StringEncoding];
  641. OCWebDAVClient *request = [OCWebDAVClient new];
  642. request = [self getRequestWithCredentials:request];
  643. [request listSharedByServer:serverPath andPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  644. if (successRequest) {
  645. NSData *responseData = (NSData*) responseObject;
  646. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  647. // NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  648. // NSLog(@"responseDataReadSharedByServer:andPath: %@", str);
  649. // NSLog(@"pathFolders: %@", path);
  650. // NSLog(@"serverPath: %@", serverPath);
  651. [parser initParserWithData:responseData];
  652. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  653. //Return success
  654. successRequest(response, sharedList, request.redirectedServer);
  655. }
  656. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  657. failureRequest(response, error, request.redirectedServer);
  658. }];
  659. }
  660. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  661. onCommunication:(OCCommunication *)sharedOCCommunication
  662. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer)) successRequest
  663. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  664. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  665. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  666. OCWebDAVClient *request = [OCWebDAVClient new];
  667. request = [self getRequestWithCredentials:request];
  668. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath andPassword:password andPermission:permission andHideDownload:hideDownload onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  669. NSData *responseData = (NSData*) responseObject;
  670. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  671. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  672. [parser initParserWithData:responseData];
  673. switch (parser.statusCode) {
  674. case kOCSharedAPISuccessful:
  675. {
  676. NSString *url = parser.url;
  677. NSString *token = parser.token;
  678. if (url != nil) {
  679. successRequest(response, url, request.redirectedServer);
  680. }else if (token != nil){
  681. //We remove the \n and the empty spaces " "
  682. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  683. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  684. successRequest(response, token, request.redirectedServer);
  685. }else{
  686. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  687. failureRequest(response, error, request.redirectedServer);
  688. }
  689. break;
  690. }
  691. default:
  692. {
  693. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  694. failureRequest(response, error, request.redirectedServer);
  695. }
  696. }
  697. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  698. failureRequest(response, error, request.redirectedServer);
  699. }];
  700. }
  701. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath
  702. onCommunication:(OCCommunication *)sharedOCCommunication
  703. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *shareLink, NSString *redirectedServer)) successRequest
  704. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  705. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  706. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  707. OCWebDAVClient *request = [OCWebDAVClient new];
  708. request = [self getRequestWithCredentials:request];
  709. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  710. NSData *responseData = (NSData*) responseObject;
  711. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  712. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  713. [parser initParserWithData:responseData];
  714. switch (parser.statusCode) {
  715. case kOCSharedAPISuccessful:
  716. {
  717. NSString *url = parser.url;
  718. NSString *token = parser.token;
  719. if (url != nil) {
  720. successRequest(response, url, request.redirectedServer);
  721. }else if (token != nil){
  722. //We remove the \n and the empty spaces " "
  723. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  724. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  725. successRequest(response, token, request.redirectedServer);
  726. }else{
  727. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  728. failureRequest(response, error, request.redirectedServer);
  729. }
  730. break;
  731. }
  732. default:
  733. {
  734. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  735. failureRequest(response, error, request.redirectedServer);
  736. }
  737. }
  738. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  739. failureRequest(response, error, request.redirectedServer);
  740. }];
  741. }
  742. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andFileOrFolderPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  743. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  744. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest{
  745. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  746. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  747. userOrGroup = [userOrGroup encodeString:NSUTF8StringEncoding];
  748. OCWebDAVClient *request = [OCWebDAVClient new];
  749. request = [self getRequestWithCredentials:request];
  750. [request shareWith:userOrGroup shareeType:shareeType inServer:serverPath andPath:filePath andPermissions:permissions onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  751. NSData *responseData = (NSData*) responseObject;
  752. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  753. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  754. [parser initParserWithData:responseData];
  755. switch (parser.statusCode) {
  756. case kOCSharedAPISuccessful:
  757. {
  758. successRequest(response, request.redirectedServer);
  759. break;
  760. }
  761. default:
  762. {
  763. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  764. failureRequest(response, error, request.redirectedServer);
  765. }
  766. }
  767. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  768. failureRequest(response, error, request.redirectedServer);
  769. }];
  770. }
  771. - (void) unShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  772. onCommunication:(OCCommunication *)sharedOCCommunication
  773. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  774. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  775. path = [path encodeString:NSUTF8StringEncoding];
  776. path = [path stringByAppendingString:k_url_acces_shared_api];
  777. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  778. OCWebDAVClient *request = [OCWebDAVClient new];
  779. request = [self getRequestWithCredentials:request];
  780. [request unShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  781. if (successRequest) {
  782. //Return success
  783. successRequest(response, request.redirectedServer);
  784. }
  785. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  786. failureRequest(response, error, request.redirectedServer);
  787. }];
  788. }
  789. - (void) isShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  790. onCommunication:(OCCommunication *)sharedOCCommunication
  791. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, BOOL isShared, id shareDto)) successRequest
  792. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  793. path = [path encodeString:NSUTF8StringEncoding];
  794. path = [path stringByAppendingString:k_url_acces_shared_api];
  795. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  796. OCWebDAVClient *request = [OCWebDAVClient new];
  797. request = [self getRequestWithCredentials:request];
  798. [request isShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  799. if (successRequest) {
  800. NSData *responseData = (NSData*) responseObject;
  801. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  802. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  803. [parser initParserWithData:responseData];
  804. BOOL isShared = NO;
  805. OCSharedDto *shareDto = nil;
  806. if (parser.shareList) {
  807. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  808. if ([sharedList count] > 0) {
  809. isShared = YES;
  810. shareDto = [sharedList objectAtIndex:0];
  811. }
  812. }
  813. //Return success
  814. successRequest(response, request.redirectedServer, isShared, shareDto);
  815. }
  816. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  817. failureRequest(response, error, request.redirectedServer);
  818. }];
  819. }
  820. - (void) updateShare:(NSInteger)shareId ofServerPath:(NSString *)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  821. onCommunication:(OCCommunication *)sharedOCCommunication
  822. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  823. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  824. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  825. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  826. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)shareId]];
  827. OCWebDAVClient *request = [OCWebDAVClient new];
  828. request = [self getRequestWithCredentials:request];
  829. [request updateShareItem:shareId ofServerPath:serverPath withPasswordProtect:password andExpirationTime:expirationTime andPermissions:permissions andHideDownload:hideDownload onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  830. NSData *responseData = (NSData*) responseObject;
  831. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  832. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  833. [parser initParserWithData:responseData];
  834. switch (parser.statusCode) {
  835. case kOCSharedAPISuccessful:
  836. {
  837. successRequest(response, request.redirectedServer);
  838. break;
  839. }
  840. default:
  841. {
  842. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  843. failureRequest(response, error, request.redirectedServer);
  844. }
  845. }
  846. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  847. failureRequest(response, error, request.redirectedServer);
  848. }];
  849. }
  850. - (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{
  851. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  852. serverPath = [serverPath stringByAppendingString:k_url_access_sharee_api];
  853. searchString = [searchString encodeString:NSUTF8StringEncoding];
  854. OCWebDAVClient *request = [OCWebDAVClient new];
  855. request = [self getRequestWithCredentials:request];
  856. [request searchUsersAndGroupsWith:searchString forPage:page with:resultsPerPage ofServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  857. NSData *responseData = (NSData*) responseObject;
  858. NSMutableArray *itemList = [NSMutableArray new];
  859. //Parse
  860. NSError *error;
  861. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  862. if (error == nil) {
  863. NSDictionary *ocsDict = [jsongParsed valueForKey:@"ocs"];
  864. NSDictionary *metaDict = [ocsDict valueForKey:@"meta"];
  865. NSInteger statusCode = [[metaDict valueForKey:@"statuscode"] integerValue];
  866. if (statusCode == kOCShareeAPISuccessful || statusCode == kOCSharedAPISuccessful) {
  867. NSDictionary *dataDict = [ocsDict valueForKey:@"data"];
  868. NSArray *exactDict = [dataDict valueForKey:@"exact"];
  869. NSArray *usersFounded = [dataDict valueForKey:@"users"];
  870. NSArray *groupsFounded = [dataDict valueForKey:@"groups"];
  871. NSArray *usersRemote = [dataDict valueForKey:@"remotes"];
  872. NSArray *usersExact = [exactDict valueForKey:@"users"];
  873. NSArray *groupsExact = [exactDict valueForKey:@"groups"];
  874. NSArray *remotesExact = [exactDict valueForKey:@"remotes"];
  875. [self addUserItemOfType:shareTypeUser fromArray:usersFounded ToList:itemList];
  876. [self addUserItemOfType:shareTypeUser fromArray:usersExact ToList:itemList];
  877. [self addUserItemOfType:shareTypeRemote fromArray:usersRemote ToList:itemList];
  878. [self addUserItemOfType:shareTypeRemote fromArray:remotesExact ToList:itemList];
  879. [self addGroupItemFromArray:groupsFounded ToList:itemList];
  880. [self addGroupItemFromArray:groupsExact ToList:itemList];
  881. }else{
  882. NSString *message = (NSString*)[metaDict objectForKey:@"message"];
  883. if ([message isKindOfClass:[NSNull class]]) {
  884. message = @"";
  885. }
  886. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  887. failureRequest(response, error, request.redirectedServer);
  888. }
  889. //Return success
  890. successRequest(response, itemList, request.redirectedServer);
  891. }
  892. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  893. failureRequest(response, error, request.redirectedServer);
  894. }];
  895. }
  896. - (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{
  897. fileName = [fileName encodeString:NSUTF8StringEncoding];
  898. OCWebDAVClient *request = [OCWebDAVClient new];
  899. request = [self getRequestWithCredentials:request];
  900. [request getSharePermissionsFile:fileName onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  901. if (successRequest) {
  902. NSString *permissions;
  903. NSData *responseData = (NSData *)responseObject;
  904. NSString *newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  905. NSLog(@"newStrReadFolder: %@", newStr);
  906. OCXMLParser *parser = [[OCXMLParser alloc]init];
  907. [parser initParserWithData:responseData];
  908. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  909. if ([directoryList count] == 1) {
  910. OCFileDto *file = [directoryList objectAtIndex:0];
  911. permissions = file.permissions;
  912. }
  913. //Return success
  914. successRequest(response, permissions, request.redirectedServer);
  915. }
  916. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  917. failureRequest(response, error, request.redirectedServer);
  918. }];
  919. }
  920. #pragma mark - Capabilities
  921. - (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{
  922. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  923. serverPath = [serverPath stringByAppendingString:k_url_capabilities];
  924. OCWebDAVClient *request = [OCWebDAVClient new];
  925. request = [self getRequestWithCredentials:request];
  926. [request getCapabilitiesOfServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  927. NSData *responseData = (NSData*) responseObject;
  928. OCCapabilities *capabilities = [OCCapabilities new];
  929. //Parse
  930. NSError *error;
  931. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  932. NSLog(@"dic: %@",jsongParsed);
  933. if (jsongParsed && jsongParsed.allKeys > 0) {
  934. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  935. NSDictionary *data = [ocs valueForKey:@"data"];
  936. NSDictionary *version = [data valueForKey:@"version"];
  937. if (ocs.count > 0 && data.count > 0 && version.count > 0) {
  938. //VERSION
  939. NSNumber *versionMajorNumber = (NSNumber*) [version valueForKey:@"major"];
  940. NSNumber *versionMinorNumber = (NSNumber*) [version valueForKey:@"minor"];
  941. NSNumber *versionMicroNumber = (NSNumber*) [version valueForKey:@"micro"];
  942. capabilities.versionMajor = versionMajorNumber.integerValue;
  943. capabilities.versionMinor = versionMinorNumber.integerValue;
  944. capabilities.versionMicro = versionMicroNumber.integerValue;
  945. capabilities.versionString = (NSString*)[version valueForKey:@"string"];
  946. capabilities.versionEdition = (NSString*)[version valueForKey:@"edition"];
  947. NSDictionary *capabilitiesDict = [data valueForKey:@"capabilities"];
  948. NSDictionary *core = [capabilitiesDict valueForKey:@"core"];
  949. //CORE
  950. NSNumber *corePollIntervalNumber = (NSNumber*)[core valueForKey:@"pollinterval"];
  951. capabilities.corePollInterval = corePollIntervalNumber.integerValue;
  952. NSDictionary *fileSharing = [capabilitiesDict valueForKey:@"files_sharing"];
  953. //FILE SHARING
  954. NSNumber *fileSharingAPIEnabled = (NSNumber*)[fileSharing valueForKey:@"api_enabled"];
  955. NSNumber *filesSharingDefaultPermissions = (NSNumber*)[fileSharing valueForKey:@"default_permissions"];
  956. NSNumber *fileSharingGroupSharing = (NSNumber*)[fileSharing valueForKey:@"group_sharing"];
  957. NSNumber *filesSharingReSharing = (NSNumber*)[fileSharing valueForKey:@"resharing"];
  958. capabilities.isFilesSharingAPIEnabled = fileSharingAPIEnabled.boolValue;
  959. capabilities.filesSharingDefaulPermissions = filesSharingDefaultPermissions.integerValue;
  960. capabilities.isFilesSharingGroupSharing = fileSharingGroupSharing.boolValue;
  961. capabilities.isFilesSharingReSharing = filesSharingReSharing.boolValue;
  962. //FILE SHARING - PUBLIC
  963. NSDictionary *fileSharingPublic = [fileSharing valueForKey:@"public"];
  964. NSNumber *filesSharingPublicShareLinkEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"enabled"];
  965. NSNumber *filesSharingAllowPublicUploadsEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"upload"];
  966. NSNumber *isFilesSharingAllowPublicUserSendMailNumber = (NSNumber*)[fileSharingPublic valueForKey:@"send_mail"];
  967. NSNumber *filesSharingAllowPublicUploadFilesDrop = (NSNumber*)[fileSharingPublic valueForKey:@"upload_files_drop"];
  968. NSNumber *filesSharingAllowPublicMultipleLinks = (NSNumber*)[fileSharingPublic valueForKey:@"multiple_links"];
  969. capabilities.isFilesSharingPublicShareLinkEnabled = filesSharingPublicShareLinkEnabledNumber.boolValue;
  970. capabilities.isFilesSharingAllowPublicUploadsEnabled = filesSharingAllowPublicUploadsEnabledNumber.boolValue;
  971. capabilities.isFilesSharingAllowPublicUserSendMail = isFilesSharingAllowPublicUserSendMailNumber.boolValue;
  972. capabilities.isFilesSharingAllowPublicUploadFilesDrop = filesSharingAllowPublicUploadFilesDrop.boolValue;
  973. capabilities.isFilesSharingAllowPublicMultipleLinks = filesSharingAllowPublicMultipleLinks.boolValue;
  974. NSDictionary *fileSharingPublicExpireDate = [fileSharingPublic valueForKey:@"expire_date"];
  975. NSNumber *filesSharingPublicExpireDateByDefaultEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enabled"];
  976. NSNumber *filesSharingPublicExpireDateEnforceEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enforced"];
  977. NSNumber *filesSharingPublicExpireDateDaysNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"days"];
  978. capabilities.isFilesSharingPublicExpireDateByDefaultEnabled = filesSharingPublicExpireDateByDefaultEnabledNumber.boolValue;
  979. capabilities.isFilesSharingPublicExpireDateEnforceEnabled = filesSharingPublicExpireDateEnforceEnabledNumber.boolValue;
  980. capabilities.filesSharingPublicExpireDateDays = filesSharingPublicExpireDateDaysNumber.integerValue;
  981. NSDictionary *fileSharingPublicPassword = [fileSharingPublic valueForKey:@"password"];
  982. NSNumber *fileSharingPublicPasswordEnforcedEnabled = (NSNumber*)[fileSharingPublicPassword valueForKey:@"enforced"];
  983. capabilities.isFilesSharingPublicPasswordEnforced = fileSharingPublicPasswordEnforcedEnabled.boolValue;
  984. //FILE SHARING - USER
  985. NSDictionary *fileSharingUser = [fileSharing valueForKey:@"user"];
  986. NSNumber *isFilesSharingAllowUserSendMailNumber = (NSNumber*)[fileSharingUser valueForKey:@"send_mail"];
  987. capabilities.isFilesSharingAllowUserSendMail = isFilesSharingAllowUserSendMailNumber.boolValue;
  988. NSDictionary *fileSharingUserExpireDate = [fileSharingUser valueForKey:@"expire_date"];
  989. NSNumber *filesSharingUserExpireDateNumber = (NSNumber*)[fileSharingUserExpireDate valueForKey:@"enabled"];
  990. capabilities.isFilesSharingUserExpireDate = filesSharingUserExpireDateNumber.boolValue;
  991. //FILE SHARING - GROUP
  992. NSDictionary *fileSharingGroup = [fileSharing valueForKey:@"group"];
  993. NSNumber *filesSharingGroupEnabled = (NSNumber*)[fileSharingGroup valueForKey:@"enabled"];
  994. capabilities.isFilesSharingGroupEnabled = filesSharingGroupEnabled.boolValue;
  995. NSDictionary *fileSharingGroupExpireDate = [fileSharingGroup valueForKey:@"expire_date"];
  996. NSNumber *filesSharingGroupExpireDateNumber = (NSNumber*)[fileSharingGroupExpireDate valueForKey:@"enabled"];
  997. capabilities.isFilesSharingGroupExpireDate = filesSharingGroupExpireDateNumber.boolValue;
  998. //FILE SHARING - FEDERATION
  999. NSDictionary *fileSharingFederation = [fileSharing valueForKey:@"federation"];
  1000. NSDictionary *fileSharingFederationExpireDate = [fileSharingFederation valueForKey:@"expire_date"];
  1001. NSNumber *filesSharingFederationAllowUserSendSharesNumber = (NSNumber*)[fileSharingFederation valueForKey:@"outgoing"];
  1002. NSNumber *filesSharingFederationAllowUserReceiveSharesNumber = (NSNumber*)[fileSharingFederation valueForKey:@"incoming"];
  1003. NSNumber *filesSharingFederationExpireDateNumber = (NSNumber*)[fileSharingFederationExpireDate valueForKey:@"enabled"];
  1004. capabilities.isFilesSharingFederationAllowUserSendShares = filesSharingFederationAllowUserSendSharesNumber.boolValue;
  1005. capabilities.isFilesSharingFederationAllowUserReceiveShares = filesSharingFederationAllowUserReceiveSharesNumber.boolValue;
  1006. capabilities.isFilesSharingFederationExpireDate = filesSharingFederationExpireDateNumber.boolValue;
  1007. //FILE SHARING - SHAREBYMAIL
  1008. NSDictionary *fileSharingShareByMail = [fileSharing valueForKey:@"sharebymail"];
  1009. NSNumber *fileSharingShareByMailEnabled = (NSNumber*)[fileSharingShareByMail valueForKey:@"enabled"];
  1010. capabilities.isFileSharingShareByMailEnabled = fileSharingShareByMailEnabled.boolValue;
  1011. NSDictionary *fileSharingShareByMailExpireDate = [fileSharingShareByMail valueForKey:@"expire_date"];
  1012. NSNumber *fileSharingShareByMailExpireDateNumber = (NSNumber*)[fileSharingShareByMailExpireDate valueForKey:@"enabled"];
  1013. capabilities.isFileSharingShareByMailExpireDate = fileSharingShareByMailExpireDateNumber.boolValue;
  1014. NSDictionary *fileSharingShareByMailPassword = [fileSharingShareByMail valueForKey:@"password"];
  1015. NSNumber *fileSharingShareByMailPasswordNumber = (NSNumber*)[fileSharingShareByMailPassword valueForKey:@"enabled"];
  1016. capabilities.isFileSharingShareByMailPassword = fileSharingShareByMailPasswordNumber.boolValue;
  1017. NSDictionary *fileSharingShareByMailUploadFilesDrop = [fileSharingShareByMail valueForKey:@"upload_files_drop"];
  1018. NSNumber *fileSharingShareByMailUploadFilesDropNumber = (NSNumber*)[fileSharingShareByMailUploadFilesDrop valueForKey:@"enabled"];
  1019. capabilities.isFileSharingShareByMailUploadFilesDrop = fileSharingShareByMailUploadFilesDropNumber.boolValue;
  1020. // EXTERNAL SITES
  1021. NSDictionary *externalSitesDic = [capabilitiesDict valueForKey:@"external"];
  1022. if (externalSitesDic) {
  1023. capabilities.isExternalSitesServerEnabled = YES;
  1024. NSArray *externalSitesArray = [externalSitesDic valueForKey:@"v1"];
  1025. capabilities.externalSiteV1 = [externalSitesArray componentsJoinedByString:@","];
  1026. }
  1027. // ACTIVITY
  1028. NSDictionary *activityDic = [capabilitiesDict valueForKey:@"activity"];
  1029. if (activityDic) {
  1030. NSArray *activityArray = [activityDic valueForKey:@"apiv2"];
  1031. if (activityArray) {
  1032. capabilities.isActivityV2Enabled = YES;
  1033. capabilities.activityV2 = [activityArray componentsJoinedByString:@","];
  1034. }
  1035. }
  1036. // NOTIFICATION
  1037. NSDictionary *notificationDic = [capabilitiesDict valueForKey:@"notifications"];
  1038. if (notificationDic) {
  1039. capabilities.isNotificationServerEnabled = YES;
  1040. NSArray *ocsendpointsArray = [notificationDic valueForKey:@"ocs-endpoints"];
  1041. capabilities.notificationOcsEndpoints = [ocsendpointsArray componentsJoinedByString:@","];
  1042. NSArray *pushArray = [notificationDic valueForKey:@"push"];
  1043. capabilities.notificationPush = [pushArray componentsJoinedByString:@","];
  1044. }
  1045. // SPREED
  1046. NSDictionary *spreedDic = [capabilitiesDict valueForKey:@"spreed"];
  1047. if (spreedDic) {
  1048. capabilities.isSpreedServerEnabled = YES;
  1049. NSArray *featuresArray = [capabilitiesDict valueForKey:@"features"];
  1050. capabilities.spreedFeatures = [featuresArray componentsJoinedByString:@","];
  1051. }
  1052. //FILES
  1053. NSDictionary *files = [capabilitiesDict valueForKey:@"files"];
  1054. NSNumber *fileBigFileChunkingEnabledNumber = (NSNumber*)[files valueForKey:@"bigfilechunking"];
  1055. NSNumber *fileUndeleteEnabledNumber = (NSNumber*)[files valueForKey:@"undelete"];
  1056. NSNumber *fileVersioningEnabledNumber = (NSNumber*)[files valueForKey:@"versioning"];
  1057. capabilities.isFileBigFileChunkingEnabled = fileBigFileChunkingEnabledNumber.boolValue;
  1058. capabilities.isFileUndeleteEnabled = fileUndeleteEnabledNumber.boolValue;
  1059. capabilities.isFileVersioningEnabled = fileVersioningEnabledNumber.boolValue;
  1060. //THEMING
  1061. NSDictionary *theming = [capabilitiesDict valueForKey:@"theming"];
  1062. if ([theming count] > 0) {
  1063. if ([theming valueForKey:@"background"] && ![[theming valueForKey:@"background"] isEqual:[NSNull null]])
  1064. capabilities.themingBackground = [theming valueForKey:@"background"];
  1065. if ([theming valueForKey:@"background-default"] && ![[theming valueForKey:@"background-default"] isEqual:[NSNull null]]) {
  1066. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-default"];
  1067. capabilities.themingBackgroundDefault = result.boolValue;
  1068. }
  1069. if ([theming valueForKey:@"background-plain"] && ![[theming valueForKey:@"background-plain"] isEqual:[NSNull null]]) {
  1070. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-plain"];
  1071. capabilities.themingBackgroundPlain = result.boolValue;
  1072. }
  1073. if ([theming valueForKey:@"color"] && ![[theming valueForKey:@"color"] isEqual:[NSNull null]])
  1074. capabilities.themingColor = [theming valueForKey:@"color"];
  1075. if ([theming valueForKey:@"color-element"] && ![[theming valueForKey:@"color-element"] isEqual:[NSNull null]])
  1076. capabilities.themingColorElement = [theming valueForKey:@"color-element"];
  1077. if ([theming valueForKey:@"color-text"] && ![[theming valueForKey:@"color-text"] isEqual:[NSNull null]])
  1078. capabilities.themingColorText = [theming valueForKey:@"color-text"];
  1079. if ([theming valueForKey:@"logo"] && ![[theming valueForKey:@"logo"] isEqual:[NSNull null]])
  1080. capabilities.themingLogo = [theming valueForKey:@"logo"];
  1081. if ([theming valueForKey:@"name"] && ![[theming valueForKey:@"name"] isEqual:[NSNull null]])
  1082. capabilities.themingName = [theming valueForKey:@"name"];
  1083. if ([theming valueForKey:@"slogan"] && ![[theming valueForKey:@"slogan"] isEqual:[NSNull null]])
  1084. capabilities.themingSlogan = [theming valueForKey:@"slogan"];
  1085. if ([theming valueForKey:@"url"] && ![[theming valueForKey:@"url"] isEqual:[NSNull null]])
  1086. capabilities.themingUrl = [theming valueForKey:@"url"];
  1087. }
  1088. //END TO END Encryption
  1089. NSDictionary *endToEndEncryption = [capabilitiesDict valueForKey:@"end-to-end-encryption"];
  1090. if ([endToEndEncryption count] > 0) {
  1091. NSNumber *endToEndEncryptionEnabled = (NSNumber*)[endToEndEncryption valueForKey:@"enabled"];
  1092. capabilities.isEndToEndEncryptionEnabled = endToEndEncryptionEnabled.boolValue;
  1093. if ([endToEndEncryption valueForKey:@"api-version"] && ![[endToEndEncryption valueForKey:@"api-version"] isEqual:[NSNull null]])
  1094. capabilities.endToEndEncryptionVersion = [endToEndEncryption valueForKey:@"api-version"];
  1095. }
  1096. //Richdocuments
  1097. NSDictionary *richdocuments = [capabilitiesDict valueForKey:@"richdocuments"];
  1098. if (richdocuments!= nil && [richdocuments count] > 0) {
  1099. capabilities.richdocumentsDirectEditing = [[richdocuments valueForKey:@"direct_editing"] boolValue];
  1100. capabilities.richdocumentsMimetypes = [richdocuments valueForKey:@"mimetypes"];
  1101. }
  1102. }
  1103. successRequest(response, capabilities, request.redirectedServer);
  1104. } else {
  1105. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1106. }
  1107. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1108. failureRequest(response, error, request.redirectedServer);
  1109. }];
  1110. }
  1111. #pragma mark - Thumbnail
  1112. - (NSURLSessionTask *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCComunication
  1113. successRequest:(void(^)(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer)) successRequest
  1114. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1115. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1116. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1117. OCWebDAVClient *request = [OCWebDAVClient new];
  1118. request = [self getRequestWithCredentials:request];
  1119. OCHTTPRequestOperation *operation = [request getRemoteThumbnailByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight onCommunication:sharedOCComunication
  1120. success:^(NSHTTPURLResponse *response, id responseObject) {
  1121. NSData *responseData = (NSData*) responseObject;
  1122. successRequest(response, responseData, request.redirectedServer);
  1123. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1124. failureRequest(response, error, request.redirectedServer);
  1125. }];
  1126. [operation resume];
  1127. return operation;
  1128. }
  1129. - (NSURLSessionTask *) getRemotePreviewByServer:(NSString*)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight andA:(NSInteger)a andMode:(NSString * _Nonnull)mode path:(NSString * _Nonnull)path onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1130. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1131. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1132. OCWebDAVClient *request = [OCWebDAVClient new];
  1133. request = [self getRequestWithCredentials:request];
  1134. OCHTTPRequestOperation *operation = [request getRemotePreviewByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight andA:a andMode:mode path:path onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1135. NSData *responseData = (NSData*) responseObject;
  1136. successRequest(response, responseData, request.redirectedServer);
  1137. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1138. failureRequest(response, error, request.redirectedServer);
  1139. }];
  1140. [operation resume];
  1141. return operation;
  1142. }
  1143. - (NSURLSessionTask *) getRemotePreviewTrashByServer:(NSString*)serverPath ofFileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1144. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1145. OCWebDAVClient *request = [OCWebDAVClient new];
  1146. request = [self getRequestWithCredentials:request];
  1147. OCHTTPRequestOperation *operation = [request getRemotePreviewTrashByServer:serverPath ofFileID:fileID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1148. NSData *responseData = (NSData*) responseObject;
  1149. successRequest(response, responseData, request.redirectedServer);
  1150. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1151. failureRequest(response, error, request.redirectedServer);
  1152. }];
  1153. [operation resume];
  1154. return operation;
  1155. }
  1156. #pragma mark - Notification
  1157. - (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 {
  1158. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1159. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_notification_api];
  1160. OCWebDAVClient *request = [OCWebDAVClient new];
  1161. request = [self getRequestWithCredentials:request];
  1162. [request getNotificationServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1163. NSData *responseData = (NSData*) responseObject;
  1164. NSMutableArray *listOfNotifications = [NSMutableArray new];
  1165. //Parse
  1166. NSError *error;
  1167. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1168. NSLog(@"[LOG] Notifications : %@",jsongParsed);
  1169. if (jsongParsed && jsongParsed.allKeys > 0) {
  1170. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1171. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1172. NSDictionary *datas = [ocs valueForKey:@"data"];
  1173. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1174. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1175. for (NSDictionary *data in datas) {
  1176. OCNotifications *notification = [OCNotifications new];
  1177. if ([data valueForKey:@"notification_id"] && ![[data valueForKey:@"notification_id"] isEqual:[NSNull null]])
  1178. notification.idNotification = [[data valueForKey:@"notification_id"] integerValue];
  1179. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1180. notification.application = [data valueForKey:@"app"];
  1181. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1182. notification.user = [data valueForKey:@"user"];
  1183. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1184. NSString *dateString = [data valueForKey:@"datetime"];
  1185. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1186. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1187. [dateFormatter setLocale:enUSPOSIXLocale];
  1188. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1189. notification.date = [dateFormatter dateFromString:dateString];
  1190. }
  1191. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1192. notification.typeObject = [data valueForKey:@"object_type"];
  1193. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1194. notification.idObject = [data valueForKey:@"object_id"];
  1195. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1196. notification.subject = [data valueForKey:@"subject"];
  1197. if ([data valueForKey:@"subjectRich"] && ![[data valueForKey:@"subjectRich"] isEqual:[NSNull null]])
  1198. notification.subjectRich = [data valueForKey:@"subjectRich"];
  1199. if ([data valueForKey:@"subjectRichParameters"] && ![[data valueForKey:@"subjectRichParameters"] isEqual:[NSNull null]])
  1200. notification.subjectRichParameters = [data valueForKey:@"subjectRichParameters"];
  1201. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1202. notification.message = [data valueForKey:@"message"];
  1203. if ([data valueForKey:@"messageRich"] && ![[data valueForKey:@"messageRich"] isEqual:[NSNull null]])
  1204. notification.messageRich = [data valueForKey:@"messageRich"];
  1205. if ([data valueForKey:@"messageRichParameters"] && ![[data valueForKey:@"messageRichParameters"] isEqual:[NSNull null]])
  1206. notification.messageRichParameters = [data valueForKey:@"messageRichParameters"];
  1207. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1208. notification.link = [data valueForKey:@"link"];
  1209. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1210. notification.icon = [data valueForKey:@"icon"];
  1211. /* ACTION */
  1212. NSMutableArray *actionsArr = [NSMutableArray new];
  1213. NSDictionary *actions = [data valueForKey:@"actions"];
  1214. for (NSDictionary *action in actions) {
  1215. OCNotificationsAction *notificationAction = [OCNotificationsAction new];
  1216. if ([action valueForKey:@"label"] && ![[action valueForKey:@"label"] isEqual:[NSNull null]])
  1217. notificationAction.label = [action valueForKey:@"label"];
  1218. if ([action valueForKey:@"link"] && ![[action valueForKey:@"link"] isEqual:[NSNull null]])
  1219. notificationAction.link = [action valueForKey:@"link"];
  1220. if ([action valueForKey:@"primary"] && ![[action valueForKey:@"primary"] isEqual:[NSNull null]])
  1221. notificationAction.primary = [[action valueForKey:@"primary"] boolValue];
  1222. if ([action valueForKey:@"type"] && ![[action valueForKey:@"type"] isEqual:[NSNull null]])
  1223. notificationAction.type = [action valueForKey:@"type"];
  1224. [actionsArr addObject:notificationAction];
  1225. }
  1226. notification.actions = [[NSArray alloc] initWithArray:actionsArr];
  1227. [listOfNotifications addObject:notification];
  1228. }
  1229. successRequest(response, listOfNotifications, request.redirectedServer);
  1230. } else {
  1231. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1232. if ([message isKindOfClass:[NSNull class]]) {
  1233. message = NSLocalizedString(@"_server_response_error_", nil);
  1234. }
  1235. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1236. }
  1237. } else {
  1238. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1239. }
  1240. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1241. failureRequest(response, error, request.redirectedServer);
  1242. }];
  1243. }
  1244. - (void)setNotificationServer:(NSString*)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1245. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1246. OCWebDAVClient *request = [OCWebDAVClient new];
  1247. request = [self getRequestWithCredentials:request];
  1248. __weak OCWebDAVClient *wrequest = request;
  1249. [request setNotificationServer:serverPath type:type onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1250. successRequest(response, wrequest.redirectedServer);
  1251. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1252. failureRequest(response, error, wrequest.redirectedServer);
  1253. }];
  1254. }
  1255. #pragma mark - Push Notification
  1256. - (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 {
  1257. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1258. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1259. OCWebDAVClient *request = [OCWebDAVClient new];
  1260. request = [self getRequestWithCredentials:request];
  1261. [request subscribingNextcloudServerPush:serverPath pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath:proxyServerPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1262. NSData *responseData = (NSData*)responseObject;
  1263. NSError *error;
  1264. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1265. if (jsongParsed && jsongParsed.allKeys > 0) {
  1266. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1267. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1268. NSDictionary *datas = [ocs valueForKey:@"data"];
  1269. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1270. if (statusCode == kOCPushNotificationAPISuccessful || statusCode == kOCPushNotificationAPINeedSendProxy) {
  1271. NSString *publicKey = [datas objectForKey:@"publicKey"];
  1272. NSString *deviceIdentifier = [datas objectForKey:@"deviceIdentifier"];
  1273. NSString *signature = [datas objectForKey:@"signature"];
  1274. successRequest(response, publicKey, deviceIdentifier, signature, request.redirectedServer);
  1275. } else {
  1276. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1277. if ([message isKindOfClass:[NSNull class]]) {
  1278. message = NSLocalizedString(@"_server_response_error_", nil);
  1279. }
  1280. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1281. }
  1282. } else {
  1283. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1284. }
  1285. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1286. failureRequest(response, error, request.redirectedServer);
  1287. }];
  1288. }
  1289. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1290. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1291. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1292. OCWebDAVClient *request = [OCWebDAVClient new];
  1293. request = [self getRequestWithCredentials:request];
  1294. [request unsubscribingNextcloudServerPush:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1295. successRequest(response, request.redirectedServer);
  1296. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1297. failureRequest(response, error, request.redirectedServer);
  1298. }];
  1299. }
  1300. - (void)subscribingPushProxy:(NSString *)serverPath pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *redirectedServer))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1301. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1302. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1303. OCWebDAVClient *request = [OCWebDAVClient new];
  1304. request = [self getRequestWithCredentials:request];
  1305. [request subscribingPushProxy:serverPath pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1306. if (successRequest) {
  1307. //Return success
  1308. successRequest(response, request.redirectedServer);
  1309. }
  1310. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1311. failureRequest(response, error, request.redirectedServer);
  1312. }];
  1313. }
  1314. - (void)unsubscribingPushProxy:(NSString *)serverPath deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *redirectedServer))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1315. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1316. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1317. OCWebDAVClient *request = [OCWebDAVClient new];
  1318. request = [self getRequestWithCredentials:request];
  1319. [request unsubscribingPushProxy:serverPath deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1320. if (successRequest) {
  1321. //Return success
  1322. successRequest(response, request.redirectedServer);
  1323. }
  1324. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1325. failureRequest(response, error, request.redirectedServer);
  1326. }];
  1327. }
  1328. #pragma mark - Activity
  1329. - (void) getActivityServer:(NSString*)serverPath since:(NSInteger)since limit:(NSInteger)limit previews:(BOOL)previews link:(NSString *)link onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1330. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1331. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_activity_api];
  1332. OCWebDAVClient *request = [OCWebDAVClient new];
  1333. request = [self getRequestWithCredentials:request];
  1334. [request getActivityServer:serverPath since:since limit:limit previews:previews link:link onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1335. NSData *responseData = (NSData*) responseObject;
  1336. NSMutableArray *listOfActivity = [NSMutableArray new];
  1337. //Parse
  1338. NSError *error;
  1339. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1340. NSLog(@"[LOG] Activity : %@",jsongParsed);
  1341. if (jsongParsed && [jsongParsed isKindOfClass:[NSDictionary class]] && jsongParsed.allKeys > 0) {
  1342. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1343. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1344. NSDictionary *datas = [ocs valueForKey:@"data"];
  1345. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1346. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1347. for (NSDictionary *data in datas) {
  1348. OCActivity *activity = [OCActivity new];
  1349. if ([data valueForKey:@"activity_id"] && ![[data valueForKey:@"activity_id"] isEqual:[NSNull null]])
  1350. activity.idActivity = [[data valueForKey:@"activity_id"] integerValue];
  1351. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1352. NSString *dateString = [data valueForKey:@"datetime"];
  1353. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1354. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1355. [dateFormatter setLocale:enUSPOSIXLocale];
  1356. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1357. activity.date = [dateFormatter dateFromString:dateString];
  1358. }
  1359. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1360. activity.app = [data valueForKey:@"app"];
  1361. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1362. activity.type = [data valueForKey:@"type"];
  1363. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1364. activity.user = [data valueForKey:@"user"];
  1365. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1366. activity.subject = [data valueForKey:@"subject"];
  1367. if ([data valueForKey:@"subject_rich"] && ![[data valueForKey:@"subject_rich"] isEqual:[NSNull null]])
  1368. activity.subject_rich = [data valueForKey:@"subject_rich"];
  1369. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1370. activity.message = [data valueForKey:@"message"];
  1371. if ([data valueForKey:@"message_rich"] && ![[data valueForKey:@"message_rich"] isEqual:[NSNull null]])
  1372. activity.message_rich = [data valueForKey:@"message_rich"];
  1373. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1374. activity.icon = [data valueForKey:@"icon"];
  1375. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1376. activity.link = [data valueForKey:@"link"];
  1377. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1378. activity.object_type = [data valueForKey:@"object_type"];
  1379. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1380. activity.object_id = [[data valueForKey:@"object_id"] integerValue];
  1381. if ([data valueForKey:@"object_name"] && ![[data valueForKey:@"object_name"] isEqual:[NSNull null]])
  1382. activity.object_name = [data valueForKey:@"object_name"];
  1383. if ([data valueForKey:@"previews"] && ![[data valueForKey:@"previews"] isEqual:[NSNull null]])
  1384. activity.previews = [data valueForKey:@"previews"];
  1385. [listOfActivity addObject:activity];
  1386. }
  1387. successRequest(response, listOfActivity, request.redirectedServer);
  1388. } else {
  1389. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1390. if ([message isKindOfClass:[NSNull class]]) {
  1391. message = NSLocalizedString(@"_server_response_error_", nil);
  1392. }
  1393. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1394. }
  1395. } else {
  1396. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1397. }
  1398. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1399. failureRequest(response, error, request.redirectedServer);
  1400. }];
  1401. }
  1402. #pragma mark - External Sites
  1403. - (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 {
  1404. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1405. serverPath = [serverPath stringByAppendingString:k_url_acces_external_sites_api];
  1406. OCWebDAVClient *request = [OCWebDAVClient new];
  1407. request = [self getRequestWithCredentials:request];
  1408. [request getExternalSitesServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1409. NSData *responseData = (NSData*) responseObject;
  1410. NSMutableArray *listOfExternalSites = [NSMutableArray new];
  1411. //Parse
  1412. NSError *error;
  1413. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1414. NSLog(@"[LOG] External Sites : %@",jsongParsed);
  1415. if (jsongParsed && jsongParsed.allKeys > 0) {
  1416. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1417. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1418. NSDictionary *datas = [ocs valueForKey:@"data"];
  1419. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1420. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1421. for (NSDictionary *data in datas) {
  1422. OCExternalSites *externalSites = [OCExternalSites new];
  1423. externalSites.idExternalSite = [[data valueForKey:@"id"] integerValue];
  1424. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1425. externalSites.icon = [data valueForKey:@"icon"];
  1426. if ([data valueForKey:@"lang"] && ![[data valueForKey:@"lang"] isEqual:[NSNull null]])
  1427. externalSites.lang = [data valueForKey:@"lang"];
  1428. if ([data valueForKey:@"name"] && ![[data valueForKey:@"name"] isEqual:[NSNull null]])
  1429. externalSites.name = [data valueForKey:@"name"];
  1430. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isEqual:[NSNull null]])
  1431. externalSites.url = [data valueForKey:@"url"];
  1432. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1433. externalSites.type = [data valueForKey:@"type"];
  1434. [listOfExternalSites addObject:externalSites];
  1435. }
  1436. successRequest(response, listOfExternalSites, request.redirectedServer);
  1437. } else {
  1438. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1439. if ([message isKindOfClass:[NSNull class]]) {
  1440. message = NSLocalizedString(@"_server_response_error_", nil);
  1441. }
  1442. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1443. }
  1444. } else {
  1445. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1446. }
  1447. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1448. failureRequest(response, error, request.redirectedServer);
  1449. }];
  1450. }
  1451. #pragma mark - User Profile
  1452. - (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 {
  1453. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_userprofile_api];
  1454. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1455. OCWebDAVClient *request = [OCWebDAVClient new];
  1456. request = [self getRequestWithCredentials:request];
  1457. [request getUserProfileServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1458. NSData *responseData = (NSData*) responseObject;
  1459. OCUserProfile *userProfile = [OCUserProfile new];
  1460. //Parse
  1461. NSError *error;
  1462. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1463. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  1464. if (jsongParsed && jsongParsed.allKeys > 0) {
  1465. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1466. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1467. NSDictionary *data = [ocs valueForKey:@"data"];
  1468. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1469. if (statusCode == kOCUserProfileAPISuccessful) {
  1470. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  1471. userProfile.address = [data valueForKey:@"address"];
  1472. if ([data valueForKey:@"display-name"] && ![[data valueForKey:@"display-name"] isKindOfClass:[NSNull class]])
  1473. userProfile.displayName = [data valueForKey:@"display-name"];
  1474. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  1475. userProfile.email = [data valueForKey:@"email"];
  1476. if ([data valueForKey:@"enabled"] && ![[data valueForKey:@"enabled"] isKindOfClass:[NSNull class]])
  1477. userProfile.enabled = [[data valueForKey:@"enabled"] boolValue];
  1478. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isKindOfClass:[NSNull class]])
  1479. userProfile.id = [data valueForKey:@"id"];
  1480. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  1481. userProfile.phone = [data valueForKey:@"phone"];
  1482. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  1483. userProfile.twitter = [data valueForKey:@"twitter"];
  1484. if ([data valueForKey:@"webpage"] && ![[data valueForKey:@"webpage"] isKindOfClass:[NSNull class]])
  1485. userProfile.webpage = [data valueForKey:@"webpage"];
  1486. /* QUOTA */
  1487. NSDictionary *quota = [data valueForKey:@"quota"];
  1488. if ([quota count] > 0) {
  1489. if ([quota valueForKey:@"free"] && ![[quota valueForKey:@"free"] isKindOfClass:[NSNull class]])
  1490. userProfile.quotaFree = [[quota valueForKey:@"free"] doubleValue];
  1491. if ([quota valueForKey:@"quota"] && ![[quota valueForKey:@"quota"] isKindOfClass:[NSNull class]])
  1492. userProfile.quota = [[quota valueForKey:@"quota"] doubleValue];
  1493. if ([quota valueForKey:@"relative"] && ![[quota valueForKey:@"relative"] isKindOfClass:[NSNull class]])
  1494. userProfile.quotaRelative = [[quota valueForKey:@"relative"] doubleValue];
  1495. if ([quota valueForKey:@"total"] && ![[quota valueForKey:@"total"] isKindOfClass:[NSNull class]])
  1496. userProfile.quotaTotal = [[quota valueForKey:@"total"] doubleValue];
  1497. if ([quota valueForKey:@"used"] && ![[quota valueForKey:@"used"] isKindOfClass:[NSNull class]])
  1498. userProfile.quotaUsed = [[quota valueForKey:@"used"] doubleValue];
  1499. }
  1500. successRequest(response, userProfile, request.redirectedServer);
  1501. } else {
  1502. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1503. if ([message isKindOfClass:[NSNull class]]) {
  1504. message = NSLocalizedString(@"_server_response_error_", nil);
  1505. }
  1506. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1507. }
  1508. } else {
  1509. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1510. }
  1511. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1512. failureRequest(response, error, request.redirectedServer);
  1513. }];
  1514. }
  1515. #pragma mark - End-to-End Encryption
  1516. - (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 {
  1517. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1518. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1519. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1520. OCWebDAVClient *request = [OCWebDAVClient new];
  1521. request = [self getRequestWithCredentials:request];
  1522. [request getEndToEndPublicKeys:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1523. NSData *responseData = (NSData*) responseObject;
  1524. NSString *publicKey = @"";
  1525. //Parse
  1526. NSError *error;
  1527. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1528. NSLog(@"[LOG] E2E Get PublicKey : %@",jsongParsed);
  1529. if (jsongParsed && jsongParsed.allKeys > 0) {
  1530. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1531. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1532. NSDictionary *data = [ocs valueForKey:@"data"];
  1533. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1534. if (statusCode == kOCUserProfileAPISuccessful) {
  1535. if ([data valueForKey:@"public-keys"] && ![[data valueForKey:@"public-keys"] isKindOfClass:[NSNull class]]) {
  1536. NSDictionary *publickeys = [data valueForKey:@"public-keys"];
  1537. publicKey = [publickeys valueForKey:self.userID];
  1538. successRequest(response, publicKey, request.redirectedServer);
  1539. } else {
  1540. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1541. }
  1542. } else {
  1543. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1544. if ([message isKindOfClass:[NSNull class]]) {
  1545. message = NSLocalizedString(@"_server_response_error_", nil);
  1546. }
  1547. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1548. }
  1549. } else {
  1550. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1551. }
  1552. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1553. //Return error
  1554. failureRequest(response, error, request.redirectedServer);
  1555. }];
  1556. }
  1557. - (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 {
  1558. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1559. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1560. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1561. OCWebDAVClient *request = [OCWebDAVClient new];
  1562. request = [self getRequestWithCredentials:request];
  1563. [request getEndToEndPrivateKeyCipher:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1564. NSData *responseData = (NSData*) responseObject;
  1565. NSString *privateKeyChiper = @"";
  1566. //Parse
  1567. NSError *error;
  1568. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1569. NSLog(@"[LOG] E2E Get PrivateKey : %@",jsongParsed);
  1570. if (jsongParsed && jsongParsed.allKeys > 0) {
  1571. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1572. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1573. NSDictionary *data = [ocs valueForKey:@"data"];
  1574. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1575. if (statusCode == kOCUserProfileAPISuccessful) {
  1576. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]]) {
  1577. privateKeyChiper = [data valueForKey:@"private-key"];
  1578. successRequest(response, privateKeyChiper, request.redirectedServer);
  1579. } else {
  1580. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1581. }
  1582. } else {
  1583. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1584. if ([message isKindOfClass:[NSNull class]]) {
  1585. message = NSLocalizedString(@"_server_response_error_", nil);
  1586. }
  1587. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1588. }
  1589. } else {
  1590. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1591. }
  1592. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1593. //Return error
  1594. failureRequest(response, error, request.redirectedServer);
  1595. }];
  1596. }
  1597. - (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 {
  1598. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1599. serverPath = [serverPath stringByAppendingString:@"/server-key"];
  1600. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1601. OCWebDAVClient *request = [OCWebDAVClient new];
  1602. request = [self getRequestWithCredentials:request];
  1603. [request getEndToEndServerPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1604. NSData *responseData = (NSData*) responseObject;
  1605. NSString *publicKey = @"";
  1606. //Parse
  1607. NSError *error;
  1608. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1609. NSLog(@"[LOG] E2E Get Server PublicKey : %@",jsongParsed);
  1610. if (jsongParsed && jsongParsed.allKeys > 0) {
  1611. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1612. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1613. NSDictionary *data = [ocs valueForKey:@"data"];
  1614. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1615. if (statusCode == kOCUserProfileAPISuccessful) {
  1616. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1617. publicKey = [data valueForKey:@"public-key"];
  1618. successRequest(response, publicKey, request.redirectedServer);
  1619. } else {
  1620. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1621. }
  1622. } else {
  1623. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1624. if ([message isKindOfClass:[NSNull class]]) {
  1625. message = NSLocalizedString(@"_server_response_error_", nil);
  1626. }
  1627. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1628. }
  1629. } else {
  1630. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1631. }
  1632. //Return success
  1633. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1634. //Return error
  1635. failureRequest(response, error, request.redirectedServer);
  1636. }];
  1637. }
  1638. - (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 {
  1639. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1640. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1641. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1642. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1643. request = [self getRequestWithCredentials:request];
  1644. [request signEndToEndPublicKey:serverPath key:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1645. NSData *responseData = (NSData*) responseObject;
  1646. NSString *publicKey = @"";
  1647. //Parse
  1648. NSError *error;
  1649. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1650. NSLog(@"[LOG] E2E Sign PublicKey : %@",jsongParsed);
  1651. if (jsongParsed && jsongParsed.allKeys > 0) {
  1652. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1653. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1654. NSDictionary *data = [ocs valueForKey:@"data"];
  1655. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1656. if (statusCode == kOCUserProfileAPISuccessful) {
  1657. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1658. publicKey = [data valueForKey:@"public-key"];
  1659. successRequest(response, publicKey, request.redirectedServer);
  1660. } else {
  1661. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1662. }
  1663. } else {
  1664. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1665. if ([message isKindOfClass:[NSNull class]]) {
  1666. message = NSLocalizedString(@"_server_response_error_", nil);
  1667. }
  1668. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1669. }
  1670. } else {
  1671. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1672. }
  1673. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1674. //Return error
  1675. failureRequest(response, error, request.redirectedServer);
  1676. }];
  1677. }
  1678. - (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 {
  1679. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1680. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1681. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1682. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1683. request = [self getRequestWithCredentials:request];
  1684. [request storeEndToEndPrivateKeyCipher:serverPath key:privateKeyChiper onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1685. NSData *responseData = (NSData*) responseObject;
  1686. NSString *privateKey = @"";
  1687. //Parse
  1688. NSError *error;
  1689. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1690. NSLog(@"[LOG] E2E Store PrivateKey : %@",jsongParsed);
  1691. if (jsongParsed && jsongParsed.allKeys > 0) {
  1692. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1693. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1694. NSDictionary *data = [ocs valueForKey:@"data"];
  1695. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1696. if (statusCode == kOCUserProfileAPISuccessful) {
  1697. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]]) {
  1698. privateKey = [data valueForKey:@"private-key"];
  1699. successRequest(response, privateKey, request.redirectedServer);
  1700. } else {
  1701. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1702. }
  1703. } else {
  1704. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1705. if ([message isKindOfClass:[NSNull class]]) {
  1706. message = NSLocalizedString(@"_server_response_error_", nil);
  1707. }
  1708. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1709. }
  1710. } else {
  1711. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1712. }
  1713. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1714. //Return error
  1715. failureRequest(response, error, request.redirectedServer);
  1716. }];
  1717. }
  1718. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1719. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1720. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1721. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1722. OCWebDAVClient *request = [OCWebDAVClient new];
  1723. request = [self getRequestWithCredentials:request];
  1724. [request deleteEndToEndPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1725. //Return success
  1726. successRequest(response, request.redirectedServer);
  1727. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1728. //Return error
  1729. failureRequest(response, error, request.redirectedServer);
  1730. }];
  1731. }
  1732. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1733. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1734. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1735. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1736. OCWebDAVClient *request = [OCWebDAVClient new];
  1737. request = [self getRequestWithCredentials:request];
  1738. [request deleteEndToEndPrivateKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1739. //Return success
  1740. successRequest(response, request.redirectedServer);
  1741. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1742. //Return error
  1743. failureRequest(response, error, request.redirectedServer);
  1744. }];
  1745. }
  1746. - (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 {
  1747. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1748. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1749. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1750. OCWebDAVClient *request = [OCWebDAVClient new];
  1751. request = [self getRequestWithCredentials:request];
  1752. [request markEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1753. //Return success
  1754. successRequest(response, request.redirectedServer);
  1755. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1756. //Return error
  1757. failureRequest(response, error, request.redirectedServer);
  1758. }];
  1759. }
  1760. - (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 {
  1761. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1762. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1763. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1764. OCWebDAVClient *request = [OCWebDAVClient new];
  1765. request = [self getRequestWithCredentials:request];
  1766. [request deletemarkEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1767. //Return success
  1768. successRequest(response, request.redirectedServer);
  1769. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1770. //Return error
  1771. failureRequest(response, error, request.redirectedServer);
  1772. }];
  1773. }
  1774. - (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 {
  1775. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1776. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1777. if (token) {
  1778. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1779. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1780. } else {
  1781. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1782. }
  1783. OCWebDAVClient *request = [OCWebDAVClient new];
  1784. request = [self getRequestWithCredentials:request];
  1785. [request lockEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1786. NSData *responseData = (NSData*) responseObject;
  1787. NSString *token = @"";
  1788. //Parse
  1789. NSError *error;
  1790. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1791. NSLog(@"[LOG] E2E Lock File : %@",jsongParsed);
  1792. if (jsongParsed && jsongParsed.allKeys > 0) {
  1793. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1794. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1795. NSDictionary *data = [ocs valueForKey:@"data"];
  1796. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1797. if (statusCode == kOCUserProfileAPISuccessful) {
  1798. if ([data valueForKey:@"token"] && ![[data valueForKey:@"token"] isKindOfClass:[NSNull class]]) {
  1799. token = [data valueForKey:@"token"];
  1800. successRequest(response, token, request.redirectedServer);
  1801. } else {
  1802. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1803. }
  1804. } else {
  1805. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1806. if ([message isKindOfClass:[NSNull class]]) {
  1807. message = NSLocalizedString(@"_server_response_error_", nil);
  1808. }
  1809. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1810. }
  1811. } else {
  1812. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1813. }
  1814. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1815. //Return error
  1816. failureRequest(response, error, request.redirectedServer);
  1817. }];
  1818. }
  1819. - (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 {
  1820. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1821. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1822. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1823. OCWebDAVClient *request = [OCWebDAVClient new];
  1824. request = [self getRequestWithCredentials:request];
  1825. [request unlockEndToEndFolderEncrypted:serverPath token:token onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1826. //Return success
  1827. successRequest(response, request.redirectedServer);
  1828. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1829. //Return error
  1830. failureRequest(response, error, request.redirectedServer);
  1831. }];
  1832. }
  1833. - (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 {
  1834. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1835. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1836. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1837. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1838. request = [self getRequestWithCredentials:request];
  1839. [request getEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1840. NSData *responseData = (NSData*) responseObject;
  1841. NSString *encryptedMetadata = @"";
  1842. //Parse
  1843. NSError *error;
  1844. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1845. NSLog(@"[LOG] E2E Get Metadata : %@",jsongParsed);
  1846. if (jsongParsed && jsongParsed.allKeys > 0) {
  1847. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1848. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1849. NSDictionary *data = [ocs valueForKey:@"data"];
  1850. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1851. if (statusCode == kOCUserProfileAPISuccessful) {
  1852. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1853. encryptedMetadata = [data valueForKey:@"meta-data"];
  1854. successRequest(response, encryptedMetadata, request.redirectedServer);
  1855. } else {
  1856. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1857. }
  1858. } else {
  1859. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1860. if ([message isKindOfClass:[NSNull class]]) {
  1861. message = NSLocalizedString(@"_server_response_error_", nil);
  1862. }
  1863. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1864. }
  1865. } else {
  1866. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1867. }
  1868. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1869. //Return error
  1870. failureRequest(response, error, request.redirectedServer);
  1871. }];
  1872. }
  1873. - (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 {
  1874. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1875. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1876. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1877. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1878. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1879. request = [self getRequestWithCredentials:request];
  1880. [request storeEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1881. NSData *responseData = (NSData*) responseObject;
  1882. NSString *encryptedMetadata = @"";
  1883. //Parse
  1884. NSError *error;
  1885. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1886. NSLog(@"[LOG] E2E Store Metadata : %@",jsongParsed);
  1887. if (jsongParsed && jsongParsed.allKeys > 0) {
  1888. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1889. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1890. NSDictionary *data = [ocs valueForKey:@"data"];
  1891. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1892. if (statusCode == kOCUserProfileAPISuccessful) {
  1893. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1894. encryptedMetadata = [data valueForKey:@"meta-data"];
  1895. successRequest(response, encryptedMetadata, request.redirectedServer);
  1896. } else {
  1897. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1898. }
  1899. } else {
  1900. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1901. if ([message isKindOfClass:[NSNull class]]) {
  1902. message = NSLocalizedString(@"_server_response_error_", nil);
  1903. }
  1904. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1905. }
  1906. } else {
  1907. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1908. }
  1909. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1910. //Return error
  1911. failureRequest(response, error, request.redirectedServer);
  1912. }];
  1913. }
  1914. - (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 {
  1915. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1916. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1917. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1918. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1919. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1920. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1921. request = [self getRequestWithCredentials:request];
  1922. [request updateEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1923. NSData *responseData = (NSData*) responseObject;
  1924. NSString *encryptedMetadata = @"";
  1925. //Parse
  1926. NSError *error;
  1927. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1928. NSLog(@"[LOG] E2E Update Metadata : %@",jsongParsed);
  1929. if (jsongParsed && jsongParsed.allKeys > 0) {
  1930. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1931. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1932. NSDictionary *data = [ocs valueForKey:@"data"];
  1933. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1934. if (statusCode == kOCUserProfileAPISuccessful) {
  1935. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1936. encryptedMetadata = [data valueForKey:@"meta-data"];
  1937. successRequest(response, encryptedMetadata, request.redirectedServer);
  1938. } else {
  1939. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1940. }
  1941. } else {
  1942. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1943. if ([message isKindOfClass:[NSNull class]]) {
  1944. message = NSLocalizedString(@"_server_response_error_", nil);
  1945. }
  1946. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1947. }
  1948. } else {
  1949. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1950. }
  1951. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1952. //Return error
  1953. failureRequest(response, error, request.redirectedServer);
  1954. }];
  1955. }
  1956. - (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 {
  1957. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1958. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1959. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1960. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1961. request = [self getRequestWithCredentials:request];
  1962. [request deleteEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1963. //Return success
  1964. successRequest(response, request.redirectedServer);
  1965. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1966. //Return error
  1967. failureRequest(response, error, request.redirectedServer);
  1968. }];
  1969. }
  1970. #pragma mark - Manage Mobile Editor OCS API
  1971. - (void)createLinkRichdocuments:(NSString *)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1972. serverPath = [serverPath stringByAppendingString:k_url_create_link_mobile_richdocuments];
  1973. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1974. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1975. request = [self getRequestWithCredentials:request];
  1976. [request createLinkRichdocuments:serverPath fileID:fileID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  1977. NSData *responseData = (NSData*) response;
  1978. //Parse
  1979. NSError *error;
  1980. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1981. NSLog(@"[LOG] Link richdocuments : %@",jsongParsed);
  1982. if (jsongParsed && jsongParsed.allKeys > 0) {
  1983. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1984. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1985. NSDictionary *data = [ocs valueForKey:@"data"];
  1986. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1987. if (statusCode == kOCUserProfileAPISuccessful) {
  1988. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isKindOfClass:[NSNull class]]) {
  1989. NSString *link = [data valueForKey:@"url"];
  1990. successRequest(response, link, request.redirectedServer);
  1991. } else {
  1992. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1993. }
  1994. } else {
  1995. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1996. if ([message isKindOfClass:[NSNull class]]) {
  1997. message = NSLocalizedString(@"_server_response_error_", nil);
  1998. }
  1999. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2000. }
  2001. } else {
  2002. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2003. }
  2004. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2005. //Return error
  2006. failureRequest(response, error, request.redirectedServer);
  2007. }];
  2008. }
  2009. - (void)geTemplatesRichdocuments:(NSString *)serverPath typeTemplate:(NSString *)typeTemplate onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2010. serverPath = [serverPath stringByAppendingString:k_url_get_template_mobile_richdocuments];
  2011. serverPath = [serverPath stringByAppendingString:typeTemplate];
  2012. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2013. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2014. request = [self getRequestWithCredentials:request];
  2015. [request geTemplatesRichdocuments:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2016. NSData *responseData = (NSData*) response;
  2017. //Parse
  2018. NSError *error;
  2019. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2020. NSLog(@"[LOG] Link richdocuments : %@",jsongParsed);
  2021. if (jsongParsed && jsongParsed.allKeys > 0) {
  2022. NSMutableArray *list = [NSMutableArray new];
  2023. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2024. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2025. NSDictionary *data = [ocs valueForKey:@"data"];
  2026. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2027. if (statusCode == kOCUserProfileAPISuccessful) {
  2028. for (NSDictionary *dicDatas in data) {
  2029. NCRichDocumentTemplate *template = [NCRichDocumentTemplate new];
  2030. if ([dicDatas valueForKey:@"id"] && ![[dicDatas valueForKey:@"id"] isEqual:[NSNull null]])
  2031. template.templateID = [[dicDatas valueForKey:@"id"] integerValue];
  2032. if ([dicDatas valueForKey:@"delete"] && ![[dicDatas valueForKey:@"delete"] isKindOfClass:[NSNull class]])
  2033. template.delete = [dicDatas valueForKey:@"delete"];
  2034. if ([dicDatas valueForKey:@"extension"] && ![[dicDatas valueForKey:@"extension"] isKindOfClass:[NSNull class]])
  2035. template.extension = [dicDatas valueForKey:@"extension"];
  2036. if ([dicDatas valueForKey:@"name"] && ![[dicDatas valueForKey:@"name"] isKindOfClass:[NSNull class]])
  2037. template.name = [dicDatas valueForKey:@"name"];
  2038. if ([dicDatas valueForKey:@"preview"] && ![[dicDatas valueForKey:@"preview"] isKindOfClass:[NSNull class]])
  2039. template.preview = [dicDatas valueForKey:@"preview"];
  2040. if ([dicDatas valueForKey:@"type"] && ![[dicDatas valueForKey:@"type"] isKindOfClass:[NSNull class]])
  2041. template.type = [dicDatas valueForKey:@"type"];
  2042. [list addObject:template];
  2043. }
  2044. successRequest(response, list, request.redirectedServer);
  2045. } else {
  2046. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2047. if ([message isKindOfClass:[NSNull class]]) {
  2048. message = NSLocalizedString(@"_server_response_error_", nil);
  2049. }
  2050. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2051. }
  2052. } else {
  2053. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2054. }
  2055. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2056. //Return error
  2057. failureRequest(response, error, request.redirectedServer);
  2058. }];
  2059. }
  2060. - (void)createNewRichdocuments:(NSString *)serverPath path:(NSString *)path templateID:(NSString *)templateID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2061. serverPath = [serverPath stringByAppendingString:k_url_create_new_mobile_richdocuments];
  2062. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2063. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2064. request = [self getRequestWithCredentials:request];
  2065. [request createNewRichdocuments:serverPath path:path templateID:templateID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2066. NSData *responseData = (NSData*) response;
  2067. //Parse
  2068. NSError *error;
  2069. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2070. NSLog(@"[LOG] URL Asset : %@",jsongParsed);
  2071. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2072. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2073. NSDictionary *data = [ocs valueForKey:@"data"];
  2074. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2075. if (statusCode == kOCUserProfileAPISuccessful) {
  2076. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isKindOfClass:[NSNull class]])
  2077. successRequest(response, [data valueForKey:@"url"], request.redirectedServer);
  2078. else
  2079. successRequest(response, nil, request.redirectedServer);
  2080. } else {
  2081. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2082. }
  2083. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2084. //Return error
  2085. failureRequest(response, error, request.redirectedServer);
  2086. }];
  2087. }
  2088. - (void)createAssetRichdocuments:(NSString *)serverPath path:(NSString *)path onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2089. serverPath = [serverPath stringByAppendingString:k_url_insert_assets_to_richdocuments];
  2090. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2091. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2092. request = [self getRequestWithCredentials:request];
  2093. [request createAssetRichdocuments:serverPath path:path onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2094. NSData *responseData = (NSData*) response;
  2095. //Parse
  2096. NSError *error;
  2097. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2098. NSLog(@"[LOG] URL Asset : %@",jsongParsed);
  2099. if (jsongParsed && jsongParsed.allKeys > 0) {
  2100. if ([jsongParsed valueForKey:@"url"] && ![[jsongParsed valueForKey:@"url"] isKindOfClass:[NSNull class]]) {
  2101. NSString *url = [jsongParsed valueForKey:@"url"];
  2102. successRequest(response, url, request.redirectedServer);
  2103. } else {
  2104. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2105. }
  2106. } else {
  2107. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2108. }
  2109. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2110. //Return error
  2111. failureRequest(response, error, request.redirectedServer);
  2112. }];
  2113. }
  2114. #pragma mark - Trash
  2115. - (void)listingTrash:(NSString *)path depth:(NSString *)depth onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2116. {
  2117. OCWebDAVClient *request = [OCWebDAVClient new];
  2118. request = [self getRequestWithCredentials:request];
  2119. path = [path encodeString:NSUTF8StringEncoding];
  2120. [request listTrash:path depth:depth onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  2121. OCXMLParser *parser = [OCXMLParser new];
  2122. [parser initParserWithData:responseObject];
  2123. NSMutableArray *list = [parser.directoryList mutableCopy];
  2124. successRequest(response, list, request.redirectedServer);
  2125. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error) {
  2126. failureRequest(response, error, request.redirectedServer);
  2127. }];
  2128. }
  2129. - (void)emptyTrash:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2130. {
  2131. OCWebDAVClient *request = [OCWebDAVClient new];
  2132. request = [self getRequestWithCredentials:request];
  2133. [request emptyTrash:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  2134. successRequest(response, request.redirectedServer);
  2135. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error) {
  2136. failureRequest(response, error, request.redirectedServer);
  2137. }];
  2138. }
  2139. #pragma mark - Third Parts
  2140. - (void)getHCUserProfile:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2141. {
  2142. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2143. OCWebDAVClient *request = [OCWebDAVClient new];
  2144. request = [self getRequestWithCredentials:request];
  2145. [request getHCUserProfile:serverPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response) {
  2146. NSData *responseData = (NSData*) response;
  2147. OCUserProfile *userProfile = [OCUserProfile new];
  2148. //Parse
  2149. NSError *error;
  2150. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2151. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  2152. if (jsongParsed && jsongParsed.allKeys > 0) {
  2153. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2154. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2155. NSDictionary *data = [ocs valueForKey:@"data"];
  2156. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2157. if (statusCode == kOCUserProfileAPISuccessful) {
  2158. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  2159. userProfile.address = [data valueForKey:@"address"];
  2160. if ([data valueForKey:@"display-name"] && ![[data valueForKey:@"display-name"] isKindOfClass:[NSNull class]])
  2161. userProfile.displayName = [data valueForKey:@"display-name"];
  2162. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  2163. userProfile.email = [data valueForKey:@"email"];
  2164. if ([data valueForKey:@"enabled"] && ![[data valueForKey:@"enabled"] isKindOfClass:[NSNull class]])
  2165. userProfile.enabled = [[data valueForKey:@"enabled"] boolValue];
  2166. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isKindOfClass:[NSNull class]])
  2167. userProfile.id = [data valueForKey:@"id"];
  2168. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  2169. userProfile.phone = [data valueForKey:@"phone"];
  2170. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  2171. userProfile.twitter = [data valueForKey:@"twitter"];
  2172. if ([data valueForKey:@"webpage"] && ![[data valueForKey:@"webpage"] isKindOfClass:[NSNull class]])
  2173. userProfile.webpage = [data valueForKey:@"webpage"];
  2174. /* QUOTA */
  2175. NSDictionary *quota = [data valueForKey:@"quota"];
  2176. if ([quota count] > 0) {
  2177. if ([quota valueForKey:@"free"] && ![[quota valueForKey:@"free"] isKindOfClass:[NSNull class]])
  2178. userProfile.quotaFree = [[quota valueForKey:@"free"] doubleValue];
  2179. if ([quota valueForKey:@"quota"] && ![[quota valueForKey:@"quota"] isKindOfClass:[NSNull class]])
  2180. userProfile.quota = [[quota valueForKey:@"quota"] doubleValue];
  2181. if ([quota valueForKey:@"relative"] && ![[quota valueForKey:@"relative"] isKindOfClass:[NSNull class]])
  2182. userProfile.quotaRelative = [[quota valueForKey:@"relative"] doubleValue];
  2183. if ([quota valueForKey:@"total"] && ![[quota valueForKey:@"total"] isKindOfClass:[NSNull class]])
  2184. userProfile.quotaTotal = [[quota valueForKey:@"total"] doubleValue];
  2185. if ([quota valueForKey:@"used"] && ![[quota valueForKey:@"used"] isKindOfClass:[NSNull class]])
  2186. userProfile.quotaUsed = [[quota valueForKey:@"used"] doubleValue];
  2187. }
  2188. successRequest(response, userProfile, request.redirectedServer);
  2189. } else {
  2190. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2191. if ([message isKindOfClass:[NSNull class]]) {
  2192. message = NSLocalizedString(@"_server_response_error_", nil);
  2193. }
  2194. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2195. }
  2196. } else {
  2197. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2198. }
  2199. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2200. failureRequest(response, error, request.redirectedServer);
  2201. }];
  2202. }
  2203. #pragma mark - Manage Mobile Editor OCS API
  2204. - (void)eraseURLCache
  2205. {
  2206. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  2207. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  2208. }
  2209. #pragma mark - Utils
  2210. - (void) addUserItemOfType:(NSInteger) shareeType fromArray:(NSArray*) usersArray ToList: (NSMutableArray *) itemList
  2211. {
  2212. for (NSDictionary *userFound in usersArray) {
  2213. OCShareUser *user = [OCShareUser new];
  2214. if ([[userFound valueForKey:@"label"] isKindOfClass:[NSNumber class]]) {
  2215. NSNumber *number = [userFound valueForKey:@"label"];
  2216. user.displayName = [NSString stringWithFormat:@"%ld", number.longValue];
  2217. }else{
  2218. user.displayName = [userFound valueForKey:@"label"];
  2219. }
  2220. NSDictionary *userValues = [userFound valueForKey:@"value"];
  2221. if ([[userValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  2222. NSNumber *number = [userValues valueForKey:@"shareWith"];
  2223. user.name = [NSString stringWithFormat:@"%ld", number.longValue];
  2224. }else{
  2225. user.name = [userValues valueForKey:@"shareWith"];
  2226. }
  2227. user.shareeType = shareeType;
  2228. user.server = [userValues valueForKey:@"server"];
  2229. [itemList addObject:user];
  2230. }
  2231. }
  2232. - (void) addGroupItemFromArray:(NSArray*) groupsArray ToList: (NSMutableArray *) itemList
  2233. {
  2234. for (NSDictionary *groupFound in groupsArray) {
  2235. OCShareUser *group = [OCShareUser new];
  2236. NSDictionary *groupValues = [groupFound valueForKey:@"value"];
  2237. if ([[groupValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  2238. NSNumber *number = [groupValues valueForKey:@"shareWith"];
  2239. group.name = [NSString stringWithFormat:@"%ld", number.longValue];
  2240. }else{
  2241. group.name = [groupValues valueForKey:@"shareWith"];
  2242. }
  2243. group.shareeType = shareTypeGroup;
  2244. [itemList addObject:group];
  2245. }
  2246. }
  2247. @end