OCCommunication.m 147 KB

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