OCNetworking.m 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. //
  2. // OCnetworking.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "NSString+Encode.h"
  27. #import "NCBridgeSwift.h"
  28. #import "NCXMLGetAppPasswordParser.h"
  29. @implementation OCNetworking
  30. + (OCNetworking *)sharedManager {
  31. static OCNetworking *sharedManager;
  32. @synchronized(self)
  33. {
  34. if (!sharedManager) {
  35. sharedManager = [OCNetworking new];
  36. sharedManager.checkRemoteUserInProgress = false;
  37. }
  38. return sharedManager;
  39. }
  40. }
  41. - (id)init
  42. {
  43. self = [super init];
  44. [self sharedOCCommunication];
  45. return self;
  46. }
  47. #pragma --------------------------------------------------------------------------------------------
  48. #pragma mark ===== OCCommunication =====
  49. #pragma --------------------------------------------------------------------------------------------
  50. - (OCCommunication *)sharedOCCommunication
  51. {
  52. static OCCommunication* sharedOCCommunication = nil;
  53. if (sharedOCCommunication == nil)
  54. {
  55. // Network
  56. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  57. configuration.allowsCellularAccess = YES;
  58. configuration.discretionary = NO;
  59. configuration.HTTPMaximumConnectionsPerHost = k_maxConcurrentOperation;
  60. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  61. OCURLSessionManager *networkSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configuration];
  62. [networkSessionManager.operationQueue setMaxConcurrentOperationCount: k_maxConcurrentOperation];
  63. networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  64. // Download
  65. NSURLSessionConfiguration *configurationDownload = [NSURLSessionConfiguration defaultSessionConfiguration];
  66. configurationDownload.allowsCellularAccess = YES;
  67. configurationDownload.discretionary = NO;
  68. configurationDownload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  69. configurationDownload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  70. configurationDownload.timeoutIntervalForRequest = k_timeout_upload;
  71. OCURLSessionManager *downloadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationDownload];
  72. [downloadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  73. [downloadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  74. return NSURLSessionAuthChallengePerformDefaultHandling;
  75. }];
  76. // Upload
  77. NSURLSessionConfiguration *configurationUpload = [NSURLSessionConfiguration defaultSessionConfiguration];
  78. configurationUpload.allowsCellularAccess = YES;
  79. configurationUpload.discretionary = NO;
  80. configurationUpload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  81. configurationUpload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  82. configurationUpload.timeoutIntervalForRequest = k_timeout_upload;
  83. OCURLSessionManager *uploadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationUpload];
  84. [uploadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  85. [uploadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  86. return NSURLSessionAuthChallengePerformDefaultHandling;
  87. }];
  88. sharedOCCommunication = [[OCCommunication alloc] initWithUploadSessionManager:uploadSessionManager andDownloadSessionManager:downloadSessionManager andNetworkSessionManager:networkSessionManager];
  89. }
  90. return sharedOCCommunication;
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark ===== Server =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)checkServerUrl:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password completion:(void (^)(NSString *message, NSInteger errorCode))completion
  96. {
  97. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  98. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  99. [communication setUserAgent:[CCUtility getUserAgent]];
  100. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  101. completion(nil, 0);
  102. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  103. NSString *message;
  104. NSInteger errorCode = response.statusCode;
  105. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  106. errorCode = error.code;
  107. // Error
  108. if (errorCode == 503)
  109. message = NSLocalizedString(@"_server_error_retry_", nil);
  110. else
  111. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  112. completion(message, errorCode);
  113. }];
  114. }
  115. - (void)getAppPassword:(NSString *)serverUrl username:(NSString *)username password:(NSString *)password completion:(void(^)(NSString *token, NSString *message, NSInteger errorCode))completion
  116. {
  117. NSData *authData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
  118. NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  119. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/core/getapppassword", serverUrl];
  120. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  121. [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  122. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  123. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  124. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  125. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  126. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  127. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  128. NSInteger errorCode = 0;
  129. NSString *message = @"";
  130. NSString *token = nil;
  131. if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
  132. NCXMLGetAppPasswordParser *parser = [NCXMLGetAppPasswordParser new];
  133. [parser initParserWithData:data];
  134. token = parser.token;
  135. } else {
  136. errorCode = httpResponse.statusCode;
  137. message = [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode];
  138. }
  139. dispatch_async(dispatch_get_main_queue(), ^{
  140. completion(token, message, errorCode);
  141. });
  142. }];
  143. [task resume];
  144. }
  145. #pragma --------------------------------------------------------------------------------------------
  146. #pragma mark ===== download / upload =====
  147. #pragma --------------------------------------------------------------------------------------------
  148. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode communication:(OCCommunication *)communication completion:(void (^)(NSString *account, int64_t length, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
  149. {
  150. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  151. if (tableAccount == nil) {
  152. completion(account, 0, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  153. } else if ([CCUtility getPassword:account].length == 0) {
  154. completion(account, 0, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  155. } else if ([CCUtility getCertificateError:account]) {
  156. completion(account, 0, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  157. }
  158. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  159. [communication setUserAgent:[CCUtility getUserAgent]];
  160. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  161. //float percent = roundf (progress.fractionCompleted * 100);
  162. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  163. int64_t totalUnitCount = 0;
  164. NSDate *date = [NSDate date];
  165. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  166. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  167. [dateFormatter setLocale:enUSPOSIXLocale];
  168. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  169. NSError *error;
  170. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  171. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  172. if(contentLength) {
  173. totalUnitCount = (int64_t) [contentLength longLongValue];
  174. }
  175. NSString *dateString = [fields objectForKey:@"Date"];
  176. if (dateString) {
  177. if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
  178. date = [NSDate date];
  179. }
  180. } else {
  181. date = [NSDate date];
  182. }
  183. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  184. if (etag == nil) {
  185. completion(account, 0, nil, nil, NSLocalizedString(@"Internal error", nil), k_CCErrorInternalError);
  186. } else {
  187. completion(account, totalUnitCount, etag, date, nil, 0);
  188. }
  189. } failureRequest:^(NSURLResponse *response, NSError *error) {
  190. NSString *message;
  191. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  192. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  193. errorCode = error.code;
  194. // Server Unauthorized
  195. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  196. #ifndef EXTENSION
  197. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  198. #endif
  199. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  200. [CCUtility setCertificateError:account error:YES];
  201. }
  202. // Error
  203. if (errorCode == 503)
  204. message = NSLocalizedString(@"_server_error_retry_", nil);
  205. else
  206. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  207. completion(account, 0, nil, nil, message, errorCode);
  208. }];
  209. return sessionTask;
  210. }
  211. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account url:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  212. {
  213. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  214. if (tableAccount == nil) {
  215. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  216. } else if ([CCUtility getPassword:account].length == 0) {
  217. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  218. } else if ([CCUtility getCertificateError:account]) {
  219. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  220. }
  221. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  222. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  223. [communication setUserAgent:[CCUtility getUserAgent]];
  224. NSURLSessionTask *sessionTask = [communication downloadFileSession:url toDestiny:fileNameLocalPath defaultPriority:YES encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  225. //float percent = roundf (progress.fractionCompleted * 100);
  226. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  227. completion(account, nil, 0);
  228. } failureRequest:^(NSURLResponse *response, NSError *error) {
  229. NSString *message;
  230. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  231. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  232. errorCode = error.code;
  233. // Server Unauthorized
  234. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  235. #ifndef EXTENSION
  236. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  237. #endif
  238. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  239. [CCUtility setCertificateError:account error:YES];
  240. }
  241. // Error
  242. if (errorCode == 503)
  243. message = NSLocalizedString(@"_server_error_retry_", nil);
  244. else
  245. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  246. completion(account, message, errorCode);
  247. }];
  248. return sessionTask;
  249. }
  250. #pragma --------------------------------------------------------------------------------------------
  251. #pragma mark ===== Share =====
  252. #pragma --------------------------------------------------------------------------------------------
  253. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  254. {
  255. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  256. if (tableAccount == nil) {
  257. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  258. } else if ([CCUtility getPassword:account].length == 0) {
  259. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  260. } else if ([CCUtility getCertificateError:account]) {
  261. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  262. }
  263. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  264. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  265. [communication setUserAgent:[CCUtility getUserAgent]];
  266. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  267. completion(account, items, nil, 0);
  268. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  269. NSString *message;
  270. NSInteger errorCode = response.statusCode;
  271. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  272. errorCode = error.code;
  273. // Error
  274. if (errorCode == 503) {
  275. message = NSLocalizedString(@"_server_error_retry_", nil);
  276. } else {
  277. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  278. }
  279. completion(account, nil, message, errorCode);
  280. }];
  281. }
  282. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  283. {
  284. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  285. if (tableAccount == nil) {
  286. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  287. } else if ([CCUtility getPassword:account].length == 0) {
  288. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  289. } else if ([CCUtility getCertificateError:account]) {
  290. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  291. }
  292. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  293. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  294. [communication setUserAgent:[CCUtility getUserAgent]];
  295. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  296. completion(account, listOfShared, nil, 0);
  297. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  298. NSString *message;
  299. NSInteger errorCode = response.statusCode;
  300. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  301. errorCode = error.code;
  302. // Error
  303. if (errorCode == 503) {
  304. message = NSLocalizedString(@"_server_error_retry_", nil);
  305. } else {
  306. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  307. }
  308. completion(account, nil, message, errorCode);
  309. }];
  310. }
  311. - (void)shareWithAccount:(NSString *)account fileName:(NSString *)fileName password:(NSString *)password permission:(NSInteger)permission hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  312. {
  313. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  314. if (tableAccount == nil) {
  315. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  316. } else if ([CCUtility getPassword:account].length == 0) {
  317. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  318. } else if ([CCUtility getCertificateError:account]) {
  319. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  320. }
  321. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  322. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  323. [communication setUserAgent:[CCUtility getUserAgent]];
  324. [communication shareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPassword:[password encodeString:NSUTF8StringEncoding] andPermission:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  325. completion(account, nil, 0);
  326. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  327. NSString *message;
  328. NSInteger errorCode = response.statusCode;
  329. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  330. errorCode = error.code;
  331. // Error
  332. if (errorCode == 503) {
  333. message = NSLocalizedString(@"_server_error_retry_", nil);
  334. } else {
  335. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  336. }
  337. completion(account, message, errorCode);
  338. }];
  339. }
  340. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  341. - (void)shareUserGroupWithAccount:(NSString *)account userOrGroup:(NSString *)userOrGroup fileName:(NSString *)fileName permission:(NSInteger)permission shareeType:(NSInteger)shareeType completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  342. {
  343. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  344. if (tableAccount == nil) {
  345. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  346. } else if ([CCUtility getPassword:account].length == 0) {
  347. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  348. } else if ([CCUtility getCertificateError:account]) {
  349. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  350. }
  351. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  352. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  353. [communication setUserAgent:[CCUtility getUserAgent]];
  354. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  355. completion(account, nil, 0);
  356. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  357. NSString *message;
  358. NSInteger errorCode = response.statusCode;
  359. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  360. errorCode = error.code;
  361. // Error
  362. if (errorCode == 503) {
  363. message = NSLocalizedString(@"_server_error_retry_", nil);
  364. } else {
  365. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  366. }
  367. completion(account, message, errorCode);
  368. }];
  369. }
  370. - (void)shareUpdateAccount:(NSString *)account shareID:(NSInteger)shareID password:(NSString *)password note:(NSString *)note permission:(NSInteger)permission expirationTime:(NSString *)expirationTime hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  371. {
  372. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  373. if (tableAccount == nil) {
  374. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  375. } else if ([CCUtility getPassword:account].length == 0) {
  376. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  377. } else if ([CCUtility getCertificateError:account]) {
  378. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  379. }
  380. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  381. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  382. [communication setUserAgent:[CCUtility getUserAgent]];
  383. [communication updateShare:shareID ofServerPath:[tableAccount.url stringByAppendingString:@"/"] withPasswordProtect:[password encodeString:NSUTF8StringEncoding] andNote:note andExpirationTime:expirationTime andPermissions:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  384. completion(account, nil, 0);
  385. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  386. NSString *message;
  387. NSInteger errorCode = response.statusCode;
  388. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  389. errorCode = error.code;
  390. // Error
  391. if (errorCode == 503) {
  392. message = NSLocalizedString(@"_server_error_retry_", nil);
  393. } else {
  394. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  395. }
  396. completion(account, message, errorCode);
  397. }];
  398. }
  399. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  400. {
  401. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  402. if (tableAccount == nil) {
  403. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  404. } else if ([CCUtility getPassword:account].length == 0) {
  405. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  406. } else if ([CCUtility getCertificateError:account]) {
  407. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  408. }
  409. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  410. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  411. [communication setUserAgent:[CCUtility getUserAgent]];
  412. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  413. completion(account, nil, 0);
  414. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  415. NSString *message;
  416. NSInteger errorCode = response.statusCode;
  417. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  418. errorCode = error.code;
  419. // Error
  420. if (errorCode == 503) {
  421. message = NSLocalizedString(@"_server_error_retry_", nil);
  422. } else {
  423. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  424. }
  425. completion(account, message, errorCode);
  426. }];
  427. }
  428. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  429. {
  430. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  431. if (tableAccount == nil) {
  432. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  433. } else if ([CCUtility getPassword:account].length == 0) {
  434. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  435. } else if ([CCUtility getCertificateError:account]) {
  436. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  437. }
  438. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  439. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  440. [communication setUserAgent:[CCUtility getUserAgent]];
  441. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  442. completion(account, itemList, nil, 0);
  443. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  444. NSString *message;
  445. NSInteger errorCode = response.statusCode;
  446. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  447. errorCode = error.code;
  448. // Error
  449. if (errorCode == 503) {
  450. message = NSLocalizedString(@"_server_error_retry_", nil);
  451. } else {
  452. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  453. }
  454. completion(account, nil, message, errorCode);
  455. }];
  456. }
  457. #pragma --------------------------------------------------------------------------------------------
  458. #pragma mark ===== VAR =====
  459. #pragma --------------------------------------------------------------------------------------------
  460. - (void)getActivityWithAccount:(NSString *)account since:(NSInteger)since limit:(NSInteger)limit objectId:(NSString *)objectId objectType:(NSString *)objectType link:(NSString *)link completion:(void(^)(NSString *account, NSArray *listOfActivity, NSString *message, NSInteger errorCode))completion
  461. {
  462. BOOL previews = false;
  463. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  464. if (tableAccount == nil) {
  465. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  466. } else if ([CCUtility getPassword:account].length == 0) {
  467. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  468. } else if ([CCUtility getCertificateError:account]) {
  469. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  470. }
  471. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  472. if (serverVersionMajor >= k_nextcloud_version_15_0) {
  473. previews = true;
  474. }
  475. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  476. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  477. [communication setUserAgent:[CCUtility getUserAgent]];
  478. [communication getActivityServer:[tableAccount.url stringByAppendingString:@"/"] since:since limit:limit objectId:objectId objectType:objectType previews:previews link:link onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  479. completion(account, listOfActivity, nil, 0);
  480. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  481. NSString *message;
  482. NSInteger errorCode = response.statusCode;
  483. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  484. errorCode = error.code;
  485. // Server Unauthorized
  486. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  487. #ifndef EXTENSION
  488. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  489. #endif
  490. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  491. [CCUtility setCertificateError:account error:YES];
  492. }
  493. // Error
  494. if (errorCode == 503)
  495. message = NSLocalizedString(@"_server_error_retry_", nil);
  496. else
  497. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  498. completion(account, nil, message, errorCode);
  499. }];
  500. }
  501. - (void)getExternalSitesWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  502. {
  503. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  504. if (tableAccount == nil) {
  505. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  506. } else if ([CCUtility getPassword:account].length == 0) {
  507. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  508. } else if ([CCUtility getCertificateError:account]) {
  509. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  510. }
  511. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  512. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  513. [communication setUserAgent:[CCUtility getUserAgent]];
  514. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  515. completion(account, listOfExternalSites, nil, 0);
  516. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  517. NSString *message;
  518. NSInteger errorCode = response.statusCode;
  519. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  520. errorCode = error.code;
  521. // Server Unauthorized
  522. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  523. #ifndef EXTENSION
  524. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  525. #endif
  526. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  527. [CCUtility setCertificateError:account error:YES];
  528. }
  529. // Error
  530. if (errorCode == 503) {
  531. message = NSLocalizedString(@"_server_error_retry_", nil);
  532. } else {
  533. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  534. }
  535. completion(account, nil, message, errorCode);
  536. }];
  537. }
  538. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  539. {
  540. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  541. if (tableAccount == nil) {
  542. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  543. } else if ([CCUtility getPassword:account].length == 0) {
  544. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  545. } else if ([CCUtility getCertificateError:account]) {
  546. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  547. }
  548. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  549. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  550. [communication setUserAgent:[CCUtility getUserAgent]];
  551. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  552. completion(account, listOfNotifications, nil, 0);
  553. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  554. NSString *message;
  555. NSInteger errorCode = response.statusCode;
  556. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  557. errorCode = error.code;
  558. // Server Unauthorized
  559. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  560. #ifndef EXTENSION
  561. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  562. #endif
  563. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  564. [CCUtility setCertificateError:account error:YES];
  565. }
  566. // Error
  567. if (errorCode == 503) {
  568. message = NSLocalizedString(@"_server_error_retry_", nil);
  569. } else {
  570. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  571. }
  572. completion(account, nil, message, errorCode);
  573. }];
  574. }
  575. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  576. {
  577. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  578. if (tableAccount == nil) {
  579. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  580. } else if ([CCUtility getPassword:account].length == 0) {
  581. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  582. } else if ([CCUtility getCertificateError:account]) {
  583. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  584. }
  585. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  586. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  587. [communication setUserAgent:[CCUtility getUserAgent]];
  588. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  589. completion(account, nil, 0);
  590. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  591. NSString *message;
  592. NSInteger errorCode = response.statusCode;
  593. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  594. errorCode = error.code;
  595. // Server Unauthorized
  596. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  597. #ifndef EXTENSION
  598. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  599. #endif
  600. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  601. [CCUtility setCertificateError:account error:YES];
  602. }
  603. // Error
  604. if (errorCode == 503) {
  605. message = NSLocalizedString(@"_server_error_retry_", nil);
  606. } else {
  607. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  608. }
  609. completion(account, message, errorCode);
  610. }];
  611. }
  612. #pragma --------------------------------------------------------------------------------------------
  613. #pragma mark ===== Push Notification =====
  614. #pragma --------------------------------------------------------------------------------------------
  615. - (void)subscribingPushNotificationWithAccount:(NSString *)account url:(NSString *)url pushToken:(NSString *)pushToken Hash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey completion:(void(^)(NSString *account, NSString *deviceIdentifier, NSString *deviceIdentifierSignature, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  616. {
  617. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  618. if (tableAccount == nil) {
  619. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  620. } else if ([CCUtility getPassword:account].length == 0) {
  621. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  622. } else if ([CCUtility getCertificateError:account]) {
  623. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  624. }
  625. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  626. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  627. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  628. #ifdef DEBUG
  629. // proxyServerPath = @"http://127.0.0.1:8088";
  630. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  631. #endif
  632. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  633. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  634. [communication setUserAgent:[CCUtility getUserAgent]];
  635. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  636. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  637. signature = [CCUtility URLEncodeStringFromString:signature];
  638. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  639. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  640. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  641. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  642. NSString *message;
  643. NSInteger errorCode = response.statusCode;
  644. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  645. errorCode = error.code;
  646. // Error
  647. if (errorCode == 503)
  648. message = NSLocalizedString(@"_server_error_retry_", nil);
  649. else
  650. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  651. completion(account, nil, nil, nil, message, errorCode);
  652. }];
  653. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  654. NSString *message;
  655. NSInteger errorCode = response.statusCode;
  656. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  657. errorCode = error.code;
  658. // Error
  659. if (errorCode == 503)
  660. message = NSLocalizedString(@"_server_error_retry_", nil);
  661. else
  662. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  663. completion(account, nil, nil, nil, message, errorCode);
  664. }];
  665. }
  666. - (void)unsubscribingPushNotificationWithAccount:(NSString *)account url:(NSString *)url deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey completion:(void (^)(NSString *account ,NSString *message, NSInteger errorCode))completion {
  667. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  668. if (tableAccount == nil) {
  669. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  670. } else if ([CCUtility getPassword:account].length == 0) {
  671. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  672. } else if ([CCUtility getCertificateError:account]) {
  673. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  674. }
  675. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  676. #ifdef DEBUG
  677. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  678. #endif
  679. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  680. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  681. [communication setUserAgent:[CCUtility getUserAgent]];
  682. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  683. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  684. completion(account, nil, 0);
  685. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  686. NSString *message;
  687. NSInteger errorCode = response.statusCode;
  688. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  689. errorCode = error.code;
  690. // Error
  691. if (errorCode == 503)
  692. message = NSLocalizedString(@"_server_error_retry_", nil);
  693. else
  694. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  695. completion(account, message, errorCode);
  696. }];
  697. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  698. NSString *message;
  699. NSInteger errorCode = response.statusCode;
  700. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  701. errorCode = error.code;
  702. // Error
  703. if (errorCode == 503)
  704. message = NSLocalizedString(@"_server_error_retry_", nil);
  705. else
  706. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  707. completion(account, message, errorCode);
  708. }];
  709. }
  710. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  711. {
  712. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  713. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  714. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  715. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  716. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  717. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  718. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  719. if (error) {
  720. NSString *message;
  721. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  722. NSInteger errorCode = httpResponse.statusCode;
  723. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  724. errorCode = error.code;
  725. // Error
  726. if (errorCode == 503)
  727. message = NSLocalizedString(@"_server_error_retry_", nil);
  728. else
  729. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  730. completion(nil, message, errorCode);
  731. } else {
  732. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  733. completion(jsongParsed, nil, 0);
  734. }
  735. }];
  736. [task resume];
  737. }
  738. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  739. {
  740. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  741. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  742. // Delete
  743. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  744. // Delete-all
  745. if (notificationId == 0) {
  746. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  747. }
  748. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  749. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  750. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  751. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  752. [request setHTTPMethod: @"DELETE"];
  753. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  754. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  755. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  756. if (error) {
  757. NSString *message;
  758. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  759. NSInteger errorCode = httpResponse.statusCode;
  760. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  761. errorCode = error.code;
  762. // Error
  763. if (errorCode == 503)
  764. message = NSLocalizedString(@"_server_error_retry_", nil);
  765. else
  766. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  767. completion(message, errorCode);
  768. } else {
  769. completion(nil, 0);
  770. }
  771. }];
  772. [task resume];
  773. }
  774. #pragma --------------------------------------------------------------------------------------------
  775. #pragma mark ===== Manage Mobile Editor OCS API =====
  776. #pragma --------------------------------------------------------------------------------------------
  777. - (void)createLinkRichdocumentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  778. {
  779. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  780. if (tableAccount == nil) {
  781. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  782. } else if ([CCUtility getPassword:account].length == 0) {
  783. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  784. } else if ([CCUtility getCertificateError:account]) {
  785. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  786. }
  787. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  788. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  789. [communication setUserAgent:[CCUtility getUserAgent]];
  790. [communication createLinkRichdocuments:[tableAccount.url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  791. completion(account, link, nil, 0);
  792. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  793. NSString *message;
  794. NSInteger errorCode = response.statusCode;
  795. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  796. errorCode = error.code;
  797. // Error
  798. if (errorCode == 503)
  799. message = NSLocalizedString(@"_server_error_retry_", nil);
  800. else
  801. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  802. completion(account, nil,message, errorCode);
  803. }];
  804. }
  805. - (void)getTemplatesRichdocumentsWithAccount:(NSString *)account typeTemplate:(NSString *)typeTemplate completion:(void(^)(NSString *account, NSArray *listOfTemplate, NSString *message, NSInteger errorCode))completion
  806. {
  807. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  808. if (tableAccount == nil) {
  809. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  810. } else if ([CCUtility getPassword:account].length == 0) {
  811. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  812. } else if ([CCUtility getCertificateError:account]) {
  813. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  814. }
  815. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  816. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  817. [communication setUserAgent:[CCUtility getUserAgent]];
  818. [communication getTemplatesRichdocuments:[tableAccount.url stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  819. completion(account, listOfTemplate, nil, 0);
  820. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  821. NSString *message;
  822. NSInteger errorCode = response.statusCode;
  823. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  824. errorCode = error.code;
  825. // Error
  826. if (errorCode == 503)
  827. message = NSLocalizedString(@"_server_error_retry_", nil);
  828. else
  829. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  830. completion(account, nil, message, errorCode);
  831. }];
  832. }
  833. - (void)createNewRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID completion:(void(^)(NSString *account, NSString *url, NSString *message, NSInteger errorCode))completion
  834. {
  835. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  836. if (tableAccount == nil) {
  837. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  838. } else if ([CCUtility getPassword:account].length == 0) {
  839. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  840. } else if ([CCUtility getCertificateError:account]) {
  841. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  842. }
  843. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  844. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  845. [communication setUserAgent:[CCUtility getUserAgent]];
  846. [communication createNewRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  847. completion(account, url, nil, 0);
  848. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  849. NSString *message;
  850. NSInteger errorCode = response.statusCode;
  851. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  852. errorCode = error.code;
  853. // Error
  854. if (errorCode == 503)
  855. message = NSLocalizedString(@"_server_error_retry_", nil);
  856. else
  857. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  858. completion(account, nil, message, errorCode);
  859. }];
  860. }
  861. - (void)createAssetRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  862. {
  863. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  864. if (tableAccount == nil) {
  865. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  866. } else if ([CCUtility getPassword:account].length == 0) {
  867. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  868. } else if ([CCUtility getCertificateError:account]) {
  869. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  870. }
  871. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:tableAccount.url];
  872. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  873. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  874. [communication setUserAgent:[CCUtility getUserAgent]];
  875. [communication createAssetRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  876. completion(account, url, nil, 0);
  877. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  878. NSString *message;
  879. NSInteger errorCode = response.statusCode;
  880. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  881. errorCode = error.code;
  882. // Error
  883. if (errorCode == 503)
  884. message = NSLocalizedString(@"_server_error_retry_", nil);
  885. else
  886. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  887. completion(account, nil, message, errorCode);
  888. }];
  889. }
  890. #pragma --------------------------------------------------------------------------------------------
  891. #pragma mark ===== Trash =====
  892. #pragma --------------------------------------------------------------------------------------------
  893. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  894. {
  895. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  896. if (tableAccount == nil) {
  897. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  898. } else if ([CCUtility getPassword:account].length == 0) {
  899. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  900. } else if ([CCUtility getCertificateError:account]) {
  901. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  902. }
  903. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  904. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  905. [communication setUserAgent:[CCUtility getUserAgent]];
  906. [communication listingTrash:[serverUrl stringByAppendingString:path] depth:depth onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  907. // Check items > 0
  908. if ([items count] == 0) {
  909. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  910. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  911. } else {
  912. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  913. NSMutableArray *listTrash = [NSMutableArray new];
  914. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  915. if ([items count] > 1) {
  916. for (NSUInteger i=1; i < [items count]; i++) {
  917. OCFileDto *itemDto = [items objectAtIndex:i];
  918. tableTrash *trash = [tableTrash new];
  919. trash.account = account;
  920. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  921. trash.directory = itemDto.isDirectory;
  922. trash.fileId = itemDto.fileId;
  923. trash.fileName = itemDto.fileName;
  924. NSArray *array = [itemDto.filePath componentsSeparatedByString:path];
  925. long len = [[array objectAtIndex:0] length];
  926. trash.filePath = [itemDto.filePath substringFromIndex:len];
  927. trash.hasPreview = itemDto.hasPreview;
  928. trash.size = itemDto.size;
  929. trash.trashbinFileName = itemDto.trashbinFileName;
  930. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  931. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  932. NSDictionary *results = [[NCCommunicationCommon shared] objcGetInternalContenTypeWithFileName:trash.trashbinFileName contentType:@"" directory:itemDto.isDirectory];
  933. trash.contentType = results[@"contentType"];
  934. trash.iconName = results[@"iconName"];
  935. trash.typeFile = results[@"typeFile"];
  936. [listTrash addObject:trash];
  937. }
  938. }
  939. dispatch_async(dispatch_get_main_queue(), ^{
  940. completion(account, listTrash, nil, 0);
  941. });
  942. });
  943. }
  944. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  945. NSString *message;
  946. NSInteger errorCode = response.statusCode;
  947. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  948. errorCode = error.code;
  949. // Error
  950. if (errorCode == 503)
  951. message = NSLocalizedString(@"_server_error_retry_", nil);
  952. else
  953. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  954. completion(account, nil,message, errorCode);
  955. }];
  956. }
  957. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  958. {
  959. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  960. if (tableAccount == nil) {
  961. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  962. } else if ([CCUtility getPassword:account].length == 0) {
  963. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  964. } else if ([CCUtility getCertificateError:account]) {
  965. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  966. }
  967. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  968. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  969. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  970. [communication setUserAgent:[CCUtility getUserAgent]];
  971. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  972. completion(account, nil, 0);
  973. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  974. NSString *message;
  975. NSInteger errorCode = response.statusCode;
  976. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  977. errorCode = error.code;
  978. // Error
  979. if (errorCode == 503)
  980. message = NSLocalizedString(@"_server_error_retry_", nil);
  981. else
  982. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  983. completion(account, message, errorCode);
  984. }];
  985. }
  986. #pragma --------------------------------------------------------------------------------------------
  987. #pragma mark ===== Comments =====
  988. #pragma --------------------------------------------------------------------------------------------
  989. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  990. {
  991. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  992. if (tableAccount == nil) {
  993. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  994. } else if ([CCUtility getPassword:account].length == 0) {
  995. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  996. } else if ([CCUtility getCertificateError:account]) {
  997. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  998. }
  999. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1000. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1001. [communication setUserAgent:[CCUtility getUserAgent]];
  1002. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  1003. completion(account, list, nil, 0);
  1004. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1005. NSString *message;
  1006. NSInteger errorCode = response.statusCode;
  1007. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1008. errorCode = error.code;
  1009. // Error
  1010. if (errorCode == 503)
  1011. message = NSLocalizedString(@"_server_error_retry_", nil);
  1012. else
  1013. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1014. completion(account, nil,message, errorCode);
  1015. }];
  1016. }
  1017. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1018. {
  1019. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1020. if (tableAccount == nil) {
  1021. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1022. } else if ([CCUtility getPassword:account].length == 0) {
  1023. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1024. } else if ([CCUtility getCertificateError:account]) {
  1025. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1026. }
  1027. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1028. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1029. [communication setUserAgent:[CCUtility getUserAgent]];
  1030. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1031. completion(account, nil, 0);
  1032. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1033. NSString *message;
  1034. NSInteger errorCode = response.statusCode;
  1035. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1036. errorCode = error.code;
  1037. // Error
  1038. if (errorCode == 503)
  1039. message = NSLocalizedString(@"_server_error_retry_", nil);
  1040. else
  1041. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1042. completion(account, message, errorCode);
  1043. }];
  1044. }
  1045. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1046. {
  1047. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1048. if (tableAccount == nil) {
  1049. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1050. } else if ([CCUtility getPassword:account].length == 0) {
  1051. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1052. } else if ([CCUtility getCertificateError:account]) {
  1053. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1054. }
  1055. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1056. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1057. [communication setUserAgent:[CCUtility getUserAgent]];
  1058. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1059. completion(account, nil, 0);
  1060. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1061. NSString *message;
  1062. NSInteger errorCode = response.statusCode;
  1063. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1064. errorCode = error.code;
  1065. // Error
  1066. if (errorCode == 503)
  1067. message = NSLocalizedString(@"_server_error_retry_", nil);
  1068. else
  1069. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1070. completion(account, message, errorCode);
  1071. }];
  1072. }
  1073. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1074. {
  1075. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1076. if (tableAccount == nil) {
  1077. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1078. } else if ([CCUtility getPassword:account].length == 0) {
  1079. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1080. } else if ([CCUtility getCertificateError:account]) {
  1081. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1082. }
  1083. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1084. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1085. [communication setUserAgent:[CCUtility getUserAgent]];
  1086. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1087. completion(account, nil, 0);
  1088. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1089. NSString *message;
  1090. NSInteger errorCode = response.statusCode;
  1091. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1092. errorCode = error.code;
  1093. // Error
  1094. if (errorCode == 503)
  1095. message = NSLocalizedString(@"_server_error_retry_", nil);
  1096. else
  1097. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1098. completion(account, message, errorCode);
  1099. }];
  1100. }
  1101. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1102. {
  1103. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1104. if (tableAccount == nil) {
  1105. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1106. } else if ([CCUtility getPassword:account].length == 0) {
  1107. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1108. } else if ([CCUtility getCertificateError:account]) {
  1109. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1110. }
  1111. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1112. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1113. [communication setUserAgent:[CCUtility getUserAgent]];
  1114. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1115. completion(account, nil, 0);
  1116. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1117. NSString *message;
  1118. NSInteger errorCode = response.statusCode;
  1119. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1120. errorCode = error.code;
  1121. // Error
  1122. if (errorCode == 503)
  1123. message = NSLocalizedString(@"_server_error_retry_", nil);
  1124. else
  1125. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1126. completion(account, message, errorCode);
  1127. }];
  1128. }
  1129. #pragma --------------------------------------------------------------------------------------------
  1130. #pragma mark ===== Third Parts =====
  1131. #pragma --------------------------------------------------------------------------------------------
  1132. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1133. {
  1134. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1135. if (tableAccount == nil) {
  1136. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1137. } else if ([CCUtility getPassword:account].length == 0) {
  1138. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1139. } else if ([CCUtility getCertificateError:account]) {
  1140. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1141. }
  1142. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1143. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1144. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1145. [communication setUserAgent:[CCUtility getUserAgent]];
  1146. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1147. completion(account, userProfile, nil, 0);
  1148. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1149. NSString *message;
  1150. NSInteger errorCode = response.statusCode;
  1151. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1152. errorCode = error.code;
  1153. // Server Unauthorized
  1154. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1155. #ifndef EXTENSION
  1156. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1157. #endif
  1158. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1159. [CCUtility setCertificateError:account error:YES];
  1160. }
  1161. // Error
  1162. if (errorCode == 503)
  1163. message = NSLocalizedString(@"_server_error_retry_", nil);
  1164. else
  1165. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1166. completion(account, nil,message, errorCode);
  1167. }];
  1168. }
  1169. - (void)putHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl address:(NSString *)address businesssize:(NSString *)businesssize businesstype:(NSString *)businesstype city:(NSString *)city company:(NSString *)company country:(NSString *)country displayname:(NSString *)displayname email:(NSString *)email phone:(NSString *)phone role_:(NSString *)role_ twitter:(NSString *)twitter website:(NSString *)website zip:(NSString *)zip completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1170. {
  1171. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1172. if (tableAccount == nil) {
  1173. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1174. } else if ([CCUtility getPassword:account].length == 0) {
  1175. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1176. } else if ([CCUtility getCertificateError:account]) {
  1177. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1178. }
  1179. // Create JSON
  1180. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  1181. if (address) [dataDic setValue:address forKey:@"address"];
  1182. if (businesssize) {
  1183. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  1184. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  1185. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  1186. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  1187. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  1188. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  1189. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  1190. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  1191. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  1192. }
  1193. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  1194. if (city) [dataDic setValue:city forKey:@"city"];
  1195. if (company) [dataDic setValue:company forKey:@"company"];
  1196. if (country) [dataDic setValue:country forKey:@"country"];
  1197. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  1198. if (email) [dataDic setValue:email forKey:@"email"];
  1199. if (phone) [dataDic setValue:phone forKey:@"phone"];
  1200. if (role_) [dataDic setValue:role_ forKey:@"role"];
  1201. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  1202. if (website) [dataDic setValue:website forKey:@"website"];
  1203. if (zip) [dataDic setValue:zip forKey:@"zip"];
  1204. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1205. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1206. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1207. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1208. [communication setUserAgent:[CCUtility getUserAgent]];
  1209. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1210. completion(account, nil, 0);
  1211. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1212. NSString *message;
  1213. NSInteger errorCode = response.statusCode;
  1214. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1215. errorCode = error.code;
  1216. // Server Unauthorized
  1217. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1218. #ifndef EXTENSION
  1219. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1220. #endif
  1221. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1222. [CCUtility setCertificateError:account error:YES];
  1223. }
  1224. // Error
  1225. if (errorCode == 503)
  1226. message = NSLocalizedString(@"_server_error_retry_", nil);
  1227. else
  1228. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1229. completion(account, message, errorCode);
  1230. }];
  1231. }
  1232. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  1233. {
  1234. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1235. if (tableAccount == nil) {
  1236. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1237. } else if ([CCUtility getPassword:account].length == 0) {
  1238. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1239. } else if ([CCUtility getCertificateError:account]) {
  1240. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1241. }
  1242. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  1243. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1244. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1245. [communication setUserAgent:[CCUtility getUserAgent]];
  1246. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  1247. completion(account, features, nil, 0);
  1248. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1249. NSString *message;
  1250. NSInteger errorCode = response.statusCode;
  1251. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1252. errorCode = error.code;
  1253. // Server Unauthorized
  1254. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1255. #ifndef EXTENSION
  1256. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1257. #endif
  1258. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1259. [CCUtility setCertificateError:account error:YES];
  1260. }
  1261. // Error
  1262. if (errorCode == 503)
  1263. message = NSLocalizedString(@"_server_error_retry_", nil);
  1264. else
  1265. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1266. completion(account, nil,message, errorCode);
  1267. }];
  1268. }
  1269. #pragma --------------------------------------------------------------------------------------------
  1270. #pragma mark ===== didReceiveChallenge =====
  1271. #pragma --------------------------------------------------------------------------------------------
  1272. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  1273. {
  1274. // The pinnning check
  1275. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1276. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1277. } else {
  1278. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1279. }
  1280. }
  1281. @end
  1282. #pragma --------------------------------------------------------------------------------------------
  1283. #pragma mark ===== OCURLSessionManager =====
  1284. #pragma --------------------------------------------------------------------------------------------
  1285. @implementation OCURLSessionManager
  1286. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1287. {
  1288. // The pinnning check
  1289. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1290. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1291. } else {
  1292. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1293. }
  1294. }
  1295. @end