OCNetworking.m 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  1. //
  2. // OCnetworking.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "OCNetworking.h"
  24. #import "CCUtility.h"
  25. #import "CCGraphics.h"
  26. #import "CCCertificate.h"
  27. #import "NSString+Encode.h"
  28. #import "NCBridgeSwift.h"
  29. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  30. @interface OCnetworking ()
  31. {
  32. NSString *_activeUser;
  33. NSString *_activeUserID;
  34. NSString *_activePassword;
  35. NSString *_activeUrl;
  36. }
  37. @end
  38. @implementation OCnetworking
  39. - (id)initWithDelegate:(id <OCNetworkingDelegate>)delegate metadataNet:(CCMetadataNet *)metadataNet withUser:(NSString *)withUser withUserID:(NSString *)withUserID withPassword:(NSString *)withPassword withUrl:(NSString *)withUrl
  40. {
  41. self = [super init];
  42. if (self) {
  43. _delegate = delegate;
  44. _metadataNet = [CCMetadataNet new];
  45. _metadataNet = [metadataNet copy];
  46. _activeUser = withUser;
  47. _activeUserID = withUserID;
  48. _activePassword = withPassword;
  49. _activeUrl = withUrl;
  50. }
  51. return self;
  52. }
  53. - (void)start
  54. {
  55. if (![NSThread isMainThread]) {
  56. [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
  57. return;
  58. }
  59. [self willChangeValueForKey:@"isExecuting"];
  60. _isExecuting = YES;
  61. [self didChangeValueForKey:@"isExecuting"];
  62. if (self.isCancelled) {
  63. [self finish];
  64. } else {
  65. [self poolNetworking];
  66. }
  67. }
  68. - (void)finish
  69. {
  70. [self willChangeValueForKey:@"isExecuting"];
  71. [self willChangeValueForKey:@"isFinished"];
  72. _isExecuting = NO;
  73. _isFinished = YES;
  74. [self didChangeValueForKey:@"isExecuting"];
  75. [self didChangeValueForKey:@"isFinished"];
  76. }
  77. - (void)cancel
  78. {
  79. if (_isExecuting) {
  80. [self complete];
  81. }
  82. [super cancel];
  83. }
  84. - (void)poolNetworking
  85. {
  86. #ifndef EXTENSION
  87. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  88. #endif
  89. if([self respondsToSelector:NSSelectorFromString(_metadataNet.action)])
  90. [self performSelector:NSSelectorFromString(_metadataNet.action)];
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark ===== Delegate =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)complete
  96. {
  97. [self finish];
  98. #ifndef EXTENSION
  99. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  100. #endif
  101. }
  102. #pragma --------------------------------------------------------------------------------------------
  103. #pragma mark ===== download =====
  104. #pragma --------------------------------------------------------------------------------------------
  105. - (NSURLSessionTask *)downloadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void (^)(int64_t length))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  106. {
  107. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  108. [communication setUserAgent:[CCUtility getUserAgent]];
  109. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
  110. //float percent = roundf (progress.fractionCompleted * 100);
  111. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  112. int64_t totalUnitCount = 0;
  113. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  114. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  115. if(contentLength) {
  116. totalUnitCount = (int64_t) [contentLength longLongValue];
  117. }
  118. success(totalUnitCount);
  119. } failureRequest:^(NSURLResponse *response, NSError *error) {
  120. NSString *message;
  121. NSInteger errorCode;
  122. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  123. errorCode = httpResponse.statusCode;
  124. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  125. errorCode = error.code;
  126. // Error
  127. if (errorCode == 503)
  128. message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  129. else
  130. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  131. failure(message, errorCode);
  132. }];
  133. return sessionTask;
  134. }
  135. #pragma --------------------------------------------------------------------------------------------
  136. #pragma mark ===== upload =====
  137. #pragma --------------------------------------------------------------------------------------------
  138. - (NSURLSessionTask *)uploadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void(^)(NSString *fileID, NSString *etag, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  139. {
  140. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  141. [communication setUserAgent:[CCUtility getUserAgent]];
  142. NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl onCommunication:communication progress:^(NSProgress *progress) {
  143. //float percent = roundf (progress.fractionCompleted * 100);
  144. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  145. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  146. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  147. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  148. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  149. success(fileID, etag, date);
  150. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  151. NSString *message;
  152. NSInteger errorCode;
  153. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  154. errorCode = httpResponse.statusCode;
  155. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  156. errorCode = error.code;
  157. // Error
  158. if (errorCode == 503)
  159. message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  160. else
  161. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  162. failure(message, errorCode);
  163. } failureBeforeRequest:^(NSError *error) {
  164. failure(@"", error.code);
  165. }];
  166. return sessionTask;
  167. }
  168. #pragma --------------------------------------------------------------------------------------------
  169. #pragma mark ===== downloadThumbnail =====
  170. #pragma --------------------------------------------------------------------------------------------
  171. - (void)downloadThumbnail
  172. {
  173. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  174. __block NSString *ext;
  175. NSInteger width = 0, height = 0;
  176. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  177. NSString *dimOfThumbnail = (NSString *)_metadataNet.options;
  178. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  179. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  180. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  181. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  182. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  183. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileID, ext]]) {
  184. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)])
  185. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:nil errorCode:0];
  186. [self complete];
  187. return;
  188. }
  189. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  190. [communication setUserAgent:[CCUtility getUserAgent]];
  191. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:_metadataNet.fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  192. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  193. if ([recordAccount.account isEqualToString:_metadataNet.account] && [thumbnail length] > 0) {
  194. UIImage *thumbnailImage = [UIImage imageWithData:thumbnail];
  195. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  196. [CCGraphics saveIcoWithEtag:_metadataNet.fileID image:thumbnailImage writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileID, ext] copy:NO move:NO fromPath:nil toPath:nil];
  197. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  198. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:nil errorCode:0];
  199. } else {
  200. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  201. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  202. }
  203. [self complete];
  204. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  205. NSString *message = [NSString new];
  206. NSInteger errorCode = [self downloadThumbnailFailureResponse:response error:error message:&message];
  207. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail]) {
  208. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:message errorCode:errorCode];
  209. }
  210. [self complete];
  211. }];
  212. }
  213. - (void)downloadThumbnailWithDimOfThumbnail:(NSString *)dimOfThumbnail fileName:(NSString *)fileName fileNameLocal:(NSString *)fileNameLocal success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  214. {
  215. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  216. __block NSString *ext;
  217. NSInteger width = 0, height = 0;
  218. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  219. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  220. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  221. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  222. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  223. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  224. NSString *fileNamePathLocal = [NSString stringWithFormat:@"%@/%@.%@", directoryUser, fileNameLocal, ext];
  225. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePathLocal]) {
  226. success();
  227. } else {
  228. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  229. [communication setUserAgent:[CCUtility getUserAgent]];
  230. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  231. [CCGraphics saveIcoWithEtag:fileNameLocal image:[UIImage imageWithData:thumbnail] writeToFile:fileNamePathLocal copy:NO move:NO fromPath:nil toPath:nil];
  232. success();
  233. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  234. NSString *message = [NSString new];
  235. NSInteger errorCode = [self downloadThumbnailFailureResponse:response error:error message:&message];
  236. failure(message, errorCode);
  237. }];
  238. }
  239. }
  240. - (NSInteger)downloadThumbnailFailureResponse:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  241. {
  242. NSInteger errorCode = response.statusCode;
  243. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  244. errorCode = error.code;
  245. // Error
  246. if (errorCode == 503)
  247. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  248. else
  249. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  250. return errorCode;
  251. }
  252. #pragma --------------------------------------------------------------------------------------------
  253. #pragma mark ===== Read Folder =====
  254. #pragma --------------------------------------------------------------------------------------------
  255. - (void)readFolder
  256. {
  257. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  258. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  259. [communication setUserAgent:[CCUtility getUserAgent]];
  260. [communication readFolder:_metadataNet.serverUrl depth:_metadataNet.depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  261. // Test active account
  262. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  263. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  264. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)])
  265. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:nil metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  266. } else {
  267. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  268. NSMutableArray *metadatas = [NSMutableArray new];
  269. tableMetadata *metadataFolder = [tableMetadata new];
  270. _metadataNet.directoryID = [self readFolderSuccessServerUrl:_metadataNet.serverUrl items:items account:_metadataNet.account metadatas:&metadatas metadataFolder:&metadataFolder];
  271. dispatch_async(dispatch_get_main_queue(), ^{
  272. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)])
  273. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:metadataFolder metadatas:metadatas message:nil errorCode:0];
  274. });
  275. });
  276. }
  277. [self complete];
  278. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  279. NSString *message = [NSString new];
  280. NSInteger errorCode = [self readFolderFailureServerUrl:_metadataNet.serverUrl response:response error:error message:&message];
  281. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)]) {
  282. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:nil metadatas:nil message:message errorCode:errorCode];
  283. }
  284. [self complete];
  285. }];
  286. }
  287. - (void)readFolderWithServerUrl:(NSString *)serverUrl depth:(NSString *)depth account:(NSString *)account success:(void(^)(NSArray *metadatas, tableMetadata *metadataFolder, NSString *directoryID))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  288. {
  289. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  290. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  291. [communication setUserAgent:[CCUtility getUserAgent]];
  292. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  293. // Test active account
  294. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  295. if (![recordAccount.account isEqualToString:account]) {
  296. failure(NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil), k_CCErrorUserNotAvailble);
  297. } else {
  298. NSMutableArray *metadatas = [NSMutableArray new];
  299. tableMetadata *metadataFolder = [tableMetadata new];
  300. NSString *directoryID = [self readFolderSuccessServerUrl:serverUrl items:items account:account metadatas:&metadatas metadataFolder:&metadataFolder];
  301. success(metadatas, metadataFolder, directoryID);
  302. }
  303. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  304. NSString *message = [NSString new];
  305. NSInteger errorCode = [self readFolderFailureServerUrl:serverUrl response:response error:error message:&message];
  306. failure(message, errorCode);
  307. }];
  308. }
  309. - (NSInteger)readFolderFailureServerUrl:(NSString *)serverUrl response:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  310. {
  311. NSInteger errorCode = response.statusCode;
  312. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  313. errorCode = error.code;
  314. // Error
  315. if (errorCode == 503)
  316. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  317. else
  318. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  319. // Request trusted certificated
  320. if ([error code] == NSURLErrorServerCertificateUntrusted)
  321. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  322. // Activity
  323. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  324. return errorCode;
  325. }
  326. - (NSString *)readFolderSuccessServerUrl:(NSString *)serverUrl items:(NSArray *)items account:(NSString *)account metadatas:(NSMutableArray **)metadatas metadataFolder:(tableMetadata **)metadataFolder
  327. {
  328. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  329. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  330. // Check items > 0
  331. if ([items count] == 0) {
  332. #ifndef EXTENSION
  333. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  334. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  335. #endif
  336. return nil;
  337. }
  338. // directory [0]
  339. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  340. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  341. NSString *directoryID = [[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite fileID:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl].directoryID;
  342. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  343. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  344. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  345. NSString *directoryIDFolder;
  346. NSString *serverUrlFolder;
  347. // Metadata . (self Folder)
  348. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl]]) {
  349. // root folder
  350. serverUrlFolder = @"..";
  351. directoryIDFolder = @"00000000-0000-0000-0000-000000000000";
  352. *metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder directoryID:directoryIDFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  353. } else {
  354. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  355. directoryIDFolder = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrlFolder];
  356. if (!directoryIDFolder) {
  357. return directoryID;
  358. }
  359. *metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder directoryID:directoryIDFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  360. }
  361. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  362. NSString *first = [(OCFileDto*)a fileName];
  363. NSString *second = [(OCFileDto*)b fileName];
  364. return [[first lowercaseString] compare:[second lowercaseString]];
  365. }];
  366. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  367. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  368. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  369. // Skip hidden files
  370. if (fileName.length > 0) {
  371. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  372. continue;
  373. } else
  374. continue;
  375. if (itemDto.isDirectory) {
  376. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:fileName]];
  377. }
  378. // ----- BUG #942 ---------
  379. if ([itemDto.etag length] == 0) {
  380. #ifndef EXTENSION
  381. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  382. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  383. #endif
  384. continue;
  385. }
  386. // ------------------------
  387. [*metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  388. }
  389. return directoryID;
  390. }
  391. #pragma --------------------------------------------------------------------------------------------
  392. #pragma mark ===== Search =====
  393. #pragma --------------------------------------------------------------------------------------------
  394. - (void)search
  395. {
  396. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  397. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  398. [communication setUserAgent:[CCUtility getUserAgent]];
  399. NSString *path = [_activeUrl stringByAppendingString:dav];
  400. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  401. NSString *dateLastModified;
  402. if (_metadataNet.date) {
  403. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  404. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  405. [dateFormatter setLocale:enUSPOSIXLocale];
  406. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  407. dateLastModified = [dateFormatter stringFromDate:_metadataNet.date];
  408. }
  409. [communication search:path folder:folder fileName: [NSString stringWithFormat:@"%%%@%%", _metadataNet.fileName] depth:_metadataNet.depth dateLastModified:dateLastModified contentType:_metadataNet.contentType withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  410. // Test active account
  411. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  412. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  413. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)])
  414. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  415. [self complete];
  416. return;
  417. }
  418. NSMutableArray *metadatas = [NSMutableArray new];
  419. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  420. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  421. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  422. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  423. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  424. for(OCFileDto *itemDto in items) {
  425. NSString *serverUrl, *directoryID;
  426. BOOL isFolderEncrypted;
  427. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  428. // Skip hidden files
  429. if (fileName.length > 0) {
  430. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  431. continue;
  432. } else
  433. continue;
  434. // ----- BUG #942 ---------
  435. if ([itemDto.etag length] == 0) {
  436. #ifndef EXTENSION
  437. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  438. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  439. #endif
  440. continue;
  441. }
  442. // ------------------------
  443. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/files/%@", dav, _activeUserID] withString:@""];
  444. if ([serverUrl hasPrefix:@"/"])
  445. serverUrl = [serverUrl substringFromIndex:1];
  446. if ([serverUrl hasSuffix:@"/"])
  447. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  448. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  449. if (itemDto.isDirectory) {
  450. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName]];
  451. }
  452. directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  453. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:_metadataNet.account];
  454. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  455. }
  456. dispatch_async(dispatch_get_main_queue(), ^{
  457. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)])
  458. [self.delegate searchSuccessFailure:_metadataNet metadatas:metadatas message:nil errorCode:0];
  459. });
  460. });
  461. [self complete];
  462. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  463. NSInteger errorCode = response.statusCode;
  464. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  465. errorCode = error.code;
  466. // Error
  467. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)]) {
  468. if (errorCode == 503)
  469. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  470. else
  471. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  472. }
  473. // Request trusted certificated
  474. if ([error code] == NSURLErrorServerCertificateUntrusted)
  475. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  476. [self complete];
  477. }];
  478. }
  479. #pragma --------------------------------------------------------------------------------------------
  480. #pragma mark ===== Setting Favorite =====
  481. #pragma --------------------------------------------------------------------------------------------
  482. - (void)settingFavorite
  483. {
  484. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  485. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  486. [communication setUserAgent:[CCUtility getUserAgent]];
  487. NSString *path = [_activeUrl stringByAppendingString:dav];
  488. [communication settingFavoriteServer:path andFileOrFolderPath:_metadataNet.fileName favorite:[_metadataNet.options boolValue] withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  489. // Test active account
  490. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  491. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  492. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)])
  493. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  494. [self complete];
  495. return;
  496. }
  497. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)])
  498. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:nil errorCode:0];
  499. [self complete];
  500. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  501. NSInteger errorCode = response.statusCode;
  502. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  503. errorCode = error.code;
  504. // Error
  505. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)]) {
  506. if (errorCode == 503)
  507. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  508. else
  509. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  510. }
  511. // Request trusted certificated
  512. if ([error code] == NSURLErrorServerCertificateUntrusted)
  513. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  514. [self complete];
  515. }];
  516. }
  517. #pragma --------------------------------------------------------------------------------------------
  518. #pragma mark ===== Listing Favorites =====
  519. #pragma --------------------------------------------------------------------------------------------
  520. - (void)listingFavorites
  521. {
  522. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  523. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  524. [communication setUserAgent:[CCUtility getUserAgent]];
  525. NSString *path = [_activeUrl stringByAppendingString:dav];
  526. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  527. [communication listingFavorites:path folder:folder withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  528. // Test active account
  529. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  530. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  531. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  532. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  533. [self complete];
  534. return;
  535. }
  536. NSMutableArray *metadatas = [NSMutableArray new];
  537. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  538. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  539. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  540. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  541. // Order by fileNamePath
  542. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  543. OCFileDto *record1 = obj1, *record2 = obj2;
  544. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  545. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  546. return [path1 compare:path2];
  547. }];
  548. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  549. for(OCFileDto *itemDto in items) {
  550. NSString *serverUrl, *directoryID;
  551. BOOL isFolderEncrypted;
  552. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  553. // Skip hidden files
  554. if (fileName.length > 0) {
  555. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  556. continue;
  557. } else
  558. continue;
  559. // ----- BUG #942 ---------
  560. if ([itemDto.etag length] == 0) {
  561. #ifndef EXTENSION
  562. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  563. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  564. #endif
  565. continue;
  566. }
  567. // ------------------------
  568. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/files/%@", dav, _activeUserID] withString:@""];
  569. if ([serverUrl hasPrefix:@"/"])
  570. serverUrl = [serverUrl substringFromIndex:1];
  571. if ([serverUrl hasSuffix:@"/"])
  572. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  573. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  574. if (itemDto.isDirectory) {
  575. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName]];
  576. }
  577. directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  578. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:_metadataNet.account];
  579. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  580. }
  581. dispatch_async(dispatch_get_main_queue(), ^{
  582. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  583. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:metadatas message:nil errorCode:0];
  584. });
  585. });
  586. [self complete];
  587. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  588. NSInteger errorCode = response.statusCode;
  589. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  590. errorCode = error.code;
  591. // Error
  592. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)]) {
  593. if (errorCode == 503)
  594. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  595. else
  596. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  597. }
  598. // Request trusted certificated
  599. if ([error code] == NSURLErrorServerCertificateUntrusted)
  600. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  601. [self complete];
  602. }];
  603. }
  604. #pragma --------------------------------------------------------------------------------------------
  605. #pragma mark ===== Create Folder =====
  606. #pragma --------------------------------------------------------------------------------------------
  607. - (void)createFolder
  608. {
  609. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  610. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  611. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  612. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  613. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  614. [communication setUserAgent:[CCUtility getUserAgent]];
  615. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  616. // Test active account
  617. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  618. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  619. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  620. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  621. [self complete];
  622. return;
  623. }
  624. NSDictionary *fields = [response allHeaderFields];
  625. _metadataNet.fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  626. _metadataNet.date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  627. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  628. [self.delegate createFolderSuccessFailure:_metadataNet message:nil errorCode:0];
  629. [self complete];
  630. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  631. NSString *message;
  632. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  633. message = nil;
  634. else
  635. message = [CCError manageErrorOC:response.statusCode error:error];
  636. NSInteger errorCode = response.statusCode;
  637. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  638. errorCode = error.code;
  639. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  640. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  641. // Request trusted certificated
  642. if ([error code] == NSURLErrorServerCertificateUntrusted)
  643. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  644. [self complete];
  645. } errorBeforeRequest:^(NSError *error) {
  646. NSString *message;
  647. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  648. message = nil;
  649. else {
  650. if (error.code == OCErrorForbidenCharacters)
  651. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  652. else
  653. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  654. }
  655. // Error
  656. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)]) {
  657. if (error.code == 503)
  658. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  659. else
  660. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:error.code];
  661. }
  662. [self complete];
  663. }];
  664. }
  665. - (void)createFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl account:(NSString *)account success:(void(^)(NSString *fileID, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  666. {
  667. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  668. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  669. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  670. [communication setUserAgent:[CCUtility getUserAgent]];
  671. [communication createFolder:serverFileUrl onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  672. if (![[[NCManageDatabase sharedInstance] getAccountActive].account isEqualToString:account]) {
  673. failure(NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil), k_CCErrorUserNotAvailble);
  674. } else {
  675. NSDictionary *fields = [response allHeaderFields];
  676. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  677. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  678. success(fileID, date);
  679. }
  680. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  681. failure(@"", error.code);
  682. } errorBeforeRequest:^(NSError *error) {
  683. NSString *message;
  684. if (error.code == OCErrorForbidenCharacters)
  685. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  686. else
  687. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  688. failure(message, error.code);
  689. }];
  690. }
  691. #pragma --------------------------------------------------------------------------------------------
  692. #pragma mark ===== Delete =====
  693. #pragma --------------------------------------------------------------------------------------------
  694. - (void)deleteFileOrFolder
  695. {
  696. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  697. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  698. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  699. [communication setUserAgent:[CCUtility getUserAgent]];
  700. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  701. // Test active account
  702. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  703. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  704. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  705. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  706. [self complete];
  707. return;
  708. }
  709. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  710. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:@"" errorCode:0];
  711. [self complete];
  712. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  713. NSString *message = [NSString new];
  714. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:_metadataNet.serverUrl response:response error:error message:&message];
  715. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)]) {
  716. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  717. }
  718. [self complete];
  719. }];
  720. }
  721. - (void)deleteFileOrFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  722. {
  723. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  724. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  725. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  726. [communication setUserAgent:[CCUtility getUserAgent]];
  727. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  728. success();
  729. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  730. NSString *message = [NSString new];
  731. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:serverUrl response:response error:error message:&message];
  732. failure(message, errorCode);
  733. }];
  734. }
  735. - (NSInteger)deleteFileOrFolderFailureServerUrl:(NSString *)serverUrl response:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  736. {
  737. NSInteger errorCode = response.statusCode;
  738. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  739. errorCode = error.code;
  740. // Error
  741. if (errorCode == 503)
  742. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  743. else
  744. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  745. // Request trusted certificated
  746. if ([error code] == NSURLErrorServerCertificateUntrusted)
  747. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  748. // Activity
  749. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionDeleteFileFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  750. return errorCode;
  751. }
  752. #pragma --------------------------------------------------------------------------------------------
  753. #pragma mark ===== Move =====
  754. #pragma --------------------------------------------------------------------------------------------
  755. - (void)moveFileOrFolder
  756. {
  757. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  758. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  759. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  760. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  761. [communication setUserAgent:[CCUtility getUserAgent]];
  762. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  763. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:)])
  764. [self.delegate renameSuccess:_metadataNet];
  765. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:)])
  766. [self.delegate moveSuccess:_metadataNet];
  767. [self complete];
  768. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  769. NSInteger errorCode = response.statusCode;
  770. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  771. errorCode = error.code;
  772. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)])
  773. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  774. // Request trusted certificated
  775. if ([error code] == NSURLErrorServerCertificateUntrusted)
  776. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  777. [self complete];
  778. } errorBeforeRequest:^(NSError *error) {
  779. NSString *message;
  780. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  781. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  782. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  783. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  784. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  785. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  786. } else {
  787. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  788. }
  789. // Error
  790. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)]) {
  791. if (error.code == 503)
  792. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  793. else
  794. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  795. }
  796. [self complete];
  797. }];
  798. }
  799. - (void)moveFileOrFolder:(NSString *)fileName fileNameTo:(NSString *)fileNameTo success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  800. {
  801. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  802. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  803. [communication setUserAgent:[CCUtility getUserAgent]];
  804. [communication moveFileOrFolder:fileName toDestiny:fileNameTo onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  805. success();
  806. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  807. NSInteger errorCode = response.statusCode;
  808. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  809. errorCode = error.code;
  810. NSString *message = [CCError manageErrorOC:response.statusCode error:error];
  811. failure(message, error.code);
  812. } errorBeforeRequest:^(NSError *error) {
  813. NSString *message;
  814. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  815. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  816. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  817. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  818. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  819. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  820. } else {
  821. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  822. }
  823. failure(message, error.code);
  824. }];
  825. }
  826. #pragma --------------------------------------------------------------------------------------------
  827. #pragma mark ===== ReadFile =====
  828. #pragma --------------------------------------------------------------------------------------------
  829. - (void)readFile
  830. {
  831. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  832. NSString *fileName;
  833. if (_metadataNet.fileName) {
  834. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  835. } else {
  836. _metadataNet.fileName = @".";
  837. fileName = _metadataNet.serverUrl;
  838. }
  839. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  840. [communication setUserAgent:[CCUtility getUserAgent]];
  841. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  842. // Test active account
  843. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  844. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  845. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  846. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  847. [self complete];
  848. return;
  849. }
  850. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:_metadataNet.serverUrl account:_metadataNet.account];
  851. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  852. tableMetadata *metadata = [tableMetadata new];
  853. OCFileDto *itemDto = [items objectAtIndex:0];
  854. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_metadataNet.serverUrl];
  855. if (directoryID) {
  856. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  857. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  858. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  859. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileName:_metadataNet.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  860. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  861. [self.delegate readFileSuccessFailure:_metadataNet metadata:metadata message:nil errorCode:0];
  862. } else {
  863. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  864. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Directory not found" errorCode:k_CCErrorInternalError];
  865. }
  866. }
  867. // BUG 1038
  868. if ([items count] == 0) {
  869. #ifndef EXTENSION
  870. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  871. [appDelegate messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  872. #endif
  873. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  874. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Read File WebDAV : [items NULL] please fix" errorCode:k_CCErrorInternalError];
  875. }
  876. [self complete];
  877. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  878. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  879. _metadataNet.errorRetry++;
  880. NSInteger errorCode = response.statusCode;
  881. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  882. errorCode = error.code;
  883. // Error
  884. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account]) {
  885. if (errorCode == 503)
  886. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  887. else
  888. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  889. }
  890. // Request trusted certificated
  891. if ([error code] == NSURLErrorServerCertificateUntrusted)
  892. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  893. [self complete];
  894. }];
  895. }
  896. #pragma --------------------------------------------------------------------------------------------
  897. #pragma mark ===== Shared =====
  898. #pragma --------------------------------------------------------------------------------------------
  899. - (void)readShareServer
  900. {
  901. #ifndef EXTENSION
  902. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  903. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  904. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  905. [communication setUserAgent:[CCUtility getUserAgent]];
  906. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  907. // Test active account
  908. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  909. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  910. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  911. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  912. [self complete];
  913. return;
  914. }
  915. BOOL openWindow = NO;
  916. [appDelegate.sharesID removeAllObjects];
  917. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  918. for (OCSharedDto *item in items)
  919. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  920. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  921. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  922. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  923. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  924. }
  925. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  926. [self.delegate readSharedSuccess:_metadataNet items:appDelegate.sharesID openWindow:openWindow];
  927. [self complete];
  928. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  929. NSInteger errorCode = response.statusCode;
  930. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  931. errorCode = error.code;
  932. // Error
  933. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  934. if (errorCode == 503)
  935. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  936. else
  937. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  938. }
  939. // Request trusted certificated
  940. if ([error code] == NSURLErrorServerCertificateUntrusted)
  941. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  942. [self complete];
  943. }];
  944. #endif
  945. }
  946. - (void)share
  947. {
  948. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  949. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  950. [communication setUserAgent:[CCUtility getUserAgent]];
  951. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  952. [self readShareServer];
  953. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  954. NSInteger errorCode = response.statusCode;
  955. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  956. errorCode = error.code;
  957. // Error
  958. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  959. if (errorCode == 503)
  960. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  961. else
  962. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  963. }
  964. // Request trusted certificated
  965. if ([error code] == NSURLErrorServerCertificateUntrusted)
  966. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  967. [self complete];
  968. }];
  969. }
  970. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  971. - (void)shareWith
  972. {
  973. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  974. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  975. [communication setUserAgent:[CCUtility getUserAgent]];
  976. [communication shareWith:_metadataNet.share shareeType:_metadataNet.shareeType inServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  977. [self readShareServer];
  978. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  979. NSInteger errorCode = response.statusCode;
  980. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  981. errorCode = error.code;
  982. // Error
  983. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  984. if (errorCode == 503)
  985. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  986. else
  987. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  988. }
  989. // Request trusted certificated
  990. if ([error code] == NSURLErrorServerCertificateUntrusted)
  991. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  992. [self complete];
  993. }];
  994. }
  995. - (void)updateShare
  996. {
  997. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  998. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  999. [communication setUserAgent:[CCUtility getUserAgent]];
  1000. [communication updateShare:[_metadataNet.share intValue] ofServerPath:[_activeUrl stringByAppendingString:@"/"] withPasswordProtect:[_metadataNet.password encodeString:NSUTF8StringEncoding] andExpirationTime:_metadataNet.expirationTime andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1001. [self readShareServer];
  1002. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1003. #ifndef EXTENSION
  1004. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1005. [appDelegate messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  1006. #endif
  1007. NSInteger errorCode = response.statusCode;
  1008. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1009. errorCode = error.code;
  1010. // Error
  1011. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  1012. if (errorCode == 503)
  1013. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1014. else
  1015. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1016. }
  1017. // Request trusted certificated
  1018. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1019. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1020. [self complete];
  1021. }];
  1022. }
  1023. - (void)unShare
  1024. {
  1025. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1026. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1027. [communication setUserAgent:[CCUtility getUserAgent]];
  1028. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1029. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  1030. [self.delegate unShareSuccess:_metadataNet];
  1031. [self complete];
  1032. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1033. #ifndef EXTENSION
  1034. [(AppDelegate *)[[UIApplication sharedApplication] delegate] messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  1035. #endif
  1036. NSInteger errorCode = response.statusCode;
  1037. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1038. errorCode = error.code;
  1039. // Error
  1040. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  1041. if (errorCode == 503)
  1042. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1043. else
  1044. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1045. }
  1046. // Request trusted certificated
  1047. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1048. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1049. [self complete];
  1050. }];
  1051. }
  1052. - (void)getUserAndGroup
  1053. {
  1054. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1055. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1056. [communication setUserAgent:[CCUtility getUserAgent]];
  1057. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1058. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  1059. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  1060. [self complete];
  1061. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1062. NSInteger errorCode = response.statusCode;
  1063. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1064. errorCode = error.code;
  1065. // Error
  1066. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  1067. if (errorCode == 503)
  1068. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1069. else
  1070. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1071. }
  1072. // Request trusted certificated
  1073. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1074. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1075. [self complete];
  1076. }];
  1077. }
  1078. - (void)getSharePermissionsFile
  1079. {
  1080. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1081. NSString *fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  1082. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1083. [communication setUserAgent:[CCUtility getUserAgent]];
  1084. [communication getSharePermissionsFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer) {
  1085. // Test active account
  1086. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1087. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1088. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)])
  1089. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1090. [self complete];
  1091. return;
  1092. }
  1093. if([self.delegate respondsToSelector:@selector(getSharePermissionsFileSuccess:permissions:)])
  1094. [self.delegate getSharePermissionsFileSuccess:_metadataNet permissions:permissions];
  1095. [self complete];
  1096. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1097. NSInteger errorCode = response.statusCode;
  1098. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1099. errorCode = error.code;
  1100. // Error
  1101. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)]) {
  1102. if (errorCode == 503)
  1103. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1104. else
  1105. [self.delegate getSharePermissionsFileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1106. }
  1107. // Request trusted certificated
  1108. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1109. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1110. [self complete];
  1111. }];
  1112. }
  1113. #pragma --------------------------------------------------------------------------------------------
  1114. #pragma mark ===== Activity =====
  1115. #pragma --------------------------------------------------------------------------------------------
  1116. - (void)getActivityServer
  1117. {
  1118. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1119. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1120. [communication setUserAgent:[CCUtility getUserAgent]];
  1121. [communication getActivityServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  1122. // Test active account
  1123. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1124. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1125. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1126. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1127. [self complete];
  1128. return;
  1129. }
  1130. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1131. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:listOfActivity message:nil errorCode:0];
  1132. [self complete];
  1133. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1134. NSInteger errorCode = response.statusCode;
  1135. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1136. errorCode = error.code;
  1137. // Error
  1138. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)]) {
  1139. if (errorCode == 503)
  1140. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1141. else
  1142. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1143. }
  1144. // Request trusted certificated
  1145. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1146. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1147. [self complete];
  1148. }];
  1149. }
  1150. #pragma --------------------------------------------------------------------------------------------
  1151. #pragma mark ===== External Sites =====
  1152. #pragma --------------------------------------------------------------------------------------------
  1153. - (void)getExternalSitesServer
  1154. {
  1155. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1156. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1157. [communication setUserAgent:[CCUtility getUserAgent]];
  1158. [communication getExternalSitesServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1159. // Test active account
  1160. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1161. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1162. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1163. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1164. [self complete];
  1165. return;
  1166. }
  1167. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1168. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:listOfExternalSites message:nil errorCode:0];
  1169. [self complete];
  1170. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1171. NSInteger errorCode = response.statusCode;
  1172. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1173. errorCode = error.code;
  1174. // Error
  1175. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)]) {
  1176. if (errorCode == 503)
  1177. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1178. else
  1179. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1180. }
  1181. // Request trusted certificated
  1182. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1183. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1184. [self complete];
  1185. }];
  1186. }
  1187. #pragma --------------------------------------------------------------------------------------------
  1188. #pragma mark ===== Middleware Ping =====
  1189. #pragma --------------------------------------------------------------------------------------------
  1190. /*
  1191. - (void)middlewarePing
  1192. {
  1193. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1194. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1195. [communication setUserAgent:[CCUtility getUserAgent]];
  1196. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1197. [self complete];
  1198. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1199. NSInteger errorCode = response.statusCode;
  1200. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1201. errorCode = error.code;
  1202. // Error
  1203. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1204. if (errorCode == 503)
  1205. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1206. else
  1207. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1208. }
  1209. // Request trusted certificated
  1210. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1211. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1212. [self complete];
  1213. }];
  1214. }
  1215. */
  1216. #pragma --------------------------------------------------------------------------------------------
  1217. #pragma mark ===== Notification =====
  1218. #pragma --------------------------------------------------------------------------------------------
  1219. - (void)getNotificationServer
  1220. {
  1221. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1222. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1223. [communication setUserAgent:[CCUtility getUserAgent]];
  1224. [communication getNotificationServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1225. // Test active account
  1226. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1227. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1228. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1229. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1230. [self complete];
  1231. return;
  1232. }
  1233. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1234. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:listOfNotifications message:nil errorCode:0];
  1235. [self complete];
  1236. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1237. NSInteger errorCode = response.statusCode;
  1238. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1239. errorCode = error.code;
  1240. // Error
  1241. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)]) {
  1242. if (errorCode == 503)
  1243. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1244. else
  1245. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1246. }
  1247. // Request trusted certificated
  1248. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1249. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1250. // Activity
  1251. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1252. [self complete];
  1253. }];
  1254. }
  1255. - (void)setNotificationServer
  1256. {
  1257. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1258. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1259. [communication setUserAgent:[CCUtility getUserAgent]];
  1260. NSString *type = _metadataNet.options;
  1261. [communication setNotificationServer:_metadataNet.serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1262. // Test active account
  1263. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1264. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1265. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1266. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1267. [self complete];
  1268. return;
  1269. }
  1270. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1271. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:nil errorCode:0];
  1272. [self complete];
  1273. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1274. NSInteger errorCode = response.statusCode;
  1275. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1276. errorCode = error.code;
  1277. // Error
  1278. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)]) {
  1279. if (errorCode == 503)
  1280. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1281. else
  1282. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1283. }
  1284. // Request trusted certificated
  1285. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1286. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1287. [self complete];
  1288. }];
  1289. }
  1290. - (void)subscribingNextcloudServer
  1291. {
  1292. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1293. communication.kindOfCredential = credentialNotSet;
  1294. [communication setUserAgent:[CCUtility getUserAgent]];
  1295. NSDictionary *parameter = _metadataNet.options;
  1296. NSString *pushToken = [parameter objectForKey:@"pushToken"];
  1297. NSString *pushTokenHash = [parameter objectForKey:@"pushTokenHash"];
  1298. NSString *devicePublicKey = [parameter objectForKey:@"devicePublicKey"];
  1299. // encode URL
  1300. devicePublicKey = [devicePublicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1301. [communication subscribingNextcloudServerPush:_activeUrl pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: [NCBrandOptions sharedInstance].pushNotificationServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1302. // encode URL
  1303. deviceIdentifier = [deviceIdentifier stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1304. signature = [signature stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1305. publicKey = [publicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1306. [communication subscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature userPublicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1307. // Activity
  1308. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1309. [self complete];
  1310. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1311. NSInteger errorCode = response.statusCode;
  1312. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1313. errorCode = error.code;
  1314. // Error
  1315. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1316. if (errorCode == 503)
  1317. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1318. else
  1319. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1320. }
  1321. // Request trusted certificated
  1322. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1323. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1324. // Activity
  1325. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1326. [self complete];
  1327. }];
  1328. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1329. NSInteger errorCode = response.statusCode;
  1330. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1331. errorCode = error.code;
  1332. // Error
  1333. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1334. if (errorCode == 503)
  1335. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1336. else
  1337. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1338. }
  1339. // Request trusted certificated
  1340. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1341. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1342. // Activity
  1343. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1344. [self complete];
  1345. }];
  1346. }
  1347. #pragma --------------------------------------------------------------------------------------------
  1348. #pragma mark ===== User Profile =====
  1349. #pragma --------------------------------------------------------------------------------------------
  1350. - (void)getUserProfile
  1351. {
  1352. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1353. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1354. [communication setUserAgent:[CCUtility getUserAgent]];
  1355. [communication getUserProfileServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1356. // Test active account
  1357. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1358. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1359. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1360. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1361. [self complete];
  1362. return;
  1363. }
  1364. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1365. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:userProfile message:nil errorCode:0];
  1366. [self complete];
  1367. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1368. NSInteger errorCode = response.statusCode;
  1369. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1370. errorCode = error.code;
  1371. // Error
  1372. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)]) {
  1373. if (errorCode == 503)
  1374. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1375. else
  1376. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1377. }
  1378. // Request trusted certificated
  1379. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1380. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1381. [self complete];
  1382. }];
  1383. }
  1384. #pragma --------------------------------------------------------------------------------------------
  1385. #pragma mark ===== Capabilities =====
  1386. #pragma --------------------------------------------------------------------------------------------
  1387. - (void)getCapabilitiesOfServer
  1388. {
  1389. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1390. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1391. [communication setUserAgent:[CCUtility getUserAgent]];
  1392. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1393. // Test active account
  1394. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1395. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1396. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1397. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1398. [self complete];
  1399. return;
  1400. }
  1401. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1402. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:capabilities message:nil errorCode:0];
  1403. [self complete];
  1404. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1405. NSInteger errorCode = response.statusCode;
  1406. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1407. errorCode = error.code;
  1408. // Error
  1409. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)]) {
  1410. if (errorCode == 503)
  1411. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1412. else
  1413. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1414. }
  1415. // Request trusted certificated
  1416. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1417. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1418. // Activity
  1419. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1420. [self complete];
  1421. }];
  1422. }
  1423. #pragma --------------------------------------------------------------------------------------------
  1424. #pragma mark ===== End-to-End Encryption =====
  1425. #pragma --------------------------------------------------------------------------------------------
  1426. - (void)getEndToEndPublicKeys
  1427. {
  1428. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1429. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1430. [communication setUserAgent:[CCUtility getUserAgent]];
  1431. [communication getEndToEndPublicKeys:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1432. _metadataNet.key = publicKey;
  1433. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysSuccess:)])
  1434. [self.delegate getEndToEndPublicKeysSuccess:_metadataNet];
  1435. [self complete];
  1436. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1437. NSInteger errorCode = response.statusCode;
  1438. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1439. errorCode = error.code;
  1440. // Error
  1441. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysFailure:message:errorCode:)])
  1442. [self.delegate getEndToEndPublicKeysFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1443. // Request trusted certificated
  1444. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1445. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1446. [self complete];
  1447. }];
  1448. }
  1449. - (void)getEndToEndPrivateKeyCipher
  1450. {
  1451. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1452. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1453. [communication setUserAgent:[CCUtility getUserAgent]];
  1454. [communication getEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  1455. _metadataNet.key = privateKeyChiper;
  1456. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherSuccess:)])
  1457. [self.delegate getEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1458. [self complete];
  1459. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1460. NSInteger errorCode = response.statusCode;
  1461. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1462. errorCode = error.code;
  1463. // Error
  1464. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1465. [self.delegate getEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1466. // Request trusted certificated
  1467. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1468. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1469. [self complete];
  1470. }];
  1471. }
  1472. - (void)signEndToEndPublicKey
  1473. {
  1474. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1475. // URL Encode
  1476. NSString *publicKey = [CCUtility URLEncodeStringFromString:_metadataNet.key];
  1477. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1478. [communication setUserAgent:[CCUtility getUserAgent]];
  1479. [communication signEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1480. _metadataNet.key = publicKey;
  1481. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeySuccess:)])
  1482. [self.delegate signEndToEndPublicKeySuccess:_metadataNet];
  1483. [self complete];
  1484. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1485. NSInteger errorCode = response.statusCode;
  1486. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1487. errorCode = error.code;
  1488. // Error
  1489. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeyFailure:message:errorCode:)])
  1490. [self.delegate signEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1491. // Request trusted certificated
  1492. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1493. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1494. [self complete];
  1495. }];
  1496. }
  1497. - (void)storeEndToEndPrivateKeyCipher
  1498. {
  1499. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1500. // URL Encode
  1501. NSString *privateKeyChiper = [CCUtility URLEncodeStringFromString:_metadataNet.keyCipher];
  1502. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1503. [communication setUserAgent:[CCUtility getUserAgent]];
  1504. [communication storeEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] privateKeyChiper:privateKeyChiper onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  1505. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherSuccess:)])
  1506. [self.delegate storeEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1507. [self complete];
  1508. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1509. NSInteger errorCode = response.statusCode;
  1510. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1511. errorCode = error.code;
  1512. // Error
  1513. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1514. [self.delegate storeEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1515. // Request trusted certificated
  1516. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1517. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1518. [self complete];
  1519. }];
  1520. }
  1521. - (void)deleteEndToEndPublicKey
  1522. {
  1523. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1524. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1525. [communication setUserAgent:[CCUtility getUserAgent]];
  1526. [communication deleteEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1527. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeySuccess:)])
  1528. [self.delegate deleteEndToEndPublicKeySuccess:_metadataNet];
  1529. [self complete];
  1530. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1531. NSInteger errorCode = response.statusCode;
  1532. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1533. errorCode = error.code;
  1534. // Error
  1535. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeyFailure:message:errorCode:)])
  1536. [self.delegate deleteEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1537. // Request trusted certificated
  1538. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1539. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1540. [self complete];
  1541. }];
  1542. }
  1543. - (void)deleteEndToEndPrivateKey
  1544. {
  1545. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1546. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1547. [communication setUserAgent:[CCUtility getUserAgent]];
  1548. [communication deleteEndToEndPrivateKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1549. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeySuccess:)])
  1550. [self.delegate deleteEndToEndPrivateKeySuccess:_metadataNet];
  1551. [self complete];
  1552. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1553. NSInteger errorCode = response.statusCode;
  1554. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1555. errorCode = error.code;
  1556. // Error
  1557. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeyFailure:message:errorCode:)])
  1558. [self.delegate deleteEndToEndPrivateKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1559. // Request trusted certificated
  1560. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1561. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1562. [self complete];
  1563. }];
  1564. }
  1565. - (void)getEndToEndServerPublicKey
  1566. {
  1567. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1568. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1569. [communication setUserAgent:[CCUtility getUserAgent]];
  1570. [communication getEndToEndServerPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1571. _metadataNet.key = publicKey;
  1572. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeySuccess:)])
  1573. [self.delegate getEndToEndServerPublicKeySuccess:_metadataNet];
  1574. [self complete];
  1575. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1576. NSInteger errorCode = response.statusCode;
  1577. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1578. errorCode = error.code;
  1579. // Error
  1580. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeyFailure:message:errorCode:)])
  1581. [self.delegate getEndToEndServerPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1582. // Request trusted certificated
  1583. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1584. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1585. [self complete];
  1586. }];
  1587. }
  1588. @end
  1589. #pragma --------------------------------------------------------------------------------------------
  1590. #pragma mark ===== OCURLSessionManager =====
  1591. #pragma --------------------------------------------------------------------------------------------
  1592. @implementation OCURLSessionManager
  1593. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1594. {
  1595. // The pinnning check
  1596. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1597. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1598. } else {
  1599. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1600. }
  1601. }
  1602. @end