OCNetworking.m 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. //
  2. // OCnetworking.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2014 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 "AppDelegate.h"
  25. #import "CCGraphics.h"
  26. #import "CCCertificate.h"
  27. #import "NSString+Encode.h"
  28. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  29. @interface OCnetworking ()
  30. {
  31. NSString *_activeUser;
  32. NSString *_activePassword;
  33. NSString *_activeUrl;
  34. NSURLSessionDownloadTask *_downloadTask;
  35. NSURLSessionUploadTask *_uploadTask;
  36. BOOL _isCryptoCloudMode;
  37. BOOL _hasServerForbiddenCharactersSupport;
  38. }
  39. @end
  40. @implementation OCnetworking
  41. - (id)initWithDelegate:(id <OCNetworkingDelegate>)delegate metadataNet:(CCMetadataNet *)metadataNet withUser:(NSString *)withUser withPassword:(NSString *)withPassword withUrl:(NSString *)withUrl isCryptoCloudMode:(BOOL)isCryptoCloudMode
  42. {
  43. self = [super init];
  44. if (self) {
  45. _delegate = delegate;
  46. _metadataNet = [CCMetadataNet new];
  47. _metadataNet = [metadataNet copy];
  48. _activeUser = withUser;
  49. _activePassword = withPassword;
  50. _activeUrl = withUrl;
  51. _isCryptoCloudMode = isCryptoCloudMode;
  52. }
  53. return self;
  54. }
  55. - (void)start
  56. {
  57. if (![NSThread isMainThread]) {
  58. [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
  59. return;
  60. }
  61. [self willChangeValueForKey:@"isExecuting"];
  62. _isExecuting = YES;
  63. [self didChangeValueForKey:@"isExecuting"];
  64. if (self.isCancelled) {
  65. [self finish];
  66. } else {
  67. [self poolNetworking];
  68. }
  69. }
  70. - (void)finish
  71. {
  72. [self willChangeValueForKey:@"isExecuting"];
  73. [self willChangeValueForKey:@"isFinished"];
  74. _isExecuting = NO;
  75. _isFinished = YES;
  76. [self didChangeValueForKey:@"isExecuting"];
  77. [self didChangeValueForKey:@"isFinished"];
  78. }
  79. - (void)cancel
  80. {
  81. if (_isExecuting) {
  82. if (_downloadTask)
  83. [_downloadTask cancel];
  84. if (_uploadTask)
  85. [_uploadTask cancel];
  86. [self complete];
  87. }
  88. [super cancel];
  89. }
  90. - (void)poolNetworking
  91. {
  92. #ifndef EXTENSION
  93. _hasServerForbiddenCharactersSupport = app.hasServerForbiddenCharactersSupport;
  94. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  95. #else
  96. _hasServerForbiddenCharactersSupport = YES;
  97. #endif
  98. if([self respondsToSelector:NSSelectorFromString(_metadataNet.action)])
  99. [self performSelector:NSSelectorFromString(_metadataNet.action)];
  100. }
  101. #pragma --------------------------------------------------------------------------------------------
  102. #pragma mark ===== Delegate =====
  103. #pragma --------------------------------------------------------------------------------------------
  104. - (void)complete
  105. {
  106. [self finish];
  107. #ifndef EXTENSION
  108. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  109. #endif
  110. }
  111. #pragma --------------------------------------------------------------------------------------------
  112. #pragma mark ===== downloadFile =====
  113. #pragma --------------------------------------------------------------------------------------------
  114. - (void)downloadFile
  115. {
  116. [[CCNetworking sharedNetworking] downloadFile:_metadataNet.metadata serverUrl:_metadataNet.serverUrl downloadData:_metadataNet.downloadData downloadPlist:_metadataNet.downloadPlist selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost session:_metadataNet.session taskStatus:_metadataNet.taskStatus delegate:self];
  117. }
  118. - (void)downloadTaskSave:(NSURLSessionDownloadTask *)downloadTask
  119. {
  120. _downloadTask= downloadTask;
  121. if ([self.delegate respondsToSelector:@selector(downloadTaskSave:)])
  122. [self.delegate downloadTaskSave:downloadTask];
  123. }
  124. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  125. {
  126. [self complete];
  127. if ([self.delegate respondsToSelector:@selector(downloadFileSuccess:serverUrl:selector:selectorPost:)])
  128. [self.delegate downloadFileSuccess:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  129. }
  130. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  131. {
  132. [self complete];
  133. if ([self.delegate respondsToSelector:@selector(downloadFileFailure:serverUrl:selector:message:errorCode:)])
  134. [self.delegate downloadFileFailure:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== uploadFile =====
  138. #pragma --------------------------------------------------------------------------------------------
  139. - (void)uploadFile
  140. {
  141. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated onlyPlist:NO session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  142. }
  143. - (void)uploadOnlyPlist
  144. {
  145. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:YES onlyPlist:YES session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  146. }
  147. - (void)uploadAsset
  148. {
  149. [[CCNetworking sharedNetworking] uploadFileFromAssetLocalIdentifier:_metadataNet.assetLocalIdentifier fileName:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  150. }
  151. - (void)uploadTemplate
  152. {
  153. [[CCNetworking sharedNetworking] uploadTemplate:_metadataNet.fileNamePrint fileNameCrypto:_metadataNet.fileName serverUrl:_metadataNet.serverUrl session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  154. }
  155. - (void)uploadTaskSave:(NSURLSessionUploadTask *)uploadTask
  156. {
  157. _uploadTask = uploadTask;
  158. if ([self.delegate respondsToSelector:@selector(uploadTaskSave:)])
  159. [self.delegate uploadTaskSave:uploadTask];
  160. }
  161. - (void)uploadFileSuccess:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  162. {
  163. [self complete];
  164. if ([self.delegate respondsToSelector:@selector(uploadFileSuccess:fileID:serverUrl:selector:selectorPost:)])
  165. [self.delegate uploadFileSuccess:_metadataNet fileID:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  166. }
  167. - (void)uploadFileFailure:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  168. {
  169. [self complete];
  170. if ([self.delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  171. [self.delegate uploadFileFailure:_metadataNet fileID:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  172. }
  173. #pragma --------------------------------------------------------------------------------------------
  174. #pragma mark ===== downloadThumbnail =====
  175. #pragma --------------------------------------------------------------------------------------------
  176. - (void)downloadThumbnail
  177. {
  178. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  179. __block NSString *ext;
  180. NSInteger width = 0, height = 0;
  181. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  182. NSString *dimOfThumbnail = (NSString *)_metadataNet.options;
  183. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  184. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  185. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  186. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  187. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  188. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext]]) {
  189. [self.delegate downloadThumbnailSuccess:_metadataNet];
  190. [self complete];
  191. return;
  192. }
  193. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  194. [communication setUserAgent:[CCUtility getUserAgent]];
  195. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:_metadataNet.fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  196. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  197. if ([recordAccount.account isEqualToString:_metadataNet.account] && [thumbnail length] > 0) {
  198. UIImage *thumbnailImage = [UIImage imageWithData:thumbnail];
  199. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  200. [CCGraphics saveIcoWithFileID:_metadataNet.fileNameLocal image:thumbnailImage writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext] copy:NO move:NO fromPath:nil toPath:nil];
  201. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccess:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  202. [self.delegate downloadThumbnailSuccess:_metadataNet];
  203. } else {
  204. if ([self.delegate respondsToSelector:@selector(downloadThumbnailFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  205. [self.delegate downloadThumbnailFailure:_metadataNet message:@"No data" errorCode:0];
  206. }
  207. [self complete];
  208. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  209. NSInteger errorCode = response.statusCode;
  210. if (errorCode == 0)
  211. errorCode = error.code;
  212. // Error
  213. if ([self.delegate respondsToSelector:@selector(downloadThumbnailFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail]) {
  214. if (errorCode == 503)
  215. [self.delegate downloadThumbnailFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  216. else
  217. [self.delegate downloadThumbnailFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  218. }
  219. [self complete];
  220. }];
  221. }
  222. #pragma --------------------------------------------------------------------------------------------
  223. #pragma mark ===== Read Folder =====
  224. #pragma --------------------------------------------------------------------------------------------
  225. - (void)readFolder
  226. {
  227. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  228. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  229. [communication setUserAgent:[CCUtility getUserAgent]];
  230. [communication readFolder:_metadataNet.serverUrl withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  231. NSMutableArray *metadatas = [NSMutableArray new];
  232. // Check items > 0
  233. if ([items count] == 0) {
  234. #ifndef EXTENSION
  235. [app messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  236. #endif
  237. dispatch_async(dispatch_get_main_queue(), ^{
  238. if ([self.delegate respondsToSelector:@selector(readFolderSuccess:permissions:etag:metadatas:)])
  239. [self.delegate readFolderSuccess:_metadataNet permissions:@"" etag:@"" metadatas:metadatas];
  240. });
  241. [self complete];
  242. return;
  243. }
  244. // directory [0]
  245. OCFileDto *itemDtoDirectory = [items objectAtIndex:0];
  246. NSString *permissions = itemDtoDirectory.permissions;
  247. NSString *etagDirectory = itemDtoDirectory.etag;
  248. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  249. NSString *directoryID = [CCCoreData addDirectory:_metadataNet.serverUrl permissions:permissions activeAccount:_metadataNet.account];
  250. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  251. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  252. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  253. // Update metadataNet.directoryID
  254. _metadataNet.directoryID = directoryID;
  255. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  256. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  257. NSString *first = [(OCFileDto*)a fileName];
  258. NSString *second = [(OCFileDto*)b fileName];
  259. return [[first lowercaseString] compare:[second lowercaseString]];
  260. }];
  261. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  262. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  263. itemDto.fileName = [itemDto.fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  264. // Not in Crypto Cloud Mode skip File Crypto
  265. if (_isCryptoCloudMode == NO) {
  266. NSString *fileName = itemDto.fileName;
  267. if (itemDto.isDirectory) {
  268. fileName = [fileName substringToIndex:[fileName length] - 1];
  269. NSString *serverUrl = [CCUtility stringAppendServerUrl:_metadataNet.serverUrl addFileName:fileName];
  270. dispatch_async(dispatch_get_main_queue(), ^{
  271. [CCCoreData addDirectory:serverUrl permissions:nil activeAccount:_metadataNet.account];
  272. });
  273. }
  274. if ([CCUtility isFileCryptated:fileName])
  275. continue;
  276. }
  277. // ----- BUG #942 ---------
  278. if ([itemDto.etag length] == 0) {
  279. #ifndef EXTENSION
  280. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  281. #endif
  282. continue;
  283. }
  284. // ------------------------
  285. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser]];
  286. }
  287. dispatch_async(dispatch_get_main_queue(), ^{
  288. if ([self.delegate respondsToSelector:@selector(readFolderSuccess:permissions:etag:metadatas:)])
  289. [self.delegate readFolderSuccess:_metadataNet permissions:permissions etag:etagDirectory metadatas:metadatas];
  290. });
  291. });
  292. [self complete];
  293. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  294. NSInteger errorCode = response.statusCode;
  295. if (errorCode == 0)
  296. errorCode = error.code;
  297. // Error
  298. if ([self.delegate respondsToSelector:@selector(readFolderFailure:message:errorCode:)]) {
  299. if (errorCode == 503)
  300. [self.delegate readFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  301. else
  302. [self.delegate readFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  303. }
  304. // Request trusted certificated
  305. if ([error code] == NSURLErrorServerCertificateUntrusted)
  306. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  307. // Activity
  308. [CCCoreData addActivityClient:_metadataNet.serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  309. [self complete];
  310. }];
  311. }
  312. #pragma --------------------------------------------------------------------------------------------
  313. #pragma mark ===== Search =====
  314. #pragma --------------------------------------------------------------------------------------------
  315. - (void)search
  316. {
  317. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  318. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  319. [communication setUserAgent:[CCUtility getUserAgent]];
  320. NSString *path = [_activeUrl stringByAppendingString:dav];
  321. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  322. [communication search:path folder:folder fileName:_metadataNet.fileName depth:_metadataNet.options withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  323. NSMutableArray *metadatas = [NSMutableArray new];
  324. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  325. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  326. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  327. for(OCFileDto *itemDto in items) {
  328. itemDto.fileName = [itemDto.fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  329. // Not in Crypto Cloud file
  330. NSString *fileName = itemDto.fileName;
  331. if (itemDto.isDirectory)
  332. fileName = [fileName substringToIndex:[fileName length] - 1];
  333. if ([CCUtility isFileCryptated:fileName])
  334. continue;
  335. // ----- BUG #942 ---------
  336. if ([itemDto.etag length] == 0) {
  337. #ifndef EXTENSION
  338. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  339. #endif
  340. continue;
  341. }
  342. // ------------------------
  343. NSString *serverUrl = [NSString stringWithFormat:@"%@/files/%@", dav, _activeUser];
  344. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:serverUrl withString:@""];
  345. /* TRIM */
  346. if ([serverUrl hasPrefix:@"/"])
  347. serverUrl = [serverUrl substringFromIndex:1];
  348. if ([serverUrl hasSuffix:@"/"])
  349. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  350. /* */
  351. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  352. NSString *directoryID = [CCCoreData addDirectory:serverUrl permissions:itemDto.permissions activeAccount:_metadataNet.account];
  353. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser]];
  354. }
  355. if ([self.delegate respondsToSelector:@selector(searchSuccess:metadatas:)])
  356. [self.delegate searchSuccess:_metadataNet metadatas:metadatas];
  357. [self complete];
  358. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  359. NSInteger errorCode = response.statusCode;
  360. if (errorCode == 0)
  361. errorCode = error.code;
  362. // Error
  363. if ([self.delegate respondsToSelector:@selector(searchFailure:message:errorCode:)]) {
  364. if (errorCode == 503)
  365. [self.delegate searchFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  366. else
  367. [self.delegate searchFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  368. }
  369. // Request trusted certificated
  370. if ([error code] == NSURLErrorServerCertificateUntrusted)
  371. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  372. [self complete];
  373. }];
  374. }
  375. #pragma --------------------------------------------------------------------------------------------
  376. #pragma mark ===== Setting Favorite =====
  377. #pragma --------------------------------------------------------------------------------------------
  378. - (void)settingFavorite
  379. {
  380. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  381. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  382. [communication setUserAgent:[CCUtility getUserAgent]];
  383. NSString *path = [_activeUrl stringByAppendingString:dav];
  384. [communication settingFavoriteServer:path andFileOrFolderPath:_metadataNet.fileName favorite:[_metadataNet.options boolValue] withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  385. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccess:)])
  386. [self.delegate settingFavoriteSuccess:_metadataNet];
  387. [self complete];
  388. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  389. NSInteger errorCode = response.statusCode;
  390. if (errorCode == 0)
  391. errorCode = error.code;
  392. // Error
  393. if ([self.delegate respondsToSelector:@selector(settingFavoriteFailure:message:errorCode:)]) {
  394. if (errorCode == 503)
  395. [self.delegate settingFavoriteFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  396. else
  397. [self.delegate settingFavoriteFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  398. }
  399. // Request trusted certificated
  400. if ([error code] == NSURLErrorServerCertificateUntrusted)
  401. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  402. [self complete];
  403. }];
  404. }
  405. #pragma --------------------------------------------------------------------------------------------
  406. #pragma mark ===== Listing Favorites =====
  407. #pragma --------------------------------------------------------------------------------------------
  408. - (void)listingFavorites
  409. {
  410. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  411. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  412. [communication setUserAgent:[CCUtility getUserAgent]];
  413. NSString *path = [_activeUrl stringByAppendingString:dav];
  414. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  415. [communication listingFavorites:path folder:folder withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  416. NSMutableArray *metadatas = [NSMutableArray new];
  417. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  418. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  419. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  420. // Order by fileNamePath
  421. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  422. OCFileDto *record1 = obj1, *record2 = obj2;
  423. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  424. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  425. return [path1 compare:path2];
  426. }];
  427. for(OCFileDto *itemDto in items) {
  428. itemDto.fileName = [itemDto.fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  429. itemDto.filePath = [itemDto.filePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  430. // Not in Crypto Cloud file
  431. NSString *fileName = itemDto.fileName;
  432. if (itemDto.isDirectory)
  433. fileName = [fileName substringToIndex:[fileName length] - 1];
  434. if ([CCUtility isFileCryptated:fileName])
  435. continue;
  436. // ----- BUG #942 ---------
  437. if ([itemDto.etag length] == 0) {
  438. #ifndef EXTENSION
  439. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  440. #endif
  441. continue;
  442. }
  443. // ------------------------
  444. NSString *serverUrl = [NSString stringWithFormat:@"%@/files/%@", dav, _activeUser];
  445. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:serverUrl withString:@""];
  446. /* TRIM */
  447. if ([serverUrl hasPrefix:@"/"])
  448. serverUrl = [serverUrl substringFromIndex:1];
  449. if ([serverUrl hasSuffix:@"/"])
  450. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  451. /* */
  452. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  453. NSString *directoryID = [CCCoreData addDirectory:serverUrl permissions:itemDto.permissions activeAccount:_metadataNet.account];
  454. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser]];
  455. }
  456. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccess:metadatas:)])
  457. [self.delegate listingFavoritesSuccess:_metadataNet metadatas:metadatas];
  458. [self complete];
  459. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  460. NSInteger errorCode = response.statusCode;
  461. if (errorCode == 0)
  462. errorCode = error.code;
  463. // Error
  464. if ([self.delegate respondsToSelector:@selector(listingFavoritesFailure:message:errorCode:)]) {
  465. if (errorCode == 503)
  466. [self.delegate listingFavoritesFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  467. else
  468. [self.delegate listingFavoritesFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  469. }
  470. // Request trusted certificated
  471. if ([error code] == NSURLErrorServerCertificateUntrusted)
  472. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  473. [self complete];
  474. }];
  475. }
  476. #pragma --------------------------------------------------------------------------------------------
  477. #pragma mark ===== Create Folder =====
  478. #pragma --------------------------------------------------------------------------------------------
  479. - (void)createFolder
  480. {
  481. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  482. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  483. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  484. [communication setUserAgent:[CCUtility getUserAgent]];
  485. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:_hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  486. if ([self.delegate respondsToSelector:@selector(createFolderSuccess:)])
  487. [self.delegate createFolderSuccess:_metadataNet];
  488. [self complete];
  489. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  490. NSString *message;
  491. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl]] == YES))
  492. message = nil;
  493. else
  494. message = [CCError manageErrorOC:response.statusCode error:error];
  495. NSInteger errorCode = response.statusCode;
  496. if (errorCode == 0)
  497. errorCode = error.code;
  498. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)])
  499. [self.delegate createFolderFailure:_metadataNet message:message errorCode:errorCode];
  500. // Request trusted certificated
  501. if ([error code] == NSURLErrorServerCertificateUntrusted)
  502. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  503. [self complete];
  504. } errorBeforeRequest:^(NSError *error) {
  505. NSString *message;
  506. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl]] == YES))
  507. message = nil;
  508. else {
  509. if (error.code == OCErrorForbidenCharacters)
  510. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  511. else
  512. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  513. }
  514. // Error
  515. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)]) {
  516. if (error.code == 503)
  517. [self.delegate createFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  518. else
  519. [self.delegate createFolderFailure:_metadataNet message:message errorCode:error.code];
  520. }
  521. [self complete];
  522. }];
  523. }
  524. - (BOOL)automaticCreateFolderSync:(NSString *)folderPathName
  525. {
  526. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  527. __block BOOL noError = YES;
  528. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  529. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  530. [communication setUserAgent:[CCUtility getUserAgent]];
  531. [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  532. dispatch_semaphore_signal(semaphore);
  533. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  534. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  535. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  536. [CCCoreData clearDateReadAccount:recordAccount.account serverUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] directoryID:nil];
  537. dispatch_semaphore_signal(semaphore);
  538. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  539. noError = NO;
  540. dispatch_semaphore_signal(semaphore);
  541. } errorBeforeRequest:^(NSError *error) {
  542. noError = NO;
  543. dispatch_semaphore_signal(semaphore);
  544. }];
  545. }];
  546. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  547. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  548. return noError;
  549. }
  550. #pragma --------------------------------------------------------------------------------------------
  551. #pragma mark ===== Delete =====
  552. #pragma --------------------------------------------------------------------------------------------
  553. - (void)deleteFileOrFolder
  554. {
  555. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  556. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  557. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  558. [communication setUserAgent:[CCUtility getUserAgent]];
  559. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  560. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccess:)])
  561. [self.delegate deleteFileOrFolderSuccess:_metadataNet];
  562. [self complete];
  563. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  564. NSInteger errorCode = response.statusCode;
  565. if (errorCode == 0)
  566. errorCode = error.code;
  567. // Error
  568. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderFailure:message:errorCode:)]) {
  569. if (errorCode == 503)
  570. [self.delegate deleteFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  571. else
  572. [self.delegate deleteFileOrFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  573. }
  574. // Request trusted certificated
  575. if ([error code] == NSURLErrorServerCertificateUntrusted)
  576. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  577. [self complete];
  578. }];
  579. }
  580. #pragma --------------------------------------------------------------------------------------------
  581. #pragma mark ===== Move =====
  582. #pragma --------------------------------------------------------------------------------------------
  583. - (void)moveFileOrFolder
  584. {
  585. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  586. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  587. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  588. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  589. [communication setUserAgent:[CCUtility getUserAgent]];
  590. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:_hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  591. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:)])
  592. [self.delegate renameSuccess:_metadataNet];
  593. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:revTo:)])
  594. [self.delegate moveSuccess:_metadataNet revTo:nil];
  595. [self complete];
  596. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  597. NSInteger errorCode = response.statusCode;
  598. if (errorCode == 0)
  599. errorCode = error.code;
  600. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)])
  601. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  602. // Request trusted certificated
  603. if ([error code] == NSURLErrorServerCertificateUntrusted)
  604. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  605. [self complete];
  606. } errorBeforeRequest:^(NSError *error) {
  607. NSString *message;
  608. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  609. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  610. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  611. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  612. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  613. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  614. } else {
  615. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  616. }
  617. // Error
  618. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)]) {
  619. if (error.code == 503)
  620. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  621. else
  622. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  623. }
  624. [self complete];
  625. }];
  626. }
  627. #pragma --------------------------------------------------------------------------------------------
  628. #pragma mark ===== ReadFile =====
  629. #pragma --------------------------------------------------------------------------------------------
  630. - (void)readFile
  631. {
  632. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  633. NSString *fileName;
  634. if (_metadataNet.fileName)
  635. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  636. else
  637. fileName = _metadataNet.serverUrl;
  638. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  639. [communication setUserAgent:[CCUtility getUserAgent]];
  640. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  641. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  642. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  643. CCMetadata *metadata = [[CCMetadata alloc] init];
  644. OCFileDto *itemDto = [items objectAtIndex:0];
  645. itemDto.fileName = _metadataNet.fileName;
  646. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_metadataNet.serverUrl activeAccount:_metadataNet.account];
  647. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  648. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  649. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  650. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:_metadataNet.fileNamePrint serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser];
  651. if([self.delegate respondsToSelector:@selector(readFileSuccess:metadata:)])
  652. [self.delegate readFileSuccess:_metadataNet metadata:metadata];
  653. }
  654. // BUG 1038
  655. if ([items count] == 0) {
  656. #ifndef EXTENSION
  657. [app messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  658. #endif
  659. }
  660. [self complete];
  661. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  662. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  663. _metadataNet.errorRetry++;
  664. NSInteger errorCode = response.statusCode;
  665. if (errorCode == 0)
  666. errorCode = error.code;
  667. // Error
  668. if ([self.delegate respondsToSelector:@selector(readFileFailure:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account]) {
  669. if (errorCode == 503)
  670. [self.delegate readFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  671. else
  672. [self.delegate readFileFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  673. }
  674. // Request trusted certificated
  675. if ([error code] == NSURLErrorServerCertificateUntrusted)
  676. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  677. [self complete];
  678. }];
  679. }
  680. - (NSError *)readFileSync:(NSString *)filePathName
  681. {
  682. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  683. __block NSError *returnError;
  684. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  685. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  686. [communication setUserAgent:[CCUtility getUserAgent]];
  687. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  688. returnError = nil;
  689. dispatch_semaphore_signal(semaphore);
  690. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  691. // Request trusted certificated
  692. if ([error code] == NSURLErrorServerCertificateUntrusted)
  693. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  694. returnError = error;
  695. dispatch_semaphore_signal(semaphore);
  696. }];
  697. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  698. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  699. return returnError;
  700. }
  701. #pragma --------------------------------------------------------------------------------------------
  702. #pragma mark ===== Shared =====
  703. #pragma --------------------------------------------------------------------------------------------
  704. - (NSMutableDictionary *)getShareID
  705. {
  706. #ifndef EXTENSION
  707. return app.sharesID;
  708. #endif
  709. return [NSMutableDictionary new];
  710. }
  711. - (void)readShareServer
  712. {
  713. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  714. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  715. [communication setUserAgent:[CCUtility getUserAgent]];
  716. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  717. BOOL openWindow = NO;
  718. [[self getShareID] removeAllObjects];
  719. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  720. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  721. for (OCSharedDto *item in items)
  722. [[self getShareID] setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  723. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  724. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  725. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  726. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  727. }
  728. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  729. [self.delegate readSharedSuccess:_metadataNet items:[self getShareID] openWindow:openWindow];
  730. [self complete];
  731. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  732. NSInteger errorCode = response.statusCode;
  733. if (errorCode == 0)
  734. errorCode = error.code;
  735. // Error
  736. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  737. if (errorCode == 503)
  738. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  739. else
  740. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  741. }
  742. // Request trusted certificated
  743. if ([error code] == NSURLErrorServerCertificateUntrusted)
  744. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  745. [self complete];
  746. }];
  747. }
  748. - (void)share
  749. {
  750. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  751. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  752. [communication setUserAgent:[CCUtility getUserAgent]];
  753. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  754. [self readShareServer];
  755. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  756. NSInteger errorCode = response.statusCode;
  757. if (errorCode == 0)
  758. errorCode = error.code;
  759. // Error
  760. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  761. if (errorCode == 503)
  762. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  763. else
  764. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  765. }
  766. // Request trusted certificated
  767. if ([error code] == NSURLErrorServerCertificateUntrusted)
  768. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  769. [self complete];
  770. }];
  771. }
  772. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  773. - (void)shareWith
  774. {
  775. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  776. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  777. [communication setUserAgent:[CCUtility getUserAgent]];
  778. [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) {
  779. [self readShareServer];
  780. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  781. NSInteger errorCode = response.statusCode;
  782. if (errorCode == 0)
  783. errorCode = error.code;
  784. // Error
  785. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  786. if (errorCode == 503)
  787. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  788. else
  789. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  790. }
  791. // Request trusted certificated
  792. if ([error code] == NSURLErrorServerCertificateUntrusted)
  793. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  794. [self complete];
  795. }];
  796. }
  797. - (void)updateShare
  798. {
  799. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  800. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  801. [communication setUserAgent:[CCUtility getUserAgent]];
  802. [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) {
  803. [self readShareServer];
  804. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  805. #ifndef EXTENSION
  806. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  807. #endif
  808. NSInteger errorCode = response.statusCode;
  809. if (errorCode == 0)
  810. errorCode = error.code;
  811. // Error
  812. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  813. if (errorCode == 503)
  814. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  815. else
  816. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  817. }
  818. // Request trusted certificated
  819. if ([error code] == NSURLErrorServerCertificateUntrusted)
  820. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  821. [self complete];
  822. }];
  823. }
  824. - (void)unShare
  825. {
  826. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  827. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  828. [communication setUserAgent:[CCUtility getUserAgent]];
  829. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  830. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  831. [self.delegate unShareSuccess:_metadataNet];
  832. [self complete];
  833. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  834. #ifndef EXTENSION
  835. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  836. #endif
  837. NSInteger errorCode = response.statusCode;
  838. if (errorCode == 0)
  839. errorCode = error.code;
  840. // Error
  841. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  842. if (errorCode == 503)
  843. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  844. else
  845. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  846. }
  847. // Request trusted certificated
  848. if ([error code] == NSURLErrorServerCertificateUntrusted)
  849. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  850. [self complete];
  851. }];
  852. }
  853. - (void)getUserAndGroup
  854. {
  855. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  856. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  857. [communication setUserAgent:[CCUtility getUserAgent]];
  858. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  859. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  860. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  861. [self complete];
  862. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  863. NSInteger errorCode = response.statusCode;
  864. if (errorCode == 0)
  865. errorCode = error.code;
  866. // Error
  867. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  868. if (errorCode == 503)
  869. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  870. else
  871. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  872. }
  873. // Request trusted certificated
  874. if ([error code] == NSURLErrorServerCertificateUntrusted)
  875. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  876. [self complete];
  877. }];
  878. }
  879. #pragma --------------------------------------------------------------------------------------------
  880. #pragma mark ===== Activity =====
  881. #pragma --------------------------------------------------------------------------------------------
  882. - (void)getActivityServer
  883. {
  884. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  885. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  886. [communication setUserAgent:[CCUtility getUserAgent]];
  887. [communication getActivityServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  888. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccess:)])
  889. [self.delegate getActivityServerSuccess:listOfActivity];
  890. [self complete];
  891. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  892. NSInteger errorCode = response.statusCode;
  893. if (errorCode == 0)
  894. errorCode = error.code;
  895. // Error
  896. if ([self.delegate respondsToSelector:@selector(getActivityServerFailure:message:errorCode:)]) {
  897. if (errorCode == 503)
  898. [self.delegate getActivityServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  899. else
  900. [self.delegate getActivityServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  901. }
  902. // Request trusted certificated
  903. if ([error code] == NSURLErrorServerCertificateUntrusted)
  904. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  905. [self complete];
  906. }];
  907. }
  908. #pragma --------------------------------------------------------------------------------------------
  909. #pragma mark ===== External Sites =====
  910. #pragma --------------------------------------------------------------------------------------------
  911. - (void)getExternalSitesServer
  912. {
  913. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  914. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  915. [communication setUserAgent:[CCUtility getUserAgent]];
  916. [communication getExternalSitesServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  917. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccess:)])
  918. [self.delegate getExternalSitesServerSuccess:listOfExternalSites];
  919. [self complete];
  920. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  921. NSInteger errorCode = response.statusCode;
  922. if (errorCode == 0)
  923. errorCode = error.code;
  924. // Error
  925. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  926. if (errorCode == 503)
  927. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  928. else
  929. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  930. }
  931. // Request trusted certificated
  932. if ([error code] == NSURLErrorServerCertificateUntrusted)
  933. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  934. [self complete];
  935. }];
  936. }
  937. #pragma --------------------------------------------------------------------------------------------
  938. #pragma mark ===== Notification =====
  939. #pragma --------------------------------------------------------------------------------------------
  940. - (void)getNotificationServer
  941. {
  942. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  943. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  944. [communication setUserAgent:[CCUtility getUserAgent]];
  945. [communication getNotificationServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  946. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccess:)])
  947. [self.delegate getNotificationServerSuccess:listOfNotifications];
  948. [self complete];
  949. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  950. NSInteger errorCode = response.statusCode;
  951. if (errorCode == 0)
  952. errorCode = error.code;
  953. // Error
  954. if ([self.delegate respondsToSelector:@selector(getNotificationServerFailure:message:errorCode:)]) {
  955. if (errorCode == 503)
  956. [self.delegate getNotificationServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  957. else
  958. [self.delegate getNotificationServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  959. }
  960. // Request trusted certificated
  961. if ([error code] == NSURLErrorServerCertificateUntrusted)
  962. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  963. // Activity
  964. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  965. [self complete];
  966. }];
  967. }
  968. - (void)setNotificationServer
  969. {
  970. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  971. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  972. [communication setUserAgent:[CCUtility getUserAgent]];
  973. NSString *type = _metadataNet.options;
  974. [communication setNotificationServer:_metadataNet.serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  975. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccess:)])
  976. [self.delegate setNotificationServerSuccess:_metadataNet];
  977. [self complete];
  978. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  979. NSInteger errorCode = response.statusCode;
  980. if (errorCode == 0)
  981. errorCode = error.code;
  982. // Error
  983. if ([self.delegate respondsToSelector:@selector(setNotificationServerFailure:message:errorCode:)]) {
  984. if (errorCode == 503)
  985. [self.delegate setNotificationServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  986. else
  987. [self.delegate setNotificationServerFailure:_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)subscribingNextcloudServer
  996. {
  997. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  998. communication.kindOfCredential = credentialNotSet;
  999. [communication setUserAgent:[CCUtility getUserAgent]];
  1000. NSDictionary *parameter = _metadataNet.options;
  1001. NSString *pushToken = [parameter objectForKey:@"pushToken"];
  1002. NSString *pushTokenHash = [parameter objectForKey:@"pushTokenHash"];
  1003. NSString *devicePublicKey = [parameter objectForKey:@"devicePublicKey"];
  1004. // encode URL
  1005. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  1006. [communication subscribingNextcloudServerPush:_activeUrl pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath:k_pushNotificationServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1007. // encode URL
  1008. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  1009. signature = [CCUtility URLEncodeStringFromString:signature];
  1010. [communication subscribingPushProxy:k_pushNotificationServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature userPublicKey:[CCUtility URLEncodeStringFromString:publicKey] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1011. // Activity
  1012. [CCCoreData addActivityClient:k_pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1013. [self complete];
  1014. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1015. NSInteger errorCode = response.statusCode;
  1016. if (errorCode == 0)
  1017. errorCode = error.code;
  1018. // Error
  1019. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1020. if (errorCode == 503)
  1021. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1022. else
  1023. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1024. }
  1025. // Request trusted certificated
  1026. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1027. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1028. // Activity
  1029. [CCCoreData addActivityClient:k_pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1030. [self complete];
  1031. }];
  1032. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1033. NSInteger errorCode = response.statusCode;
  1034. if (errorCode == 0)
  1035. errorCode = error.code;
  1036. // Error
  1037. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1038. if (errorCode == 503)
  1039. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1040. else
  1041. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1042. }
  1043. // Request trusted certificated
  1044. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1045. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1046. // Activity
  1047. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1048. [self complete];
  1049. }];
  1050. }
  1051. #pragma --------------------------------------------------------------------------------------------
  1052. #pragma mark ===== User Profile =====
  1053. #pragma --------------------------------------------------------------------------------------------
  1054. - (void)getUserProfile
  1055. {
  1056. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1057. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1058. [communication setUserAgent:[CCUtility getUserAgent]];
  1059. [communication getUserProfileServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1060. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccess:userProfile:)])
  1061. [self.delegate getUserProfileSuccess:_metadataNet userProfile:userProfile];
  1062. [self complete];
  1063. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1064. NSInteger errorCode = response.statusCode;
  1065. if (errorCode == 0)
  1066. errorCode = error.code;
  1067. // Error
  1068. if ([self.delegate respondsToSelector:@selector(getUserProfileFailure:message:errorCode:)]) {
  1069. if (errorCode == 503)
  1070. [self.delegate getUserProfileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1071. else
  1072. [self.delegate getUserProfileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1073. }
  1074. // Request trusted certificated
  1075. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1076. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1077. [self complete];
  1078. }];
  1079. }
  1080. #pragma --------------------------------------------------------------------------------------------
  1081. #pragma mark ===== Server =====
  1082. #pragma --------------------------------------------------------------------------------------------
  1083. - (NSError *)checkServerSync:(NSString *)serverUrl
  1084. {
  1085. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1086. __block NSError *returnError;
  1087. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1088. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1089. [communication setUserAgent:[CCUtility getUserAgent]];
  1090. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1091. returnError = nil;
  1092. dispatch_semaphore_signal(semaphore);
  1093. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1094. // Request trusted certificated
  1095. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1096. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1097. returnError = error;
  1098. dispatch_semaphore_signal(semaphore);
  1099. }];
  1100. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1101. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1102. return returnError;
  1103. }
  1104. - (void)getFeaturesSupportedByServer
  1105. {
  1106. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1107. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1108. [communication setUserAgent:[CCUtility getUserAgent]];
  1109. [communication getFeaturesSupportedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer) {
  1110. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  1111. if ([self.delegate respondsToSelector:@selector(getFeaturesSupportedByServerSuccess:hasForbiddenCharactersSupport:hasShareSupport:hasShareeSupport:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  1112. [self.delegate getFeaturesSupportedByServerSuccess:hasCapabilitiesSupport hasForbiddenCharactersSupport:hasForbiddenCharactersSupport hasShareSupport:hasShareSupport hasShareeSupport:hasShareeSupport];
  1113. [self complete];
  1114. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1115. NSInteger errorCode = response.statusCode;
  1116. if (errorCode == 0)
  1117. errorCode = error.code;
  1118. // Error
  1119. if ([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)]) {
  1120. if (errorCode == 503)
  1121. [self.delegate getInfoServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1122. else
  1123. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1124. }
  1125. // Request trusted certificated
  1126. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1127. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1128. // Activity
  1129. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionFeatures selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1130. [self complete];
  1131. }];
  1132. }
  1133. - (void)getCapabilitiesOfServer
  1134. {
  1135. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1136. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1137. [communication setUserAgent:[CCUtility getUserAgent]];
  1138. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1139. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  1140. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccess:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  1141. [self.delegate getCapabilitiesOfServerSuccess:capabilities];
  1142. [self complete];
  1143. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1144. NSInteger errorCode = response.statusCode;
  1145. if (errorCode == 0)
  1146. errorCode = error.code;
  1147. // Error
  1148. if ([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)]) {
  1149. if (errorCode == 503)
  1150. [self.delegate getInfoServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1151. else
  1152. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1153. }
  1154. // Request trusted certificated
  1155. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1156. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1157. // Activity
  1158. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1159. [self complete];
  1160. }];
  1161. }
  1162. @end
  1163. #pragma --------------------------------------------------------------------------------------------
  1164. #pragma mark ===== OCURLSessionManager =====
  1165. #pragma --------------------------------------------------------------------------------------------
  1166. @implementation OCURLSessionManager
  1167. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1168. {
  1169. // The pinnning check
  1170. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1171. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1172. } else {
  1173. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1174. }
  1175. }
  1176. @end