OCNetworking.m 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  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. /*
  116. - (void)serverStatusUrl:(NSString *)serverUrl completion:(void(^)(NSString *serverProductName, NSInteger versionMajor, NSInteger versionMicro, NSInteger versionMinor, BOOL extendedSupport,NSString *message, NSInteger errorCode))completion
  117. {
  118. NSString *urlTest = [serverUrl stringByAppendingString:k_serverStatus];
  119. // Remove stored cookies
  120. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  121. for (NSHTTPCookie *cookie in [storage cookies])
  122. {
  123. [storage deleteCookie:cookie];
  124. }
  125. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlTest] cachePolicy:0 timeoutInterval:20.0];
  126. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  127. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  128. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  129. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  130. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  131. if (error) {
  132. NSString *message;
  133. NSInteger errorCode;
  134. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  135. errorCode = httpResponse.statusCode;
  136. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  137. errorCode = error.code;
  138. // Error
  139. if (errorCode == 503)
  140. message = NSLocalizedString(@"_server_error_retry_", nil);
  141. else
  142. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  143. dispatch_sync(dispatch_get_main_queue(), ^{
  144. completion(nil, 0, 0, 0, false, message, errorCode);
  145. });
  146. } else {
  147. NSString *serverProductName = @"";
  148. NSString *serverVersion = @"0.0.0";
  149. NSString *serverVersionString = @"0.0.0";
  150. NSInteger versionMajor = 0;
  151. NSInteger versionMicro = 0;
  152. NSInteger versionMinor = 0;
  153. BOOL extendedSupport = FALSE;
  154. NSError *error;
  155. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  156. if (error) {
  157. dispatch_sync(dispatch_get_main_queue(), ^{
  158. completion(nil, 0, 0, 0, extendedSupport, NSLocalizedString(@"_no_nextcloud_found_", nil), k_CCErrorInternalError);
  159. });
  160. } else {
  161. serverProductName = [[jsongParsed valueForKey:@"productname"] lowercaseString];
  162. serverVersion = [jsongParsed valueForKey:@"version"];
  163. serverVersionString = [jsongParsed valueForKey:@"versionstring"];
  164. NSArray *arrayVersion = [serverVersionString componentsSeparatedByString:@"."];
  165. if (arrayVersion.count == 1) {
  166. versionMajor = [arrayVersion[0] integerValue];
  167. } else if (arrayVersion.count == 2) {
  168. versionMajor = [arrayVersion[0] integerValue];
  169. versionMinor = [arrayVersion[1] integerValue];
  170. } else if (arrayVersion.count >= 3) {
  171. versionMajor = [arrayVersion[0] integerValue];
  172. versionMinor = [arrayVersion[1] integerValue];
  173. versionMicro = [arrayVersion[2] integerValue];
  174. }
  175. extendedSupport = [[jsongParsed valueForKey:@"extendedSupport"] boolValue];
  176. dispatch_sync(dispatch_get_main_queue(), ^{
  177. completion(serverProductName, versionMajor, versionMicro, versionMinor, extendedSupport, nil, 0);
  178. });
  179. }
  180. }
  181. }];
  182. [task resume];
  183. }
  184. */
  185. /*
  186. - (void)downloadContentsOfUrl:(NSString *)serverUrl completion:(void(^)(NSData *data, NSString *message, NSInteger errorCode))completion
  187. {
  188. // Remove stored cookies
  189. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  190. for (NSHTTPCookie *cookie in [storage cookies])
  191. {
  192. [storage deleteCookie:cookie];
  193. }
  194. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverUrl] cachePolicy:0 timeoutInterval:20.0];
  195. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  196. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  197. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  198. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  199. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  200. dispatch_sync(dispatch_get_main_queue(), ^{
  201. if (error) {
  202. NSString *message;
  203. NSInteger errorCode;
  204. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  205. errorCode = httpResponse.statusCode;
  206. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  207. errorCode = error.code;
  208. // Error
  209. if (errorCode == 503)
  210. message = NSLocalizedString(@"_server_error_retry_", nil);
  211. else
  212. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  213. completion(nil, message, errorCode);
  214. } else {
  215. completion(data, nil, 0);
  216. }
  217. });
  218. }];
  219. [task resume];
  220. }
  221. */
  222. - (void)getAppPassword:(NSString *)serverUrl username:(NSString *)username password:(NSString *)password completion:(void(^)(NSString *token, NSString *message, NSInteger errorCode))completion
  223. {
  224. NSData *authData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
  225. NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  226. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/core/getapppassword", serverUrl];
  227. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  228. [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  229. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  230. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  231. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  232. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  233. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  234. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  235. NSInteger errorCode = 0;
  236. NSString *message = @"";
  237. NSString *token = nil;
  238. if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
  239. NCXMLGetAppPasswordParser *parser = [NCXMLGetAppPasswordParser new];
  240. [parser initParserWithData:data];
  241. token = parser.token;
  242. } else {
  243. errorCode = httpResponse.statusCode;
  244. message = [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode];
  245. }
  246. dispatch_async(dispatch_get_main_queue(), ^{
  247. completion(token, message, errorCode);
  248. });
  249. }];
  250. [task resume];
  251. }
  252. #pragma --------------------------------------------------------------------------------------------
  253. #pragma mark ===== download / upload =====
  254. #pragma --------------------------------------------------------------------------------------------
  255. - (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
  256. {
  257. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  258. if (tableAccount == nil) {
  259. completion(account, 0, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  260. } else if ([CCUtility getPassword:account].length == 0) {
  261. completion(account, 0, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  262. } else if ([CCUtility getCertificateError:account]) {
  263. completion(account, 0, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  264. }
  265. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  266. [communication setUserAgent:[CCUtility getUserAgent]];
  267. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  268. //float percent = roundf (progress.fractionCompleted * 100);
  269. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  270. int64_t totalUnitCount = 0;
  271. NSDate *date = [NSDate date];
  272. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  273. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  274. [dateFormatter setLocale:enUSPOSIXLocale];
  275. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  276. NSError *error;
  277. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  278. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  279. if(contentLength) {
  280. totalUnitCount = (int64_t) [contentLength longLongValue];
  281. }
  282. NSString *dateString = [fields objectForKey:@"Date"];
  283. if (dateString) {
  284. if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
  285. date = [NSDate date];
  286. }
  287. } else {
  288. date = [NSDate date];
  289. }
  290. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  291. if (etag == nil) {
  292. completion(account, 0, nil, nil, NSLocalizedString(@"Internal error", nil), k_CCErrorInternalError);
  293. } else {
  294. completion(account, totalUnitCount, etag, date, nil, 0);
  295. }
  296. } failureRequest:^(NSURLResponse *response, NSError *error) {
  297. NSString *message;
  298. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  299. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  300. errorCode = error.code;
  301. // Server Unauthorized
  302. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  303. [[OCNetworking sharedManager] checkRemoteUser:account function:@"download" errorCode:errorCode];
  304. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  305. [CCUtility setCertificateError:account error:YES];
  306. }
  307. // Error
  308. if (errorCode == 503)
  309. message = NSLocalizedString(@"_server_error_retry_", nil);
  310. else
  311. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  312. completion(account, 0, nil, nil, message, errorCode);
  313. }];
  314. return sessionTask;
  315. }
  316. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account url:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  317. {
  318. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  319. if (tableAccount == nil) {
  320. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  321. } else if ([CCUtility getPassword:account].length == 0) {
  322. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  323. } else if ([CCUtility getCertificateError:account]) {
  324. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  325. }
  326. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  327. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  328. [communication setUserAgent:[CCUtility getUserAgent]];
  329. NSURLSessionTask *sessionTask = [communication downloadFileSession:url toDestiny:fileNameLocalPath defaultPriority:YES encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  330. //float percent = roundf (progress.fractionCompleted * 100);
  331. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  332. completion(account, nil, 0);
  333. } failureRequest:^(NSURLResponse *response, NSError *error) {
  334. NSString *message;
  335. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  336. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  337. errorCode = error.code;
  338. // Server Unauthorized
  339. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  340. [[OCNetworking sharedManager] checkRemoteUser:account function:@"download" errorCode:errorCode];
  341. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  342. [CCUtility setCertificateError:account error:YES];
  343. }
  344. // Error
  345. if (errorCode == 503)
  346. message = NSLocalizedString(@"_server_error_retry_", nil);
  347. else
  348. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  349. completion(account, message, errorCode);
  350. }];
  351. return sessionTask;
  352. }
  353. - (NSURLSessionTask *)uploadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode communication:(OCCommunication *)communication progress:(void(^)(NSProgress *progress))uploadProgress completion:(void(^)(NSString *account, NSString *ocId, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
  354. {
  355. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  356. if (tableAccount == nil) {
  357. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  358. } else if ([CCUtility getPassword:account].length == 0) {
  359. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  360. } else if ([CCUtility getCertificateError:account]) {
  361. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  362. }
  363. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  364. [communication setUserAgent:[CCUtility getUserAgent]];
  365. NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  366. uploadProgress(progress);
  367. //float percent = roundf (progress.fractionCompleted * 100);
  368. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  369. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  370. NSString *ocId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  371. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  372. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  373. completion(account, ocId, etag, date, nil, 0);
  374. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  375. NSString *message;
  376. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  377. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  378. errorCode = error.code;
  379. // Server Unauthorized
  380. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  381. [[OCNetworking sharedManager] checkRemoteUser:account function:@"upload" errorCode:errorCode];
  382. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  383. [CCUtility setCertificateError:account error:YES];
  384. }
  385. // Error
  386. if (errorCode == 503)
  387. message = NSLocalizedString(@"_server_error_retry_", nil);
  388. else
  389. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  390. completion(account, nil, nil, nil, message, errorCode);
  391. } failureBeforeRequest:^(NSError *error) {
  392. completion(account, nil, nil, nil, error.description, error.code);
  393. }];
  394. return sessionTask;
  395. }
  396. #pragma --------------------------------------------------------------------------------------------
  397. #pragma mark ===== WebDav =====
  398. #pragma --------------------------------------------------------------------------------------------
  399. - (void)readFolderWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void(^)(NSString *account, NSArray *metadatas, tableMetadata *metadataFolder, NSString *message, NSInteger errorCode))completion
  400. {
  401. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  402. if (tableAccount == nil) {
  403. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  404. } else if ([CCUtility getPassword:account].length == 0) {
  405. completion(account, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  406. } else if ([CCUtility getCertificateError:account]) {
  407. completion(account, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  408. }
  409. NSString *url = tableAccount.url;
  410. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  411. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:url];
  412. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  413. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  414. [communication setUserAgent:[CCUtility getUserAgent]];
  415. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  416. // Check items > 0
  417. if ([items count] == 0) {
  418. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  419. completion(account, nil, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  420. } else {
  421. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  422. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  423. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  424. // directory [0]
  425. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  426. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  427. NSMutableArray *metadatas = [NSMutableArray new];
  428. tableMetadata *metadataFolder = [tableMetadata new];
  429. NSString *serverUrlFolder;
  430. // Metadata . (self Folder)
  431. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:url]]) {
  432. // root folder
  433. serverUrlFolder = k_serverUrl_root;
  434. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted];
  435. } else {
  436. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  437. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted];
  438. }
  439. // Add metadata folder
  440. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite ocId:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl richWorkspace:nil account:account];
  441. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  442. NSString *first = [(OCFileDto*)a fileName];
  443. NSString *second = [(OCFileDto*)b fileName];
  444. return [[first lowercaseString] compare:[second lowercaseString]];
  445. }];
  446. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  447. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  448. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  449. // Skip hidden files
  450. if (fileName.length > 0) {
  451. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  452. continue;
  453. } else {
  454. continue;
  455. }
  456. if (itemDto.isDirectory) {
  457. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite ocId:itemDto.ocId permissions:itemDto.permissions serverUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:fileName] richWorkspace: nil account:account];
  458. }
  459. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted]];
  460. }
  461. dispatch_async(dispatch_get_main_queue(), ^{
  462. completion(account, metadatas, metadataFolder, nil, 0);
  463. });
  464. });
  465. }
  466. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  467. NSString *message;
  468. NSInteger errorCode = response.statusCode;
  469. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  470. errorCode = error.code;
  471. // Server Unauthorized
  472. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  473. [[OCNetworking sharedManager] checkRemoteUser:account function:@"read file or folder" errorCode:errorCode];
  474. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  475. [CCUtility setCertificateError:account error:YES];
  476. }
  477. // Error
  478. if (errorCode == 503)
  479. message = NSLocalizedString(@"_server_error_retry_", nil);
  480. else
  481. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  482. completion(account, nil, nil, message, errorCode);
  483. }];
  484. }
  485. - (void)readFileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl fileName:(NSString *)fileName completion:(void(^)(NSString *account, tableMetadata *metadata, NSString *message, NSInteger errorCode))completion
  486. {
  487. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  488. if (tableAccount == nil) {
  489. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  490. } else if ([CCUtility getPassword:account].length == 0) {
  491. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  492. } else if ([CCUtility getCertificateError:account]) {
  493. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  494. }
  495. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  496. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  497. NSString *fileNamePath;
  498. if (fileName) {
  499. fileNamePath = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  500. } else {
  501. fileName= @".";
  502. fileNamePath = serverUrl;
  503. }
  504. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  505. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  506. [communication setUserAgent:[CCUtility getUserAgent]];
  507. [communication readFile:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  508. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  509. if ([items count] > 0) {
  510. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  511. tableMetadata *metadata = [tableMetadata new];
  512. OCFileDto *itemDto = [items objectAtIndex:0];
  513. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileName:fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted];
  514. dispatch_async(dispatch_get_main_queue(), ^{
  515. completion(account, metadata, nil, 0);
  516. });
  517. });
  518. // BUG 1038 item == 0
  519. } else {
  520. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  521. completion(account, nil, NSLocalizedString(@"Read File WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  522. }
  523. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  524. NSString *message;
  525. NSInteger errorCode = response.statusCode;
  526. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  527. errorCode = error.code;
  528. // Server Unauthorized
  529. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  530. [[OCNetworking sharedManager] checkRemoteUser:account function:@"read file or folder" errorCode:errorCode];
  531. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  532. [CCUtility setCertificateError:account error:YES];
  533. }
  534. // Error
  535. if (errorCode == 503)
  536. message = NSLocalizedString(@"_server_error_retry_", nil);
  537. else
  538. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  539. completion(account, nil, message, errorCode);
  540. }];
  541. }
  542. - (void)createFolderWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl fileName:(NSString *)fileName completion:(void(^)(NSString *account, NSString *ocId, NSDate *date, NSString *message, NSInteger errorCode))completion
  543. {
  544. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  545. if (tableAccount == nil) {
  546. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  547. } else if ([CCUtility getPassword:account].length == 0) {
  548. completion(account, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  549. } else if ([CCUtility getCertificateError:account]) {
  550. completion(account, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  551. }
  552. NSString *path = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  553. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  554. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  555. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  556. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  557. [communication setUserAgent:[CCUtility getUserAgent]];
  558. [communication createFolder:path onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  559. NSDictionary *fields = [response allHeaderFields];
  560. NSString *ocId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  561. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  562. completion(account, ocId, date, nil, 0);
  563. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  564. NSString *message;
  565. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  566. message = nil;
  567. else
  568. message = [CCError manageErrorOC:response.statusCode error:error];
  569. NSInteger errorCode = response.statusCode;
  570. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  571. errorCode = error.code;
  572. // Server Unauthorized
  573. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  574. [[OCNetworking sharedManager] checkRemoteUser:account function:@"create folder" errorCode:errorCode];
  575. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  576. [CCUtility setCertificateError:account error:YES];
  577. }
  578. completion(account, nil, nil, message, errorCode);
  579. } errorBeforeRequest:^(NSError *error) {
  580. NSString *message;
  581. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  582. message = nil;
  583. else {
  584. if (error.code == OCErrorForbidenCharacters)
  585. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  586. else
  587. message = NSLocalizedString(@"_unknow_response_server_", nil);
  588. }
  589. completion(account, nil, nil, message, error.code);
  590. }];
  591. }
  592. - (void)deleteFileOrFolderWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  593. {
  594. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  595. if (tableAccount == nil) {
  596. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  597. } else if ([CCUtility getPassword:account].length == 0) {
  598. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  599. } else if ([CCUtility getCertificateError:account]) {
  600. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  601. }
  602. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  603. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  604. [communication setUserAgent:[CCUtility getUserAgent]];
  605. [communication deleteFileOrFolder:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  606. completion(account, nil, 0);
  607. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  608. NSString *message;
  609. NSInteger errorCode = response.statusCode;
  610. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  611. errorCode = error.code;
  612. // Server Unauthorized
  613. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  614. [[OCNetworking sharedManager] checkRemoteUser:account function:@"delete file or folder" errorCode:errorCode];
  615. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  616. [CCUtility setCertificateError:account error:YES];
  617. }
  618. // Error
  619. if (errorCode == 503)
  620. message = NSLocalizedString(@"_server_error_retry_", nil);
  621. else
  622. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  623. completion(account, message, errorCode);
  624. }];
  625. }
  626. - (void)moveFileOrFolderWithAccount:(NSString *)account fileName:(NSString *)fileName fileNameTo:(NSString *)fileNameTo completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  627. {
  628. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  629. if (tableAccount == nil) {
  630. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  631. } else if ([CCUtility getPassword:account].length == 0) {
  632. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  633. } else if ([CCUtility getCertificateError:account]) {
  634. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  635. }
  636. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  637. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  638. [communication setUserAgent:[CCUtility getUserAgent]];
  639. [communication moveFileOrFolder:fileName toDestiny:fileNameTo onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  640. completion(account, 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. // Server Unauthorized
  647. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  648. [[OCNetworking sharedManager] checkRemoteUser:account function:@"move file or folder" errorCode:errorCode];
  649. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  650. [CCUtility setCertificateError:account error:YES];
  651. }
  652. // Error
  653. if (errorCode == 503) {
  654. message = NSLocalizedString(@"_server_error_retry_", nil);
  655. } else {
  656. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  657. }
  658. completion(account, message, errorCode);
  659. } errorBeforeRequest:^(NSError *error) {
  660. NSString *message;
  661. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  662. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  663. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  664. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  665. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  666. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  667. } else {
  668. message = NSLocalizedString(@"_unknow_response_server_", nil);
  669. }
  670. completion(account, message, error.code);
  671. }];
  672. }
  673. - (void)searchWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl contentType:(NSArray *)contentType lteDateLastModified:(NSDate *)lteDateLastModified gteDateLastModified:(NSDate *)gteDateLastModified depth:(NSString *)depth completion:(void(^)(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode))completion
  674. {
  675. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  676. if (tableAccount == nil) {
  677. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  678. } else if ([CCUtility getPassword:account].length == 0) {
  679. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  680. } else if ([CCUtility getCertificateError:account]) {
  681. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  682. }
  683. NSString *path = [tableAccount.url stringByAppendingString:k_dav];
  684. NSString *folder = [serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:tableAccount.url] withString:@""];
  685. NSString *lteDateLastModifiedString;
  686. NSString *gteDateLastModifiedString;
  687. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  688. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  689. NSString *url = tableAccount.url;
  690. NSString *userID = tableAccount.userID;
  691. if (lteDateLastModified) {
  692. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  693. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  694. [dateFormatter setLocale:enUSPOSIXLocale];
  695. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  696. lteDateLastModifiedString = [dateFormatter stringFromDate:lteDateLastModified];
  697. }
  698. if (gteDateLastModified) {
  699. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  700. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  701. [dateFormatter setLocale:enUSPOSIXLocale];
  702. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  703. gteDateLastModifiedString = [dateFormatter stringFromDate:gteDateLastModified];
  704. }
  705. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  706. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  707. [communication setUserAgent:[CCUtility getUserAgent]];
  708. [communication search:path folder:folder fileName: [NSString stringWithFormat:@"%%%@%%", fileName] depth:depth lteDateLastModified:lteDateLastModifiedString gteDateLastModified:gteDateLastModifiedString contentType:contentType withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  709. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  710. NSMutableArray *metadatas = [NSMutableArray new];
  711. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  712. for (OCFileDto *itemDto in items) {
  713. NSString *serverUrl;
  714. BOOL isFolderEncrypted;
  715. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  716. // Skip hidden files
  717. if (fileName.length > 0) {
  718. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  719. continue;
  720. } else
  721. continue;
  722. NSRange firstInstance = [itemDto.filePath rangeOfString:[NSString stringWithFormat:@"%@/files/%@", k_dav, userID]];
  723. NSString *serverPath = [itemDto.filePath substringFromIndex:firstInstance.length+firstInstance.location+1];
  724. if ([serverPath hasSuffix:@"/"]) serverPath = [serverPath substringToIndex:[serverPath length] - 1];
  725. serverUrl = [CCUtility stringAppendServerUrl:[url stringByAppendingString:k_webDAV] addFileName:serverPath];
  726. if (itemDto.isDirectory) {
  727. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite ocId:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName] richWorkspace:nil account:account];
  728. }
  729. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  730. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted]];
  731. }
  732. dispatch_async(dispatch_get_main_queue(), ^{
  733. completion(account, metadatas, nil, 0);
  734. });
  735. });
  736. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  737. NSString *message;
  738. NSInteger errorCode = response.statusCode;
  739. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  740. errorCode = error.code;
  741. // Server Unauthorized
  742. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  743. [[OCNetworking sharedManager] checkRemoteUser:account function:@"search" errorCode:errorCode];
  744. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  745. [CCUtility setCertificateError:account error:YES];
  746. }
  747. // Error
  748. if (errorCode == 503) {
  749. message = NSLocalizedString(@"_server_error_retry_", nil);
  750. } else {
  751. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  752. }
  753. completion(account, nil, message, errorCode);
  754. }];
  755. }
  756. - (void)searchWithAccount:(NSString *)account folder:(NSString *)folder fileName:(NSString *)fileName dateLastModified:(NSDate *)dateLastModified numberOfItem:(NSInteger)numberOfItem completion:(void(^)(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode))completion
  757. {
  758. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  759. if (tableAccount == nil) {
  760. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  761. } else if ([CCUtility getPassword:account].length == 0) {
  762. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  763. } else if ([CCUtility getCertificateError:account]) {
  764. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  765. }
  766. NSString *path = [tableAccount.url stringByAppendingString:k_dav];
  767. folder = [folder stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:tableAccount.url] withString:@""];
  768. fileName = [fileName stringByAppendingString:@"%"];
  769. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  770. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  771. [dateFormatter setLocale:enUSPOSIXLocale];
  772. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  773. NSString *dateLastModifiedString = [dateFormatter stringFromDate:dateLastModified];
  774. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  775. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  776. [communication setUserAgent:[CCUtility getUserAgent]];
  777. [communication search:path folder:folder fileName:fileName dateLastModified:dateLastModifiedString numberOfItem:numberOfItem withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  778. completion(account, nil, nil, 0);
  779. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  780. NSString *message;
  781. NSInteger errorCode = response.statusCode;
  782. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  783. errorCode = error.code;
  784. // Server Unauthorized
  785. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  786. [[OCNetworking sharedManager] checkRemoteUser:account function:@"search" errorCode:errorCode];
  787. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  788. [CCUtility setCertificateError:account error:YES];
  789. }
  790. // Error
  791. if (errorCode == 503) {
  792. message = NSLocalizedString(@"_server_error_retry_", nil);
  793. } else {
  794. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  795. }
  796. completion(account, nil, message, errorCode);
  797. }];
  798. }
  799. #pragma --------------------------------------------------------------------------------------------
  800. #pragma mark ===== downloadPreview =====
  801. #pragma --------------------------------------------------------------------------------------------
  802. /*
  803. - (void)downloadThumbnailWithMetadata:(tableMetadata*)metadata withWidth:(CGFloat)width andHeight:(CGFloat)height completion:(void (^)(NSString *message, NSInteger errorCode))completion
  804. {
  805. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageocId:metadata.ocId], metadata.fileNameView];
  806. if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
  807. completion(nil, 0);
  808. } else {
  809. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  810. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  811. [communication setUserAgent:[CCUtility getUserAgent]];
  812. [communication getRemoteThumbnailByServer:_activeUrl ofFilePath:[CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:metadata.serverUrl activeUrl:_activeUrl] withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  813. [thumbnail writeToFile:file atomically:YES];
  814. completion(nil, 0);
  815. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  816. NSString *message;
  817. NSInteger errorCode = response.statusCode;
  818. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  819. errorCode = error.code;
  820. // Error
  821. if (errorCode == 503)
  822. message = NSLocalizedString(@"_server_error_retry_", nil);
  823. else
  824. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  825. completion(message, errorCode);
  826. }];
  827. }
  828. }
  829. */
  830. - (void)downloadPreviewWithAccount:(NSString *)account metadata:(tableMetadata*)metadata withWidth:(CGFloat)width andHeight:(CGFloat)height completion:(void (^)(NSString *account, UIImage *image, NSString *message, NSInteger errorCode))completion
  831. {
  832. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  833. if (tableAccount == nil) {
  834. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  835. } else if ([CCUtility getPassword:account].length == 0) {
  836. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  837. } else if ([CCUtility getCertificateError:account]) {
  838. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  839. }
  840. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageOcId:metadata.ocId], metadata.fileNameView];
  841. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  842. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  843. [communication setUserAgent:[CCUtility getUserAgent]];
  844. [communication getRemotePreviewByServer:tableAccount.url ofFilePath:[CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:metadata.serverUrl activeUrl:tableAccount.url] withWidth:width andHeight:height andA:1 andMode:@"cover" path:@"" onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer) {
  845. [preview writeToFile:file atomically:YES];
  846. completion(account, [UIImage imageWithData:preview], nil, 0);
  847. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  848. NSString *message;
  849. NSInteger errorCode = response.statusCode;
  850. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  851. errorCode = error.code;
  852. // Error
  853. if (errorCode == 503)
  854. message = NSLocalizedString(@"_server_error_retry_", nil);
  855. else
  856. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  857. completion(account, nil, message, errorCode);
  858. }];
  859. }
  860. - (void)downloadPreviewWithAccount:(NSString *)account serverPath:(NSString *)serverPath fileNamePath:(NSString *)fileNamePath completion:(void (^)(NSString *account, UIImage *image, NSString *message, NSInteger errorCode))completion
  861. {
  862. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  863. if (tableAccount == nil) {
  864. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  865. } else if ([CCUtility getPassword:account].length == 0) {
  866. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  867. } else if ([CCUtility getCertificateError:account]) {
  868. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  869. }
  870. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  871. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  872. [communication setUserAgent:[CCUtility getUserAgent]];
  873. [communication getRemotePreviewByServer:tableAccount.url ofFilePath:@"" withWidth:0 andHeight:0 andA:0 andMode:@"" path:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer) {
  874. [preview writeToFile:fileNamePath atomically:YES];
  875. completion(account, [UIImage imageWithData:preview], nil, 0);
  876. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  877. NSString *message;
  878. NSInteger errorCode = response.statusCode;
  879. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  880. errorCode = error.code;
  881. // Error
  882. if (errorCode == 503)
  883. message = NSLocalizedString(@"_server_error_retry_", nil);
  884. else
  885. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  886. completion(account, nil, message, errorCode);
  887. }];
  888. }
  889. - (void)downloadPreviewTrashWithAccount:(NSString *)account fileId:(NSString *)fileId size:(NSString *)size fileName:(NSString *)fileName completion:(void (^)(NSString *account, UIImage *image, NSString *message, NSInteger errorCode))completion
  890. {
  891. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  892. if (tableAccount == nil) {
  893. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  894. } else if ([CCUtility getPassword:account].length == 0) {
  895. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  896. } else if ([CCUtility getCertificateError:account]) {
  897. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  898. }
  899. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageOcId:fileId], fileName];
  900. if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
  901. completion(account, [UIImage imageWithContentsOfFile:file], nil, 0);
  902. } else {
  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 getRemotePreviewTrashByServer:tableAccount.url ofFileId:fileId size:size onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer) {
  907. [preview writeToFile:file atomically:YES];
  908. completion(account, [UIImage imageWithData:preview], nil, 0);
  909. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  910. NSString *message;
  911. NSInteger errorCode = response.statusCode;
  912. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  913. errorCode = error.code;
  914. // Error
  915. if (errorCode == 503)
  916. message = NSLocalizedString(@"_server_error_retry_", nil);
  917. else
  918. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  919. completion(account, nil, message, errorCode);
  920. }];
  921. }
  922. }
  923. #pragma --------------------------------------------------------------------------------------------
  924. #pragma mark ===== Favorite =====
  925. #pragma --------------------------------------------------------------------------------------------
  926. - (void)settingFavoriteWithAccount:(NSString *)account fileName:(NSString *)fileName favorite:(BOOL)favorite completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  927. {
  928. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  929. if (tableAccount == nil) {
  930. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  931. } else if ([CCUtility getPassword:account].length == 0) {
  932. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  933. } else if ([CCUtility getCertificateError:account]) {
  934. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  935. }
  936. NSString *server = [tableAccount.url stringByAppendingString:k_dav];
  937. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  938. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  939. [communication setUserAgent:[CCUtility getUserAgent]];
  940. [communication settingFavoriteServer:server andFileOrFolderPath:fileName favorite:favorite withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  941. completion(account, nil, 0);
  942. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  943. NSString *message;
  944. NSInteger errorCode = response.statusCode;
  945. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  946. errorCode = error.code;
  947. // Server Unauthorized
  948. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  949. [[OCNetworking sharedManager] checkRemoteUser:account function:@"setting favorite" errorCode:errorCode];
  950. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  951. [CCUtility setCertificateError:account error:YES];
  952. }
  953. // Error
  954. if (errorCode == 503)
  955. message = NSLocalizedString(@"_server_error_retry_", nil);
  956. else
  957. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  958. completion(account, message, errorCode);
  959. }];
  960. }
  961. #pragma --------------------------------------------------------------------------------------------
  962. #pragma mark ===== Share =====
  963. #pragma --------------------------------------------------------------------------------------------
  964. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  965. {
  966. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  967. if (tableAccount == nil) {
  968. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  969. } else if ([CCUtility getPassword:account].length == 0) {
  970. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  971. } else if ([CCUtility getCertificateError:account]) {
  972. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  973. }
  974. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  975. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  976. [communication setUserAgent:[CCUtility getUserAgent]];
  977. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  978. completion(account, items, nil, 0);
  979. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  980. NSString *message;
  981. NSInteger errorCode = response.statusCode;
  982. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  983. errorCode = error.code;
  984. // Error
  985. if (errorCode == 503) {
  986. message = NSLocalizedString(@"_server_error_retry_", nil);
  987. } else {
  988. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  989. }
  990. completion(account, nil, message, errorCode);
  991. }];
  992. }
  993. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  994. {
  995. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  996. if (tableAccount == nil) {
  997. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  998. } else if ([CCUtility getPassword:account].length == 0) {
  999. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1000. } else if ([CCUtility getCertificateError:account]) {
  1001. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1002. }
  1003. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1004. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1005. [communication setUserAgent:[CCUtility getUserAgent]];
  1006. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  1007. completion(account, listOfShared, nil, 0);
  1008. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1009. NSString *message;
  1010. NSInteger errorCode = response.statusCode;
  1011. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1012. errorCode = error.code;
  1013. // Error
  1014. if (errorCode == 503) {
  1015. message = NSLocalizedString(@"_server_error_retry_", nil);
  1016. } else {
  1017. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1018. }
  1019. completion(account, nil, message, errorCode);
  1020. }];
  1021. }
  1022. - (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
  1023. {
  1024. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1025. if (tableAccount == nil) {
  1026. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1027. } else if ([CCUtility getPassword:account].length == 0) {
  1028. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1029. } else if ([CCUtility getCertificateError:account]) {
  1030. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1031. }
  1032. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1033. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1034. [communication setUserAgent:[CCUtility getUserAgent]];
  1035. [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) {
  1036. completion(account, nil, 0);
  1037. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1038. NSString *message;
  1039. NSInteger errorCode = response.statusCode;
  1040. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1041. errorCode = error.code;
  1042. // Error
  1043. if (errorCode == 503) {
  1044. message = NSLocalizedString(@"_server_error_retry_", nil);
  1045. } else {
  1046. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1047. }
  1048. completion(account, message, errorCode);
  1049. }];
  1050. }
  1051. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  1052. - (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
  1053. {
  1054. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1055. if (tableAccount == nil) {
  1056. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1057. } else if ([CCUtility getPassword:account].length == 0) {
  1058. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1059. } else if ([CCUtility getCertificateError:account]) {
  1060. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1061. }
  1062. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1063. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1064. [communication setUserAgent:[CCUtility getUserAgent]];
  1065. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1066. completion(account, nil, 0);
  1067. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1068. NSString *message;
  1069. NSInteger errorCode = response.statusCode;
  1070. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1071. errorCode = error.code;
  1072. // Error
  1073. if (errorCode == 503) {
  1074. message = NSLocalizedString(@"_server_error_retry_", nil);
  1075. } else {
  1076. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1077. }
  1078. completion(account, message, errorCode);
  1079. }];
  1080. }
  1081. - (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
  1082. {
  1083. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1084. if (tableAccount == nil) {
  1085. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1086. } else if ([CCUtility getPassword:account].length == 0) {
  1087. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1088. } else if ([CCUtility getCertificateError:account]) {
  1089. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1090. }
  1091. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1092. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1093. [communication setUserAgent:[CCUtility getUserAgent]];
  1094. [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) {
  1095. completion(account, nil, 0);
  1096. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1097. NSString *message;
  1098. NSInteger errorCode = response.statusCode;
  1099. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1100. errorCode = error.code;
  1101. // Error
  1102. if (errorCode == 503) {
  1103. message = NSLocalizedString(@"_server_error_retry_", nil);
  1104. } else {
  1105. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1106. }
  1107. completion(account, message, errorCode);
  1108. }];
  1109. }
  1110. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1111. {
  1112. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1113. if (tableAccount == nil) {
  1114. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1115. } else if ([CCUtility getPassword:account].length == 0) {
  1116. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1117. } else if ([CCUtility getCertificateError:account]) {
  1118. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1119. }
  1120. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1121. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1122. [communication setUserAgent:[CCUtility getUserAgent]];
  1123. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1124. completion(account, nil, 0);
  1125. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1126. NSString *message;
  1127. NSInteger errorCode = response.statusCode;
  1128. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1129. errorCode = error.code;
  1130. // Error
  1131. if (errorCode == 503) {
  1132. message = NSLocalizedString(@"_server_error_retry_", nil);
  1133. } else {
  1134. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1135. }
  1136. completion(account, message, errorCode);
  1137. }];
  1138. }
  1139. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  1140. {
  1141. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1142. if (tableAccount == nil) {
  1143. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1144. } else if ([CCUtility getPassword:account].length == 0) {
  1145. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1146. } else if ([CCUtility getCertificateError:account]) {
  1147. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1148. }
  1149. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1150. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1151. [communication setUserAgent:[CCUtility getUserAgent]];
  1152. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1153. completion(account, itemList, nil, 0);
  1154. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1155. NSString *message;
  1156. NSInteger errorCode = response.statusCode;
  1157. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1158. errorCode = error.code;
  1159. // Error
  1160. if (errorCode == 503) {
  1161. message = NSLocalizedString(@"_server_error_retry_", nil);
  1162. } else {
  1163. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1164. }
  1165. completion(account, nil, message, errorCode);
  1166. }];
  1167. }
  1168. #pragma --------------------------------------------------------------------------------------------
  1169. #pragma mark ===== VAR =====
  1170. #pragma --------------------------------------------------------------------------------------------
  1171. - (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
  1172. {
  1173. BOOL previews = false;
  1174. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1175. if (tableAccount == nil) {
  1176. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1177. } else if ([CCUtility getPassword:account].length == 0) {
  1178. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1179. } else if ([CCUtility getCertificateError:account]) {
  1180. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1181. }
  1182. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:account];
  1183. if (capabilities != nil && capabilities.versionMajor >= k_nextcloud_version_15_0) {
  1184. previews = true;
  1185. }
  1186. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1187. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1188. [communication setUserAgent:[CCUtility getUserAgent]];
  1189. [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) {
  1190. completion(account, listOfActivity, nil, 0);
  1191. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1192. NSString *message;
  1193. NSInteger errorCode = response.statusCode;
  1194. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1195. errorCode = error.code;
  1196. // Server Unauthorized
  1197. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1198. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get activity" errorCode:errorCode];
  1199. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1200. [CCUtility setCertificateError:account error:YES];
  1201. }
  1202. // Error
  1203. if (errorCode == 503)
  1204. message = NSLocalizedString(@"_server_error_retry_", nil);
  1205. else
  1206. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1207. completion(account, nil, message, errorCode);
  1208. }];
  1209. }
  1210. - (void)getExternalSitesWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  1211. {
  1212. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1213. if (tableAccount == nil) {
  1214. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1215. } else if ([CCUtility getPassword:account].length == 0) {
  1216. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1217. } else if ([CCUtility getCertificateError:account]) {
  1218. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1219. }
  1220. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1221. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1222. [communication setUserAgent:[CCUtility getUserAgent]];
  1223. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1224. completion(account, listOfExternalSites, nil, 0);
  1225. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1226. NSString *message;
  1227. NSInteger errorCode = response.statusCode;
  1228. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1229. errorCode = error.code;
  1230. // Server Unauthorized
  1231. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1232. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get external sites" errorCode:errorCode];
  1233. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1234. [CCUtility setCertificateError:account error:YES];
  1235. }
  1236. // Error
  1237. if (errorCode == 503) {
  1238. message = NSLocalizedString(@"_server_error_retry_", nil);
  1239. } else {
  1240. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1241. }
  1242. completion(account, nil, message, errorCode);
  1243. }];
  1244. }
  1245. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  1246. {
  1247. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1248. if (tableAccount == nil) {
  1249. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1250. } else if ([CCUtility getPassword:account].length == 0) {
  1251. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1252. } else if ([CCUtility getCertificateError:account]) {
  1253. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1254. }
  1255. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1256. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1257. [communication setUserAgent:[CCUtility getUserAgent]];
  1258. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1259. completion(account, listOfNotifications, nil, 0);
  1260. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1261. NSString *message;
  1262. NSInteger errorCode = response.statusCode;
  1263. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1264. errorCode = error.code;
  1265. // Server Unauthorized
  1266. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1267. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get notification" errorCode:errorCode];
  1268. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1269. [CCUtility setCertificateError:account error:YES];
  1270. }
  1271. // Error
  1272. if (errorCode == 503) {
  1273. message = NSLocalizedString(@"_server_error_retry_", nil);
  1274. } else {
  1275. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1276. }
  1277. completion(account, nil, message, errorCode);
  1278. }];
  1279. }
  1280. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1281. {
  1282. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1283. if (tableAccount == nil) {
  1284. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1285. } else if ([CCUtility getPassword:account].length == 0) {
  1286. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1287. } else if ([CCUtility getCertificateError:account]) {
  1288. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1289. }
  1290. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1291. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1292. [communication setUserAgent:[CCUtility getUserAgent]];
  1293. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1294. completion(account, nil, 0);
  1295. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1296. NSString *message;
  1297. NSInteger errorCode = response.statusCode;
  1298. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1299. errorCode = error.code;
  1300. // Server Unauthorized
  1301. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1302. [[OCNetworking sharedManager] checkRemoteUser:account function:@"set notification" errorCode:errorCode];
  1303. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1304. [CCUtility setCertificateError:account error:YES];
  1305. }
  1306. // Error
  1307. if (errorCode == 503) {
  1308. message = NSLocalizedString(@"_server_error_retry_", nil);
  1309. } else {
  1310. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1311. }
  1312. completion(account, message, errorCode);
  1313. }];
  1314. }
  1315. - (void)getCapabilitiesWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCCapabilities *capabilities, NSString *message, NSInteger errorCode))completion
  1316. {
  1317. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1318. if (tableAccount == nil) {
  1319. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1320. } else if ([CCUtility getPassword:account].length == 0) {
  1321. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1322. } else if ([CCUtility getCertificateError:account]) {
  1323. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1324. }
  1325. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1326. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1327. [communication setUserAgent:[CCUtility getUserAgent]];
  1328. [communication getCapabilitiesOfServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1329. completion(account, capabilities, nil, 0);
  1330. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1331. NSString *message;
  1332. NSInteger errorCode = response.statusCode;
  1333. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1334. errorCode = error.code;
  1335. // Server Unauthorized
  1336. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1337. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get capabilities" errorCode:errorCode];
  1338. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1339. [CCUtility setCertificateError:account error:YES];
  1340. }
  1341. // Error
  1342. if (errorCode == 503) {
  1343. message = NSLocalizedString(@"_server_error_retry_", nil);
  1344. } else {
  1345. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1346. }
  1347. completion(account, nil, message, errorCode);
  1348. }];
  1349. }
  1350. - (void)getUserProfileWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1351. {
  1352. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1353. if (tableAccount == nil) {
  1354. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1355. } else if ([CCUtility getPassword:account].length == 0) {
  1356. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1357. } else if ([CCUtility getCertificateError:account]) {
  1358. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1359. }
  1360. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1361. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1362. [communication setUserAgent:[CCUtility getUserAgent]];
  1363. [communication getUserProfileServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1364. completion(account, userProfile, nil, 0);
  1365. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1366. NSString *message;
  1367. NSInteger errorCode = response.statusCode;
  1368. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1369. errorCode = error.code;
  1370. // Server Unauthorized
  1371. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1372. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get user profile" errorCode:errorCode];
  1373. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1374. [CCUtility setCertificateError:account error:YES];
  1375. }
  1376. // Error
  1377. if (errorCode == 503) {
  1378. message = NSLocalizedString(@"_server_error_retry_", nil);
  1379. } else {
  1380. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1381. }
  1382. completion(account, nil, message, errorCode);
  1383. }];
  1384. }
  1385. #pragma --------------------------------------------------------------------------------------------
  1386. #pragma mark ===== Push Notification =====
  1387. #pragma --------------------------------------------------------------------------------------------
  1388. - (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
  1389. {
  1390. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1391. if (tableAccount == nil) {
  1392. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1393. } else if ([CCUtility getPassword:account].length == 0) {
  1394. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1395. } else if ([CCUtility getCertificateError:account]) {
  1396. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1397. }
  1398. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  1399. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  1400. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  1401. #ifdef DEBUG
  1402. // proxyServerPath = @"http://127.0.0.1:8088";
  1403. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  1404. #endif
  1405. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1406. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1407. [communication setUserAgent:[CCUtility getUserAgent]];
  1408. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1409. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  1410. signature = [CCUtility URLEncodeStringFromString:signature];
  1411. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  1412. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1413. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  1414. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1415. NSString *message;
  1416. NSInteger errorCode = response.statusCode;
  1417. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1418. errorCode = error.code;
  1419. // Error
  1420. if (errorCode == 503)
  1421. message = NSLocalizedString(@"_server_error_retry_", nil);
  1422. else
  1423. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1424. completion(account, nil, nil, nil, message, errorCode);
  1425. }];
  1426. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1427. NSString *message;
  1428. NSInteger errorCode = response.statusCode;
  1429. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1430. errorCode = error.code;
  1431. // Error
  1432. if (errorCode == 503)
  1433. message = NSLocalizedString(@"_server_error_retry_", nil);
  1434. else
  1435. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1436. completion(account, nil, nil, nil, message, errorCode);
  1437. }];
  1438. }
  1439. - (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 {
  1440. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1441. if (tableAccount == nil) {
  1442. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1443. } else if ([CCUtility getPassword:account].length == 0) {
  1444. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1445. } else if ([CCUtility getCertificateError:account]) {
  1446. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1447. }
  1448. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  1449. #ifdef DEBUG
  1450. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  1451. #endif
  1452. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1453. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1454. [communication setUserAgent:[CCUtility getUserAgent]];
  1455. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1456. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1457. completion(account, nil, 0);
  1458. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1459. NSString *message;
  1460. NSInteger errorCode = response.statusCode;
  1461. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1462. errorCode = error.code;
  1463. // Error
  1464. if (errorCode == 503)
  1465. message = NSLocalizedString(@"_server_error_retry_", nil);
  1466. else
  1467. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1468. completion(account, message, errorCode);
  1469. }];
  1470. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1471. NSString *message;
  1472. NSInteger errorCode = response.statusCode;
  1473. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1474. errorCode = error.code;
  1475. // Error
  1476. if (errorCode == 503)
  1477. message = NSLocalizedString(@"_server_error_retry_", nil);
  1478. else
  1479. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1480. completion(account, message, errorCode);
  1481. }];
  1482. }
  1483. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  1484. {
  1485. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  1486. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  1487. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  1488. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1489. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1490. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  1491. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  1492. if (error) {
  1493. NSString *message;
  1494. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  1495. NSInteger errorCode = httpResponse.statusCode;
  1496. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1497. errorCode = error.code;
  1498. // Error
  1499. if (errorCode == 503)
  1500. message = NSLocalizedString(@"_server_error_retry_", nil);
  1501. else
  1502. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1503. completion(nil, message, errorCode);
  1504. } else {
  1505. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  1506. completion(jsongParsed, nil, 0);
  1507. }
  1508. }];
  1509. [task resume];
  1510. }
  1511. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  1512. {
  1513. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  1514. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  1515. // Delete
  1516. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  1517. // Delete-all
  1518. if (notificationId == 0) {
  1519. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  1520. }
  1521. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  1522. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  1523. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  1524. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1525. [request setHTTPMethod: @"DELETE"];
  1526. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1527. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  1528. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  1529. if (error) {
  1530. NSString *message;
  1531. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  1532. NSInteger errorCode = httpResponse.statusCode;
  1533. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1534. errorCode = error.code;
  1535. // Error
  1536. if (errorCode == 503)
  1537. message = NSLocalizedString(@"_server_error_retry_", nil);
  1538. else
  1539. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1540. completion(message, errorCode);
  1541. } else {
  1542. completion(nil, 0);
  1543. }
  1544. }];
  1545. [task resume];
  1546. }
  1547. #pragma --------------------------------------------------------------------------------------------
  1548. #pragma mark ===== Manage Mobile Editor OCS API =====
  1549. #pragma --------------------------------------------------------------------------------------------
  1550. - (void)createLinkRichdocumentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1551. {
  1552. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1553. if (tableAccount == nil) {
  1554. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1555. } else if ([CCUtility getPassword:account].length == 0) {
  1556. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1557. } else if ([CCUtility getCertificateError:account]) {
  1558. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1559. }
  1560. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1561. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1562. [communication setUserAgent:[CCUtility getUserAgent]];
  1563. [communication createLinkRichdocuments:[tableAccount.url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  1564. completion(account, link, nil, 0);
  1565. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1566. NSString *message;
  1567. NSInteger errorCode = response.statusCode;
  1568. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1569. errorCode = error.code;
  1570. // Error
  1571. if (errorCode == 503)
  1572. message = NSLocalizedString(@"_server_error_retry_", nil);
  1573. else
  1574. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1575. completion(account, nil,message, errorCode);
  1576. }];
  1577. }
  1578. - (void)getTemplatesRichdocumentsWithAccount:(NSString *)account typeTemplate:(NSString *)typeTemplate completion:(void(^)(NSString *account, NSArray *listOfTemplate, NSString *message, NSInteger errorCode))completion
  1579. {
  1580. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1581. if (tableAccount == nil) {
  1582. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1583. } else if ([CCUtility getPassword:account].length == 0) {
  1584. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1585. } else if ([CCUtility getCertificateError:account]) {
  1586. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1587. }
  1588. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1589. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1590. [communication setUserAgent:[CCUtility getUserAgent]];
  1591. [communication getTemplatesRichdocuments:[tableAccount.url stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  1592. completion(account, listOfTemplate, nil, 0);
  1593. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1594. NSString *message;
  1595. NSInteger errorCode = response.statusCode;
  1596. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1597. errorCode = error.code;
  1598. // Error
  1599. if (errorCode == 503)
  1600. message = NSLocalizedString(@"_server_error_retry_", nil);
  1601. else
  1602. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1603. completion(account, nil, message, errorCode);
  1604. }];
  1605. }
  1606. - (void)createNewRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID completion:(void(^)(NSString *account, NSString *url, NSString *message, NSInteger errorCode))completion
  1607. {
  1608. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1609. if (tableAccount == nil) {
  1610. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1611. } else if ([CCUtility getPassword:account].length == 0) {
  1612. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1613. } else if ([CCUtility getCertificateError:account]) {
  1614. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1615. }
  1616. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1617. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1618. [communication setUserAgent:[CCUtility getUserAgent]];
  1619. [communication createNewRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1620. completion(account, url, nil, 0);
  1621. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1622. NSString *message;
  1623. NSInteger errorCode = response.statusCode;
  1624. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1625. errorCode = error.code;
  1626. // Error
  1627. if (errorCode == 503)
  1628. message = NSLocalizedString(@"_server_error_retry_", nil);
  1629. else
  1630. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1631. completion(account, nil, message, errorCode);
  1632. }];
  1633. }
  1634. - (void)createAssetRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1635. {
  1636. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1637. if (tableAccount == nil) {
  1638. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1639. } else if ([CCUtility getPassword:account].length == 0) {
  1640. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1641. } else if ([CCUtility getCertificateError:account]) {
  1642. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1643. }
  1644. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:tableAccount.url];
  1645. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1646. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1647. [communication setUserAgent:[CCUtility getUserAgent]];
  1648. [communication createAssetRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1649. completion(account, url, nil, 0);
  1650. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1651. NSString *message;
  1652. NSInteger errorCode = response.statusCode;
  1653. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1654. errorCode = error.code;
  1655. // Error
  1656. if (errorCode == 503)
  1657. message = NSLocalizedString(@"_server_error_retry_", nil);
  1658. else
  1659. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1660. completion(account, nil, message, errorCode);
  1661. }];
  1662. }
  1663. #pragma --------------------------------------------------------------------------------------------
  1664. #pragma mark ===== Full Text Search =====
  1665. #pragma --------------------------------------------------------------------------------------------
  1666. - (void)fullTextSearchWithAccount:(NSString *)account text:(NSString *)text page:(NSInteger)page completion:(void(^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1667. {
  1668. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1669. if (tableAccount == nil) {
  1670. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1671. } else if ([CCUtility getPassword:account].length == 0) {
  1672. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1673. } else if ([CCUtility getCertificateError:account]) {
  1674. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1675. }
  1676. // Create JSON
  1677. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  1678. [dataDic setValue:@"files" forKey:@"providers"];
  1679. [dataDic setValue:text forKey:@"search"];
  1680. [dataDic setValue:[NSNumber numberWithInteger:page] forKey:@"page"];
  1681. [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"size"];
  1682. NSMutableDictionary *options = [NSMutableDictionary new];
  1683. [options setValue:@"" forKey:@"files_within_dir"];
  1684. [options setValue:@"" forKey:@"files_local"];
  1685. [options setValue:@"" forKey:@"files_extension"];
  1686. [dataDic setValue:options forKey:@"options"];
  1687. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1688. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1689. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1690. [communication setUserAgent:[CCUtility getUserAgent]];
  1691. [communication fullTextSearch:[tableAccount.url stringByAppendingString:@"/"] data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1692. completion(account, items, nil, 0);
  1693. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1694. NSString *message;
  1695. NSInteger errorCode = response.statusCode;
  1696. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1697. errorCode = error.code;
  1698. // Error
  1699. if (errorCode == 503)
  1700. message = NSLocalizedString(@"_server_error_retry_", nil);
  1701. else
  1702. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1703. completion(account, nil, message, errorCode);
  1704. }];
  1705. }
  1706. #pragma --------------------------------------------------------------------------------------------
  1707. #pragma mark ===== Check remote user =====
  1708. #pragma --------------------------------------------------------------------------------------------
  1709. - (void)checkRemoteUser:(NSString *)account function:(NSString *)function errorCode:(NSInteger)errorCode
  1710. {
  1711. #ifndef EXTENSION
  1712. @synchronized(self) {
  1713. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1714. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:account];
  1715. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1716. NSString *token = [CCUtility getPassword:account];
  1717. if (self.checkRemoteUserInProgress || tableAccount == nil || token == nil) {
  1718. return;
  1719. } else {
  1720. self.checkRemoteUserInProgress = true;
  1721. }
  1722. if (capabilities != nil && capabilities.versionMajor >= k_nextcloud_version_17_0) {
  1723. [[OCNetworking sharedManager] getRemoteWipeStatusWithAccount:account token:token completion:^(NSString *account, BOOL wipe, NSString *message, NSInteger errorCode) {
  1724. if (wipe) {
  1725. [appDelegate deleteAccount:account wipe:true];
  1726. [[NCContentPresenter shared] messageNotification:tableAccount.user description:@"_wipe_account_" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  1727. [[OCNetworking sharedManager] setRemoteWipeCompletitionWithUser:tableAccount.user userID:tableAccount.userID url:tableAccount.url token:token completion:^(NSString *message, NSInteger errorCode) {
  1728. NSLog(@"Wiped");
  1729. }];
  1730. } else {
  1731. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && [appDelegate.reachability isReachable]) {
  1732. [[NCContentPresenter shared] messageNotification:@"_error_" description:[NSString stringWithFormat:@"During the function request: %@ the server responded with error %ld password re-entry is required", function, (long)errorCode] delay:k_dismissAfterSecond*2 type:messageTypeError errorCode:errorCode];
  1733. [CCUtility setPassword:account password:nil];
  1734. }
  1735. }
  1736. self.checkRemoteUserInProgress = false;
  1737. }];
  1738. } else if ([CCUtility getPassword:account] != nil) {
  1739. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && [appDelegate.reachability isReachable]) {
  1740. [[NCContentPresenter shared] messageNotification:@"_error_" description:[NSString stringWithFormat:@"During the function request: %@ the server responded with error %ld password re-entry is required", function, (long)errorCode] delay:k_dismissAfterSecond*2 type:messageTypeError errorCode:errorCode];
  1741. [CCUtility setPassword:account password:nil];
  1742. }
  1743. }
  1744. }
  1745. #endif
  1746. }
  1747. - (void)getRemoteWipeStatusWithAccount:(NSString *)account token:(NSString *)token completion:(void(^)(NSString *account, BOOL wipe, NSString *message, NSInteger errorCode))completion
  1748. {
  1749. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1750. if (tableAccount == nil) {
  1751. completion(account, false, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1752. } else if ([CCUtility getPassword:account].length == 0) {
  1753. completion(account, false, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1754. } else if ([CCUtility getCertificateError:account]) {
  1755. completion(account, false, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1756. }
  1757. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1758. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1759. [communication setUserAgent:[CCUtility getUserAgent]];
  1760. [communication getRemoteWipeStatus:tableAccount.url token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, BOOL wipe, NSString *redirectedServer) {
  1761. completion(account, wipe, nil, 0);
  1762. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1763. NSString *message;
  1764. NSInteger errorCode = response.statusCode;
  1765. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1766. errorCode = error.code;
  1767. // Server Unauthorized
  1768. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1769. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get remote wipe status" errorCode:errorCode];
  1770. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1771. [CCUtility setCertificateError:account error:YES];
  1772. }
  1773. // Error
  1774. if (errorCode == 503)
  1775. message = NSLocalizedString(@"_server_error_retry_", nil);
  1776. else
  1777. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1778. completion(account, false, message, errorCode);
  1779. }];
  1780. }
  1781. - (void)setRemoteWipeCompletitionWithUser:(NSString *)user userID:(NSString *)userID url:(NSString *)url token:(NSString *)token completion:(void(^)(NSString *message, NSInteger errorCode))completion
  1782. {
  1783. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1784. [communication setCredentialsWithUser:user andUserID:userID andPassword:token];
  1785. [communication setUserAgent:[CCUtility getUserAgent]];
  1786. [communication setRemoteWipeCompletition:url token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1787. completion(nil, 0);
  1788. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1789. NSString *message;
  1790. NSInteger errorCode = response.statusCode;
  1791. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1792. errorCode = error.code;
  1793. // Error
  1794. if (errorCode == 503)
  1795. message = NSLocalizedString(@"_server_error_retry_", nil);
  1796. else
  1797. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1798. completion(message, errorCode);
  1799. }];
  1800. }
  1801. #pragma --------------------------------------------------------------------------------------------
  1802. #pragma mark ===== Trash =====
  1803. #pragma --------------------------------------------------------------------------------------------
  1804. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1805. {
  1806. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1807. if (tableAccount == nil) {
  1808. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1809. } else if ([CCUtility getPassword:account].length == 0) {
  1810. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1811. } else if ([CCUtility getCertificateError:account]) {
  1812. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1813. }
  1814. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1815. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1816. [communication setUserAgent:[CCUtility getUserAgent]];
  1817. [communication listingTrash:[serverUrl stringByAppendingString:path] depth:depth onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1818. // Check items > 0
  1819. if ([items count] == 0) {
  1820. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  1821. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  1822. } else {
  1823. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1824. NSMutableArray *listTrash = [NSMutableArray new];
  1825. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  1826. if ([items count] > 1) {
  1827. for (NSUInteger i=1; i < [items count]; i++) {
  1828. OCFileDto *itemDto = [items objectAtIndex:i];
  1829. tableTrash *trash = [tableTrash new];
  1830. trash.account = account;
  1831. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  1832. trash.directory = itemDto.isDirectory;
  1833. trash.fileId = itemDto.fileId;
  1834. trash.fileName = itemDto.fileName;
  1835. NSArray *array = [itemDto.filePath componentsSeparatedByString:path];
  1836. long len = [[array objectAtIndex:0] length];
  1837. trash.filePath = [itemDto.filePath substringFromIndex:len];
  1838. trash.hasPreview = itemDto.hasPreview;
  1839. trash.size = itemDto.size;
  1840. trash.trashbinFileName = itemDto.trashbinFileName;
  1841. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  1842. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  1843. [CCUtility insertTypeFileIconName:trash.trashbinFileName metadata:(tableMetadata *)trash];
  1844. [listTrash addObject:trash];
  1845. }
  1846. }
  1847. dispatch_async(dispatch_get_main_queue(), ^{
  1848. completion(account, listTrash, nil, 0);
  1849. });
  1850. });
  1851. }
  1852. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1853. NSString *message;
  1854. NSInteger errorCode = response.statusCode;
  1855. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1856. errorCode = error.code;
  1857. // Error
  1858. if (errorCode == 503)
  1859. message = NSLocalizedString(@"_server_error_retry_", nil);
  1860. else
  1861. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1862. completion(account, nil,message, errorCode);
  1863. }];
  1864. }
  1865. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1866. {
  1867. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1868. if (tableAccount == nil) {
  1869. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1870. } else if ([CCUtility getPassword:account].length == 0) {
  1871. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1872. } else if ([CCUtility getCertificateError:account]) {
  1873. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1874. }
  1875. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  1876. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1877. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1878. [communication setUserAgent:[CCUtility getUserAgent]];
  1879. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1880. completion(account, nil, 0);
  1881. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1882. NSString *message;
  1883. NSInteger errorCode = response.statusCode;
  1884. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1885. errorCode = error.code;
  1886. // Error
  1887. if (errorCode == 503)
  1888. message = NSLocalizedString(@"_server_error_retry_", nil);
  1889. else
  1890. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1891. completion(account, message, errorCode);
  1892. }];
  1893. }
  1894. #pragma --------------------------------------------------------------------------------------------
  1895. #pragma mark ===== Comments =====
  1896. #pragma --------------------------------------------------------------------------------------------
  1897. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1898. {
  1899. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1900. if (tableAccount == nil) {
  1901. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1902. } else if ([CCUtility getPassword:account].length == 0) {
  1903. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1904. } else if ([CCUtility getCertificateError:account]) {
  1905. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1906. }
  1907. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1908. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1909. [communication setUserAgent:[CCUtility getUserAgent]];
  1910. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  1911. completion(account, list, nil, 0);
  1912. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1913. NSString *message;
  1914. NSInteger errorCode = response.statusCode;
  1915. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1916. errorCode = error.code;
  1917. // Error
  1918. if (errorCode == 503)
  1919. message = NSLocalizedString(@"_server_error_retry_", nil);
  1920. else
  1921. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1922. completion(account, nil,message, errorCode);
  1923. }];
  1924. }
  1925. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1926. {
  1927. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1928. if (tableAccount == nil) {
  1929. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1930. } else if ([CCUtility getPassword:account].length == 0) {
  1931. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1932. } else if ([CCUtility getCertificateError:account]) {
  1933. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1934. }
  1935. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1936. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1937. [communication setUserAgent:[CCUtility getUserAgent]];
  1938. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1939. completion(account, nil, 0);
  1940. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1941. NSString *message;
  1942. NSInteger errorCode = response.statusCode;
  1943. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1944. errorCode = error.code;
  1945. // Error
  1946. if (errorCode == 503)
  1947. message = NSLocalizedString(@"_server_error_retry_", nil);
  1948. else
  1949. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1950. completion(account, message, errorCode);
  1951. }];
  1952. }
  1953. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1954. {
  1955. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1956. if (tableAccount == nil) {
  1957. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1958. } else if ([CCUtility getPassword:account].length == 0) {
  1959. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1960. } else if ([CCUtility getCertificateError:account]) {
  1961. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1962. }
  1963. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1964. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1965. [communication setUserAgent:[CCUtility getUserAgent]];
  1966. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1967. completion(account, nil, 0);
  1968. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1969. NSString *message;
  1970. NSInteger errorCode = response.statusCode;
  1971. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1972. errorCode = error.code;
  1973. // Error
  1974. if (errorCode == 503)
  1975. message = NSLocalizedString(@"_server_error_retry_", nil);
  1976. else
  1977. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1978. completion(account, message, errorCode);
  1979. }];
  1980. }
  1981. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1982. {
  1983. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1984. if (tableAccount == nil) {
  1985. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1986. } else if ([CCUtility getPassword:account].length == 0) {
  1987. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1988. } else if ([CCUtility getCertificateError:account]) {
  1989. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1990. }
  1991. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1992. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1993. [communication setUserAgent:[CCUtility getUserAgent]];
  1994. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1995. completion(account, nil, 0);
  1996. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1997. NSString *message;
  1998. NSInteger errorCode = response.statusCode;
  1999. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  2000. errorCode = error.code;
  2001. // Error
  2002. if (errorCode == 503)
  2003. message = NSLocalizedString(@"_server_error_retry_", nil);
  2004. else
  2005. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  2006. completion(account, message, errorCode);
  2007. }];
  2008. }
  2009. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  2010. {
  2011. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  2012. if (tableAccount == nil) {
  2013. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  2014. } else if ([CCUtility getPassword:account].length == 0) {
  2015. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  2016. } else if ([CCUtility getCertificateError:account]) {
  2017. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  2018. }
  2019. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  2020. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  2021. [communication setUserAgent:[CCUtility getUserAgent]];
  2022. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  2023. completion(account, nil, 0);
  2024. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  2025. NSString *message;
  2026. NSInteger errorCode = response.statusCode;
  2027. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  2028. errorCode = error.code;
  2029. // Error
  2030. if (errorCode == 503)
  2031. message = NSLocalizedString(@"_server_error_retry_", nil);
  2032. else
  2033. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  2034. completion(account, message, errorCode);
  2035. }];
  2036. }
  2037. #pragma --------------------------------------------------------------------------------------------
  2038. #pragma mark ===== Third Parts =====
  2039. #pragma --------------------------------------------------------------------------------------------
  2040. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  2041. {
  2042. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  2043. if (tableAccount == nil) {
  2044. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  2045. } else if ([CCUtility getPassword:account].length == 0) {
  2046. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  2047. } else if ([CCUtility getCertificateError:account]) {
  2048. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  2049. }
  2050. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  2051. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  2052. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  2053. [communication setUserAgent:[CCUtility getUserAgent]];
  2054. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  2055. completion(account, userProfile, nil, 0);
  2056. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  2057. NSString *message;
  2058. NSInteger errorCode = response.statusCode;
  2059. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  2060. errorCode = error.code;
  2061. // Server Unauthorized
  2062. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  2063. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get user profile" errorCode:errorCode];
  2064. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  2065. [CCUtility setCertificateError:account error:YES];
  2066. }
  2067. // Error
  2068. if (errorCode == 503)
  2069. message = NSLocalizedString(@"_server_error_retry_", nil);
  2070. else
  2071. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  2072. completion(account, nil,message, errorCode);
  2073. }];
  2074. }
  2075. - (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
  2076. {
  2077. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  2078. if (tableAccount == nil) {
  2079. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  2080. } else if ([CCUtility getPassword:account].length == 0) {
  2081. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  2082. } else if ([CCUtility getCertificateError:account]) {
  2083. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  2084. }
  2085. // Create JSON
  2086. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  2087. if (address) [dataDic setValue:address forKey:@"address"];
  2088. if (businesssize) {
  2089. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  2090. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  2091. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  2092. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  2093. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  2094. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  2095. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  2096. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  2097. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  2098. }
  2099. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  2100. if (city) [dataDic setValue:city forKey:@"city"];
  2101. if (company) [dataDic setValue:company forKey:@"company"];
  2102. if (country) [dataDic setValue:country forKey:@"country"];
  2103. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  2104. if (email) [dataDic setValue:email forKey:@"email"];
  2105. if (phone) [dataDic setValue:phone forKey:@"phone"];
  2106. if (role_) [dataDic setValue:role_ forKey:@"role"];
  2107. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  2108. if (website) [dataDic setValue:website forKey:@"website"];
  2109. if (zip) [dataDic setValue:zip forKey:@"zip"];
  2110. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  2111. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  2112. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  2113. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  2114. [communication setUserAgent:[CCUtility getUserAgent]];
  2115. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  2116. completion(account, nil, 0);
  2117. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  2118. NSString *message;
  2119. NSInteger errorCode = response.statusCode;
  2120. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  2121. errorCode = error.code;
  2122. // Server Unauthorized
  2123. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  2124. [[OCNetworking sharedManager] checkRemoteUser:account function:@"put user profile" errorCode:errorCode];
  2125. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  2126. [CCUtility setCertificateError:account error:YES];
  2127. }
  2128. // Error
  2129. if (errorCode == 503)
  2130. message = NSLocalizedString(@"_server_error_retry_", nil);
  2131. else
  2132. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  2133. completion(account, message, errorCode);
  2134. }];
  2135. }
  2136. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  2137. {
  2138. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  2139. if (tableAccount == nil) {
  2140. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  2141. } else if ([CCUtility getPassword:account].length == 0) {
  2142. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  2143. } else if ([CCUtility getCertificateError:account]) {
  2144. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  2145. }
  2146. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  2147. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  2148. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  2149. [communication setUserAgent:[CCUtility getUserAgent]];
  2150. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  2151. completion(account, features, nil, 0);
  2152. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  2153. NSString *message;
  2154. NSInteger errorCode = response.statusCode;
  2155. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  2156. errorCode = error.code;
  2157. // Server Unauthorized
  2158. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  2159. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get features" errorCode:errorCode];
  2160. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  2161. [CCUtility setCertificateError:account error:YES];
  2162. }
  2163. // Error
  2164. if (errorCode == 503)
  2165. message = NSLocalizedString(@"_server_error_retry_", nil);
  2166. else
  2167. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  2168. completion(account, nil,message, errorCode);
  2169. }];
  2170. }
  2171. /*
  2172. - (void)middlewarePing
  2173. {
  2174. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  2175. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  2176. [communication setUserAgent:[CCUtility getUserAgent]];
  2177. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  2178. [self complete];
  2179. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  2180. NSInteger errorCode = response.statusCode;
  2181. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  2182. errorCode = error.code;
  2183. // Error
  2184. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  2185. if (errorCode == 503)
  2186. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  2187. else
  2188. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  2189. }
  2190. // Request trusted certificated
  2191. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  2192. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  2193. [self complete];
  2194. }];
  2195. }
  2196. */
  2197. #pragma --------------------------------------------------------------------------------------------
  2198. #pragma mark ===== didReceiveChallenge =====
  2199. #pragma --------------------------------------------------------------------------------------------
  2200. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  2201. {
  2202. // The pinnning check
  2203. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  2204. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  2205. } else {
  2206. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  2207. }
  2208. }
  2209. @end
  2210. #pragma --------------------------------------------------------------------------------------------
  2211. #pragma mark ===== OCURLSessionManager =====
  2212. #pragma --------------------------------------------------------------------------------------------
  2213. @implementation OCURLSessionManager
  2214. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  2215. {
  2216. // The pinnning check
  2217. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  2218. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  2219. } else {
  2220. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  2221. }
  2222. }
  2223. @end