OCNetworking.m 162 KB

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