OCCommunication.m 181 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489
  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. #import "HCFeatures.h"
  54. @interface OCCommunication ()
  55. @property (nonatomic, strong) NSString *currentServerVersion;
  56. @end
  57. @implementation OCCommunication
  58. -(id) init {
  59. self = [super init];
  60. if (self) {
  61. //Init the Donwload queue array
  62. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  63. //Credentials not set yet
  64. self.kindOfCredential = credentialNotSet;
  65. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  66. self.isCookiesAvailable = YES;
  67. self.isForbiddenCharactersAvailable = NO;
  68. #ifdef UNIT_TEST
  69. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  70. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  71. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  72. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  73. #else
  74. //Network Upload queue for NSURLSession (iOS 7)
  75. NSURLSessionConfiguration *uploadConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_session_name];
  76. uploadConfiguration.HTTPShouldUsePipelining = YES;
  77. uploadConfiguration.HTTPMaximumConnectionsPerHost = 1;
  78. uploadConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  79. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:uploadConfiguration];
  80. [self.uploadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  81. //Network Download queue for NSURLSession (iOS 7)
  82. NSURLSessionConfiguration *downConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_download_session_name];
  83. downConfiguration.HTTPShouldUsePipelining = YES;
  84. downConfiguration.HTTPMaximumConnectionsPerHost = 1;
  85. downConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  86. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:downConfiguration];
  87. [self.downloadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  88. //Network Download queue for NSURLSession (iOS 7)
  89. NSURLSessionConfiguration *networkConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  90. networkConfiguration.HTTPShouldUsePipelining = YES;
  91. networkConfiguration.HTTPMaximumConnectionsPerHost = 1;
  92. networkConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  93. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:networkConfiguration];
  94. [self.networkSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  95. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  96. #endif
  97. }
  98. return self;
  99. }
  100. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager {
  101. self = [super init];
  102. if (self) {
  103. //Init the Donwload queue array
  104. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  105. self.isCookiesAvailable = YES;
  106. self.isForbiddenCharactersAvailable = NO;
  107. //Credentials not set yet
  108. self.kindOfCredential = credentialNotSet;
  109. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  110. self.uploadSessionManager = uploadSessionManager;
  111. }
  112. return self;
  113. }
  114. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager andDownloadSessionManager:(AFURLSessionManager *) downloadSessionManager andNetworkSessionManager:(AFURLSessionManager *) networkSessionManager {
  115. self = [super init];
  116. if (self) {
  117. //Init the Donwload queue array
  118. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  119. //Credentials not set yet
  120. self.kindOfCredential = credentialNotSet;
  121. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  122. self.uploadSessionManager = uploadSessionManager;
  123. self.downloadSessionManager = downloadSessionManager;
  124. self.networkSessionManager = networkSessionManager;
  125. }
  126. return self;
  127. }
  128. - (AFSecurityPolicy *) createSecurityPolicy {
  129. return [AFSecurityPolicy defaultPolicy];
  130. }
  131. - (void)setSecurityPolicyManagers:(AFSecurityPolicy *)securityPolicy {
  132. self.securityPolicy = securityPolicy;
  133. self.uploadSessionManager.securityPolicy = securityPolicy;
  134. self.downloadSessionManager.securityPolicy = securityPolicy;
  135. }
  136. #pragma mark - Setting Credentials
  137. - (void) setCredentialsWithUser:(NSString*) user andUserID:(NSString *) userID andPassword:(NSString*) password {
  138. self.kindOfCredential = credentialNormal;
  139. self.user = user;
  140. self.userID = userID;
  141. self.password = password;
  142. }
  143. - (void) setCredentialsWithCookie:(NSString*) cookie {
  144. self.kindOfCredential = credentialCookie;
  145. self.password = cookie;
  146. }
  147. - (void) setCredentialsOauthWithToken:(NSString*) token {
  148. self.kindOfCredential = credentialOauth;
  149. self.password = token;
  150. }
  151. ///-----------------------------------
  152. /// @name getRequestWithCredentials
  153. ///-----------------------------------
  154. /**
  155. * Method to return the request with the right credential
  156. *
  157. * @param OCWebDAVClient like a dinamic typed
  158. *
  159. * @return OCWebDAVClient like a dinamic typed
  160. *
  161. */
  162. - (id) getRequestWithCredentials:(id) request {
  163. if ([request isKindOfClass:[NSMutableURLRequest class]]) {
  164. NSMutableURLRequest *myRequest = (NSMutableURLRequest *)request;
  165. switch (self.kindOfCredential) {
  166. case credentialNotSet:
  167. //Without credentials
  168. break;
  169. case credentialNormal:
  170. {
  171. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.user, self.password];
  172. [myRequest addValue:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString:basicAuthCredentials]] forHTTPHeaderField:@"Authorization"];
  173. break;
  174. }
  175. case credentialCookie:
  176. NSLog(@"Cookie: %@", self.password);
  177. [myRequest addValue:self.password forHTTPHeaderField:@"Cookie"];
  178. break;
  179. case credentialOauth:
  180. [myRequest addValue:[NSString stringWithFormat:@"Bearer %@", self.password] forHTTPHeaderField:@"Authorization"];
  181. break;
  182. default:
  183. break;
  184. }
  185. if (self.userAgent) {
  186. [myRequest addValue:self.userAgent forHTTPHeaderField:@"User-Agent"];
  187. }
  188. return myRequest;
  189. } else if([request isKindOfClass:[OCWebDAVClient class]]) {
  190. OCWebDAVClient *myRequest = (OCWebDAVClient *)request;
  191. switch (self.kindOfCredential) {
  192. case credentialNotSet:
  193. //Without credentials
  194. break;
  195. case credentialNormal:
  196. [myRequest setAuthorizationHeaderWithUsername:self.user password:self.password];
  197. break;
  198. case credentialCookie:
  199. [myRequest setAuthorizationHeaderWithCookie:self.password];
  200. break;
  201. case credentialOauth:
  202. [myRequest setAuthorizationHeaderWithToken:[NSString stringWithFormat:@"Bearer %@", self.password]];
  203. break;
  204. default:
  205. break;
  206. }
  207. if (self.userAgent) {
  208. [myRequest setUserAgent:self.userAgent];
  209. }
  210. return request;
  211. } else {
  212. NSLog(@"We do not know witch kind of object is");
  213. return request;
  214. }
  215. }
  216. #pragma mark - WebDav network Operations
  217. ///-----------------------------------
  218. /// @name Check Server
  219. ///-----------------------------------
  220. - (void) checkServer: (NSString *) path
  221. onCommunication:(OCCommunication *)sharedOCCommunication
  222. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  223. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  224. OCWebDAVClient *request = [OCWebDAVClient new];
  225. request = [self getRequestWithCredentials:request];
  226. if (self.userAgent) {
  227. [request setUserAgent:self.userAgent];
  228. }
  229. path = [path encodeString:NSUTF8StringEncoding];
  230. [request checkServer:path onCommunication:sharedOCCommunication
  231. success:^(NSHTTPURLResponse *response, id responseObject) {
  232. if (successRequest) {
  233. successRequest(response, request.redirectedServer);
  234. }
  235. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  236. failureRequest(response, error, request.redirectedServer);
  237. }];
  238. }
  239. ///-----------------------------------
  240. /// @name Create a folder
  241. ///-----------------------------------
  242. - (void) createFolder: (NSString *) path
  243. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  244. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  245. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  246. errorBeforeRequest:(void(^)(NSError *error)) errorBeforeRequest {
  247. if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:path] withForbiddenCharactersSupported:isFCSupported]) {
  248. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorForbidenCharacters];
  249. errorBeforeRequest(error);
  250. } else {
  251. OCWebDAVClient *request = [OCWebDAVClient new];
  252. request = [self getRequestWithCredentials:request];
  253. path = [path encodeString:NSUTF8StringEncoding];
  254. [request makeCollection:path onCommunication:sharedOCCommunication
  255. success:^(NSHTTPURLResponse *response, id responseObject) {
  256. if (successRequest) {
  257. successRequest(response, request.redirectedServer);
  258. }
  259. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  260. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  261. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  262. if (err) {
  263. failureRequest(response, err, request.redirectedServer);
  264. }else{
  265. failureRequest(response, error, request.redirectedServer);
  266. }
  267. }];
  268. }];
  269. }
  270. }
  271. ///-----------------------------------
  272. /// @name Move a file or a folder
  273. ///-----------------------------------
  274. - (void) moveFileOrFolder:(NSString *)sourcePath
  275. toDestiny:(NSString *)destinyPath
  276. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  277. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectServer))successRequest
  278. failureRequest:(void (^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest
  279. errorBeforeRequest:(void (^)(NSError *error))errorBeforeRequest {
  280. if ([UtilsFramework isTheSameFileOrFolderByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  281. //We check that we are not trying to move the file to the same place
  282. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingTheDestinyAndOriginAreTheSame];
  283. errorBeforeRequest(error);
  284. } else if ([UtilsFramework isAFolderUnderItByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  285. //We check we are not trying to move a folder inside himself
  286. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingFolderInsideHimself];
  287. errorBeforeRequest(error);
  288. } else if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:destinyPath] withForbiddenCharactersSupported:isFCSupported]) {
  289. //We check that we are making a move not a rename to prevent special characters problems
  290. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingDestinyNameHaveForbiddenCharacters];
  291. errorBeforeRequest(error);
  292. } else {
  293. sourcePath = [sourcePath encodeString:NSUTF8StringEncoding];
  294. destinyPath = [destinyPath encodeString:NSUTF8StringEncoding];
  295. OCWebDAVClient *request = [OCWebDAVClient new];
  296. request = [self getRequestWithCredentials:request];
  297. [request movePath:sourcePath toPath:destinyPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  298. if (successRequest) {
  299. successRequest(response, request.redirectedServer);
  300. }
  301. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  302. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  303. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  304. if (err) {
  305. failureRequest(response, err, request.redirectedServer);
  306. }else{
  307. failureRequest(response, error, request.redirectedServer);
  308. }
  309. }];
  310. }];
  311. }
  312. }
  313. ///-----------------------------------
  314. /// @name Delete a file or a folder
  315. ///-----------------------------------
  316. - (void) deleteFileOrFolder:(NSString *)path
  317. onCommunication:(OCCommunication *)sharedOCCommunication
  318. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  319. failureRquest:(void (^)(NSHTTPURLResponse *resposne, NSError *error, NSString *redirectedServer))failureRequest {
  320. path = [path encodeString:NSUTF8StringEncoding];
  321. OCWebDAVClient *request = [OCWebDAVClient new];
  322. request = [self getRequestWithCredentials:request];
  323. [request deletePath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  324. if (successRequest) {
  325. successRequest(response, request.redirectedServer);
  326. }
  327. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  328. failureRequest(response, error, request.redirectedServer);
  329. }];
  330. }
  331. ///-----------------------------------
  332. /// @name Read folder
  333. ///-----------------------------------
  334. - (void) readFolder: (NSString *) path depth:(NSString *)depth withUserSessionToken:(NSString *)token
  335. onCommunication:(OCCommunication *)sharedOCCommunication
  336. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest
  337. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  338. if (!token){
  339. token = @"no token";
  340. }
  341. path = [path encodeString:NSUTF8StringEncoding];
  342. OCWebDAVClient *request = [OCWebDAVClient new];
  343. request = [self getRequestWithCredentials:request];
  344. [request listPath:path depth:depth onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  345. if (successRequest) {
  346. NSData *responseData = (NSData*) responseObject;
  347. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  348. // NSLog(@"newStrReadFolder: %@", newStr);
  349. OCXMLParser *parser = [[OCXMLParser alloc]init];
  350. [parser initParserWithData:responseData];
  351. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  352. //Return success
  353. successRequest(response, directoryList, request.redirectedServer, token);
  354. }
  355. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  356. NSLog(@"Failure");
  357. failureRequest(response, error, token, request.redirectedServer);
  358. }];
  359. }
  360. ///-----------------------------------
  361. /// @name Download File Session
  362. ///-----------------------------------
  363. - (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 {
  364. OCWebDAVClient *request = [OCWebDAVClient new];
  365. request = [self getRequestWithCredentials:request];
  366. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  367. NSURLSessionDownloadTask *downloadTask = [request downloadWithSessionPath:remotePath toPath:localPath defaultPriority:defaultPriority onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  368. downloadProgress(progress);
  369. } success:^(NSURLResponse *response, NSURL *filePath) {
  370. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  371. successRequest(response,filePath);
  372. } failure:^(NSURLResponse *response, NSError *error) {
  373. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  374. failureRequest(response,error);
  375. }];
  376. return downloadTask;
  377. }
  378. ///-----------------------------------
  379. /// @name Set Download Task Complete Block
  380. ///-----------------------------------
  381. - (void)setDownloadTaskComleteBlock: (NSURL * (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block{
  382. [self.downloadSessionManager setDownloadTaskDidFinishDownloadingBlock:block];
  383. }
  384. ///-----------------------------------
  385. /// @name Set Download Task Did Get Body Data Block
  386. ///-----------------------------------
  387. - (void) setDownloadTaskDidGetBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)) block{
  388. [self.downloadSessionManager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  389. block(session,downloadTask,bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);
  390. }];
  391. }
  392. ///-----------------------------------
  393. /// @name Upload File Session
  394. ///-----------------------------------
  395. - (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 {
  396. OCWebDAVClient *request = [OCWebDAVClient new];
  397. request = [self getRequestWithCredentials:request];
  398. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  399. NSURLSessionUploadTask *uploadTask = [request putWithSessionLocalPath:localPath atRemotePath:remotePath onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  400. uploadProgress(progress);
  401. } success:^(NSURLResponse *response, id responseObjec){
  402. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  403. //TODO: The second parameter is the redirected server
  404. successRequest(response, @"");
  405. } failure:^(NSURLResponse *response, id responseObject, NSError *error) {
  406. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  407. //TODO: The second parameter is the redirected server
  408. NSData *responseData = (NSData*) responseObject;
  409. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  410. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  411. if (err) {
  412. failureRequest(response, @"", err);
  413. }else{
  414. failureRequest(response, @"", error);
  415. }
  416. }];
  417. } failureBeforeRequest:^(NSError *error) {
  418. failureBeforeRequest(error);
  419. }];
  420. return uploadTask;
  421. }
  422. ///-----------------------------------
  423. /// @name Set Task Did Complete Block
  424. ///-----------------------------------
  425. - (void) setTaskDidCompleteBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, NSError *error)) block{
  426. [self.uploadSessionManager setTaskDidCompleteBlock:^(NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  427. block(session, task, error);
  428. }];
  429. }
  430. ///-----------------------------------
  431. /// @name Set Task Did Send Body Data Block
  432. ///-----------------------------------
  433. - (void) setTaskDidSendBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend)) block{
  434. [self.uploadSessionManager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
  435. block(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend);
  436. }];
  437. }
  438. ///-----------------------------------
  439. /// @name Read File
  440. ///-----------------------------------
  441. - (void) readFile: (NSString *) path
  442. onCommunication:(OCCommunication *)sharedOCCommunication
  443. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer)) successRequest
  444. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  445. path = [path encodeString:NSUTF8StringEncoding];
  446. OCWebDAVClient *request = [OCWebDAVClient new];
  447. request = [self getRequestWithCredentials:request];
  448. [request propertiesOfPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  449. if (successRequest) {
  450. NSData *responseData = (NSData*) responseObject;
  451. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  452. // NSLog(@"newStrReadFile: %@", newStr);
  453. OCXMLParser *parser = [[OCXMLParser alloc]init];
  454. [parser initParserWithData:responseData];
  455. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  456. //Return success
  457. successRequest(response, directoryList, request.redirectedServer);
  458. }
  459. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  460. failureRequest(response, error, request.redirectedServer);
  461. }];
  462. }
  463. ///-----------------------------------
  464. /// @name search
  465. ///-----------------------------------
  466. - (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{
  467. if (!token){
  468. token = @"no token";
  469. }
  470. path = [path encodeString:NSUTF8StringEncoding];
  471. OCWebDAVClient *request = [OCWebDAVClient new];
  472. request = [self getRequestWithCredentials:request];
  473. [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) {
  474. if (successRequest) {
  475. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  476. NSData *responseData = (NSData*) responseObject;
  477. OCXMLListParser *parser = [OCXMLListParser new];
  478. [parser initParserWithData:responseData];
  479. NSMutableArray *searchList = [parser.searchList mutableCopy];
  480. //Return success
  481. dispatch_async(dispatch_get_main_queue(), ^{
  482. successRequest(response, searchList, request.redirectedServer, token);
  483. });
  484. });
  485. }
  486. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  487. failureRequest(response, error, token, request.redirectedServer);
  488. }];
  489. }
  490. ///-----------------------------------
  491. /// @name Setting favorite
  492. ///-----------------------------------
  493. - (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 {
  494. if (!token){
  495. token = @"no token";
  496. }
  497. path = [NSString stringWithFormat:@"%@/files/%@/%@", path, _userID, filePath];
  498. path = [path encodeString:NSUTF8StringEncoding];
  499. OCWebDAVClient *request = [OCWebDAVClient new];
  500. request = [self getRequestWithCredentials:request];
  501. [request settingFavorite:path favorite:favorite onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  502. if (successRequest) {
  503. //Return success
  504. successRequest(response, request.redirectedServer, token);
  505. }
  506. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  507. NSLog(@"Failure");
  508. failureRequest(response, error, token, request.redirectedServer);
  509. }];
  510. }
  511. ///-----------------------------------
  512. /// @name Listing favorites
  513. ///-----------------------------------
  514. - (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{
  515. if (!token){
  516. token = @"no token";
  517. }
  518. path = [path encodeString:NSUTF8StringEncoding];
  519. OCWebDAVClient *request = [OCWebDAVClient new];
  520. request = [self getRequestWithCredentials:request];
  521. [request listingFavorites:path folder:folder user:_user userID:_userID onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  522. if (successRequest) {
  523. NSData *responseData = (NSData*) responseObject;
  524. OCXMLListParser *parser = [OCXMLListParser new];
  525. [parser initParserWithData:responseData];
  526. NSMutableArray *searchList = [parser.searchList mutableCopy];
  527. //Return success
  528. successRequest(response, searchList, request.redirectedServer, token);
  529. }
  530. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  531. failureRequest(response, error, token, request.redirectedServer);
  532. }];
  533. }
  534. #pragma mark - OC/NC API Calls
  535. - (NSString *) getCurrentServerVersion {
  536. return self.currentServerVersion;
  537. }
  538. - (void) getServerVersionWithPath:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  539. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *serverVersion, NSString *redirectedServer)) success
  540. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  541. OCWebDAVClient *request = [OCWebDAVClient new];
  542. if (self.userAgent) {
  543. [request setUserAgent:self.userAgent];
  544. }
  545. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  546. NSData *data = (NSData*) responseObject;
  547. NSString *versionString = [NSString new];
  548. NSError* error=nil;
  549. if (data) {
  550. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
  551. if(error) {
  552. NSLog(@"Error parsing JSON: %@", error);
  553. } else {
  554. //Obtain the server version from the version field
  555. versionString = [jsonArray valueForKey:@"version"];
  556. self.currentServerVersion = versionString;
  557. }
  558. } else {
  559. NSLog(@"Error parsing JSON: data is null");
  560. }
  561. success(response, versionString, request.redirectedServer);
  562. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  563. failure(response, error, request.redirectedServer);
  564. }];
  565. }
  566. ///-----------------------------------
  567. /// @name Get UserName by cookie
  568. ///-----------------------------------
  569. - (void) getUserNameByCookie:(NSString *) cookieString ofServerPath:(NSString *)path onCommunication:
  570. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *response, NSData *responseData, NSString *redirectedServer))success
  571. failure:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failure{
  572. OCWebDAVClient *request = [OCWebDAVClient new];
  573. request = [self getRequestWithCredentials:request];
  574. [request requestUserNameOfServer: path byCookie:cookieString onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  575. success(response, responseObject, request.redirectedServer);
  576. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  577. failure(response, error, request.redirectedServer);
  578. }];
  579. }
  580. - (void) getFeaturesSupportedByServer:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  581. successRequest:(void(^)(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer)) success
  582. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  583. OCWebDAVClient *request = [OCWebDAVClient new];
  584. if (self.userAgent) {
  585. [request setUserAgent:self.userAgent];
  586. }
  587. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  588. if (responseObject) {
  589. NSError* error = nil;
  590. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: (NSData*) responseObject options: NSJSONReadingMutableContainers error: &error];
  591. if(error) {
  592. // NSLog(@"Error parsing JSON: %@", error);
  593. failure(response, error, request.redirectedServer);
  594. }else{
  595. self.currentServerVersion = [jsonArray valueForKey:@"version"];
  596. BOOL hasShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_shared];
  597. BOOL hasShareeSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_sharee_api];
  598. BOOL hasCookiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_cookies];
  599. BOOL hasForbiddenCharactersSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_forbidden_characters];
  600. BOOL hasCapabilitiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_capabilities];
  601. BOOL hasFedSharesOptionShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_share_option_fed_share];
  602. success(response, hasShareSupport, hasShareeSupport, hasCookiesSupport, hasForbiddenCharactersSupport, hasCapabilitiesSupport, hasFedSharesOptionShareSupport, request.redirectedServer);
  603. }
  604. } else {
  605. // NSLog(@"Error parsing JSON: data is null");
  606. failure(response, nil, request.redirectedServer);
  607. }
  608. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  609. failure(response, error, request.redirectedServer);
  610. }];
  611. }
  612. #pragma mark - Share
  613. - (void) readSharedByServer: (NSString *) path
  614. onCommunication:(OCCommunication *)sharedOCCommunication
  615. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  616. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  617. path = [path encodeString:NSUTF8StringEncoding];
  618. path = [path stringByAppendingString:k_url_acces_shared_api];
  619. OCWebDAVClient *request = [OCWebDAVClient new];
  620. request = [self getRequestWithCredentials:request];
  621. [request listSharedByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  622. if (successRequest) {
  623. NSData *responseData = (NSData*) responseObject;
  624. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  625. //NSLog(@"response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
  626. [parser initParserWithData:responseData];
  627. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  628. //Return success
  629. successRequest(response, sharedList, request.redirectedServer);
  630. }
  631. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  632. failureRequest(response, error, request.redirectedServer);
  633. }];
  634. }
  635. - (void) readSharedByServer: (NSString *) serverPath andPath: (NSString *) path
  636. onCommunication:(OCCommunication *)sharedOCCommunication
  637. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  638. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  639. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  640. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  641. path = [path encodeString:NSUTF8StringEncoding];
  642. OCWebDAVClient *request = [OCWebDAVClient new];
  643. request = [self getRequestWithCredentials:request];
  644. [request listSharedByServer:serverPath andPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  645. if (successRequest) {
  646. NSData *responseData = (NSData*) responseObject;
  647. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  648. // NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  649. // NSLog(@"responseDataReadSharedByServer:andPath: %@", str);
  650. // NSLog(@"pathFolders: %@", path);
  651. // NSLog(@"serverPath: %@", serverPath);
  652. [parser initParserWithData:responseData];
  653. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  654. //Return success
  655. successRequest(response, sharedList, request.redirectedServer);
  656. }
  657. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  658. failureRequest(response, error, request.redirectedServer);
  659. }];
  660. }
  661. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  662. onCommunication:(OCCommunication *)sharedOCCommunication
  663. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer)) successRequest
  664. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  665. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  666. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  667. OCWebDAVClient *request = [OCWebDAVClient new];
  668. request = [self getRequestWithCredentials:request];
  669. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath andPassword:password andPermission:permission andHideDownload:hideDownload onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  670. NSData *responseData = (NSData*) responseObject;
  671. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  672. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  673. [parser initParserWithData:responseData];
  674. switch (parser.statusCode) {
  675. case kOCSharedAPISuccessful:
  676. {
  677. NSString *url = parser.url;
  678. NSString *token = parser.token;
  679. if (url != nil) {
  680. successRequest(response, url, request.redirectedServer);
  681. }else if (token != nil){
  682. //We remove the \n and the empty spaces " "
  683. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  684. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  685. successRequest(response, token, request.redirectedServer);
  686. }else{
  687. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  688. failureRequest(response, error, request.redirectedServer);
  689. }
  690. break;
  691. }
  692. default:
  693. {
  694. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  695. failureRequest(response, error, request.redirectedServer);
  696. }
  697. }
  698. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  699. failureRequest(response, error, request.redirectedServer);
  700. }];
  701. }
  702. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath
  703. onCommunication:(OCCommunication *)sharedOCCommunication
  704. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *shareLink, NSString *redirectedServer)) successRequest
  705. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  706. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  707. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  708. OCWebDAVClient *request = [OCWebDAVClient new];
  709. request = [self getRequestWithCredentials:request];
  710. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  711. NSData *responseData = (NSData*) responseObject;
  712. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  713. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  714. [parser initParserWithData:responseData];
  715. switch (parser.statusCode) {
  716. case kOCSharedAPISuccessful:
  717. {
  718. NSString *url = parser.url;
  719. NSString *token = parser.token;
  720. if (url != nil) {
  721. successRequest(response, url, request.redirectedServer);
  722. }else if (token != nil){
  723. //We remove the \n and the empty spaces " "
  724. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  725. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  726. successRequest(response, token, request.redirectedServer);
  727. }else{
  728. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  729. failureRequest(response, error, request.redirectedServer);
  730. }
  731. break;
  732. }
  733. default:
  734. {
  735. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  736. failureRequest(response, error, request.redirectedServer);
  737. }
  738. }
  739. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  740. failureRequest(response, error, request.redirectedServer);
  741. }];
  742. }
  743. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andFileOrFolderPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  744. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  745. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest{
  746. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  747. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  748. userOrGroup = [userOrGroup encodeString:NSUTF8StringEncoding];
  749. OCWebDAVClient *request = [OCWebDAVClient new];
  750. request = [self getRequestWithCredentials:request];
  751. [request shareWith:userOrGroup shareeType:shareeType inServer:serverPath andPath:filePath andPermissions:permissions onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  752. NSData *responseData = (NSData*) responseObject;
  753. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  754. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  755. [parser initParserWithData:responseData];
  756. switch (parser.statusCode) {
  757. case kOCSharedAPISuccessful:
  758. {
  759. successRequest(response, request.redirectedServer);
  760. break;
  761. }
  762. default:
  763. {
  764. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  765. failureRequest(response, error, request.redirectedServer);
  766. }
  767. }
  768. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  769. failureRequest(response, error, request.redirectedServer);
  770. }];
  771. }
  772. - (void) unShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  773. onCommunication:(OCCommunication *)sharedOCCommunication
  774. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  775. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  776. path = [path encodeString:NSUTF8StringEncoding];
  777. path = [path stringByAppendingString:k_url_acces_shared_api];
  778. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  779. OCWebDAVClient *request = [OCWebDAVClient new];
  780. request = [self getRequestWithCredentials:request];
  781. [request unShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  782. if (successRequest) {
  783. //Return success
  784. successRequest(response, request.redirectedServer);
  785. }
  786. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  787. failureRequest(response, error, request.redirectedServer);
  788. }];
  789. }
  790. - (void) isShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  791. onCommunication:(OCCommunication *)sharedOCCommunication
  792. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, BOOL isShared, id shareDto)) successRequest
  793. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  794. path = [path encodeString:NSUTF8StringEncoding];
  795. path = [path stringByAppendingString:k_url_acces_shared_api];
  796. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  797. OCWebDAVClient *request = [OCWebDAVClient new];
  798. request = [self getRequestWithCredentials:request];
  799. [request isShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  800. if (successRequest) {
  801. NSData *responseData = (NSData*) responseObject;
  802. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  803. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  804. [parser initParserWithData:responseData];
  805. BOOL isShared = NO;
  806. OCSharedDto *shareDto = nil;
  807. if (parser.shareList) {
  808. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  809. if ([sharedList count] > 0) {
  810. isShared = YES;
  811. shareDto = [sharedList objectAtIndex:0];
  812. }
  813. }
  814. //Return success
  815. successRequest(response, request.redirectedServer, isShared, shareDto);
  816. }
  817. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  818. failureRequest(response, error, request.redirectedServer);
  819. }];
  820. }
  821. - (void) updateShare:(NSInteger)shareId ofServerPath:(NSString *)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  822. onCommunication:(OCCommunication *)sharedOCCommunication
  823. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  824. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  825. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  826. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  827. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)shareId]];
  828. OCWebDAVClient *request = [OCWebDAVClient new];
  829. request = [self getRequestWithCredentials:request];
  830. [request updateShareItem:shareId ofServerPath:serverPath withPasswordProtect:password andExpirationTime:expirationTime andPermissions:permissions andHideDownload:hideDownload onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  831. NSData *responseData = (NSData*) responseObject;
  832. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  833. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  834. [parser initParserWithData:responseData];
  835. switch (parser.statusCode) {
  836. case kOCSharedAPISuccessful:
  837. {
  838. successRequest(response, request.redirectedServer);
  839. break;
  840. }
  841. default:
  842. {
  843. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  844. failureRequest(response, error, request.redirectedServer);
  845. }
  846. }
  847. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  848. failureRequest(response, error, request.redirectedServer);
  849. }];
  850. }
  851. - (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{
  852. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  853. serverPath = [serverPath stringByAppendingString:k_url_access_sharee_api];
  854. searchString = [searchString encodeString:NSUTF8StringEncoding];
  855. OCWebDAVClient *request = [OCWebDAVClient new];
  856. request = [self getRequestWithCredentials:request];
  857. [request searchUsersAndGroupsWith:searchString forPage:page with:resultsPerPage ofServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  858. NSData *responseData = (NSData*) responseObject;
  859. NSMutableArray *itemList = [NSMutableArray new];
  860. //Parse
  861. NSError *error;
  862. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  863. if (error == nil) {
  864. NSDictionary *ocsDict = [jsongParsed valueForKey:@"ocs"];
  865. NSDictionary *metaDict = [ocsDict valueForKey:@"meta"];
  866. NSInteger statusCode = [[metaDict valueForKey:@"statuscode"] integerValue];
  867. if (statusCode == kOCShareeAPISuccessful || statusCode == kOCSharedAPISuccessful) {
  868. NSDictionary *dataDict = [ocsDict valueForKey:@"data"];
  869. NSArray *exactDict = [dataDict valueForKey:@"exact"];
  870. NSArray *usersFounded = [dataDict valueForKey:@"users"];
  871. NSArray *groupsFounded = [dataDict valueForKey:@"groups"];
  872. NSArray *usersRemote = [dataDict valueForKey:@"remotes"];
  873. NSArray *usersExact = [exactDict valueForKey:@"users"];
  874. NSArray *groupsExact = [exactDict valueForKey:@"groups"];
  875. NSArray *remotesExact = [exactDict valueForKey:@"remotes"];
  876. [self addUserItemOfType:shareTypeUser fromArray:usersFounded ToList:itemList];
  877. [self addUserItemOfType:shareTypeUser fromArray:usersExact ToList:itemList];
  878. [self addUserItemOfType:shareTypeRemote fromArray:usersRemote ToList:itemList];
  879. [self addUserItemOfType:shareTypeRemote fromArray:remotesExact ToList:itemList];
  880. [self addGroupItemFromArray:groupsFounded ToList:itemList];
  881. [self addGroupItemFromArray:groupsExact ToList:itemList];
  882. }else{
  883. NSString *message = (NSString*)[metaDict objectForKey:@"message"];
  884. if ([message isKindOfClass:[NSNull class]]) {
  885. message = @"";
  886. }
  887. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  888. failureRequest(response, error, request.redirectedServer);
  889. }
  890. //Return success
  891. successRequest(response, itemList, request.redirectedServer);
  892. }
  893. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  894. failureRequest(response, error, request.redirectedServer);
  895. }];
  896. }
  897. - (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{
  898. fileName = [fileName encodeString:NSUTF8StringEncoding];
  899. OCWebDAVClient *request = [OCWebDAVClient new];
  900. request = [self getRequestWithCredentials:request];
  901. [request getSharePermissionsFile:fileName onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  902. if (successRequest) {
  903. NSString *permissions;
  904. NSData *responseData = (NSData *)responseObject;
  905. NSString *newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  906. NSLog(@"newStrReadFolder: %@", newStr);
  907. OCXMLParser *parser = [[OCXMLParser alloc]init];
  908. [parser initParserWithData:responseData];
  909. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  910. if ([directoryList count] == 1) {
  911. OCFileDto *file = [directoryList objectAtIndex:0];
  912. permissions = file.permissions;
  913. }
  914. //Return success
  915. successRequest(response, permissions, request.redirectedServer);
  916. }
  917. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  918. failureRequest(response, error, request.redirectedServer);
  919. }];
  920. }
  921. #pragma mark - Capabilities
  922. - (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{
  923. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  924. serverPath = [serverPath stringByAppendingString:k_url_capabilities];
  925. OCWebDAVClient *request = [OCWebDAVClient new];
  926. request = [self getRequestWithCredentials:request];
  927. [request getCapabilitiesOfServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  928. NSData *responseData = (NSData*) responseObject;
  929. OCCapabilities *capabilities = [OCCapabilities new];
  930. //Parse
  931. NSError *error;
  932. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  933. NSLog(@"dic: %@",jsongParsed);
  934. if (jsongParsed && jsongParsed.allKeys > 0) {
  935. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  936. NSDictionary *data = [ocs valueForKey:@"data"];
  937. NSDictionary *version = [data valueForKey:@"version"];
  938. if (ocs.count > 0 && data.count > 0 && version.count > 0) {
  939. //VERSION
  940. NSNumber *versionMajorNumber = (NSNumber*) [version valueForKey:@"major"];
  941. NSNumber *versionMinorNumber = (NSNumber*) [version valueForKey:@"minor"];
  942. NSNumber *versionMicroNumber = (NSNumber*) [version valueForKey:@"micro"];
  943. capabilities.versionMajor = versionMajorNumber.integerValue;
  944. capabilities.versionMinor = versionMinorNumber.integerValue;
  945. capabilities.versionMicro = versionMicroNumber.integerValue;
  946. capabilities.versionString = (NSString*)[version valueForKey:@"string"];
  947. capabilities.versionEdition = (NSString*)[version valueForKey:@"edition"];
  948. NSDictionary *capabilitiesDict = [data valueForKey:@"capabilities"];
  949. NSDictionary *core = [capabilitiesDict valueForKey:@"core"];
  950. //CORE
  951. NSNumber *corePollIntervalNumber = (NSNumber*)[core valueForKey:@"pollinterval"];
  952. capabilities.corePollInterval = corePollIntervalNumber.integerValue;
  953. NSDictionary *fileSharing = [capabilitiesDict valueForKey:@"files_sharing"];
  954. //FILE SHARING
  955. NSNumber *fileSharingAPIEnabled = (NSNumber*)[fileSharing valueForKey:@"api_enabled"];
  956. NSNumber *filesSharingDefaultPermissions = (NSNumber*)[fileSharing valueForKey:@"default_permissions"];
  957. NSNumber *fileSharingGroupSharing = (NSNumber*)[fileSharing valueForKey:@"group_sharing"];
  958. NSNumber *filesSharingReSharing = (NSNumber*)[fileSharing valueForKey:@"resharing"];
  959. capabilities.isFilesSharingAPIEnabled = fileSharingAPIEnabled.boolValue;
  960. capabilities.filesSharingDefaulPermissions = filesSharingDefaultPermissions.integerValue;
  961. capabilities.isFilesSharingGroupSharing = fileSharingGroupSharing.boolValue;
  962. capabilities.isFilesSharingReSharing = filesSharingReSharing.boolValue;
  963. //FILE SHARING - PUBLIC
  964. NSDictionary *fileSharingPublic = [fileSharing valueForKey:@"public"];
  965. NSNumber *filesSharingPublicShareLinkEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"enabled"];
  966. NSNumber *filesSharingAllowPublicUploadsEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"upload"];
  967. NSNumber *isFilesSharingAllowPublicUserSendMailNumber = (NSNumber*)[fileSharingPublic valueForKey:@"send_mail"];
  968. NSNumber *filesSharingAllowPublicUploadFilesDrop = (NSNumber*)[fileSharingPublic valueForKey:@"upload_files_drop"];
  969. NSNumber *filesSharingAllowPublicMultipleLinks = (NSNumber*)[fileSharingPublic valueForKey:@"multiple_links"];
  970. capabilities.isFilesSharingPublicShareLinkEnabled = filesSharingPublicShareLinkEnabledNumber.boolValue;
  971. capabilities.isFilesSharingAllowPublicUploadsEnabled = filesSharingAllowPublicUploadsEnabledNumber.boolValue;
  972. capabilities.isFilesSharingAllowPublicUserSendMail = isFilesSharingAllowPublicUserSendMailNumber.boolValue;
  973. capabilities.isFilesSharingAllowPublicUploadFilesDrop = filesSharingAllowPublicUploadFilesDrop.boolValue;
  974. capabilities.isFilesSharingAllowPublicMultipleLinks = filesSharingAllowPublicMultipleLinks.boolValue;
  975. NSDictionary *fileSharingPublicExpireDate = [fileSharingPublic valueForKey:@"expire_date"];
  976. NSNumber *filesSharingPublicExpireDateByDefaultEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enabled"];
  977. NSNumber *filesSharingPublicExpireDateEnforceEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enforced"];
  978. NSNumber *filesSharingPublicExpireDateDaysNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"days"];
  979. capabilities.isFilesSharingPublicExpireDateByDefaultEnabled = filesSharingPublicExpireDateByDefaultEnabledNumber.boolValue;
  980. capabilities.isFilesSharingPublicExpireDateEnforceEnabled = filesSharingPublicExpireDateEnforceEnabledNumber.boolValue;
  981. capabilities.filesSharingPublicExpireDateDays = filesSharingPublicExpireDateDaysNumber.integerValue;
  982. NSDictionary *fileSharingPublicPassword = [fileSharingPublic valueForKey:@"password"];
  983. NSNumber *fileSharingPublicPasswordEnforcedEnabled = (NSNumber*)[fileSharingPublicPassword valueForKey:@"enforced"];
  984. capabilities.isFilesSharingPublicPasswordEnforced = fileSharingPublicPasswordEnforcedEnabled.boolValue;
  985. //FILE SHARING - USER
  986. NSDictionary *fileSharingUser = [fileSharing valueForKey:@"user"];
  987. NSNumber *isFilesSharingAllowUserSendMailNumber = (NSNumber*)[fileSharingUser valueForKey:@"send_mail"];
  988. capabilities.isFilesSharingAllowUserSendMail = isFilesSharingAllowUserSendMailNumber.boolValue;
  989. NSDictionary *fileSharingUserExpireDate = [fileSharingUser valueForKey:@"expire_date"];
  990. NSNumber *filesSharingUserExpireDateNumber = (NSNumber*)[fileSharingUserExpireDate valueForKey:@"enabled"];
  991. capabilities.isFilesSharingUserExpireDate = filesSharingUserExpireDateNumber.boolValue;
  992. //FILE SHARING - GROUP
  993. NSDictionary *fileSharingGroup = [fileSharing valueForKey:@"group"];
  994. NSNumber *filesSharingGroupEnabled = (NSNumber*)[fileSharingGroup valueForKey:@"enabled"];
  995. capabilities.isFilesSharingGroupEnabled = filesSharingGroupEnabled.boolValue;
  996. NSDictionary *fileSharingGroupExpireDate = [fileSharingGroup valueForKey:@"expire_date"];
  997. NSNumber *filesSharingGroupExpireDateNumber = (NSNumber*)[fileSharingGroupExpireDate valueForKey:@"enabled"];
  998. capabilities.isFilesSharingGroupExpireDate = filesSharingGroupExpireDateNumber.boolValue;
  999. //FILE SHARING - FEDERATION
  1000. NSDictionary *fileSharingFederation = [fileSharing valueForKey:@"federation"];
  1001. NSDictionary *fileSharingFederationExpireDate = [fileSharingFederation valueForKey:@"expire_date"];
  1002. NSNumber *filesSharingFederationAllowUserSendSharesNumber = (NSNumber*)[fileSharingFederation valueForKey:@"outgoing"];
  1003. NSNumber *filesSharingFederationAllowUserReceiveSharesNumber = (NSNumber*)[fileSharingFederation valueForKey:@"incoming"];
  1004. NSNumber *filesSharingFederationExpireDateNumber = (NSNumber*)[fileSharingFederationExpireDate valueForKey:@"enabled"];
  1005. capabilities.isFilesSharingFederationAllowUserSendShares = filesSharingFederationAllowUserSendSharesNumber.boolValue;
  1006. capabilities.isFilesSharingFederationAllowUserReceiveShares = filesSharingFederationAllowUserReceiveSharesNumber.boolValue;
  1007. capabilities.isFilesSharingFederationExpireDate = filesSharingFederationExpireDateNumber.boolValue;
  1008. //FILE SHARING - SHAREBYMAIL
  1009. NSDictionary *fileSharingShareByMail = [fileSharing valueForKey:@"sharebymail"];
  1010. NSNumber *fileSharingShareByMailEnabled = (NSNumber*)[fileSharingShareByMail valueForKey:@"enabled"];
  1011. capabilities.isFileSharingShareByMailEnabled = fileSharingShareByMailEnabled.boolValue;
  1012. NSDictionary *fileSharingShareByMailExpireDate = [fileSharingShareByMail valueForKey:@"expire_date"];
  1013. NSNumber *fileSharingShareByMailExpireDateNumber = (NSNumber*)[fileSharingShareByMailExpireDate valueForKey:@"enabled"];
  1014. capabilities.isFileSharingShareByMailExpireDate = fileSharingShareByMailExpireDateNumber.boolValue;
  1015. NSDictionary *fileSharingShareByMailPassword = [fileSharingShareByMail valueForKey:@"password"];
  1016. NSNumber *fileSharingShareByMailPasswordNumber = (NSNumber*)[fileSharingShareByMailPassword valueForKey:@"enabled"];
  1017. capabilities.isFileSharingShareByMailPassword = fileSharingShareByMailPasswordNumber.boolValue;
  1018. NSDictionary *fileSharingShareByMailUploadFilesDrop = [fileSharingShareByMail valueForKey:@"upload_files_drop"];
  1019. NSNumber *fileSharingShareByMailUploadFilesDropNumber = (NSNumber*)[fileSharingShareByMailUploadFilesDrop valueForKey:@"enabled"];
  1020. capabilities.isFileSharingShareByMailUploadFilesDrop = fileSharingShareByMailUploadFilesDropNumber.boolValue;
  1021. // EXTERNAL SITES
  1022. NSDictionary *externalSitesDic = [capabilitiesDict valueForKey:@"external"];
  1023. if (externalSitesDic) {
  1024. capabilities.isExternalSitesServerEnabled = YES;
  1025. NSArray *externalSitesArray = [externalSitesDic valueForKey:@"v1"];
  1026. capabilities.externalSiteV1 = [externalSitesArray componentsJoinedByString:@","];
  1027. }
  1028. // ACTIVITY
  1029. NSDictionary *activityDic = [capabilitiesDict valueForKey:@"activity"];
  1030. if (activityDic) {
  1031. NSArray *activityArray = [activityDic valueForKey:@"apiv2"];
  1032. if (activityArray) {
  1033. capabilities.isActivityV2Enabled = YES;
  1034. capabilities.activityV2 = [activityArray componentsJoinedByString:@","];
  1035. }
  1036. }
  1037. // NOTIFICATION
  1038. NSDictionary *notificationDic = [capabilitiesDict valueForKey:@"notifications"];
  1039. if (notificationDic) {
  1040. capabilities.isNotificationServerEnabled = YES;
  1041. NSArray *ocsendpointsArray = [notificationDic valueForKey:@"ocs-endpoints"];
  1042. capabilities.notificationOcsEndpoints = [ocsendpointsArray componentsJoinedByString:@","];
  1043. NSArray *pushArray = [notificationDic valueForKey:@"push"];
  1044. capabilities.notificationPush = [pushArray componentsJoinedByString:@","];
  1045. }
  1046. // SPREED
  1047. NSDictionary *spreedDic = [capabilitiesDict valueForKey:@"spreed"];
  1048. if (spreedDic) {
  1049. capabilities.isSpreedServerEnabled = YES;
  1050. NSArray *featuresArray = [capabilitiesDict valueForKey:@"features"];
  1051. capabilities.spreedFeatures = [featuresArray componentsJoinedByString:@","];
  1052. }
  1053. //FILES
  1054. NSDictionary *files = [capabilitiesDict valueForKey:@"files"];
  1055. NSNumber *fileBigFileChunkingEnabledNumber = (NSNumber*)[files valueForKey:@"bigfilechunking"];
  1056. NSNumber *fileUndeleteEnabledNumber = (NSNumber*)[files valueForKey:@"undelete"];
  1057. NSNumber *fileVersioningEnabledNumber = (NSNumber*)[files valueForKey:@"versioning"];
  1058. capabilities.isFileBigFileChunkingEnabled = fileBigFileChunkingEnabledNumber.boolValue;
  1059. capabilities.isFileUndeleteEnabled = fileUndeleteEnabledNumber.boolValue;
  1060. capabilities.isFileVersioningEnabled = fileVersioningEnabledNumber.boolValue;
  1061. //THEMING
  1062. NSDictionary *theming = [capabilitiesDict valueForKey:@"theming"];
  1063. if ([theming count] > 0) {
  1064. if ([theming valueForKey:@"background"] && ![[theming valueForKey:@"background"] isEqual:[NSNull null]])
  1065. capabilities.themingBackground = [theming valueForKey:@"background"];
  1066. if ([theming valueForKey:@"background-default"] && ![[theming valueForKey:@"background-default"] isEqual:[NSNull null]]) {
  1067. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-default"];
  1068. capabilities.themingBackgroundDefault = result.boolValue;
  1069. }
  1070. if ([theming valueForKey:@"background-plain"] && ![[theming valueForKey:@"background-plain"] isEqual:[NSNull null]]) {
  1071. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-plain"];
  1072. capabilities.themingBackgroundPlain = result.boolValue;
  1073. }
  1074. if ([theming valueForKey:@"color"] && ![[theming valueForKey:@"color"] isEqual:[NSNull null]])
  1075. capabilities.themingColor = [theming valueForKey:@"color"];
  1076. if ([theming valueForKey:@"color-element"] && ![[theming valueForKey:@"color-element"] isEqual:[NSNull null]])
  1077. capabilities.themingColorElement = [theming valueForKey:@"color-element"];
  1078. if ([theming valueForKey:@"color-text"] && ![[theming valueForKey:@"color-text"] isEqual:[NSNull null]])
  1079. capabilities.themingColorText = [theming valueForKey:@"color-text"];
  1080. if ([theming valueForKey:@"logo"] && ![[theming valueForKey:@"logo"] isEqual:[NSNull null]])
  1081. capabilities.themingLogo = [theming valueForKey:@"logo"];
  1082. if ([theming valueForKey:@"name"] && ![[theming valueForKey:@"name"] isEqual:[NSNull null]])
  1083. capabilities.themingName = [theming valueForKey:@"name"];
  1084. if ([theming valueForKey:@"slogan"] && ![[theming valueForKey:@"slogan"] isEqual:[NSNull null]])
  1085. capabilities.themingSlogan = [theming valueForKey:@"slogan"];
  1086. if ([theming valueForKey:@"url"] && ![[theming valueForKey:@"url"] isEqual:[NSNull null]])
  1087. capabilities.themingUrl = [theming valueForKey:@"url"];
  1088. }
  1089. //END TO END Encryption
  1090. NSDictionary *endToEndEncryption = [capabilitiesDict valueForKey:@"end-to-end-encryption"];
  1091. if ([endToEndEncryption count] > 0) {
  1092. NSNumber *endToEndEncryptionEnabled = (NSNumber*)[endToEndEncryption valueForKey:@"enabled"];
  1093. capabilities.isEndToEndEncryptionEnabled = endToEndEncryptionEnabled.boolValue;
  1094. if ([endToEndEncryption valueForKey:@"api-version"] && ![[endToEndEncryption valueForKey:@"api-version"] isEqual:[NSNull null]])
  1095. capabilities.endToEndEncryptionVersion = [endToEndEncryption valueForKey:@"api-version"];
  1096. }
  1097. //Richdocuments
  1098. NSDictionary *richdocuments = [capabilitiesDict valueForKey:@"richdocuments"];
  1099. if (richdocuments!= nil && [richdocuments count] > 0) {
  1100. capabilities.richdocumentsDirectEditing = [[richdocuments valueForKey:@"direct_editing"] boolValue];
  1101. capabilities.richdocumentsMimetypes = [richdocuments valueForKey:@"mimetypes"];
  1102. }
  1103. //Handwerkcloud
  1104. NSDictionary *handwerkcloudDic = [capabilitiesDict valueForKey:@"handwerkcloud"];
  1105. if (handwerkcloudDic) {
  1106. NSNumber *isHandwerkcloudEnabledNumber = (NSNumber*)[handwerkcloudDic valueForKey:@"enabled"];
  1107. capabilities.isHandwerkcloudEnabled = isHandwerkcloudEnabledNumber.boolValue;
  1108. if ([handwerkcloudDic valueForKey:@"shop_url"] && ![[handwerkcloudDic valueForKey:@"shop_url"] isEqual:[NSNull null]])
  1109. capabilities.HCShopUrl = [handwerkcloudDic valueForKey:@"shop_url"];
  1110. }
  1111. //Imagemeter
  1112. NSDictionary *imagemeterDic = [capabilitiesDict valueForKey:@"imagemeter"];
  1113. if (imagemeterDic) {
  1114. NSNumber *isImagemeterEnabledNumber = (NSNumber*)[imagemeterDic valueForKey:@"enabled"];
  1115. capabilities.isImagemeterEnabled = isImagemeterEnabledNumber.boolValue;
  1116. }
  1117. //Fulltextsearch
  1118. NSDictionary *fulltextsearchDic = [capabilitiesDict valueForKey:@"fulltextsearch"];
  1119. if (fulltextsearchDic) {
  1120. NSNumber *isFulltextsearchEnabledNumber = (NSNumber*)[fulltextsearchDic valueForKey:@"remote"];
  1121. capabilities.isFulltextsearchEnabled = isFulltextsearchEnabledNumber.boolValue;
  1122. }
  1123. }
  1124. successRequest(response, capabilities, request.redirectedServer);
  1125. } else {
  1126. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1127. }
  1128. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1129. failureRequest(response, error, request.redirectedServer);
  1130. }];
  1131. }
  1132. #pragma mark - Thumbnail
  1133. - (NSURLSessionTask *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCComunication
  1134. successRequest:(void(^)(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer)) successRequest
  1135. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1136. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1137. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1138. OCWebDAVClient *request = [OCWebDAVClient new];
  1139. request = [self getRequestWithCredentials:request];
  1140. OCHTTPRequestOperation *operation = [request getRemoteThumbnailByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight onCommunication:sharedOCComunication
  1141. success:^(NSHTTPURLResponse *response, id responseObject) {
  1142. NSData *responseData = (NSData*) responseObject;
  1143. successRequest(response, responseData, request.redirectedServer);
  1144. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1145. failureRequest(response, error, request.redirectedServer);
  1146. }];
  1147. [operation resume];
  1148. return operation;
  1149. }
  1150. - (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 {
  1151. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1152. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1153. OCWebDAVClient *request = [OCWebDAVClient new];
  1154. request = [self getRequestWithCredentials:request];
  1155. OCHTTPRequestOperation *operation = [request getRemotePreviewByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight andA:a andMode:mode path:path onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1156. NSData *responseData = (NSData*) responseObject;
  1157. successRequest(response, responseData, request.redirectedServer);
  1158. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1159. failureRequest(response, error, request.redirectedServer);
  1160. }];
  1161. [operation resume];
  1162. return operation;
  1163. }
  1164. - (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 {
  1165. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1166. OCWebDAVClient *request = [OCWebDAVClient new];
  1167. request = [self getRequestWithCredentials:request];
  1168. OCHTTPRequestOperation *operation = [request getRemotePreviewTrashByServer:serverPath ofFileID:fileID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1169. NSData *responseData = (NSData*) responseObject;
  1170. successRequest(response, responseData, request.redirectedServer);
  1171. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1172. failureRequest(response, error, request.redirectedServer);
  1173. }];
  1174. [operation resume];
  1175. return operation;
  1176. }
  1177. #pragma mark - Notification
  1178. - (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 {
  1179. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1180. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_notification_api];
  1181. OCWebDAVClient *request = [OCWebDAVClient new];
  1182. request = [self getRequestWithCredentials:request];
  1183. [request getNotificationServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1184. NSData *responseData = (NSData*) responseObject;
  1185. NSMutableArray *listOfNotifications = [NSMutableArray new];
  1186. //Parse
  1187. NSError *error;
  1188. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1189. NSLog(@"[LOG] Notifications : %@",jsongParsed);
  1190. if (jsongParsed && jsongParsed.allKeys > 0) {
  1191. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1192. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1193. NSDictionary *datas = [ocs valueForKey:@"data"];
  1194. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1195. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1196. for (NSDictionary *data in datas) {
  1197. OCNotifications *notification = [OCNotifications new];
  1198. if ([data valueForKey:@"notification_id"] && ![[data valueForKey:@"notification_id"] isEqual:[NSNull null]])
  1199. notification.idNotification = [[data valueForKey:@"notification_id"] integerValue];
  1200. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1201. notification.application = [data valueForKey:@"app"];
  1202. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1203. notification.user = [data valueForKey:@"user"];
  1204. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1205. NSString *dateString = [data valueForKey:@"datetime"];
  1206. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1207. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1208. [dateFormatter setLocale:enUSPOSIXLocale];
  1209. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1210. notification.date = [dateFormatter dateFromString:dateString];
  1211. }
  1212. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1213. notification.typeObject = [data valueForKey:@"object_type"];
  1214. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1215. notification.idObject = [data valueForKey:@"object_id"];
  1216. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1217. notification.subject = [data valueForKey:@"subject"];
  1218. if ([data valueForKey:@"subjectRich"] && ![[data valueForKey:@"subjectRich"] isEqual:[NSNull null]])
  1219. notification.subjectRich = [data valueForKey:@"subjectRich"];
  1220. if ([data valueForKey:@"subjectRichParameters"] && ![[data valueForKey:@"subjectRichParameters"] isEqual:[NSNull null]])
  1221. notification.subjectRichParameters = [data valueForKey:@"subjectRichParameters"];
  1222. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1223. notification.message = [data valueForKey:@"message"];
  1224. if ([data valueForKey:@"messageRich"] && ![[data valueForKey:@"messageRich"] isEqual:[NSNull null]])
  1225. notification.messageRich = [data valueForKey:@"messageRich"];
  1226. if ([data valueForKey:@"messageRichParameters"] && ![[data valueForKey:@"messageRichParameters"] isEqual:[NSNull null]])
  1227. notification.messageRichParameters = [data valueForKey:@"messageRichParameters"];
  1228. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1229. notification.link = [data valueForKey:@"link"];
  1230. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1231. notification.icon = [data valueForKey:@"icon"];
  1232. /* ACTION */
  1233. NSMutableArray *actionsArr = [NSMutableArray new];
  1234. NSDictionary *actions = [data valueForKey:@"actions"];
  1235. for (NSDictionary *action in actions) {
  1236. OCNotificationsAction *notificationAction = [OCNotificationsAction new];
  1237. if ([action valueForKey:@"label"] && ![[action valueForKey:@"label"] isEqual:[NSNull null]])
  1238. notificationAction.label = [action valueForKey:@"label"];
  1239. if ([action valueForKey:@"link"] && ![[action valueForKey:@"link"] isEqual:[NSNull null]])
  1240. notificationAction.link = [action valueForKey:@"link"];
  1241. if ([action valueForKey:@"primary"] && ![[action valueForKey:@"primary"] isEqual:[NSNull null]])
  1242. notificationAction.primary = [[action valueForKey:@"primary"] boolValue];
  1243. if ([action valueForKey:@"type"] && ![[action valueForKey:@"type"] isEqual:[NSNull null]])
  1244. notificationAction.type = [action valueForKey:@"type"];
  1245. [actionsArr addObject:notificationAction];
  1246. }
  1247. notification.actions = [[NSArray alloc] initWithArray:actionsArr];
  1248. [listOfNotifications addObject:notification];
  1249. }
  1250. successRequest(response, listOfNotifications, request.redirectedServer);
  1251. } else {
  1252. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1253. if ([message isKindOfClass:[NSNull class]]) {
  1254. message = NSLocalizedString(@"_server_response_error_", nil);
  1255. }
  1256. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1257. }
  1258. } else {
  1259. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1260. }
  1261. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1262. failureRequest(response, error, request.redirectedServer);
  1263. }];
  1264. }
  1265. - (void)setNotificationServer:(NSString*)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1266. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1267. OCWebDAVClient *request = [OCWebDAVClient new];
  1268. request = [self getRequestWithCredentials:request];
  1269. __weak OCWebDAVClient *wrequest = request;
  1270. [request setNotificationServer:serverPath type:type onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1271. successRequest(response, wrequest.redirectedServer);
  1272. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1273. failureRequest(response, error, wrequest.redirectedServer);
  1274. }];
  1275. }
  1276. #pragma mark - Push Notification
  1277. - (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 {
  1278. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1279. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1280. OCWebDAVClient *request = [OCWebDAVClient new];
  1281. request = [self getRequestWithCredentials:request];
  1282. [request subscribingNextcloudServerPush:serverPath pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath:proxyServerPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1283. NSData *responseData = (NSData*)responseObject;
  1284. NSError *error;
  1285. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1286. if (jsongParsed && jsongParsed.allKeys > 0) {
  1287. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1288. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1289. NSDictionary *datas = [ocs valueForKey:@"data"];
  1290. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1291. if (statusCode == kOCPushNotificationAPISuccessful || statusCode == kOCPushNotificationAPINeedSendProxy) {
  1292. NSString *publicKey = [datas objectForKey:@"publicKey"];
  1293. NSString *deviceIdentifier = [datas objectForKey:@"deviceIdentifier"];
  1294. NSString *signature = [datas objectForKey:@"signature"];
  1295. successRequest(response, publicKey, deviceIdentifier, signature, request.redirectedServer);
  1296. } else {
  1297. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1298. if ([message isKindOfClass:[NSNull class]]) {
  1299. message = NSLocalizedString(@"_server_response_error_", nil);
  1300. }
  1301. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1302. }
  1303. } else {
  1304. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1305. }
  1306. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1307. failureRequest(response, error, request.redirectedServer);
  1308. }];
  1309. }
  1310. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1311. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1312. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1313. OCWebDAVClient *request = [OCWebDAVClient new];
  1314. request = [self getRequestWithCredentials:request];
  1315. [request unsubscribingNextcloudServerPush:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1316. successRequest(response, request.redirectedServer);
  1317. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1318. failureRequest(response, error, request.redirectedServer);
  1319. }];
  1320. }
  1321. - (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 {
  1322. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1323. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1324. OCWebDAVClient *request = [OCWebDAVClient new];
  1325. request = [self getRequestWithCredentials:request];
  1326. [request subscribingPushProxy:serverPath pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1327. if (successRequest) {
  1328. //Return success
  1329. successRequest(response, request.redirectedServer);
  1330. }
  1331. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1332. failureRequest(response, error, request.redirectedServer);
  1333. }];
  1334. }
  1335. - (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 {
  1336. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1337. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1338. OCWebDAVClient *request = [OCWebDAVClient new];
  1339. request = [self getRequestWithCredentials:request];
  1340. [request unsubscribingPushProxy:serverPath deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1341. if (successRequest) {
  1342. //Return success
  1343. successRequest(response, request.redirectedServer);
  1344. }
  1345. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1346. failureRequest(response, error, request.redirectedServer);
  1347. }];
  1348. }
  1349. #pragma mark - Activity
  1350. - (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 {
  1351. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1352. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_activity_api];
  1353. OCWebDAVClient *request = [OCWebDAVClient new];
  1354. request = [self getRequestWithCredentials:request];
  1355. [request getActivityServer:serverPath since:since limit:limit previews:previews link:link onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1356. NSData *responseData = (NSData*) responseObject;
  1357. NSMutableArray *listOfActivity = [NSMutableArray new];
  1358. //Parse
  1359. NSError *error;
  1360. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1361. NSLog(@"[LOG] Activity : %@",jsongParsed);
  1362. if (jsongParsed && [jsongParsed isKindOfClass:[NSDictionary class]] && jsongParsed.allKeys > 0) {
  1363. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1364. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1365. NSDictionary *datas = [ocs valueForKey:@"data"];
  1366. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1367. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1368. for (NSDictionary *data in datas) {
  1369. OCActivity *activity = [OCActivity new];
  1370. if ([data valueForKey:@"activity_id"] && ![[data valueForKey:@"activity_id"] isEqual:[NSNull null]])
  1371. activity.idActivity = [[data valueForKey:@"activity_id"] integerValue];
  1372. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1373. NSString *dateString = [data valueForKey:@"datetime"];
  1374. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1375. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1376. [dateFormatter setLocale:enUSPOSIXLocale];
  1377. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1378. activity.date = [dateFormatter dateFromString:dateString];
  1379. }
  1380. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1381. activity.app = [data valueForKey:@"app"];
  1382. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1383. activity.type = [data valueForKey:@"type"];
  1384. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1385. activity.user = [data valueForKey:@"user"];
  1386. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1387. activity.subject = [data valueForKey:@"subject"];
  1388. if ([data valueForKey:@"subject_rich"] && ![[data valueForKey:@"subject_rich"] isEqual:[NSNull null]])
  1389. activity.subject_rich = [data valueForKey:@"subject_rich"];
  1390. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1391. activity.message = [data valueForKey:@"message"];
  1392. if ([data valueForKey:@"message_rich"] && ![[data valueForKey:@"message_rich"] isEqual:[NSNull null]])
  1393. activity.message_rich = [data valueForKey:@"message_rich"];
  1394. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1395. activity.icon = [data valueForKey:@"icon"];
  1396. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1397. activity.link = [data valueForKey:@"link"];
  1398. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1399. activity.object_type = [data valueForKey:@"object_type"];
  1400. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1401. activity.object_id = [[data valueForKey:@"object_id"] integerValue];
  1402. if ([data valueForKey:@"object_name"] && ![[data valueForKey:@"object_name"] isEqual:[NSNull null]])
  1403. activity.object_name = [data valueForKey:@"object_name"];
  1404. if ([data valueForKey:@"previews"] && ![[data valueForKey:@"previews"] isEqual:[NSNull null]])
  1405. activity.previews = [data valueForKey:@"previews"];
  1406. [listOfActivity addObject:activity];
  1407. }
  1408. successRequest(response, listOfActivity, request.redirectedServer);
  1409. } else {
  1410. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1411. if ([message isKindOfClass:[NSNull class]]) {
  1412. message = NSLocalizedString(@"_server_response_error_", nil);
  1413. }
  1414. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1415. }
  1416. } else {
  1417. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1418. }
  1419. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1420. failureRequest(response, error, request.redirectedServer);
  1421. }];
  1422. }
  1423. #pragma mark - External Sites
  1424. - (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 {
  1425. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1426. serverPath = [serverPath stringByAppendingString:k_url_acces_external_sites_api];
  1427. OCWebDAVClient *request = [OCWebDAVClient new];
  1428. request = [self getRequestWithCredentials:request];
  1429. [request getExternalSitesServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1430. NSData *responseData = (NSData*) responseObject;
  1431. NSMutableArray *listOfExternalSites = [NSMutableArray new];
  1432. //Parse
  1433. NSError *error;
  1434. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1435. NSLog(@"[LOG] External Sites : %@",jsongParsed);
  1436. if (jsongParsed && jsongParsed.allKeys > 0) {
  1437. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1438. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1439. NSDictionary *datas = [ocs valueForKey:@"data"];
  1440. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1441. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1442. for (NSDictionary *data in datas) {
  1443. OCExternalSites *externalSites = [OCExternalSites new];
  1444. externalSites.idExternalSite = [[data valueForKey:@"id"] integerValue];
  1445. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1446. externalSites.icon = [data valueForKey:@"icon"];
  1447. if ([data valueForKey:@"lang"] && ![[data valueForKey:@"lang"] isEqual:[NSNull null]])
  1448. externalSites.lang = [data valueForKey:@"lang"];
  1449. if ([data valueForKey:@"name"] && ![[data valueForKey:@"name"] isEqual:[NSNull null]])
  1450. externalSites.name = [data valueForKey:@"name"];
  1451. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isEqual:[NSNull null]])
  1452. externalSites.url = [data valueForKey:@"url"];
  1453. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1454. externalSites.type = [data valueForKey:@"type"];
  1455. [listOfExternalSites addObject:externalSites];
  1456. }
  1457. successRequest(response, listOfExternalSites, request.redirectedServer);
  1458. } else {
  1459. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1460. if ([message isKindOfClass:[NSNull class]]) {
  1461. message = NSLocalizedString(@"_server_response_error_", nil);
  1462. }
  1463. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1464. }
  1465. } else {
  1466. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1467. }
  1468. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1469. failureRequest(response, error, request.redirectedServer);
  1470. }];
  1471. }
  1472. #pragma mark - User Profile
  1473. - (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 {
  1474. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_userprofile_api];
  1475. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1476. OCWebDAVClient *request = [OCWebDAVClient new];
  1477. request = [self getRequestWithCredentials:request];
  1478. [request getUserProfileServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1479. NSData *responseData = (NSData*) responseObject;
  1480. OCUserProfile *userProfile = [OCUserProfile new];
  1481. //Parse
  1482. NSError *error;
  1483. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1484. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  1485. if (jsongParsed && jsongParsed.allKeys > 0) {
  1486. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1487. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1488. NSDictionary *data = [ocs valueForKey:@"data"];
  1489. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1490. if (statusCode == kOCUserProfileAPISuccessful) {
  1491. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  1492. userProfile.address = [data valueForKey:@"address"];
  1493. if ([data valueForKey:@"display-name"] && ![[data valueForKey:@"display-name"] isKindOfClass:[NSNull class]])
  1494. userProfile.displayName = [data valueForKey:@"display-name"];
  1495. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  1496. userProfile.email = [data valueForKey:@"email"];
  1497. if ([data valueForKey:@"enabled"] && ![[data valueForKey:@"enabled"] isKindOfClass:[NSNull class]])
  1498. userProfile.enabled = [[data valueForKey:@"enabled"] boolValue];
  1499. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isKindOfClass:[NSNull class]])
  1500. userProfile.id = [data valueForKey:@"id"];
  1501. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  1502. userProfile.phone = [data valueForKey:@"phone"];
  1503. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  1504. userProfile.twitter = [data valueForKey:@"twitter"];
  1505. if ([data valueForKey:@"webpage"] && ![[data valueForKey:@"webpage"] isKindOfClass:[NSNull class]])
  1506. userProfile.webpage = [data valueForKey:@"webpage"];
  1507. /* QUOTA */
  1508. NSDictionary *quota = [data valueForKey:@"quota"];
  1509. if ([quota count] > 0) {
  1510. if ([quota valueForKey:@"free"] && ![[quota valueForKey:@"free"] isKindOfClass:[NSNull class]])
  1511. userProfile.quotaFree = [[quota valueForKey:@"free"] doubleValue];
  1512. if ([quota valueForKey:@"quota"] && ![[quota valueForKey:@"quota"] isKindOfClass:[NSNull class]])
  1513. userProfile.quota = [[quota valueForKey:@"quota"] doubleValue];
  1514. if ([quota valueForKey:@"relative"] && ![[quota valueForKey:@"relative"] isKindOfClass:[NSNull class]])
  1515. userProfile.quotaRelative = [[quota valueForKey:@"relative"] doubleValue];
  1516. if ([quota valueForKey:@"total"] && ![[quota valueForKey:@"total"] isKindOfClass:[NSNull class]])
  1517. userProfile.quotaTotal = [[quota valueForKey:@"total"] doubleValue];
  1518. if ([quota valueForKey:@"used"] && ![[quota valueForKey:@"used"] isKindOfClass:[NSNull class]])
  1519. userProfile.quotaUsed = [[quota valueForKey:@"used"] doubleValue];
  1520. }
  1521. successRequest(response, userProfile, request.redirectedServer);
  1522. } else {
  1523. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1524. if ([message isKindOfClass:[NSNull class]]) {
  1525. message = NSLocalizedString(@"_server_response_error_", nil);
  1526. }
  1527. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1528. }
  1529. } else {
  1530. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1531. }
  1532. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1533. failureRequest(response, error, request.redirectedServer);
  1534. }];
  1535. }
  1536. #pragma mark - End-to-End Encryption
  1537. - (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 {
  1538. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1539. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1540. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1541. OCWebDAVClient *request = [OCWebDAVClient new];
  1542. request = [self getRequestWithCredentials:request];
  1543. [request getEndToEndPublicKeys:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1544. NSData *responseData = (NSData*) responseObject;
  1545. NSString *publicKey = @"";
  1546. //Parse
  1547. NSError *error;
  1548. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1549. NSLog(@"[LOG] E2E Get PublicKey : %@",jsongParsed);
  1550. if (jsongParsed && jsongParsed.allKeys > 0) {
  1551. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1552. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1553. NSDictionary *data = [ocs valueForKey:@"data"];
  1554. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1555. if (statusCode == kOCUserProfileAPISuccessful) {
  1556. if ([data valueForKey:@"public-keys"] && ![[data valueForKey:@"public-keys"] isKindOfClass:[NSNull class]]) {
  1557. NSDictionary *publickeys = [data valueForKey:@"public-keys"];
  1558. publicKey = [publickeys valueForKey:self.userID];
  1559. successRequest(response, publicKey, request.redirectedServer);
  1560. } else {
  1561. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1562. }
  1563. } else {
  1564. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1565. if ([message isKindOfClass:[NSNull class]]) {
  1566. message = NSLocalizedString(@"_server_response_error_", nil);
  1567. }
  1568. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1569. }
  1570. } else {
  1571. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1572. }
  1573. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1574. //Return error
  1575. failureRequest(response, error, request.redirectedServer);
  1576. }];
  1577. }
  1578. - (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 {
  1579. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1580. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1581. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1582. OCWebDAVClient *request = [OCWebDAVClient new];
  1583. request = [self getRequestWithCredentials:request];
  1584. [request getEndToEndPrivateKeyCipher:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1585. NSData *responseData = (NSData*) responseObject;
  1586. NSString *privateKeyChiper = @"";
  1587. //Parse
  1588. NSError *error;
  1589. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1590. NSLog(@"[LOG] E2E Get PrivateKey : %@",jsongParsed);
  1591. if (jsongParsed && jsongParsed.allKeys > 0) {
  1592. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1593. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1594. NSDictionary *data = [ocs valueForKey:@"data"];
  1595. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1596. if (statusCode == kOCUserProfileAPISuccessful) {
  1597. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]]) {
  1598. privateKeyChiper = [data valueForKey:@"private-key"];
  1599. successRequest(response, privateKeyChiper, request.redirectedServer);
  1600. } else {
  1601. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1602. }
  1603. } else {
  1604. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1605. if ([message isKindOfClass:[NSNull class]]) {
  1606. message = NSLocalizedString(@"_server_response_error_", nil);
  1607. }
  1608. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1609. }
  1610. } else {
  1611. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1612. }
  1613. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1614. //Return error
  1615. failureRequest(response, error, request.redirectedServer);
  1616. }];
  1617. }
  1618. - (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 {
  1619. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1620. serverPath = [serverPath stringByAppendingString:@"/server-key"];
  1621. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1622. OCWebDAVClient *request = [OCWebDAVClient new];
  1623. request = [self getRequestWithCredentials:request];
  1624. [request getEndToEndServerPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1625. NSData *responseData = (NSData*) responseObject;
  1626. NSString *publicKey = @"";
  1627. //Parse
  1628. NSError *error;
  1629. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1630. NSLog(@"[LOG] E2E Get Server PublicKey : %@",jsongParsed);
  1631. if (jsongParsed && jsongParsed.allKeys > 0) {
  1632. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1633. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1634. NSDictionary *data = [ocs valueForKey:@"data"];
  1635. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1636. if (statusCode == kOCUserProfileAPISuccessful) {
  1637. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1638. publicKey = [data valueForKey:@"public-key"];
  1639. successRequest(response, publicKey, request.redirectedServer);
  1640. } else {
  1641. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1642. }
  1643. } else {
  1644. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1645. if ([message isKindOfClass:[NSNull class]]) {
  1646. message = NSLocalizedString(@"_server_response_error_", nil);
  1647. }
  1648. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1649. }
  1650. } else {
  1651. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1652. }
  1653. //Return success
  1654. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1655. //Return error
  1656. failureRequest(response, error, request.redirectedServer);
  1657. }];
  1658. }
  1659. - (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 {
  1660. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1661. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1662. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1663. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1664. request = [self getRequestWithCredentials:request];
  1665. [request signEndToEndPublicKey:serverPath key:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1666. NSData *responseData = (NSData*) responseObject;
  1667. NSString *publicKey = @"";
  1668. //Parse
  1669. NSError *error;
  1670. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1671. NSLog(@"[LOG] E2E Sign PublicKey : %@",jsongParsed);
  1672. if (jsongParsed && jsongParsed.allKeys > 0) {
  1673. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1674. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1675. NSDictionary *data = [ocs valueForKey:@"data"];
  1676. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1677. if (statusCode == kOCUserProfileAPISuccessful) {
  1678. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1679. publicKey = [data valueForKey:@"public-key"];
  1680. successRequest(response, publicKey, request.redirectedServer);
  1681. } else {
  1682. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1683. }
  1684. } else {
  1685. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1686. if ([message isKindOfClass:[NSNull class]]) {
  1687. message = NSLocalizedString(@"_server_response_error_", nil);
  1688. }
  1689. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1690. }
  1691. } else {
  1692. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1693. }
  1694. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1695. //Return error
  1696. failureRequest(response, error, request.redirectedServer);
  1697. }];
  1698. }
  1699. - (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 {
  1700. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1701. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1702. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1703. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1704. request = [self getRequestWithCredentials:request];
  1705. [request storeEndToEndPrivateKeyCipher:serverPath key:privateKeyChiper onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1706. NSData *responseData = (NSData*) responseObject;
  1707. NSString *privateKey = @"";
  1708. //Parse
  1709. NSError *error;
  1710. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1711. NSLog(@"[LOG] E2E Store PrivateKey : %@",jsongParsed);
  1712. if (jsongParsed && jsongParsed.allKeys > 0) {
  1713. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1714. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1715. NSDictionary *data = [ocs valueForKey:@"data"];
  1716. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1717. if (statusCode == kOCUserProfileAPISuccessful) {
  1718. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]]) {
  1719. privateKey = [data valueForKey:@"private-key"];
  1720. successRequest(response, privateKey, request.redirectedServer);
  1721. } else {
  1722. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1723. }
  1724. } else {
  1725. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1726. if ([message isKindOfClass:[NSNull class]]) {
  1727. message = NSLocalizedString(@"_server_response_error_", nil);
  1728. }
  1729. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1730. }
  1731. } else {
  1732. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1733. }
  1734. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1735. //Return error
  1736. failureRequest(response, error, request.redirectedServer);
  1737. }];
  1738. }
  1739. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1740. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1741. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1742. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1743. OCWebDAVClient *request = [OCWebDAVClient new];
  1744. request = [self getRequestWithCredentials:request];
  1745. [request deleteEndToEndPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1746. //Return success
  1747. successRequest(response, request.redirectedServer);
  1748. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1749. //Return error
  1750. failureRequest(response, error, request.redirectedServer);
  1751. }];
  1752. }
  1753. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1754. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1755. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1756. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1757. OCWebDAVClient *request = [OCWebDAVClient new];
  1758. request = [self getRequestWithCredentials:request];
  1759. [request deleteEndToEndPrivateKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1760. //Return success
  1761. successRequest(response, request.redirectedServer);
  1762. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1763. //Return error
  1764. failureRequest(response, error, request.redirectedServer);
  1765. }];
  1766. }
  1767. - (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 {
  1768. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1769. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1770. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1771. OCWebDAVClient *request = [OCWebDAVClient new];
  1772. request = [self getRequestWithCredentials:request];
  1773. [request markEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1774. //Return success
  1775. successRequest(response, request.redirectedServer);
  1776. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1777. //Return error
  1778. failureRequest(response, error, request.redirectedServer);
  1779. }];
  1780. }
  1781. - (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 {
  1782. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1783. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1784. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1785. OCWebDAVClient *request = [OCWebDAVClient new];
  1786. request = [self getRequestWithCredentials:request];
  1787. [request deletemarkEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1788. //Return success
  1789. successRequest(response, request.redirectedServer);
  1790. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1791. //Return error
  1792. failureRequest(response, error, request.redirectedServer);
  1793. }];
  1794. }
  1795. - (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 {
  1796. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1797. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1798. if (token) {
  1799. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1800. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1801. } else {
  1802. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1803. }
  1804. OCWebDAVClient *request = [OCWebDAVClient new];
  1805. request = [self getRequestWithCredentials:request];
  1806. [request lockEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1807. NSData *responseData = (NSData*) responseObject;
  1808. NSString *token = @"";
  1809. //Parse
  1810. NSError *error;
  1811. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1812. NSLog(@"[LOG] E2E Lock File : %@",jsongParsed);
  1813. if (jsongParsed && jsongParsed.allKeys > 0) {
  1814. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1815. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1816. NSDictionary *data = [ocs valueForKey:@"data"];
  1817. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1818. if (statusCode == kOCUserProfileAPISuccessful) {
  1819. if ([data valueForKey:@"token"] && ![[data valueForKey:@"token"] isKindOfClass:[NSNull class]]) {
  1820. token = [data valueForKey:@"token"];
  1821. successRequest(response, token, request.redirectedServer);
  1822. } else {
  1823. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1824. }
  1825. } else {
  1826. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1827. if ([message isKindOfClass:[NSNull class]]) {
  1828. message = NSLocalizedString(@"_server_response_error_", nil);
  1829. }
  1830. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1831. }
  1832. } else {
  1833. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1834. }
  1835. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1836. //Return error
  1837. failureRequest(response, error, request.redirectedServer);
  1838. }];
  1839. }
  1840. - (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 {
  1841. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1842. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1843. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1844. OCWebDAVClient *request = [OCWebDAVClient new];
  1845. request = [self getRequestWithCredentials:request];
  1846. [request unlockEndToEndFolderEncrypted:serverPath token:token onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1847. //Return success
  1848. successRequest(response, request.redirectedServer);
  1849. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1850. //Return error
  1851. failureRequest(response, error, request.redirectedServer);
  1852. }];
  1853. }
  1854. - (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 {
  1855. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1856. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1857. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1858. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1859. request = [self getRequestWithCredentials:request];
  1860. [request getEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1861. NSData *responseData = (NSData*) responseObject;
  1862. NSString *encryptedMetadata = @"";
  1863. //Parse
  1864. NSError *error;
  1865. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1866. NSLog(@"[LOG] E2E Get Metadata : %@",jsongParsed);
  1867. if (jsongParsed && jsongParsed.allKeys > 0) {
  1868. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1869. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1870. NSDictionary *data = [ocs valueForKey:@"data"];
  1871. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1872. if (statusCode == kOCUserProfileAPISuccessful) {
  1873. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1874. encryptedMetadata = [data valueForKey:@"meta-data"];
  1875. successRequest(response, encryptedMetadata, request.redirectedServer);
  1876. } else {
  1877. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1878. }
  1879. } else {
  1880. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1881. if ([message isKindOfClass:[NSNull class]]) {
  1882. message = NSLocalizedString(@"_server_response_error_", nil);
  1883. }
  1884. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1885. }
  1886. } else {
  1887. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1888. }
  1889. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1890. //Return error
  1891. failureRequest(response, error, request.redirectedServer);
  1892. }];
  1893. }
  1894. - (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 {
  1895. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1896. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1897. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1898. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1899. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1900. request = [self getRequestWithCredentials:request];
  1901. [request storeEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1902. NSData *responseData = (NSData*) responseObject;
  1903. NSString *encryptedMetadata = @"";
  1904. //Parse
  1905. NSError *error;
  1906. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1907. NSLog(@"[LOG] E2E Store Metadata : %@",jsongParsed);
  1908. if (jsongParsed && jsongParsed.allKeys > 0) {
  1909. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1910. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1911. NSDictionary *data = [ocs valueForKey:@"data"];
  1912. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1913. if (statusCode == kOCUserProfileAPISuccessful) {
  1914. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1915. encryptedMetadata = [data valueForKey:@"meta-data"];
  1916. successRequest(response, encryptedMetadata, request.redirectedServer);
  1917. } else {
  1918. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1919. }
  1920. } else {
  1921. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1922. if ([message isKindOfClass:[NSNull class]]) {
  1923. message = NSLocalizedString(@"_server_response_error_", nil);
  1924. }
  1925. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1926. }
  1927. } else {
  1928. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1929. }
  1930. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1931. //Return error
  1932. failureRequest(response, error, request.redirectedServer);
  1933. }];
  1934. }
  1935. - (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 {
  1936. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1937. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1938. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1939. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1940. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1941. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1942. request = [self getRequestWithCredentials:request];
  1943. [request updateEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1944. NSData *responseData = (NSData*) responseObject;
  1945. NSString *encryptedMetadata = @"";
  1946. //Parse
  1947. NSError *error;
  1948. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1949. NSLog(@"[LOG] E2E Update Metadata : %@",jsongParsed);
  1950. if (jsongParsed && jsongParsed.allKeys > 0) {
  1951. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1952. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1953. NSDictionary *data = [ocs valueForKey:@"data"];
  1954. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1955. if (statusCode == kOCUserProfileAPISuccessful) {
  1956. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1957. encryptedMetadata = [data valueForKey:@"meta-data"];
  1958. successRequest(response, encryptedMetadata, request.redirectedServer);
  1959. } else {
  1960. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1961. }
  1962. } else {
  1963. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1964. if ([message isKindOfClass:[NSNull class]]) {
  1965. message = NSLocalizedString(@"_server_response_error_", nil);
  1966. }
  1967. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1968. }
  1969. } else {
  1970. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1971. }
  1972. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1973. //Return error
  1974. failureRequest(response, error, request.redirectedServer);
  1975. }];
  1976. }
  1977. - (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 {
  1978. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1979. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1980. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1981. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1982. request = [self getRequestWithCredentials:request];
  1983. [request deleteEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1984. //Return success
  1985. successRequest(response, request.redirectedServer);
  1986. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1987. //Return error
  1988. failureRequest(response, error, request.redirectedServer);
  1989. }];
  1990. }
  1991. #pragma mark - Manage Mobile Editor OCS API
  1992. - (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 {
  1993. serverPath = [serverPath stringByAppendingString:k_url_create_link_mobile_richdocuments];
  1994. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1995. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1996. request = [self getRequestWithCredentials:request];
  1997. [request createLinkRichdocuments:serverPath fileID:fileID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  1998. NSData *responseData = (NSData*) response;
  1999. //Parse
  2000. NSError *error;
  2001. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2002. NSLog(@"[LOG] Link richdocuments : %@",jsongParsed);
  2003. if (jsongParsed && jsongParsed.allKeys > 0) {
  2004. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2005. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2006. NSDictionary *data = [ocs valueForKey:@"data"];
  2007. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2008. if (statusCode == kOCUserProfileAPISuccessful) {
  2009. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isKindOfClass:[NSNull class]]) {
  2010. NSString *link = [data valueForKey:@"url"];
  2011. successRequest(response, link, request.redirectedServer);
  2012. } else {
  2013. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2014. }
  2015. } else {
  2016. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2017. if ([message isKindOfClass:[NSNull class]]) {
  2018. message = NSLocalizedString(@"_server_response_error_", nil);
  2019. }
  2020. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2021. }
  2022. } else {
  2023. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2024. }
  2025. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2026. //Return error
  2027. failureRequest(response, error, request.redirectedServer);
  2028. }];
  2029. }
  2030. - (void)getTemplatesRichdocuments:(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 {
  2031. serverPath = [serverPath stringByAppendingString:k_url_get_template_mobile_richdocuments];
  2032. serverPath = [serverPath stringByAppendingString:typeTemplate];
  2033. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2034. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2035. request = [self getRequestWithCredentials:request];
  2036. [request getTemplatesRichdocuments:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2037. NSData *responseData = (NSData*) response;
  2038. //Parse
  2039. NSError *error;
  2040. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2041. NSLog(@"[LOG] Link richdocuments : %@",jsongParsed);
  2042. if (jsongParsed && jsongParsed.allKeys > 0) {
  2043. NSMutableArray *list = [NSMutableArray new];
  2044. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2045. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2046. NSDictionary *data = [ocs valueForKey:@"data"];
  2047. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2048. if (statusCode == kOCUserProfileAPISuccessful) {
  2049. for (NSDictionary *dicDatas in data) {
  2050. NCRichDocumentTemplate *template = [NCRichDocumentTemplate new];
  2051. if ([dicDatas valueForKey:@"id"] && ![[dicDatas valueForKey:@"id"] isEqual:[NSNull null]])
  2052. template.templateID = [[dicDatas valueForKey:@"id"] integerValue];
  2053. if ([dicDatas valueForKey:@"delete"] && ![[dicDatas valueForKey:@"delete"] isKindOfClass:[NSNull class]])
  2054. template.delete = [dicDatas valueForKey:@"delete"];
  2055. if ([dicDatas valueForKey:@"extension"] && ![[dicDatas valueForKey:@"extension"] isKindOfClass:[NSNull class]])
  2056. template.extension = [dicDatas valueForKey:@"extension"];
  2057. if ([dicDatas valueForKey:@"name"] && ![[dicDatas valueForKey:@"name"] isKindOfClass:[NSNull class]])
  2058. template.name = [dicDatas valueForKey:@"name"];
  2059. if ([dicDatas valueForKey:@"preview"] && ![[dicDatas valueForKey:@"preview"] isKindOfClass:[NSNull class]])
  2060. template.preview = [dicDatas valueForKey:@"preview"];
  2061. if ([dicDatas valueForKey:@"type"] && ![[dicDatas valueForKey:@"type"] isKindOfClass:[NSNull class]])
  2062. template.type = [dicDatas valueForKey:@"type"];
  2063. [list addObject:template];
  2064. }
  2065. successRequest(response, list, request.redirectedServer);
  2066. } else {
  2067. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2068. if ([message isKindOfClass:[NSNull class]]) {
  2069. message = NSLocalizedString(@"_server_response_error_", nil);
  2070. }
  2071. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2072. }
  2073. } else {
  2074. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2075. }
  2076. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2077. //Return error
  2078. failureRequest(response, error, request.redirectedServer);
  2079. }];
  2080. }
  2081. - (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 {
  2082. serverPath = [serverPath stringByAppendingString:k_url_create_new_mobile_richdocuments];
  2083. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2084. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2085. request = [self getRequestWithCredentials:request];
  2086. [request createNewRichdocuments:serverPath path:path templateID:templateID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2087. NSData *responseData = (NSData*) response;
  2088. //Parse
  2089. NSError *error;
  2090. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2091. NSLog(@"[LOG] URL Asset : %@",jsongParsed);
  2092. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2093. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2094. NSDictionary *data = [ocs valueForKey:@"data"];
  2095. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2096. if (statusCode == kOCUserProfileAPISuccessful) {
  2097. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isKindOfClass:[NSNull class]])
  2098. successRequest(response, [data valueForKey:@"url"], request.redirectedServer);
  2099. else
  2100. successRequest(response, nil, request.redirectedServer);
  2101. } else {
  2102. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2103. }
  2104. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2105. //Return error
  2106. failureRequest(response, error, request.redirectedServer);
  2107. }];
  2108. }
  2109. - (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 {
  2110. serverPath = [serverPath stringByAppendingString:k_url_insert_assets_to_richdocuments];
  2111. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2112. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2113. request = [self getRequestWithCredentials:request];
  2114. [request createAssetRichdocuments:serverPath path:path onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2115. NSData *responseData = (NSData*) response;
  2116. //Parse
  2117. NSError *error;
  2118. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2119. NSLog(@"[LOG] URL Asset : %@",jsongParsed);
  2120. if (jsongParsed && jsongParsed.allKeys > 0) {
  2121. if ([jsongParsed valueForKey:@"url"] && ![[jsongParsed valueForKey:@"url"] isKindOfClass:[NSNull class]]) {
  2122. NSString *url = [jsongParsed valueForKey:@"url"];
  2123. successRequest(response, url, request.redirectedServer);
  2124. } else {
  2125. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2126. }
  2127. } else {
  2128. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2129. }
  2130. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2131. //Return error
  2132. failureRequest(response, error, request.redirectedServer);
  2133. }];
  2134. }
  2135. #pragma mark - Trash
  2136. - (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
  2137. {
  2138. OCWebDAVClient *request = [OCWebDAVClient new];
  2139. request = [self getRequestWithCredentials:request];
  2140. path = [path encodeString:NSUTF8StringEncoding];
  2141. [request listTrash:path depth:depth onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  2142. OCXMLParser *parser = [OCXMLParser new];
  2143. [parser initParserWithData:responseObject];
  2144. NSMutableArray *list = [parser.directoryList mutableCopy];
  2145. successRequest(response, list, request.redirectedServer);
  2146. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error) {
  2147. failureRequest(response, error, request.redirectedServer);
  2148. }];
  2149. }
  2150. - (void)emptyTrash:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2151. {
  2152. OCWebDAVClient *request = [OCWebDAVClient new];
  2153. request = [self getRequestWithCredentials:request];
  2154. [request emptyTrash:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  2155. successRequest(response, request.redirectedServer);
  2156. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error) {
  2157. failureRequest(response, error, request.redirectedServer);
  2158. }];
  2159. }
  2160. #pragma mark - Fulltextsearch
  2161. - (void)fullTextSearch:(NSString *)serverPath providers:(NSString *)providers text:(NSString *)text page:(NSInteger)page options:(NSString *)options size:(NSInteger)size onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2162. serverPath = [serverPath stringByAppendingString:k_url_fulltextsearch];
  2163. serverPath = [NSString stringWithFormat:@"?providers:%@", providers];
  2164. serverPath = [NSString stringWithFormat:@"?search:%@", text];
  2165. serverPath = [NSString stringWithFormat:@"?page:%ld", (long) page];
  2166. serverPath = [NSString stringWithFormat:@"?options:%@", options];
  2167. serverPath = [NSString stringWithFormat:@"?size:%ld", (long) size];
  2168. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2169. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2170. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2171. request = [self getRequestWithCredentials:request];
  2172. [request fullTextSearch:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response) {
  2173. NSData *responseData = (NSData*) response;
  2174. //Parse
  2175. NSError *error;
  2176. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2177. NSLog(@"[LOG] Link richdocuments : %@",jsongParsed);
  2178. if (jsongParsed && jsongParsed.allKeys > 0) {
  2179. NSMutableArray *list = [NSMutableArray new];
  2180. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2181. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2182. NSDictionary *data = [ocs valueForKey:@"data"];
  2183. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2184. if (statusCode == kOCUserProfileAPISuccessful) {
  2185. for (NSDictionary *dicDatas in data) {
  2186. NCRichDocumentTemplate *template = [NCRichDocumentTemplate new];
  2187. if ([dicDatas valueForKey:@"id"] && ![[dicDatas valueForKey:@"id"] isEqual:[NSNull null]])
  2188. template.templateID = [[dicDatas valueForKey:@"id"] integerValue];
  2189. if ([dicDatas valueForKey:@"delete"] && ![[dicDatas valueForKey:@"delete"] isKindOfClass:[NSNull class]])
  2190. template.delete = [dicDatas valueForKey:@"delete"];
  2191. if ([dicDatas valueForKey:@"extension"] && ![[dicDatas valueForKey:@"extension"] isKindOfClass:[NSNull class]])
  2192. template.extension = [dicDatas valueForKey:@"extension"];
  2193. if ([dicDatas valueForKey:@"name"] && ![[dicDatas valueForKey:@"name"] isKindOfClass:[NSNull class]])
  2194. template.name = [dicDatas valueForKey:@"name"];
  2195. if ([dicDatas valueForKey:@"preview"] && ![[dicDatas valueForKey:@"preview"] isKindOfClass:[NSNull class]])
  2196. template.preview = [dicDatas valueForKey:@"preview"];
  2197. if ([dicDatas valueForKey:@"type"] && ![[dicDatas valueForKey:@"type"] isKindOfClass:[NSNull class]])
  2198. template.type = [dicDatas valueForKey:@"type"];
  2199. [list addObject:template];
  2200. }
  2201. successRequest(response, list, request.redirectedServer);
  2202. } else {
  2203. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2204. if ([message isKindOfClass:[NSNull class]]) {
  2205. message = NSLocalizedString(@"_server_response_error_", nil);
  2206. }
  2207. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2208. }
  2209. } else {
  2210. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2211. }
  2212. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2213. //Return error
  2214. failureRequest(response, error, request.redirectedServer);
  2215. }];
  2216. }
  2217. #pragma mark - Third Parts
  2218. - (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
  2219. {
  2220. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2221. OCWebDAVClient *request = [OCWebDAVClient new];
  2222. request = [self getRequestWithCredentials:request];
  2223. [request getHCUserProfile:serverPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response) {
  2224. NSData *responseData = (NSData*) response;
  2225. OCUserProfile *userProfile = [OCUserProfile new];
  2226. //Parse
  2227. NSError *error;
  2228. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2229. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  2230. if (jsongParsed && jsongParsed.allKeys > 0) {
  2231. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2232. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2233. NSDictionary *data = [ocs valueForKey:@"data"];
  2234. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2235. data = [data valueForKey:@"data"];
  2236. if (statusCode == kOCUserProfileAPISuccessful) {
  2237. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  2238. userProfile.address = [data valueForKey:@"address"];
  2239. if ([data valueForKey:@"displayname"] && ![[data valueForKey:@"displayname"] isKindOfClass:[NSNull class]])
  2240. userProfile.displayName = [data valueForKey:@"displayname"];
  2241. if ([data valueForKey:@"businesssize"] && ![[data valueForKey:@"businesssize"] isKindOfClass:[NSNull class]]) {
  2242. switch ([[data valueForKey:@"businesssize"] integerValue]) {
  2243. case 1:
  2244. userProfile.businessSize = @"1-4";
  2245. break;
  2246. case 5:
  2247. userProfile.businessSize = @"5-9";
  2248. break;
  2249. case 10:
  2250. userProfile.businessSize = @"10-19";
  2251. break;
  2252. case 20:
  2253. userProfile.businessSize = @"20-49";
  2254. break;
  2255. case 50:
  2256. userProfile.businessSize = @"50-99";
  2257. break;
  2258. case 100:
  2259. userProfile.businessSize = @"100-249";
  2260. break;
  2261. case 250:
  2262. userProfile.businessSize = @"250-499";
  2263. break;
  2264. case 500:
  2265. userProfile.businessSize = @"500-999";
  2266. break;
  2267. case 1000:
  2268. userProfile.businessSize = @"1000+";
  2269. break;
  2270. default:
  2271. break;
  2272. }
  2273. }
  2274. if ([data valueForKey:@"businesstype"] && ![[data valueForKey:@"businesstype"] isKindOfClass:[NSNull class]])
  2275. userProfile.businessType = [data valueForKey:@"businesstype"];
  2276. if ([data valueForKey:@"city"] && ![[data valueForKey:@"city"] isKindOfClass:[NSNull class]])
  2277. userProfile.city = [data valueForKey:@"city"];
  2278. if ([data valueForKey:@"company"] && ![[data valueForKey:@"company"] isKindOfClass:[NSNull class]])
  2279. userProfile.company = [data valueForKey:@"company"];
  2280. if ([data valueForKey:@"country"] && ![[data valueForKey:@"country"] isKindOfClass:[NSNull class]])
  2281. userProfile.country = [data valueForKey:@"country"];
  2282. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  2283. userProfile.email = [data valueForKey:@"email"];
  2284. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  2285. userProfile.phone = [data valueForKey:@"phone"];
  2286. if ([data valueForKey:@"role"] && ![[data valueForKey:@"role"] isKindOfClass:[NSNull class]])
  2287. userProfile.role = [data valueForKey:@"role"];
  2288. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  2289. userProfile.twitter = [data valueForKey:@"twitter"];
  2290. if ([data valueForKey:@"website"] && ![[data valueForKey:@"website"] isKindOfClass:[NSNull class]])
  2291. userProfile.webpage = [data valueForKey:@"website"];
  2292. if ([data valueForKey:@"zip"] && ![[data valueForKey:@"zip"] isKindOfClass:[NSNull class]])
  2293. userProfile.zip = [data valueForKey:@"zip"];
  2294. successRequest(response, userProfile, request.redirectedServer);
  2295. } else {
  2296. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2297. if ([message isKindOfClass:[NSNull class]]) {
  2298. message = NSLocalizedString(@"_server_response_error_", nil);
  2299. }
  2300. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2301. }
  2302. } else {
  2303. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2304. }
  2305. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2306. failureRequest(response, error, request.redirectedServer);
  2307. }];
  2308. }
  2309. - (void)putHCUserProfile:(NSString*)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2310. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2311. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2312. request = [self getRequestWithCredentials:request];
  2313. [request putHCUserProfile:serverPath data:data onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2314. successRequest(response, request.redirectedServer);
  2315. } failure:^(NSHTTPURLResponse *response, id responseObject, NSError *error) {
  2316. failureRequest(response, error, request.redirectedServer);
  2317. }];
  2318. }
  2319. - (void)getHCFeatures:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2320. {
  2321. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2322. OCWebDAVClient *request = [OCWebDAVClient new];
  2323. request = [self getRequestWithCredentials:request];
  2324. [request getHCUserProfile:serverPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response) {
  2325. NSData *responseData = (NSData*) response;
  2326. HCFeatures *features = [HCFeatures new];
  2327. //Parse
  2328. NSError *error;
  2329. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2330. NSLog(@"[LOG] User Profile Features : %@",jsongParsed);
  2331. if (jsongParsed && jsongParsed.allKeys > 0) {
  2332. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2333. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2334. NSDictionary *data = [ocs valueForKey:@"data"];
  2335. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2336. data = [data valueForKey:@"data"];
  2337. if (statusCode == kOCUserProfileAPISuccessful) {
  2338. if ([data valueForKey:@"is_trial"] && ![[data valueForKey:@"is_trial"] isKindOfClass:[NSNull class]])
  2339. features.isTrial = [[data valueForKey:@"is_trial"] boolValue];
  2340. if ([data valueForKey:@"trial_expired"] && ![[data valueForKey:@"trial_expired"] isKindOfClass:[NSNull class]])
  2341. features.trialExpired = [[data valueForKey:@"trial_expired"] boolValue];
  2342. if ([data valueForKey:@"trial_remaining_sec"] && ![[data valueForKey:@"trial_remaining_sec"] isKindOfClass:[NSNull class]])
  2343. features.trialRemainingSec = [[data valueForKey:@"trial_remaining_sec"] integerValue];
  2344. if ([data valueForKey:@"trial_end_time"] && ![[data valueForKey:@"trial_end_time"] isKindOfClass:[NSNull class]])
  2345. features.trialEndTime = [[data valueForKey:@"trial_end_time"] integerValue];
  2346. if ([data valueForKey:@"trial_end"] && ![[data valueForKey:@"trial_end"] isKindOfClass:[NSNull class]])
  2347. features.trialEnd = [data valueForKey:@"trial_end"];
  2348. if ([data valueForKey:@"account_remove_expired"] && ![[data valueForKey:@"account_remove_expired"] isKindOfClass:[NSNull class]])
  2349. features.accountRemoveExpired = [[data valueForKey:@"account_remove_expired"] boolValue];
  2350. if ([data valueForKey:@"account_remove_remaining_sec"] && ![[data valueForKey:@"account_remove_remaining_sec"] isKindOfClass:[NSNull class]])
  2351. features.accountRemoveRemainingSec = [[data valueForKey:@"account_remove_remaining_sec"] integerValue];
  2352. if ([data valueForKey:@"account_remove_time"] && ![[data valueForKey:@"account_remove_time"] isKindOfClass:[NSNull class]])
  2353. features.accountRemoveTime = [[data valueForKey:@"account_remove_time"] integerValue];
  2354. if ([data valueForKey:@"account_remove"] && ![[data valueForKey:@"account_remove"] isKindOfClass:[NSNull class]])
  2355. features.accountRemove = [data valueForKey:@"account_remove"];
  2356. NSDictionary *nextGroupExpirationDic = [data valueForKey:@"next_group_expiration"];
  2357. if (nextGroupExpirationDic) {
  2358. if ([nextGroupExpirationDic valueForKey:@"group"] && ![[nextGroupExpirationDic valueForKey:@"group"] isKindOfClass:[NSNull class]])
  2359. features.nextGroupExpirationGroup = [nextGroupExpirationDic valueForKey:@"group"];
  2360. if ([nextGroupExpirationDic valueForKey:@"group_expired"] && ![[nextGroupExpirationDic valueForKey:@"group_expired"] isKindOfClass:[NSNull class]])
  2361. features.nextGroupExpirationGroupExpired = [[nextGroupExpirationDic valueForKey:@"group_expired"] boolValue];
  2362. if ([nextGroupExpirationDic valueForKey:@"expires_time"] && ![[nextGroupExpirationDic valueForKey:@"expires_time"] isKindOfClass:[NSNull class]])
  2363. features.nextGroupExpirationExpiresTime = [[nextGroupExpirationDic valueForKey:@"expires_time"] integerValue];
  2364. if ([nextGroupExpirationDic valueForKey:@"expires"] && ![[nextGroupExpirationDic valueForKey:@"expires"] isKindOfClass:[NSNull class]])
  2365. features.nextGroupExpirationExpires = [nextGroupExpirationDic valueForKey:@"expires"];
  2366. }
  2367. successRequest(response, features, request.redirectedServer);
  2368. } else {
  2369. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2370. if ([message isKindOfClass:[NSNull class]]) {
  2371. message = NSLocalizedString(@"_server_response_error_", nil);
  2372. }
  2373. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2374. }
  2375. } else {
  2376. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2377. }
  2378. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2379. failureRequest(response, error, request.redirectedServer);
  2380. }];
  2381. }
  2382. #pragma mark - Manage Mobile Editor OCS API
  2383. - (void)eraseURLCache
  2384. {
  2385. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  2386. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  2387. }
  2388. #pragma mark - Utils
  2389. - (void) addUserItemOfType:(NSInteger) shareeType fromArray:(NSArray*) usersArray ToList: (NSMutableArray *) itemList
  2390. {
  2391. for (NSDictionary *userFound in usersArray) {
  2392. OCShareUser *user = [OCShareUser new];
  2393. if ([[userFound valueForKey:@"label"] isKindOfClass:[NSNumber class]]) {
  2394. NSNumber *number = [userFound valueForKey:@"label"];
  2395. user.displayName = [NSString stringWithFormat:@"%ld", number.longValue];
  2396. }else{
  2397. user.displayName = [userFound valueForKey:@"label"];
  2398. }
  2399. NSDictionary *userValues = [userFound valueForKey:@"value"];
  2400. if ([[userValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  2401. NSNumber *number = [userValues valueForKey:@"shareWith"];
  2402. user.name = [NSString stringWithFormat:@"%ld", number.longValue];
  2403. }else{
  2404. user.name = [userValues valueForKey:@"shareWith"];
  2405. }
  2406. user.shareeType = shareeType;
  2407. user.server = [userValues valueForKey:@"server"];
  2408. [itemList addObject:user];
  2409. }
  2410. }
  2411. - (void) addGroupItemFromArray:(NSArray*) groupsArray ToList: (NSMutableArray *) itemList
  2412. {
  2413. for (NSDictionary *groupFound in groupsArray) {
  2414. OCShareUser *group = [OCShareUser new];
  2415. NSDictionary *groupValues = [groupFound valueForKey:@"value"];
  2416. if ([[groupValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  2417. NSNumber *number = [groupValues valueForKey:@"shareWith"];
  2418. group.name = [NSString stringWithFormat:@"%ld", number.longValue];
  2419. }else{
  2420. group.name = [groupValues valueForKey:@"shareWith"];
  2421. }
  2422. group.shareeType = shareTypeGroup;
  2423. [itemList addObject:group];
  2424. }
  2425. }
  2426. @end