OCCommunication.m 177 KB

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