OCCommunication.m 176 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397
  1. //
  2. // OCCommunication.m
  3. // Owncloud iOs Client
  4. //
  5. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. // Add : getNotificationServer & setNotificationServer
  25. // Add : getUserProfileServer
  26. // Add : Support for Favorite
  27. // Add : getActivityServer
  28. //
  29. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  30. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  31. //
  32. #import "OCCommunication.h"
  33. #import "OCHTTPRequestOperation.h"
  34. #import "UtilsFramework.h"
  35. #import "OCXMLParser.h"
  36. #import "OCXMLSharedParser.h"
  37. #import "OCXMLServerErrorsParser.h"
  38. #import "OCXMLListParser.h"
  39. #import "NSString+Encode.h"
  40. #import "OCFrameworkConstants.h"
  41. #import "OCWebDAVClient.h"
  42. #import "OCXMLShareByLinkParser.h"
  43. #import "OCErrorMsg.h"
  44. #import "OCShareUser.h"
  45. #import "OCActivity.h"
  46. #import "OCExternalSites.h"
  47. #import "OCCapabilities.h"
  48. #import "OCNotifications.h"
  49. #import "OCNotificationsAction.h"
  50. #import "OCRichObjectStrings.h"
  51. #import "OCUserProfile.h"
  52. #import "NCRichDocumentTemplate.h"
  53. #import "HCFeatures.h"
  54. @interface OCCommunication ()
  55. @property (nonatomic, strong) NSString *currentServerVersion;
  56. @end
  57. @implementation OCCommunication
  58. -(id) init {
  59. self = [super init];
  60. if (self) {
  61. //Init the Donwload queue array
  62. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  63. //Credentials not set yet
  64. self.kindOfCredential = credentialNotSet;
  65. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  66. self.isCookiesAvailable = YES;
  67. self.isForbiddenCharactersAvailable = NO;
  68. #ifdef UNIT_TEST
  69. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  70. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  71. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  72. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  73. #else
  74. //Network Upload queue for NSURLSession (iOS 7)
  75. NSURLSessionConfiguration *uploadConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_session_name];
  76. uploadConfiguration.HTTPShouldUsePipelining = YES;
  77. uploadConfiguration.HTTPMaximumConnectionsPerHost = 1;
  78. uploadConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  79. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:uploadConfiguration];
  80. [self.uploadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  81. //Network Download queue for NSURLSession (iOS 7)
  82. NSURLSessionConfiguration *downConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_download_session_name];
  83. downConfiguration.HTTPShouldUsePipelining = YES;
  84. downConfiguration.HTTPMaximumConnectionsPerHost = 1;
  85. downConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  86. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:downConfiguration];
  87. [self.downloadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  88. //Network Download queue for NSURLSession (iOS 7)
  89. NSURLSessionConfiguration *networkConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  90. networkConfiguration.HTTPShouldUsePipelining = YES;
  91. networkConfiguration.HTTPMaximumConnectionsPerHost = 1;
  92. networkConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  93. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:networkConfiguration];
  94. [self.networkSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  95. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  96. #endif
  97. }
  98. return self;
  99. }
  100. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager {
  101. self = [super init];
  102. if (self) {
  103. //Init the Donwload queue array
  104. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  105. self.isCookiesAvailable = YES;
  106. self.isForbiddenCharactersAvailable = NO;
  107. //Credentials not set yet
  108. self.kindOfCredential = credentialNotSet;
  109. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  110. self.uploadSessionManager = uploadSessionManager;
  111. }
  112. return self;
  113. }
  114. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager andDownloadSessionManager:(AFURLSessionManager *) downloadSessionManager andNetworkSessionManager:(AFURLSessionManager *) networkSessionManager {
  115. self = [super init];
  116. if (self) {
  117. //Init the Donwload queue array
  118. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  119. //Credentials not set yet
  120. self.kindOfCredential = credentialNotSet;
  121. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  122. self.uploadSessionManager = uploadSessionManager;
  123. self.downloadSessionManager = downloadSessionManager;
  124. self.networkSessionManager = networkSessionManager;
  125. }
  126. return self;
  127. }
  128. - (AFSecurityPolicy *) createSecurityPolicy {
  129. return [AFSecurityPolicy defaultPolicy];
  130. }
  131. - (void)setSecurityPolicyManagers:(AFSecurityPolicy *)securityPolicy {
  132. self.securityPolicy = securityPolicy;
  133. self.uploadSessionManager.securityPolicy = securityPolicy;
  134. self.downloadSessionManager.securityPolicy = securityPolicy;
  135. }
  136. #pragma mark - Setting Credentials
  137. - (void) setCredentialsWithUser:(NSString*) user andUserID:(NSString *) userID andPassword:(NSString*) password {
  138. self.kindOfCredential = credentialNormal;
  139. self.user = user;
  140. self.userID = userID;
  141. self.password = password;
  142. }
  143. - (void) setCredentialsWithCookie:(NSString*) cookie {
  144. self.kindOfCredential = credentialCookie;
  145. self.password = cookie;
  146. }
  147. - (void) setCredentialsOauthWithToken:(NSString*) token {
  148. self.kindOfCredential = credentialOauth;
  149. self.password = token;
  150. }
  151. ///-----------------------------------
  152. /// @name getRequestWithCredentials
  153. ///-----------------------------------
  154. /**
  155. * Method to return the request with the right credential
  156. *
  157. * @param OCWebDAVClient like a dinamic typed
  158. *
  159. * @return OCWebDAVClient like a dinamic typed
  160. *
  161. */
  162. - (id) getRequestWithCredentials:(id) request {
  163. if ([request isKindOfClass:[NSMutableURLRequest class]]) {
  164. NSMutableURLRequest *myRequest = (NSMutableURLRequest *)request;
  165. switch (self.kindOfCredential) {
  166. case credentialNotSet:
  167. //Without credentials
  168. break;
  169. case credentialNormal:
  170. {
  171. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.user, self.password];
  172. [myRequest addValue:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString:basicAuthCredentials]] forHTTPHeaderField:@"Authorization"];
  173. break;
  174. }
  175. case credentialCookie:
  176. NSLog(@"Cookie: %@", self.password);
  177. [myRequest addValue:self.password forHTTPHeaderField:@"Cookie"];
  178. break;
  179. case credentialOauth:
  180. [myRequest addValue:[NSString stringWithFormat:@"Bearer %@", self.password] forHTTPHeaderField:@"Authorization"];
  181. break;
  182. default:
  183. break;
  184. }
  185. if (self.userAgent) {
  186. [myRequest addValue:self.userAgent forHTTPHeaderField:@"User-Agent"];
  187. }
  188. return myRequest;
  189. } else if([request isKindOfClass:[OCWebDAVClient class]]) {
  190. OCWebDAVClient *myRequest = (OCWebDAVClient *)request;
  191. switch (self.kindOfCredential) {
  192. case credentialNotSet:
  193. //Without credentials
  194. break;
  195. case credentialNormal:
  196. [myRequest setAuthorizationHeaderWithUsername:self.user password:self.password];
  197. break;
  198. case credentialCookie:
  199. [myRequest setAuthorizationHeaderWithCookie:self.password];
  200. break;
  201. case credentialOauth:
  202. [myRequest setAuthorizationHeaderWithToken:[NSString stringWithFormat:@"Bearer %@", self.password]];
  203. break;
  204. default:
  205. break;
  206. }
  207. if (self.userAgent) {
  208. [myRequest setUserAgent:self.userAgent];
  209. }
  210. return request;
  211. } else {
  212. NSLog(@"We do not know witch kind of object is");
  213. return request;
  214. }
  215. }
  216. #pragma mark - WebDav network Operations
  217. ///-----------------------------------
  218. /// @name Check Server
  219. ///-----------------------------------
  220. - (void) checkServer: (NSString *) path
  221. onCommunication:(OCCommunication *)sharedOCCommunication
  222. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  223. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  224. OCWebDAVClient *request = [OCWebDAVClient new];
  225. request = [self getRequestWithCredentials:request];
  226. if (self.userAgent) {
  227. [request setUserAgent:self.userAgent];
  228. }
  229. path = [path encodeString:NSUTF8StringEncoding];
  230. [request checkServer:path onCommunication:sharedOCCommunication
  231. success:^(NSHTTPURLResponse *response, id responseObject) {
  232. if (successRequest) {
  233. successRequest(response, request.redirectedServer);
  234. }
  235. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  236. failureRequest(response, error, request.redirectedServer);
  237. }];
  238. }
  239. ///-----------------------------------
  240. /// @name Create a folder
  241. ///-----------------------------------
  242. - (void) createFolder: (NSString *) path
  243. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  244. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  245. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  246. errorBeforeRequest:(void(^)(NSError *error)) errorBeforeRequest {
  247. if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:path] withForbiddenCharactersSupported:isFCSupported]) {
  248. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorForbidenCharacters];
  249. errorBeforeRequest(error);
  250. } else {
  251. OCWebDAVClient *request = [OCWebDAVClient new];
  252. request = [self getRequestWithCredentials:request];
  253. path = [path encodeString:NSUTF8StringEncoding];
  254. [request makeCollection:path onCommunication:sharedOCCommunication
  255. success:^(NSHTTPURLResponse *response, id responseObject) {
  256. if (successRequest) {
  257. successRequest(response, request.redirectedServer);
  258. }
  259. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  260. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  261. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  262. if (err) {
  263. failureRequest(response, err, request.redirectedServer);
  264. }else{
  265. failureRequest(response, error, request.redirectedServer);
  266. }
  267. }];
  268. }];
  269. }
  270. }
  271. ///-----------------------------------
  272. /// @name Move a file or a folder
  273. ///-----------------------------------
  274. - (void) moveFileOrFolder:(NSString *)sourcePath
  275. toDestiny:(NSString *)destinyPath
  276. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  277. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectServer))successRequest
  278. failureRequest:(void (^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest
  279. errorBeforeRequest:(void (^)(NSError *error))errorBeforeRequest {
  280. if ([UtilsFramework isTheSameFileOrFolderByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  281. //We check that we are not trying to move the file to the same place
  282. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingTheDestinyAndOriginAreTheSame];
  283. errorBeforeRequest(error);
  284. } else if ([UtilsFramework isAFolderUnderItByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  285. //We check we are not trying to move a folder inside himself
  286. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingFolderInsideHimself];
  287. errorBeforeRequest(error);
  288. } else if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:destinyPath] withForbiddenCharactersSupported:isFCSupported]) {
  289. //We check that we are making a move not a rename to prevent special characters problems
  290. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingDestinyNameHaveForbiddenCharacters];
  291. errorBeforeRequest(error);
  292. } else {
  293. sourcePath = [sourcePath encodeString:NSUTF8StringEncoding];
  294. destinyPath = [destinyPath encodeString:NSUTF8StringEncoding];
  295. OCWebDAVClient *request = [OCWebDAVClient new];
  296. request = [self getRequestWithCredentials:request];
  297. [request movePath:sourcePath toPath:destinyPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  298. if (successRequest) {
  299. successRequest(response, request.redirectedServer);
  300. }
  301. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  302. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  303. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  304. if (err) {
  305. failureRequest(response, err, request.redirectedServer);
  306. }else{
  307. failureRequest(response, error, request.redirectedServer);
  308. }
  309. }];
  310. }];
  311. }
  312. }
  313. ///-----------------------------------
  314. /// @name Delete a file or a folder
  315. ///-----------------------------------
  316. - (void) deleteFileOrFolder:(NSString *)path
  317. onCommunication:(OCCommunication *)sharedOCCommunication
  318. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  319. failureRquest:(void (^)(NSHTTPURLResponse *resposne, NSError *error, NSString *redirectedServer))failureRequest {
  320. path = [path encodeString:NSUTF8StringEncoding];
  321. OCWebDAVClient *request = [OCWebDAVClient new];
  322. request = [self getRequestWithCredentials:request];
  323. [request deletePath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  324. if (successRequest) {
  325. successRequest(response, request.redirectedServer);
  326. }
  327. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  328. failureRequest(response, error, request.redirectedServer);
  329. }];
  330. }
  331. ///-----------------------------------
  332. /// @name Read folder
  333. ///-----------------------------------
  334. - (void) readFolder: (NSString *) path depth:(NSString *)depth withUserSessionToken:(NSString *)token
  335. onCommunication:(OCCommunication *)sharedOCCommunication
  336. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest
  337. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  338. if (!token){
  339. token = @"no token";
  340. }
  341. path = [path encodeString:NSUTF8StringEncoding];
  342. OCWebDAVClient *request = [OCWebDAVClient new];
  343. request = [self getRequestWithCredentials:request];
  344. [request listPath:path depth:depth onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  345. if (successRequest) {
  346. NSData *responseData = (NSData*) responseObject;
  347. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  348. // NSLog(@"newStrReadFolder: %@", newStr);
  349. OCXMLParser *parser = [[OCXMLParser alloc]init];
  350. [parser initParserWithData:responseData];
  351. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  352. //Return success
  353. successRequest(response, directoryList, request.redirectedServer, token);
  354. }
  355. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  356. NSLog(@"Failure");
  357. failureRequest(response, error, token, request.redirectedServer);
  358. }];
  359. }
  360. ///-----------------------------------
  361. /// @name Download File Session
  362. ///-----------------------------------
  363. - (NSURLSessionDownloadTask *) downloadFileSession:(NSString *)remotePath toDestiny:(NSString *)localPath defaultPriority:(BOOL)defaultPriority onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))downloadProgress successRequest:(void(^)(NSURLResponse *response, NSURL *filePath)) successRequest failureRequest:(void(^)(NSURLResponse *response, NSError *error)) failureRequest {
  364. OCWebDAVClient *request = [OCWebDAVClient new];
  365. request = [self getRequestWithCredentials:request];
  366. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  367. NSURLSessionDownloadTask *downloadTask = [request downloadWithSessionPath:remotePath toPath:localPath defaultPriority:defaultPriority onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  368. downloadProgress(progress);
  369. } success:^(NSURLResponse *response, NSURL *filePath) {
  370. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  371. successRequest(response,filePath);
  372. } failure:^(NSURLResponse *response, NSError *error) {
  373. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  374. failureRequest(response,error);
  375. }];
  376. return downloadTask;
  377. }
  378. ///-----------------------------------
  379. /// @name Set Download Task Complete Block
  380. ///-----------------------------------
  381. - (void)setDownloadTaskComleteBlock: (NSURL * (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block{
  382. [self.downloadSessionManager setDownloadTaskDidFinishDownloadingBlock:block];
  383. }
  384. ///-----------------------------------
  385. /// @name Set Download Task Did Get Body Data Block
  386. ///-----------------------------------
  387. - (void) setDownloadTaskDidGetBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)) block{
  388. [self.downloadSessionManager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  389. block(session,downloadTask,bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);
  390. }];
  391. }
  392. ///-----------------------------------
  393. /// @name Upload File Session
  394. ///-----------------------------------
  395. - (NSURLSessionUploadTask *) uploadFileSession:(NSString *) localPath toDestiny:(NSString *) remotePath onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))uploadProgress successRequest:(void(^)(NSURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSURLResponse *response, NSString *redirectedServer, NSError *error)) failureRequest failureBeforeRequest:(void(^)(NSError *error)) failureBeforeRequest {
  396. OCWebDAVClient *request = [OCWebDAVClient new];
  397. request = [self getRequestWithCredentials:request];
  398. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  399. NSURLSessionUploadTask *uploadTask = [request putWithSessionLocalPath:localPath atRemotePath:remotePath onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  400. uploadProgress(progress);
  401. } success:^(NSURLResponse *response, id responseObjec){
  402. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  403. //TODO: The second parameter is the redirected server
  404. successRequest(response, @"");
  405. } failure:^(NSURLResponse *response, id responseObject, NSError *error) {
  406. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  407. //TODO: The second parameter is the redirected server
  408. NSData *responseData = (NSData*) responseObject;
  409. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  410. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  411. if (err) {
  412. failureRequest(response, @"", err);
  413. }else{
  414. failureRequest(response, @"", error);
  415. }
  416. }];
  417. } failureBeforeRequest:^(NSError *error) {
  418. failureBeforeRequest(error);
  419. }];
  420. return uploadTask;
  421. }
  422. ///-----------------------------------
  423. /// @name Set Task Did Complete Block
  424. ///-----------------------------------
  425. - (void) setTaskDidCompleteBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, NSError *error)) block{
  426. [self.uploadSessionManager setTaskDidCompleteBlock:^(NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  427. block(session, task, error);
  428. }];
  429. }
  430. ///-----------------------------------
  431. /// @name Set Task Did Send Body Data Block
  432. ///-----------------------------------
  433. - (void) setTaskDidSendBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend)) block{
  434. [self.uploadSessionManager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
  435. block(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend);
  436. }];
  437. }
  438. ///-----------------------------------
  439. /// @name Read File
  440. ///-----------------------------------
  441. - (void) readFile: (NSString *) path
  442. onCommunication:(OCCommunication *)sharedOCCommunication
  443. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer)) successRequest
  444. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  445. path = [path encodeString:NSUTF8StringEncoding];
  446. OCWebDAVClient *request = [OCWebDAVClient new];
  447. request = [self getRequestWithCredentials:request];
  448. [request propertiesOfPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  449. if (successRequest) {
  450. NSData *responseData = (NSData*) responseObject;
  451. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  452. // NSLog(@"newStrReadFile: %@", newStr);
  453. OCXMLParser *parser = [[OCXMLParser alloc]init];
  454. [parser initParserWithData:responseData];
  455. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  456. //Return success
  457. successRequest(response, directoryList, request.redirectedServer);
  458. }
  459. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  460. failureRequest(response, error, request.redirectedServer);
  461. }];
  462. }
  463. ///-----------------------------------
  464. /// @name search
  465. ///-----------------------------------
  466. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName depth:(NSString *)depth lteDateLastModified:(NSString *)lteDateLastModified gteDateLastModified:(NSString *)gteDateLastModified contentType:(NSArray *)contentType withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  467. if (!token){
  468. token = @"no token";
  469. }
  470. path = [path encodeString:NSUTF8StringEncoding];
  471. OCWebDAVClient *request = [OCWebDAVClient new];
  472. request = [self getRequestWithCredentials:request];
  473. [request search:path folder:folder fileName:fileName depth:depth lteDateLastModified:lteDateLastModified gteDateLastModified:gteDateLastModified contentType:contentType user:_user userID:_userID onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  474. if (successRequest) {
  475. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  476. NSData *responseData = (NSData*) responseObject;
  477. OCXMLListParser *parser = [OCXMLListParser new];
  478. [parser initParserWithData:responseData];
  479. NSMutableArray *searchList = [parser.searchList mutableCopy];
  480. //Return success
  481. dispatch_async(dispatch_get_main_queue(), ^{
  482. successRequest(response, searchList, request.redirectedServer, token);
  483. });
  484. });
  485. }
  486. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  487. failureRequest(response, error, token, request.redirectedServer);
  488. }];
  489. }
  490. ///-----------------------------------
  491. /// @name Setting favorite
  492. ///-----------------------------------
  493. - (void)settingFavoriteServer:(NSString *)path andFileOrFolderPath:(NSString *)filePath favorite:(BOOL)favorite withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest {
  494. if (!token){
  495. token = @"no token";
  496. }
  497. path = [NSString stringWithFormat:@"%@/files/%@/%@", path, _userID, filePath];
  498. path = [path encodeString:NSUTF8StringEncoding];
  499. OCWebDAVClient *request = [OCWebDAVClient new];
  500. request = [self getRequestWithCredentials:request];
  501. [request settingFavorite:path favorite:favorite onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  502. if (successRequest) {
  503. //Return success
  504. successRequest(response, request.redirectedServer, token);
  505. }
  506. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  507. NSLog(@"Failure");
  508. failureRequest(response, error, token, request.redirectedServer);
  509. }];
  510. }
  511. ///-----------------------------------
  512. /// @name Listing favorites
  513. ///-----------------------------------
  514. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  515. if (!token){
  516. token = @"no token";
  517. }
  518. path = [path encodeString:NSUTF8StringEncoding];
  519. OCWebDAVClient *request = [OCWebDAVClient new];
  520. request = [self getRequestWithCredentials:request];
  521. [request listingFavorites:path folder:folder user:_user userID:_userID onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  522. if (successRequest) {
  523. NSData *responseData = (NSData*) responseObject;
  524. OCXMLListParser *parser = [OCXMLListParser new];
  525. [parser initParserWithData:responseData];
  526. NSMutableArray *searchList = [parser.searchList mutableCopy];
  527. //Return success
  528. successRequest(response, searchList, request.redirectedServer, token);
  529. }
  530. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  531. failureRequest(response, error, token, request.redirectedServer);
  532. }];
  533. }
  534. #pragma mark - OC/NC API Calls
  535. - (NSString *) getCurrentServerVersion {
  536. return self.currentServerVersion;
  537. }
  538. - (void) getServerVersionWithPath:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  539. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *serverVersion, NSString *redirectedServer)) success
  540. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  541. OCWebDAVClient *request = [OCWebDAVClient new];
  542. if (self.userAgent) {
  543. [request setUserAgent:self.userAgent];
  544. }
  545. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  546. NSData *data = (NSData*) responseObject;
  547. NSString *versionString = [NSString new];
  548. NSError* error=nil;
  549. if (data) {
  550. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
  551. if(error) {
  552. NSLog(@"Error parsing JSON: %@", error);
  553. } else {
  554. //Obtain the server version from the version field
  555. versionString = [jsonArray valueForKey:@"version"];
  556. self.currentServerVersion = versionString;
  557. }
  558. } else {
  559. NSLog(@"Error parsing JSON: data is null");
  560. }
  561. success(response, versionString, request.redirectedServer);
  562. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  563. failure(response, error, request.redirectedServer);
  564. }];
  565. }
  566. ///-----------------------------------
  567. /// @name Get UserName by cookie
  568. ///-----------------------------------
  569. - (void) getUserNameByCookie:(NSString *) cookieString ofServerPath:(NSString *)path onCommunication:
  570. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *response, NSData *responseData, NSString *redirectedServer))success
  571. failure:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failure{
  572. OCWebDAVClient *request = [OCWebDAVClient new];
  573. request = [self getRequestWithCredentials:request];
  574. [request requestUserNameOfServer: path byCookie:cookieString onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  575. success(response, responseObject, request.redirectedServer);
  576. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  577. failure(response, error, request.redirectedServer);
  578. }];
  579. }
  580. - (void) getFeaturesSupportedByServer:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  581. successRequest:(void(^)(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer)) success
  582. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  583. OCWebDAVClient *request = [OCWebDAVClient new];
  584. if (self.userAgent) {
  585. [request setUserAgent:self.userAgent];
  586. }
  587. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  588. if (responseObject) {
  589. NSError* error = nil;
  590. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: (NSData*) responseObject options: NSJSONReadingMutableContainers error: &error];
  591. if(error) {
  592. // NSLog(@"Error parsing JSON: %@", error);
  593. failure(response, error, request.redirectedServer);
  594. }else{
  595. self.currentServerVersion = [jsonArray valueForKey:@"version"];
  596. BOOL hasShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_shared];
  597. BOOL hasShareeSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_sharee_api];
  598. BOOL hasCookiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_cookies];
  599. BOOL hasForbiddenCharactersSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_forbidden_characters];
  600. BOOL hasCapabilitiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_capabilities];
  601. BOOL hasFedSharesOptionShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_share_option_fed_share];
  602. success(response, hasShareSupport, hasShareeSupport, hasCookiesSupport, hasForbiddenCharactersSupport, hasCapabilitiesSupport, hasFedSharesOptionShareSupport, request.redirectedServer);
  603. }
  604. } else {
  605. // NSLog(@"Error parsing JSON: data is null");
  606. failure(response, nil, request.redirectedServer);
  607. }
  608. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  609. failure(response, error, request.redirectedServer);
  610. }];
  611. }
  612. #pragma mark - Share
  613. - (void) readSharedByServer: (NSString *) path
  614. onCommunication:(OCCommunication *)sharedOCCommunication
  615. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  616. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  617. path = [path encodeString:NSUTF8StringEncoding];
  618. path = [path stringByAppendingString:k_url_acces_shared_api];
  619. OCWebDAVClient *request = [OCWebDAVClient new];
  620. request = [self getRequestWithCredentials:request];
  621. [request listSharedByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  622. if (successRequest) {
  623. NSData *responseData = (NSData*) responseObject;
  624. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  625. //NSLog(@"response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
  626. [parser initParserWithData:responseData];
  627. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  628. //Return success
  629. successRequest(response, sharedList, request.redirectedServer);
  630. }
  631. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  632. failureRequest(response, error, request.redirectedServer);
  633. }];
  634. }
  635. - (void) readSharedByServer: (NSString *) serverPath andPath: (NSString *) path
  636. onCommunication:(OCCommunication *)sharedOCCommunication
  637. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  638. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  639. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  640. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  641. path = [path encodeString:NSUTF8StringEncoding];
  642. OCWebDAVClient *request = [OCWebDAVClient new];
  643. request = [self getRequestWithCredentials:request];
  644. [request listSharedByServer:serverPath andPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  645. if (successRequest) {
  646. NSData *responseData = (NSData*) responseObject;
  647. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  648. // NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  649. // NSLog(@"responseDataReadSharedByServer:andPath: %@", str);
  650. // NSLog(@"pathFolders: %@", path);
  651. // NSLog(@"serverPath: %@", serverPath);
  652. [parser initParserWithData:responseData];
  653. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  654. //Return success
  655. successRequest(response, sharedList, request.redirectedServer);
  656. }
  657. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  658. failureRequest(response, error, request.redirectedServer);
  659. }];
  660. }
  661. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  662. onCommunication:(OCCommunication *)sharedOCCommunication
  663. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer)) successRequest
  664. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  665. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  666. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  667. OCWebDAVClient *request = [OCWebDAVClient new];
  668. request = [self getRequestWithCredentials:request];
  669. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath andPassword:password andPermission:permission andHideDownload:hideDownload onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  670. NSData *responseData = (NSData*) responseObject;
  671. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  672. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  673. [parser initParserWithData:responseData];
  674. switch (parser.statusCode) {
  675. case kOCSharedAPISuccessful:
  676. {
  677. NSString *url = parser.url;
  678. NSString *token = parser.token;
  679. if (url != nil) {
  680. successRequest(response, url, request.redirectedServer);
  681. }else if (token != nil){
  682. //We remove the \n and the empty spaces " "
  683. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  684. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  685. successRequest(response, token, request.redirectedServer);
  686. }else{
  687. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  688. failureRequest(response, error, request.redirectedServer);
  689. }
  690. break;
  691. }
  692. default:
  693. {
  694. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  695. failureRequest(response, error, request.redirectedServer);
  696. }
  697. }
  698. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  699. failureRequest(response, error, request.redirectedServer);
  700. }];
  701. }
  702. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath
  703. onCommunication:(OCCommunication *)sharedOCCommunication
  704. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *shareLink, NSString *redirectedServer)) successRequest
  705. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  706. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  707. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  708. OCWebDAVClient *request = [OCWebDAVClient new];
  709. request = [self getRequestWithCredentials:request];
  710. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  711. NSData *responseData = (NSData*) responseObject;
  712. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  713. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  714. [parser initParserWithData:responseData];
  715. switch (parser.statusCode) {
  716. case kOCSharedAPISuccessful:
  717. {
  718. NSString *url = parser.url;
  719. NSString *token = parser.token;
  720. if (url != nil) {
  721. successRequest(response, url, request.redirectedServer);
  722. }else if (token != nil){
  723. //We remove the \n and the empty spaces " "
  724. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  725. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  726. successRequest(response, token, request.redirectedServer);
  727. }else{
  728. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  729. failureRequest(response, error, request.redirectedServer);
  730. }
  731. break;
  732. }
  733. default:
  734. {
  735. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  736. failureRequest(response, error, request.redirectedServer);
  737. }
  738. }
  739. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  740. failureRequest(response, error, request.redirectedServer);
  741. }];
  742. }
  743. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andFileOrFolderPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  744. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  745. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest{
  746. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  747. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  748. userOrGroup = [userOrGroup encodeString:NSUTF8StringEncoding];
  749. OCWebDAVClient *request = [OCWebDAVClient new];
  750. request = [self getRequestWithCredentials:request];
  751. [request shareWith:userOrGroup shareeType:shareeType inServer:serverPath andPath:filePath andPermissions:permissions onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  752. NSData *responseData = (NSData*) responseObject;
  753. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  754. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  755. [parser initParserWithData:responseData];
  756. switch (parser.statusCode) {
  757. case kOCSharedAPISuccessful:
  758. {
  759. successRequest(response, request.redirectedServer);
  760. break;
  761. }
  762. default:
  763. {
  764. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  765. failureRequest(response, error, request.redirectedServer);
  766. }
  767. }
  768. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  769. failureRequest(response, error, request.redirectedServer);
  770. }];
  771. }
  772. - (void) unShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  773. onCommunication:(OCCommunication *)sharedOCCommunication
  774. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  775. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  776. path = [path encodeString:NSUTF8StringEncoding];
  777. path = [path stringByAppendingString:k_url_acces_shared_api];
  778. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  779. OCWebDAVClient *request = [OCWebDAVClient new];
  780. request = [self getRequestWithCredentials:request];
  781. [request unShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  782. if (successRequest) {
  783. //Return success
  784. successRequest(response, request.redirectedServer);
  785. }
  786. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  787. failureRequest(response, error, request.redirectedServer);
  788. }];
  789. }
  790. - (void) isShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  791. onCommunication:(OCCommunication *)sharedOCCommunication
  792. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, BOOL isShared, id shareDto)) successRequest
  793. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  794. path = [path encodeString:NSUTF8StringEncoding];
  795. path = [path stringByAppendingString:k_url_acces_shared_api];
  796. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  797. OCWebDAVClient *request = [OCWebDAVClient new];
  798. request = [self getRequestWithCredentials:request];
  799. [request isShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  800. if (successRequest) {
  801. NSData *responseData = (NSData*) responseObject;
  802. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  803. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  804. [parser initParserWithData:responseData];
  805. BOOL isShared = NO;
  806. OCSharedDto *shareDto = nil;
  807. if (parser.shareList) {
  808. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  809. if ([sharedList count] > 0) {
  810. isShared = YES;
  811. shareDto = [sharedList objectAtIndex:0];
  812. }
  813. }
  814. //Return success
  815. successRequest(response, request.redirectedServer, isShared, shareDto);
  816. }
  817. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  818. failureRequest(response, error, request.redirectedServer);
  819. }];
  820. }
  821. - (void) updateShare:(NSInteger)shareId ofServerPath:(NSString *)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  822. onCommunication:(OCCommunication *)sharedOCCommunication
  823. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  824. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  825. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  826. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  827. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)shareId]];
  828. OCWebDAVClient *request = [OCWebDAVClient new];
  829. request = [self getRequestWithCredentials:request];
  830. [request updateShareItem:shareId ofServerPath:serverPath withPasswordProtect:password andExpirationTime:expirationTime andPermissions:permissions andHideDownload:hideDownload onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  831. NSData *responseData = (NSData*) responseObject;
  832. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  833. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  834. [parser initParserWithData:responseData];
  835. switch (parser.statusCode) {
  836. case kOCSharedAPISuccessful:
  837. {
  838. successRequest(response, request.redirectedServer);
  839. break;
  840. }
  841. default:
  842. {
  843. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  844. failureRequest(response, error, request.redirectedServer);
  845. }
  846. }
  847. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  848. failureRequest(response, error, request.redirectedServer);
  849. }];
  850. }
  851. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  852. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  853. serverPath = [serverPath stringByAppendingString:k_url_access_sharee_api];
  854. searchString = [searchString encodeString:NSUTF8StringEncoding];
  855. OCWebDAVClient *request = [OCWebDAVClient new];
  856. request = [self getRequestWithCredentials:request];
  857. [request searchUsersAndGroupsWith:searchString forPage:page with:resultsPerPage ofServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  858. NSData *responseData = (NSData*) responseObject;
  859. NSMutableArray *itemList = [NSMutableArray new];
  860. //Parse
  861. NSError *error;
  862. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  863. if (error == nil) {
  864. NSDictionary *ocsDict = [jsongParsed valueForKey:@"ocs"];
  865. NSDictionary *metaDict = [ocsDict valueForKey:@"meta"];
  866. NSInteger statusCode = [[metaDict valueForKey:@"statuscode"] integerValue];
  867. if (statusCode == kOCShareeAPISuccessful || statusCode == kOCSharedAPISuccessful) {
  868. NSDictionary *dataDict = [ocsDict valueForKey:@"data"];
  869. NSArray *exactDict = [dataDict valueForKey:@"exact"];
  870. NSArray *usersFounded = [dataDict valueForKey:@"users"];
  871. NSArray *groupsFounded = [dataDict valueForKey:@"groups"];
  872. NSArray *usersRemote = [dataDict valueForKey:@"remotes"];
  873. NSArray *usersExact = [exactDict valueForKey:@"users"];
  874. NSArray *groupsExact = [exactDict valueForKey:@"groups"];
  875. NSArray *remotesExact = [exactDict valueForKey:@"remotes"];
  876. [self addUserItemOfType:shareTypeUser fromArray:usersFounded ToList:itemList];
  877. [self addUserItemOfType:shareTypeUser fromArray:usersExact ToList:itemList];
  878. [self addUserItemOfType:shareTypeRemote fromArray:usersRemote ToList:itemList];
  879. [self addUserItemOfType:shareTypeRemote fromArray:remotesExact ToList:itemList];
  880. [self addGroupItemFromArray:groupsFounded ToList:itemList];
  881. [self addGroupItemFromArray:groupsExact ToList:itemList];
  882. }else{
  883. NSString *message = (NSString*)[metaDict objectForKey:@"message"];
  884. if ([message isKindOfClass:[NSNull class]]) {
  885. message = @"";
  886. }
  887. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  888. failureRequest(response, error, request.redirectedServer);
  889. }
  890. //Return success
  891. successRequest(response, itemList, request.redirectedServer);
  892. }
  893. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  894. failureRequest(response, error, request.redirectedServer);
  895. }];
  896. }
  897. - (void)getSharePermissionsFile:(NSString *)fileName onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  898. fileName = [fileName encodeString:NSUTF8StringEncoding];
  899. OCWebDAVClient *request = [OCWebDAVClient new];
  900. request = [self getRequestWithCredentials:request];
  901. [request getSharePermissionsFile:fileName onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  902. if (successRequest) {
  903. NSString *permissions;
  904. NSData *responseData = (NSData *)responseObject;
  905. NSString *newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  906. NSLog(@"newStrReadFolder: %@", newStr);
  907. OCXMLParser *parser = [[OCXMLParser alloc]init];
  908. [parser initParserWithData:responseData];
  909. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  910. if ([directoryList count] == 1) {
  911. OCFileDto *file = [directoryList objectAtIndex:0];
  912. permissions = file.permissions;
  913. }
  914. //Return success
  915. successRequest(response, permissions, request.redirectedServer);
  916. }
  917. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  918. failureRequest(response, error, request.redirectedServer);
  919. }];
  920. }
  921. #pragma mark - Capabilities
  922. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  923. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  924. serverPath = [serverPath stringByAppendingString:k_url_capabilities];
  925. OCWebDAVClient *request = [OCWebDAVClient new];
  926. request = [self getRequestWithCredentials:request];
  927. [request getCapabilitiesOfServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  928. NSData *responseData = (NSData*) responseObject;
  929. OCCapabilities *capabilities = [OCCapabilities new];
  930. //Parse
  931. NSError *error;
  932. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  933. NSLog(@"dic: %@",jsongParsed);
  934. if (jsongParsed && jsongParsed.allKeys > 0) {
  935. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  936. NSDictionary *data = [ocs valueForKey:@"data"];
  937. NSDictionary *version = [data valueForKey:@"version"];
  938. if (ocs.count > 0 && data.count > 0 && version.count > 0) {
  939. //VERSION
  940. NSNumber *versionMajorNumber = (NSNumber*) [version valueForKey:@"major"];
  941. NSNumber *versionMinorNumber = (NSNumber*) [version valueForKey:@"minor"];
  942. NSNumber *versionMicroNumber = (NSNumber*) [version valueForKey:@"micro"];
  943. capabilities.versionMajor = versionMajorNumber.integerValue;
  944. capabilities.versionMinor = versionMinorNumber.integerValue;
  945. capabilities.versionMicro = versionMicroNumber.integerValue;
  946. capabilities.versionString = (NSString*)[version valueForKey:@"string"];
  947. capabilities.versionEdition = (NSString*)[version valueForKey:@"edition"];
  948. NSDictionary *capabilitiesDict = [data valueForKey:@"capabilities"];
  949. NSDictionary *core = [capabilitiesDict valueForKey:@"core"];
  950. //CORE
  951. NSNumber *corePollIntervalNumber = (NSNumber*)[core valueForKey:@"pollinterval"];
  952. capabilities.corePollInterval = corePollIntervalNumber.integerValue;
  953. NSDictionary *fileSharing = [capabilitiesDict valueForKey:@"files_sharing"];
  954. //FILE SHARING
  955. NSNumber *fileSharingAPIEnabled = (NSNumber*)[fileSharing valueForKey:@"api_enabled"];
  956. NSNumber *filesSharingDefaultPermissions = (NSNumber*)[fileSharing valueForKey:@"default_permissions"];
  957. NSNumber *fileSharingGroupSharing = (NSNumber*)[fileSharing valueForKey:@"group_sharing"];
  958. NSNumber *filesSharingReSharing = (NSNumber*)[fileSharing valueForKey:@"resharing"];
  959. capabilities.isFilesSharingAPIEnabled = fileSharingAPIEnabled.boolValue;
  960. capabilities.filesSharingDefaulPermissions = filesSharingDefaultPermissions.integerValue;
  961. capabilities.isFilesSharingGroupSharing = fileSharingGroupSharing.boolValue;
  962. capabilities.isFilesSharingReSharing = filesSharingReSharing.boolValue;
  963. //FILE SHARING - PUBLIC
  964. NSDictionary *fileSharingPublic = [fileSharing valueForKey:@"public"];
  965. NSNumber *filesSharingPublicShareLinkEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"enabled"];
  966. NSNumber *filesSharingAllowPublicUploadsEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"upload"];
  967. NSNumber *isFilesSharingAllowPublicUserSendMailNumber = (NSNumber*)[fileSharingPublic valueForKey:@"send_mail"];
  968. NSNumber *filesSharingAllowPublicUploadFilesDrop = (NSNumber*)[fileSharingPublic valueForKey:@"upload_files_drop"];
  969. NSNumber *filesSharingAllowPublicMultipleLinks = (NSNumber*)[fileSharingPublic valueForKey:@"multiple_links"];
  970. capabilities.isFilesSharingPublicShareLinkEnabled = filesSharingPublicShareLinkEnabledNumber.boolValue;
  971. capabilities.isFilesSharingAllowPublicUploadsEnabled = filesSharingAllowPublicUploadsEnabledNumber.boolValue;
  972. capabilities.isFilesSharingAllowPublicUserSendMail = isFilesSharingAllowPublicUserSendMailNumber.boolValue;
  973. capabilities.isFilesSharingAllowPublicUploadFilesDrop = filesSharingAllowPublicUploadFilesDrop.boolValue;
  974. capabilities.isFilesSharingAllowPublicMultipleLinks = filesSharingAllowPublicMultipleLinks.boolValue;
  975. NSDictionary *fileSharingPublicExpireDate = [fileSharingPublic valueForKey:@"expire_date"];
  976. NSNumber *filesSharingPublicExpireDateByDefaultEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enabled"];
  977. NSNumber *filesSharingPublicExpireDateEnforceEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enforced"];
  978. NSNumber *filesSharingPublicExpireDateDaysNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"days"];
  979. capabilities.isFilesSharingPublicExpireDateByDefaultEnabled = filesSharingPublicExpireDateByDefaultEnabledNumber.boolValue;
  980. capabilities.isFilesSharingPublicExpireDateEnforceEnabled = filesSharingPublicExpireDateEnforceEnabledNumber.boolValue;
  981. capabilities.filesSharingPublicExpireDateDays = filesSharingPublicExpireDateDaysNumber.integerValue;
  982. NSDictionary *fileSharingPublicPassword = [fileSharingPublic valueForKey:@"password"];
  983. NSNumber *fileSharingPublicPasswordEnforcedEnabled = (NSNumber*)[fileSharingPublicPassword valueForKey:@"enforced"];
  984. capabilities.isFilesSharingPublicPasswordEnforced = fileSharingPublicPasswordEnforcedEnabled.boolValue;
  985. //FILE SHARING - USER
  986. NSDictionary *fileSharingUser = [fileSharing valueForKey:@"user"];
  987. NSNumber *isFilesSharingAllowUserSendMailNumber = (NSNumber*)[fileSharingUser valueForKey:@"send_mail"];
  988. capabilities.isFilesSharingAllowUserSendMail = isFilesSharingAllowUserSendMailNumber.boolValue;
  989. NSDictionary *fileSharingUserExpireDate = [fileSharingUser valueForKey:@"expire_date"];
  990. NSNumber *filesSharingUserExpireDateNumber = (NSNumber*)[fileSharingUserExpireDate valueForKey:@"enabled"];
  991. capabilities.isFilesSharingUserExpireDate = filesSharingUserExpireDateNumber.boolValue;
  992. //FILE SHARING - GROUP
  993. NSDictionary *fileSharingGroup = [fileSharing valueForKey:@"group"];
  994. NSNumber *filesSharingGroupEnabled = (NSNumber*)[fileSharingGroup valueForKey:@"enabled"];
  995. capabilities.isFilesSharingGroupEnabled = filesSharingGroupEnabled.boolValue;
  996. NSDictionary *fileSharingGroupExpireDate = [fileSharingGroup valueForKey:@"expire_date"];
  997. NSNumber *filesSharingGroupExpireDateNumber = (NSNumber*)[fileSharingGroupExpireDate valueForKey:@"enabled"];
  998. capabilities.isFilesSharingGroupExpireDate = filesSharingGroupExpireDateNumber.boolValue;
  999. //FILE SHARING - FEDERATION
  1000. NSDictionary *fileSharingFederation = [fileSharing valueForKey:@"federation"];
  1001. NSDictionary *fileSharingFederationExpireDate = [fileSharingFederation valueForKey:@"expire_date"];
  1002. NSNumber *filesSharingFederationAllowUserSendSharesNumber = (NSNumber*)[fileSharingFederation valueForKey:@"outgoing"];
  1003. NSNumber *filesSharingFederationAllowUserReceiveSharesNumber = (NSNumber*)[fileSharingFederation valueForKey:@"incoming"];
  1004. NSNumber *filesSharingFederationExpireDateNumber = (NSNumber*)[fileSharingFederationExpireDate valueForKey:@"enabled"];
  1005. capabilities.isFilesSharingFederationAllowUserSendShares = filesSharingFederationAllowUserSendSharesNumber.boolValue;
  1006. capabilities.isFilesSharingFederationAllowUserReceiveShares = filesSharingFederationAllowUserReceiveSharesNumber.boolValue;
  1007. capabilities.isFilesSharingFederationExpireDate = filesSharingFederationExpireDateNumber.boolValue;
  1008. //FILE SHARING - SHAREBYMAIL
  1009. NSDictionary *fileSharingShareByMail = [fileSharing valueForKey:@"sharebymail"];
  1010. NSNumber *fileSharingShareByMailEnabled = (NSNumber*)[fileSharingShareByMail valueForKey:@"enabled"];
  1011. capabilities.isFileSharingShareByMailEnabled = fileSharingShareByMailEnabled.boolValue;
  1012. NSDictionary *fileSharingShareByMailExpireDate = [fileSharingShareByMail valueForKey:@"expire_date"];
  1013. NSNumber *fileSharingShareByMailExpireDateNumber = (NSNumber*)[fileSharingShareByMailExpireDate valueForKey:@"enabled"];
  1014. capabilities.isFileSharingShareByMailExpireDate = fileSharingShareByMailExpireDateNumber.boolValue;
  1015. NSDictionary *fileSharingShareByMailPassword = [fileSharingShareByMail valueForKey:@"password"];
  1016. NSNumber *fileSharingShareByMailPasswordNumber = (NSNumber*)[fileSharingShareByMailPassword valueForKey:@"enabled"];
  1017. capabilities.isFileSharingShareByMailPassword = fileSharingShareByMailPasswordNumber.boolValue;
  1018. NSDictionary *fileSharingShareByMailUploadFilesDrop = [fileSharingShareByMail valueForKey:@"upload_files_drop"];
  1019. NSNumber *fileSharingShareByMailUploadFilesDropNumber = (NSNumber*)[fileSharingShareByMailUploadFilesDrop valueForKey:@"enabled"];
  1020. capabilities.isFileSharingShareByMailUploadFilesDrop = fileSharingShareByMailUploadFilesDropNumber.boolValue;
  1021. // EXTERNAL SITES
  1022. NSDictionary *externalSitesDic = [capabilitiesDict valueForKey:@"external"];
  1023. if (externalSitesDic) {
  1024. capabilities.isExternalSitesServerEnabled = YES;
  1025. NSArray *externalSitesArray = [externalSitesDic valueForKey:@"v1"];
  1026. capabilities.externalSiteV1 = [externalSitesArray componentsJoinedByString:@","];
  1027. }
  1028. // ACTIVITY
  1029. NSDictionary *activityDic = [capabilitiesDict valueForKey:@"activity"];
  1030. if (activityDic) {
  1031. NSArray *activityArray = [activityDic valueForKey:@"apiv2"];
  1032. if (activityArray) {
  1033. capabilities.isActivityV2Enabled = YES;
  1034. capabilities.activityV2 = [activityArray componentsJoinedByString:@","];
  1035. }
  1036. }
  1037. // NOTIFICATION
  1038. NSDictionary *notificationDic = [capabilitiesDict valueForKey:@"notifications"];
  1039. if (notificationDic) {
  1040. capabilities.isNotificationServerEnabled = YES;
  1041. NSArray *ocsendpointsArray = [notificationDic valueForKey:@"ocs-endpoints"];
  1042. capabilities.notificationOcsEndpoints = [ocsendpointsArray componentsJoinedByString:@","];
  1043. NSArray *pushArray = [notificationDic valueForKey:@"push"];
  1044. capabilities.notificationPush = [pushArray componentsJoinedByString:@","];
  1045. }
  1046. // SPREED
  1047. NSDictionary *spreedDic = [capabilitiesDict valueForKey:@"spreed"];
  1048. if (spreedDic) {
  1049. capabilities.isSpreedServerEnabled = YES;
  1050. NSArray *featuresArray = [capabilitiesDict valueForKey:@"features"];
  1051. capabilities.spreedFeatures = [featuresArray componentsJoinedByString:@","];
  1052. }
  1053. //FILES
  1054. NSDictionary *files = [capabilitiesDict valueForKey:@"files"];
  1055. NSNumber *fileBigFileChunkingEnabledNumber = (NSNumber*)[files valueForKey:@"bigfilechunking"];
  1056. NSNumber *fileUndeleteEnabledNumber = (NSNumber*)[files valueForKey:@"undelete"];
  1057. NSNumber *fileVersioningEnabledNumber = (NSNumber*)[files valueForKey:@"versioning"];
  1058. capabilities.isFileBigFileChunkingEnabled = fileBigFileChunkingEnabledNumber.boolValue;
  1059. capabilities.isFileUndeleteEnabled = fileUndeleteEnabledNumber.boolValue;
  1060. capabilities.isFileVersioningEnabled = fileVersioningEnabledNumber.boolValue;
  1061. //THEMING
  1062. NSDictionary *theming = [capabilitiesDict valueForKey:@"theming"];
  1063. if ([theming count] > 0) {
  1064. if ([theming valueForKey:@"background"] && ![[theming valueForKey:@"background"] isEqual:[NSNull null]])
  1065. capabilities.themingBackground = [theming valueForKey:@"background"];
  1066. if ([theming valueForKey:@"background-default"] && ![[theming valueForKey:@"background-default"] isEqual:[NSNull null]]) {
  1067. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-default"];
  1068. capabilities.themingBackgroundDefault = result.boolValue;
  1069. }
  1070. if ([theming valueForKey:@"background-plain"] && ![[theming valueForKey:@"background-plain"] isEqual:[NSNull null]]) {
  1071. NSNumber *result = (NSNumber*)[theming valueForKey:@"background-plain"];
  1072. capabilities.themingBackgroundPlain = result.boolValue;
  1073. }
  1074. if ([theming valueForKey:@"color"] && ![[theming valueForKey:@"color"] isEqual:[NSNull null]])
  1075. capabilities.themingColor = [theming valueForKey:@"color"];
  1076. if ([theming valueForKey:@"color-element"] && ![[theming valueForKey:@"color-element"] isEqual:[NSNull null]])
  1077. capabilities.themingColorElement = [theming valueForKey:@"color-element"];
  1078. if ([theming valueForKey:@"color-text"] && ![[theming valueForKey:@"color-text"] isEqual:[NSNull null]])
  1079. capabilities.themingColorText = [theming valueForKey:@"color-text"];
  1080. if ([theming valueForKey:@"logo"] && ![[theming valueForKey:@"logo"] isEqual:[NSNull null]])
  1081. capabilities.themingLogo = [theming valueForKey:@"logo"];
  1082. if ([theming valueForKey:@"name"] && ![[theming valueForKey:@"name"] isEqual:[NSNull null]])
  1083. capabilities.themingName = [theming valueForKey:@"name"];
  1084. if ([theming valueForKey:@"slogan"] && ![[theming valueForKey:@"slogan"] isEqual:[NSNull null]])
  1085. capabilities.themingSlogan = [theming valueForKey:@"slogan"];
  1086. if ([theming valueForKey:@"url"] && ![[theming valueForKey:@"url"] isEqual:[NSNull null]])
  1087. capabilities.themingUrl = [theming valueForKey:@"url"];
  1088. }
  1089. //END TO END Encryption
  1090. NSDictionary *endToEndEncryption = [capabilitiesDict valueForKey:@"end-to-end-encryption"];
  1091. if ([endToEndEncryption count] > 0) {
  1092. NSNumber *endToEndEncryptionEnabled = (NSNumber*)[endToEndEncryption valueForKey:@"enabled"];
  1093. capabilities.isEndToEndEncryptionEnabled = endToEndEncryptionEnabled.boolValue;
  1094. if ([endToEndEncryption valueForKey:@"api-version"] && ![[endToEndEncryption valueForKey:@"api-version"] isEqual:[NSNull null]])
  1095. capabilities.endToEndEncryptionVersion = [endToEndEncryption valueForKey:@"api-version"];
  1096. }
  1097. //Richdocuments
  1098. NSDictionary *richdocuments = [capabilitiesDict valueForKey:@"richdocuments"];
  1099. if (richdocuments!= nil && [richdocuments count] > 0) {
  1100. capabilities.richdocumentsDirectEditing = [[richdocuments valueForKey:@"direct_editing"] boolValue];
  1101. capabilities.richdocumentsMimetypes = [richdocuments valueForKey:@"mimetypes"];
  1102. }
  1103. //Handwerkcloud
  1104. NSDictionary *handwerkcloudDic = [capabilitiesDict valueForKey:@"handwerkcloud"];
  1105. if (handwerkcloudDic) {
  1106. NSNumber *isHandwerkcloudEnabledNumber = (NSNumber*)[handwerkcloudDic valueForKey:@"enabled"];
  1107. capabilities.isHandwerkcloudEnabled = isHandwerkcloudEnabledNumber.boolValue;
  1108. if ([handwerkcloudDic valueForKey:@"shop_url"] && ![[handwerkcloudDic valueForKey:@"shop_url"] isEqual:[NSNull null]])
  1109. capabilities.HCShopUrl = [handwerkcloudDic valueForKey:@"shop_url"];
  1110. }
  1111. //Imagemeter
  1112. NSDictionary *imagemeterDic = [capabilitiesDict valueForKey:@"imagemeter"];
  1113. if (imagemeterDic) {
  1114. NSNumber *isImagemeterEnabledNumber = (NSNumber*)[imagemeterDic valueForKey:@"enabled"];
  1115. capabilities.isImagemeterEnabled = isImagemeterEnabledNumber.boolValue;
  1116. }
  1117. }
  1118. successRequest(response, capabilities, request.redirectedServer);
  1119. } else {
  1120. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1121. }
  1122. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1123. failureRequest(response, error, request.redirectedServer);
  1124. }];
  1125. }
  1126. #pragma mark - Thumbnail
  1127. - (NSURLSessionTask *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCComunication
  1128. successRequest:(void(^)(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer)) successRequest
  1129. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1130. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1131. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1132. OCWebDAVClient *request = [OCWebDAVClient new];
  1133. request = [self getRequestWithCredentials:request];
  1134. OCHTTPRequestOperation *operation = [request getRemoteThumbnailByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight onCommunication:sharedOCComunication
  1135. success:^(NSHTTPURLResponse *response, id responseObject) {
  1136. NSData *responseData = (NSData*) responseObject;
  1137. successRequest(response, responseData, request.redirectedServer);
  1138. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1139. failureRequest(response, error, request.redirectedServer);
  1140. }];
  1141. [operation resume];
  1142. return operation;
  1143. }
  1144. - (NSURLSessionTask *) getRemotePreviewByServer:(NSString*)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight andA:(NSInteger)a andMode:(NSString * _Nonnull)mode path:(NSString * _Nonnull)path onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1145. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1146. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1147. OCWebDAVClient *request = [OCWebDAVClient new];
  1148. request = [self getRequestWithCredentials:request];
  1149. OCHTTPRequestOperation *operation = [request getRemotePreviewByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight andA:a andMode:mode path:path onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1150. NSData *responseData = (NSData*) responseObject;
  1151. successRequest(response, responseData, request.redirectedServer);
  1152. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1153. failureRequest(response, error, request.redirectedServer);
  1154. }];
  1155. [operation resume];
  1156. return operation;
  1157. }
  1158. - (NSURLSessionTask *) getRemotePreviewTrashByServer:(NSString*)serverPath ofFileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1159. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1160. OCWebDAVClient *request = [OCWebDAVClient new];
  1161. request = [self getRequestWithCredentials:request];
  1162. OCHTTPRequestOperation *operation = [request getRemotePreviewTrashByServer:serverPath ofFileID:fileID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1163. NSData *responseData = (NSData*) responseObject;
  1164. successRequest(response, responseData, request.redirectedServer);
  1165. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1166. failureRequest(response, error, request.redirectedServer);
  1167. }];
  1168. [operation resume];
  1169. return operation;
  1170. }
  1171. #pragma mark - Notification
  1172. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1173. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1174. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_notification_api];
  1175. OCWebDAVClient *request = [OCWebDAVClient new];
  1176. request = [self getRequestWithCredentials:request];
  1177. [request getNotificationServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1178. NSData *responseData = (NSData*) responseObject;
  1179. NSMutableArray *listOfNotifications = [NSMutableArray new];
  1180. //Parse
  1181. NSError *error;
  1182. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1183. NSLog(@"[LOG] Notifications : %@",jsongParsed);
  1184. if (jsongParsed && jsongParsed.allKeys > 0) {
  1185. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1186. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1187. NSDictionary *datas = [ocs valueForKey:@"data"];
  1188. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1189. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1190. for (NSDictionary *data in datas) {
  1191. OCNotifications *notification = [OCNotifications new];
  1192. if ([data valueForKey:@"notification_id"] && ![[data valueForKey:@"notification_id"] isEqual:[NSNull null]])
  1193. notification.idNotification = [[data valueForKey:@"notification_id"] integerValue];
  1194. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1195. notification.application = [data valueForKey:@"app"];
  1196. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1197. notification.user = [data valueForKey:@"user"];
  1198. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1199. NSString *dateString = [data valueForKey:@"datetime"];
  1200. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1201. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1202. [dateFormatter setLocale:enUSPOSIXLocale];
  1203. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1204. notification.date = [dateFormatter dateFromString:dateString];
  1205. }
  1206. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1207. notification.typeObject = [data valueForKey:@"object_type"];
  1208. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1209. notification.idObject = [data valueForKey:@"object_id"];
  1210. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1211. notification.subject = [data valueForKey:@"subject"];
  1212. if ([data valueForKey:@"subjectRich"] && ![[data valueForKey:@"subjectRich"] isEqual:[NSNull null]])
  1213. notification.subjectRich = [data valueForKey:@"subjectRich"];
  1214. if ([data valueForKey:@"subjectRichParameters"] && ![[data valueForKey:@"subjectRichParameters"] isEqual:[NSNull null]])
  1215. notification.subjectRichParameters = [data valueForKey:@"subjectRichParameters"];
  1216. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1217. notification.message = [data valueForKey:@"message"];
  1218. if ([data valueForKey:@"messageRich"] && ![[data valueForKey:@"messageRich"] isEqual:[NSNull null]])
  1219. notification.messageRich = [data valueForKey:@"messageRich"];
  1220. if ([data valueForKey:@"messageRichParameters"] && ![[data valueForKey:@"messageRichParameters"] isEqual:[NSNull null]])
  1221. notification.messageRichParameters = [data valueForKey:@"messageRichParameters"];
  1222. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1223. notification.link = [data valueForKey:@"link"];
  1224. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1225. notification.icon = [data valueForKey:@"icon"];
  1226. /* ACTION */
  1227. NSMutableArray *actionsArr = [NSMutableArray new];
  1228. NSDictionary *actions = [data valueForKey:@"actions"];
  1229. for (NSDictionary *action in actions) {
  1230. OCNotificationsAction *notificationAction = [OCNotificationsAction new];
  1231. if ([action valueForKey:@"label"] && ![[action valueForKey:@"label"] isEqual:[NSNull null]])
  1232. notificationAction.label = [action valueForKey:@"label"];
  1233. if ([action valueForKey:@"link"] && ![[action valueForKey:@"link"] isEqual:[NSNull null]])
  1234. notificationAction.link = [action valueForKey:@"link"];
  1235. if ([action valueForKey:@"primary"] && ![[action valueForKey:@"primary"] isEqual:[NSNull null]])
  1236. notificationAction.primary = [[action valueForKey:@"primary"] boolValue];
  1237. if ([action valueForKey:@"type"] && ![[action valueForKey:@"type"] isEqual:[NSNull null]])
  1238. notificationAction.type = [action valueForKey:@"type"];
  1239. [actionsArr addObject:notificationAction];
  1240. }
  1241. notification.actions = [[NSArray alloc] initWithArray:actionsArr];
  1242. [listOfNotifications addObject:notification];
  1243. }
  1244. successRequest(response, listOfNotifications, request.redirectedServer);
  1245. } else {
  1246. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1247. if ([message isKindOfClass:[NSNull class]]) {
  1248. message = NSLocalizedString(@"_server_response_error_", nil);
  1249. }
  1250. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1251. }
  1252. } else {
  1253. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1254. }
  1255. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1256. failureRequest(response, error, request.redirectedServer);
  1257. }];
  1258. }
  1259. - (void)setNotificationServer:(NSString*)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1260. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1261. OCWebDAVClient *request = [OCWebDAVClient new];
  1262. request = [self getRequestWithCredentials:request];
  1263. __weak OCWebDAVClient *wrequest = request;
  1264. [request setNotificationServer:serverPath type:type onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1265. successRequest(response, wrequest.redirectedServer);
  1266. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1267. failureRequest(response, error, wrequest.redirectedServer);
  1268. }];
  1269. }
  1270. #pragma mark - Push Notification
  1271. - (void)subscribingNextcloudServerPush:(NSString *)serverPath pushTokenHash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey proxyServerPath:(NSString *)proxyServerPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer)) successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1272. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1273. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1274. OCWebDAVClient *request = [OCWebDAVClient new];
  1275. request = [self getRequestWithCredentials:request];
  1276. [request subscribingNextcloudServerPush:serverPath pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath:proxyServerPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1277. NSData *responseData = (NSData*)responseObject;
  1278. NSError *error;
  1279. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1280. if (jsongParsed && jsongParsed.allKeys > 0) {
  1281. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1282. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1283. NSDictionary *datas = [ocs valueForKey:@"data"];
  1284. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1285. if (statusCode == kOCPushNotificationAPISuccessful || statusCode == kOCPushNotificationAPINeedSendProxy) {
  1286. NSString *publicKey = [datas objectForKey:@"publicKey"];
  1287. NSString *deviceIdentifier = [datas objectForKey:@"deviceIdentifier"];
  1288. NSString *signature = [datas objectForKey:@"signature"];
  1289. successRequest(response, publicKey, deviceIdentifier, signature, request.redirectedServer);
  1290. } else {
  1291. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1292. if ([message isKindOfClass:[NSNull class]]) {
  1293. message = NSLocalizedString(@"_server_response_error_", nil);
  1294. }
  1295. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1296. }
  1297. } else {
  1298. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1299. }
  1300. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1301. failureRequest(response, error, request.redirectedServer);
  1302. }];
  1303. }
  1304. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1305. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1306. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1307. OCWebDAVClient *request = [OCWebDAVClient new];
  1308. request = [self getRequestWithCredentials:request];
  1309. [request unsubscribingNextcloudServerPush:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1310. successRequest(response, request.redirectedServer);
  1311. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1312. failureRequest(response, error, request.redirectedServer);
  1313. }];
  1314. }
  1315. - (void)subscribingPushProxy:(NSString *)serverPath pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *redirectedServer))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1316. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1317. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1318. OCWebDAVClient *request = [OCWebDAVClient new];
  1319. request = [self getRequestWithCredentials:request];
  1320. [request subscribingPushProxy:serverPath pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1321. if (successRequest) {
  1322. //Return success
  1323. successRequest(response, request.redirectedServer);
  1324. }
  1325. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1326. failureRequest(response, error, request.redirectedServer);
  1327. }];
  1328. }
  1329. - (void)unsubscribingPushProxy:(NSString *)serverPath deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *redirectedServer))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1330. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1331. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1332. OCWebDAVClient *request = [OCWebDAVClient new];
  1333. request = [self getRequestWithCredentials:request];
  1334. [request unsubscribingPushProxy:serverPath deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1335. if (successRequest) {
  1336. //Return success
  1337. successRequest(response, request.redirectedServer);
  1338. }
  1339. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1340. failureRequest(response, error, request.redirectedServer);
  1341. }];
  1342. }
  1343. #pragma mark - Activity
  1344. - (void) getActivityServer:(NSString*)serverPath since:(NSInteger)since limit:(NSInteger)limit previews:(BOOL)previews link:(NSString *)link onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1345. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1346. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_activity_api];
  1347. OCWebDAVClient *request = [OCWebDAVClient new];
  1348. request = [self getRequestWithCredentials:request];
  1349. [request getActivityServer:serverPath since:since limit:limit previews:previews link:link onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1350. NSData *responseData = (NSData*) responseObject;
  1351. NSMutableArray *listOfActivity = [NSMutableArray new];
  1352. //Parse
  1353. NSError *error;
  1354. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1355. NSLog(@"[LOG] Activity : %@",jsongParsed);
  1356. if (jsongParsed && [jsongParsed isKindOfClass:[NSDictionary class]] && jsongParsed.allKeys > 0) {
  1357. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1358. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1359. NSDictionary *datas = [ocs valueForKey:@"data"];
  1360. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1361. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1362. for (NSDictionary *data in datas) {
  1363. OCActivity *activity = [OCActivity new];
  1364. if ([data valueForKey:@"activity_id"] && ![[data valueForKey:@"activity_id"] isEqual:[NSNull null]])
  1365. activity.idActivity = [[data valueForKey:@"activity_id"] integerValue];
  1366. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1367. NSString *dateString = [data valueForKey:@"datetime"];
  1368. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1369. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1370. [dateFormatter setLocale:enUSPOSIXLocale];
  1371. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1372. activity.date = [dateFormatter dateFromString:dateString];
  1373. }
  1374. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1375. activity.app = [data valueForKey:@"app"];
  1376. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1377. activity.type = [data valueForKey:@"type"];
  1378. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1379. activity.user = [data valueForKey:@"user"];
  1380. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1381. activity.subject = [data valueForKey:@"subject"];
  1382. if ([data valueForKey:@"subject_rich"] && ![[data valueForKey:@"subject_rich"] isEqual:[NSNull null]])
  1383. activity.subject_rich = [data valueForKey:@"subject_rich"];
  1384. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1385. activity.message = [data valueForKey:@"message"];
  1386. if ([data valueForKey:@"message_rich"] && ![[data valueForKey:@"message_rich"] isEqual:[NSNull null]])
  1387. activity.message_rich = [data valueForKey:@"message_rich"];
  1388. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1389. activity.icon = [data valueForKey:@"icon"];
  1390. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1391. activity.link = [data valueForKey:@"link"];
  1392. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1393. activity.object_type = [data valueForKey:@"object_type"];
  1394. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1395. activity.object_id = [[data valueForKey:@"object_id"] integerValue];
  1396. if ([data valueForKey:@"object_name"] && ![[data valueForKey:@"object_name"] isEqual:[NSNull null]])
  1397. activity.object_name = [data valueForKey:@"object_name"];
  1398. if ([data valueForKey:@"previews"] && ![[data valueForKey:@"previews"] isEqual:[NSNull null]])
  1399. activity.previews = [data valueForKey:@"previews"];
  1400. [listOfActivity addObject:activity];
  1401. }
  1402. successRequest(response, listOfActivity, request.redirectedServer);
  1403. } else {
  1404. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1405. if ([message isKindOfClass:[NSNull class]]) {
  1406. message = NSLocalizedString(@"_server_response_error_", nil);
  1407. }
  1408. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1409. }
  1410. } else {
  1411. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1412. }
  1413. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1414. failureRequest(response, error, request.redirectedServer);
  1415. }];
  1416. }
  1417. #pragma mark - External Sites
  1418. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1419. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1420. serverPath = [serverPath stringByAppendingString:k_url_acces_external_sites_api];
  1421. OCWebDAVClient *request = [OCWebDAVClient new];
  1422. request = [self getRequestWithCredentials:request];
  1423. [request getExternalSitesServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1424. NSData *responseData = (NSData*) responseObject;
  1425. NSMutableArray *listOfExternalSites = [NSMutableArray new];
  1426. //Parse
  1427. NSError *error;
  1428. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1429. NSLog(@"[LOG] External Sites : %@",jsongParsed);
  1430. if (jsongParsed && jsongParsed.allKeys > 0) {
  1431. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1432. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1433. NSDictionary *datas = [ocs valueForKey:@"data"];
  1434. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1435. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1436. for (NSDictionary *data in datas) {
  1437. OCExternalSites *externalSites = [OCExternalSites new];
  1438. externalSites.idExternalSite = [[data valueForKey:@"id"] integerValue];
  1439. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1440. externalSites.icon = [data valueForKey:@"icon"];
  1441. if ([data valueForKey:@"lang"] && ![[data valueForKey:@"lang"] isEqual:[NSNull null]])
  1442. externalSites.lang = [data valueForKey:@"lang"];
  1443. if ([data valueForKey:@"name"] && ![[data valueForKey:@"name"] isEqual:[NSNull null]])
  1444. externalSites.name = [data valueForKey:@"name"];
  1445. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isEqual:[NSNull null]])
  1446. externalSites.url = [data valueForKey:@"url"];
  1447. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1448. externalSites.type = [data valueForKey:@"type"];
  1449. [listOfExternalSites addObject:externalSites];
  1450. }
  1451. successRequest(response, listOfExternalSites, request.redirectedServer);
  1452. } else {
  1453. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1454. if ([message isKindOfClass:[NSNull class]]) {
  1455. message = NSLocalizedString(@"_server_response_error_", nil);
  1456. }
  1457. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1458. }
  1459. } else {
  1460. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1461. }
  1462. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1463. failureRequest(response, error, request.redirectedServer);
  1464. }];
  1465. }
  1466. #pragma mark - User Profile
  1467. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1468. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_userprofile_api];
  1469. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1470. OCWebDAVClient *request = [OCWebDAVClient new];
  1471. request = [self getRequestWithCredentials:request];
  1472. [request getUserProfileServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1473. NSData *responseData = (NSData*) responseObject;
  1474. OCUserProfile *userProfile = [OCUserProfile new];
  1475. //Parse
  1476. NSError *error;
  1477. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1478. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  1479. if (jsongParsed && jsongParsed.allKeys > 0) {
  1480. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1481. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1482. NSDictionary *data = [ocs valueForKey:@"data"];
  1483. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1484. if (statusCode == kOCUserProfileAPISuccessful) {
  1485. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  1486. userProfile.address = [data valueForKey:@"address"];
  1487. if ([data valueForKey:@"display-name"] && ![[data valueForKey:@"display-name"] isKindOfClass:[NSNull class]])
  1488. userProfile.displayName = [data valueForKey:@"display-name"];
  1489. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  1490. userProfile.email = [data valueForKey:@"email"];
  1491. if ([data valueForKey:@"enabled"] && ![[data valueForKey:@"enabled"] isKindOfClass:[NSNull class]])
  1492. userProfile.enabled = [[data valueForKey:@"enabled"] boolValue];
  1493. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isKindOfClass:[NSNull class]])
  1494. userProfile.id = [data valueForKey:@"id"];
  1495. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  1496. userProfile.phone = [data valueForKey:@"phone"];
  1497. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  1498. userProfile.twitter = [data valueForKey:@"twitter"];
  1499. if ([data valueForKey:@"webpage"] && ![[data valueForKey:@"webpage"] isKindOfClass:[NSNull class]])
  1500. userProfile.webpage = [data valueForKey:@"webpage"];
  1501. /* QUOTA */
  1502. NSDictionary *quota = [data valueForKey:@"quota"];
  1503. if ([quota count] > 0) {
  1504. if ([quota valueForKey:@"free"] && ![[quota valueForKey:@"free"] isKindOfClass:[NSNull class]])
  1505. userProfile.quotaFree = [[quota valueForKey:@"free"] doubleValue];
  1506. if ([quota valueForKey:@"quota"] && ![[quota valueForKey:@"quota"] isKindOfClass:[NSNull class]])
  1507. userProfile.quota = [[quota valueForKey:@"quota"] doubleValue];
  1508. if ([quota valueForKey:@"relative"] && ![[quota valueForKey:@"relative"] isKindOfClass:[NSNull class]])
  1509. userProfile.quotaRelative = [[quota valueForKey:@"relative"] doubleValue];
  1510. if ([quota valueForKey:@"total"] && ![[quota valueForKey:@"total"] isKindOfClass:[NSNull class]])
  1511. userProfile.quotaTotal = [[quota valueForKey:@"total"] doubleValue];
  1512. if ([quota valueForKey:@"used"] && ![[quota valueForKey:@"used"] isKindOfClass:[NSNull class]])
  1513. userProfile.quotaUsed = [[quota valueForKey:@"used"] doubleValue];
  1514. }
  1515. successRequest(response, userProfile, request.redirectedServer);
  1516. } else {
  1517. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1518. if ([message isKindOfClass:[NSNull class]]) {
  1519. message = NSLocalizedString(@"_server_response_error_", nil);
  1520. }
  1521. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1522. }
  1523. } else {
  1524. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1525. }
  1526. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1527. failureRequest(response, error, request.redirectedServer);
  1528. }];
  1529. }
  1530. #pragma mark - End-to-End Encryption
  1531. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1532. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1533. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1534. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1535. OCWebDAVClient *request = [OCWebDAVClient new];
  1536. request = [self getRequestWithCredentials:request];
  1537. [request getEndToEndPublicKeys:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1538. NSData *responseData = (NSData*) responseObject;
  1539. NSString *publicKey = @"";
  1540. //Parse
  1541. NSError *error;
  1542. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1543. NSLog(@"[LOG] E2E Get PublicKey : %@",jsongParsed);
  1544. if (jsongParsed && jsongParsed.allKeys > 0) {
  1545. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1546. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1547. NSDictionary *data = [ocs valueForKey:@"data"];
  1548. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1549. if (statusCode == kOCUserProfileAPISuccessful) {
  1550. if ([data valueForKey:@"public-keys"] && ![[data valueForKey:@"public-keys"] isKindOfClass:[NSNull class]]) {
  1551. NSDictionary *publickeys = [data valueForKey:@"public-keys"];
  1552. publicKey = [publickeys valueForKey:self.userID];
  1553. successRequest(response, publicKey, request.redirectedServer);
  1554. } else {
  1555. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1556. }
  1557. } else {
  1558. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1559. if ([message isKindOfClass:[NSNull class]]) {
  1560. message = NSLocalizedString(@"_server_response_error_", nil);
  1561. }
  1562. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1563. }
  1564. } else {
  1565. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1566. }
  1567. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1568. //Return error
  1569. failureRequest(response, error, request.redirectedServer);
  1570. }];
  1571. }
  1572. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1573. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1574. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1575. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1576. OCWebDAVClient *request = [OCWebDAVClient new];
  1577. request = [self getRequestWithCredentials:request];
  1578. [request getEndToEndPrivateKeyCipher:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1579. NSData *responseData = (NSData*) responseObject;
  1580. NSString *privateKeyChiper = @"";
  1581. //Parse
  1582. NSError *error;
  1583. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1584. NSLog(@"[LOG] E2E Get PrivateKey : %@",jsongParsed);
  1585. if (jsongParsed && jsongParsed.allKeys > 0) {
  1586. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1587. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1588. NSDictionary *data = [ocs valueForKey:@"data"];
  1589. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1590. if (statusCode == kOCUserProfileAPISuccessful) {
  1591. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]]) {
  1592. privateKeyChiper = [data valueForKey:@"private-key"];
  1593. successRequest(response, privateKeyChiper, request.redirectedServer);
  1594. } else {
  1595. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1596. }
  1597. } else {
  1598. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1599. if ([message isKindOfClass:[NSNull class]]) {
  1600. message = NSLocalizedString(@"_server_response_error_", nil);
  1601. }
  1602. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1603. }
  1604. } else {
  1605. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1606. }
  1607. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1608. //Return error
  1609. failureRequest(response, error, request.redirectedServer);
  1610. }];
  1611. }
  1612. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1613. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1614. serverPath = [serverPath stringByAppendingString:@"/server-key"];
  1615. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1616. OCWebDAVClient *request = [OCWebDAVClient new];
  1617. request = [self getRequestWithCredentials:request];
  1618. [request getEndToEndServerPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1619. NSData *responseData = (NSData*) responseObject;
  1620. NSString *publicKey = @"";
  1621. //Parse
  1622. NSError *error;
  1623. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1624. NSLog(@"[LOG] E2E Get Server PublicKey : %@",jsongParsed);
  1625. if (jsongParsed && jsongParsed.allKeys > 0) {
  1626. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1627. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1628. NSDictionary *data = [ocs valueForKey:@"data"];
  1629. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1630. if (statusCode == kOCUserProfileAPISuccessful) {
  1631. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1632. publicKey = [data valueForKey:@"public-key"];
  1633. successRequest(response, publicKey, request.redirectedServer);
  1634. } else {
  1635. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1636. }
  1637. } else {
  1638. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1639. if ([message isKindOfClass:[NSNull class]]) {
  1640. message = NSLocalizedString(@"_server_response_error_", nil);
  1641. }
  1642. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1643. }
  1644. } else {
  1645. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1646. }
  1647. //Return success
  1648. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1649. //Return error
  1650. failureRequest(response, error, request.redirectedServer);
  1651. }];
  1652. }
  1653. - (void)signEndToEndPublicKey:(NSString*)serverPath publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey,NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1654. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1655. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1656. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1657. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1658. request = [self getRequestWithCredentials:request];
  1659. [request signEndToEndPublicKey:serverPath key:publicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1660. NSData *responseData = (NSData*) responseObject;
  1661. NSString *publicKey = @"";
  1662. //Parse
  1663. NSError *error;
  1664. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1665. NSLog(@"[LOG] E2E Sign PublicKey : %@",jsongParsed);
  1666. if (jsongParsed && jsongParsed.allKeys > 0) {
  1667. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1668. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1669. NSDictionary *data = [ocs valueForKey:@"data"];
  1670. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1671. if (statusCode == kOCUserProfileAPISuccessful) {
  1672. if ([data valueForKey:@"public-key"] && ![[data valueForKey:@"public-key"] isKindOfClass:[NSNull class]]) {
  1673. publicKey = [data valueForKey:@"public-key"];
  1674. successRequest(response, publicKey, request.redirectedServer);
  1675. } else {
  1676. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1677. }
  1678. } else {
  1679. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1680. if ([message isKindOfClass:[NSNull class]]) {
  1681. message = NSLocalizedString(@"_server_response_error_", nil);
  1682. }
  1683. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1684. }
  1685. } else {
  1686. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1687. }
  1688. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1689. //Return error
  1690. failureRequest(response, error, request.redirectedServer);
  1691. }];
  1692. }
  1693. - (void)storeEndToEndPrivateKeyCipher:(NSString*)serverPath privateKeyChiper:(NSString *)privateKeyChiper onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1694. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1695. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1696. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1697. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1698. request = [self getRequestWithCredentials:request];
  1699. [request storeEndToEndPrivateKeyCipher:serverPath key:privateKeyChiper onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1700. NSData *responseData = (NSData*) responseObject;
  1701. NSString *privateKey = @"";
  1702. //Parse
  1703. NSError *error;
  1704. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1705. NSLog(@"[LOG] E2E Store PrivateKey : %@",jsongParsed);
  1706. if (jsongParsed && jsongParsed.allKeys > 0) {
  1707. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1708. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1709. NSDictionary *data = [ocs valueForKey:@"data"];
  1710. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1711. if (statusCode == kOCUserProfileAPISuccessful) {
  1712. if ([data valueForKey:@"private-key"] && ![[data valueForKey:@"private-key"] isKindOfClass:[NSNull class]]) {
  1713. privateKey = [data valueForKey:@"private-key"];
  1714. successRequest(response, privateKey, request.redirectedServer);
  1715. } else {
  1716. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1717. }
  1718. } else {
  1719. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1720. if ([message isKindOfClass:[NSNull class]]) {
  1721. message = NSLocalizedString(@"_server_response_error_", nil);
  1722. }
  1723. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1724. }
  1725. } else {
  1726. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1727. }
  1728. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1729. //Return error
  1730. failureRequest(response, error, request.redirectedServer);
  1731. }];
  1732. }
  1733. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1734. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1735. serverPath = [serverPath stringByAppendingString:@"/public-key"];
  1736. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1737. OCWebDAVClient *request = [OCWebDAVClient new];
  1738. request = [self getRequestWithCredentials:request];
  1739. [request deleteEndToEndPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1740. //Return success
  1741. successRequest(response, request.redirectedServer);
  1742. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1743. //Return error
  1744. failureRequest(response, error, request.redirectedServer);
  1745. }];
  1746. }
  1747. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1748. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1749. serverPath = [serverPath stringByAppendingString:@"/private-key"];
  1750. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1751. OCWebDAVClient *request = [OCWebDAVClient new];
  1752. request = [self getRequestWithCredentials:request];
  1753. [request deleteEndToEndPrivateKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1754. //Return success
  1755. successRequest(response, request.redirectedServer);
  1756. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1757. //Return error
  1758. failureRequest(response, error, request.redirectedServer);
  1759. }];
  1760. }
  1761. - (void)markEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1762. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1763. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1764. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1765. OCWebDAVClient *request = [OCWebDAVClient new];
  1766. request = [self getRequestWithCredentials:request];
  1767. [request markEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1768. //Return success
  1769. successRequest(response, request.redirectedServer);
  1770. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1771. //Return error
  1772. failureRequest(response, error, request.redirectedServer);
  1773. }];
  1774. }
  1775. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1776. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1777. serverPath = [NSString stringWithFormat:@"%@/encrypted/%@", serverPath, fileID];
  1778. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1779. OCWebDAVClient *request = [OCWebDAVClient new];
  1780. request = [self getRequestWithCredentials:request];
  1781. [request deletemarkEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1782. //Return success
  1783. successRequest(response, request.redirectedServer);
  1784. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1785. //Return error
  1786. failureRequest(response, error, request.redirectedServer);
  1787. }];
  1788. }
  1789. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID token:(NSString *)token onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1790. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1791. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1792. if (token) {
  1793. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1794. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1795. } else {
  1796. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1797. }
  1798. OCWebDAVClient *request = [OCWebDAVClient new];
  1799. request = [self getRequestWithCredentials:request];
  1800. [request lockEndToEndFolderEncrypted:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1801. NSData *responseData = (NSData*) responseObject;
  1802. NSString *token = @"";
  1803. //Parse
  1804. NSError *error;
  1805. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1806. NSLog(@"[LOG] E2E Lock File : %@",jsongParsed);
  1807. if (jsongParsed && jsongParsed.allKeys > 0) {
  1808. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1809. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1810. NSDictionary *data = [ocs valueForKey:@"data"];
  1811. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1812. if (statusCode == kOCUserProfileAPISuccessful) {
  1813. if ([data valueForKey:@"token"] && ![[data valueForKey:@"token"] isKindOfClass:[NSNull class]]) {
  1814. token = [data valueForKey:@"token"];
  1815. successRequest(response, token, request.redirectedServer);
  1816. } else {
  1817. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1818. }
  1819. } else {
  1820. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1821. if ([message isKindOfClass:[NSNull class]]) {
  1822. message = NSLocalizedString(@"_server_response_error_", nil);
  1823. }
  1824. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1825. }
  1826. } else {
  1827. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1828. }
  1829. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1830. //Return error
  1831. failureRequest(response, error, request.redirectedServer);
  1832. }];
  1833. }
  1834. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath fileID:(NSString *)fileID token:(NSString *)token onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1835. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1836. serverPath = [NSString stringWithFormat:@"%@/lock/%@", serverPath, fileID];
  1837. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1838. OCWebDAVClient *request = [OCWebDAVClient new];
  1839. request = [self getRequestWithCredentials:request];
  1840. [request unlockEndToEndFolderEncrypted:serverPath token:token onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1841. //Return success
  1842. successRequest(response, request.redirectedServer);
  1843. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1844. //Return error
  1845. failureRequest(response, error, request.redirectedServer);
  1846. }];
  1847. }
  1848. - (void)getEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1849. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1850. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1851. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1852. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1853. request = [self getRequestWithCredentials:request];
  1854. [request getEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1855. NSData *responseData = (NSData*) responseObject;
  1856. NSString *encryptedMetadata = @"";
  1857. //Parse
  1858. NSError *error;
  1859. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1860. NSLog(@"[LOG] E2E Get Metadata : %@",jsongParsed);
  1861. if (jsongParsed && jsongParsed.allKeys > 0) {
  1862. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1863. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1864. NSDictionary *data = [ocs valueForKey:@"data"];
  1865. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1866. if (statusCode == kOCUserProfileAPISuccessful) {
  1867. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1868. encryptedMetadata = [data valueForKey:@"meta-data"];
  1869. successRequest(response, encryptedMetadata, request.redirectedServer);
  1870. } else {
  1871. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1872. }
  1873. } else {
  1874. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1875. if ([message isKindOfClass:[NSNull class]]) {
  1876. message = NSLocalizedString(@"_server_response_error_", nil);
  1877. }
  1878. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1879. }
  1880. } else {
  1881. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1882. }
  1883. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1884. //Return error
  1885. failureRequest(response, error, request.redirectedServer);
  1886. }];
  1887. }
  1888. - (void)storeEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID encryptedMetadata:(NSString *)encryptedMetadata onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1889. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1890. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1891. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1892. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1893. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1894. request = [self getRequestWithCredentials:request];
  1895. [request storeEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1896. NSData *responseData = (NSData*) responseObject;
  1897. NSString *encryptedMetadata = @"";
  1898. //Parse
  1899. NSError *error;
  1900. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1901. NSLog(@"[LOG] E2E Store Metadata : %@",jsongParsed);
  1902. if (jsongParsed && jsongParsed.allKeys > 0) {
  1903. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1904. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1905. NSDictionary *data = [ocs valueForKey:@"data"];
  1906. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1907. if (statusCode == kOCUserProfileAPISuccessful) {
  1908. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1909. encryptedMetadata = [data valueForKey:@"meta-data"];
  1910. successRequest(response, encryptedMetadata, request.redirectedServer);
  1911. } else {
  1912. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1913. }
  1914. } else {
  1915. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1916. if ([message isKindOfClass:[NSNull class]]) {
  1917. message = NSLocalizedString(@"_server_response_error_", nil);
  1918. }
  1919. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1920. }
  1921. } else {
  1922. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1923. }
  1924. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1925. //Return error
  1926. failureRequest(response, error, request.redirectedServer);
  1927. }];
  1928. }
  1929. - (void)updateEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID encryptedMetadata:(NSString *)encryptedMetadata token:(NSString *)token onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1930. encryptedMetadata = [encryptedMetadata encodeString:NSUTF8StringEncoding];
  1931. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1932. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1933. serverPath = [NSString stringWithFormat:@"%@?token=%@", serverPath, token];
  1934. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  1935. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1936. request = [self getRequestWithCredentials:request];
  1937. [request updateEndToEndMetadata:serverPath metadata:encryptedMetadata onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1938. NSData *responseData = (NSData*) responseObject;
  1939. NSString *encryptedMetadata = @"";
  1940. //Parse
  1941. NSError *error;
  1942. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1943. NSLog(@"[LOG] E2E Update Metadata : %@",jsongParsed);
  1944. if (jsongParsed && jsongParsed.allKeys > 0) {
  1945. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1946. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1947. NSDictionary *data = [ocs valueForKey:@"data"];
  1948. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1949. if (statusCode == kOCUserProfileAPISuccessful) {
  1950. if ([data valueForKey:@"meta-data"] && ![[data valueForKey:@"meta-data"] isKindOfClass:[NSNull class]]) {
  1951. encryptedMetadata = [data valueForKey:@"meta-data"];
  1952. successRequest(response, encryptedMetadata, request.redirectedServer);
  1953. } else {
  1954. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1955. }
  1956. } else {
  1957. NSString *message = (NSString *)[meta objectForKey:@"message"];
  1958. if ([message isKindOfClass:[NSNull class]]) {
  1959. message = NSLocalizedString(@"_server_response_error_", nil);
  1960. }
  1961. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  1962. }
  1963. } else {
  1964. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  1965. }
  1966. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1967. //Return error
  1968. failureRequest(response, error, request.redirectedServer);
  1969. }];
  1970. }
  1971. - (void)deleteEndToEndMetadata:(NSString*)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1972. serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
  1973. serverPath = [NSString stringWithFormat:@"%@/meta-data/%@", serverPath, fileID];
  1974. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1975. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1976. request = [self getRequestWithCredentials:request];
  1977. [request deleteEndToEndMetadata:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1978. //Return success
  1979. successRequest(response, request.redirectedServer);
  1980. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1981. //Return error
  1982. failureRequest(response, error, request.redirectedServer);
  1983. }];
  1984. }
  1985. #pragma mark - Manage Mobile Editor OCS API
  1986. - (void)createLinkRichdocuments:(NSString *)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1987. serverPath = [serverPath stringByAppendingString:k_url_create_link_mobile_richdocuments];
  1988. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  1989. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  1990. request = [self getRequestWithCredentials:request];
  1991. [request createLinkRichdocuments:serverPath fileID:fileID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  1992. NSData *responseData = (NSData*) response;
  1993. //Parse
  1994. NSError *error;
  1995. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1996. NSLog(@"[LOG] Link richdocuments : %@",jsongParsed);
  1997. if (jsongParsed && jsongParsed.allKeys > 0) {
  1998. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1999. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2000. NSDictionary *data = [ocs valueForKey:@"data"];
  2001. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2002. if (statusCode == kOCUserProfileAPISuccessful) {
  2003. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isKindOfClass:[NSNull class]]) {
  2004. NSString *link = [data valueForKey:@"url"];
  2005. successRequest(response, link, request.redirectedServer);
  2006. } else {
  2007. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2008. }
  2009. } else {
  2010. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2011. if ([message isKindOfClass:[NSNull class]]) {
  2012. message = NSLocalizedString(@"_server_response_error_", nil);
  2013. }
  2014. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2015. }
  2016. } else {
  2017. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2018. }
  2019. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2020. //Return error
  2021. failureRequest(response, error, request.redirectedServer);
  2022. }];
  2023. }
  2024. - (void)geTemplatesRichdocuments:(NSString *)serverPath typeTemplate:(NSString *)typeTemplate onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2025. serverPath = [serverPath stringByAppendingString:k_url_get_template_mobile_richdocuments];
  2026. serverPath = [serverPath stringByAppendingString:typeTemplate];
  2027. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2028. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2029. request = [self getRequestWithCredentials:request];
  2030. [request geTemplatesRichdocuments:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2031. NSData *responseData = (NSData*) response;
  2032. //Parse
  2033. NSError *error;
  2034. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2035. NSLog(@"[LOG] Link richdocuments : %@",jsongParsed);
  2036. if (jsongParsed && jsongParsed.allKeys > 0) {
  2037. NSMutableArray *list = [NSMutableArray new];
  2038. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2039. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2040. NSDictionary *data = [ocs valueForKey:@"data"];
  2041. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2042. if (statusCode == kOCUserProfileAPISuccessful) {
  2043. for (NSDictionary *dicDatas in data) {
  2044. NCRichDocumentTemplate *template = [NCRichDocumentTemplate new];
  2045. if ([dicDatas valueForKey:@"id"] && ![[dicDatas valueForKey:@"id"] isEqual:[NSNull null]])
  2046. template.templateID = [[dicDatas valueForKey:@"id"] integerValue];
  2047. if ([dicDatas valueForKey:@"delete"] && ![[dicDatas valueForKey:@"delete"] isKindOfClass:[NSNull class]])
  2048. template.delete = [dicDatas valueForKey:@"delete"];
  2049. if ([dicDatas valueForKey:@"extension"] && ![[dicDatas valueForKey:@"extension"] isKindOfClass:[NSNull class]])
  2050. template.extension = [dicDatas valueForKey:@"extension"];
  2051. if ([dicDatas valueForKey:@"name"] && ![[dicDatas valueForKey:@"name"] isKindOfClass:[NSNull class]])
  2052. template.name = [dicDatas valueForKey:@"name"];
  2053. if ([dicDatas valueForKey:@"preview"] && ![[dicDatas valueForKey:@"preview"] isKindOfClass:[NSNull class]])
  2054. template.preview = [dicDatas valueForKey:@"preview"];
  2055. if ([dicDatas valueForKey:@"type"] && ![[dicDatas valueForKey:@"type"] isKindOfClass:[NSNull class]])
  2056. template.type = [dicDatas valueForKey:@"type"];
  2057. [list addObject:template];
  2058. }
  2059. successRequest(response, list, request.redirectedServer);
  2060. } else {
  2061. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2062. if ([message isKindOfClass:[NSNull class]]) {
  2063. message = NSLocalizedString(@"_server_response_error_", nil);
  2064. }
  2065. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2066. }
  2067. } else {
  2068. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2069. }
  2070. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2071. //Return error
  2072. failureRequest(response, error, request.redirectedServer);
  2073. }];
  2074. }
  2075. - (void)createNewRichdocuments:(NSString *)serverPath path:(NSString *)path templateID:(NSString *)templateID onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2076. serverPath = [serverPath stringByAppendingString:k_url_create_new_mobile_richdocuments];
  2077. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2078. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2079. request = [self getRequestWithCredentials:request];
  2080. [request createNewRichdocuments:serverPath path:path templateID:templateID onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2081. NSData *responseData = (NSData*) response;
  2082. //Parse
  2083. NSError *error;
  2084. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2085. NSLog(@"[LOG] URL Asset : %@",jsongParsed);
  2086. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2087. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2088. NSDictionary *data = [ocs valueForKey:@"data"];
  2089. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2090. if (statusCode == kOCUserProfileAPISuccessful) {
  2091. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isKindOfClass:[NSNull class]])
  2092. successRequest(response, [data valueForKey:@"url"], request.redirectedServer);
  2093. else
  2094. successRequest(response, nil, request.redirectedServer);
  2095. } else {
  2096. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2097. }
  2098. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2099. //Return error
  2100. failureRequest(response, error, request.redirectedServer);
  2101. }];
  2102. }
  2103. - (void)createAssetRichdocuments:(NSString *)serverPath path:(NSString *)path onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2104. serverPath = [serverPath stringByAppendingString:k_url_insert_assets_to_richdocuments];
  2105. serverPath = [serverPath stringByAppendingString:@"?format=json"];
  2106. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2107. request = [self getRequestWithCredentials:request];
  2108. [request createAssetRichdocuments:serverPath path:path onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2109. NSData *responseData = (NSData*) response;
  2110. //Parse
  2111. NSError *error;
  2112. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2113. NSLog(@"[LOG] URL Asset : %@",jsongParsed);
  2114. if (jsongParsed && jsongParsed.allKeys > 0) {
  2115. if ([jsongParsed valueForKey:@"url"] && ![[jsongParsed valueForKey:@"url"] isKindOfClass:[NSNull class]]) {
  2116. NSString *url = [jsongParsed valueForKey:@"url"];
  2117. successRequest(response, url, request.redirectedServer);
  2118. } else {
  2119. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2120. }
  2121. } else {
  2122. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2123. }
  2124. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2125. //Return error
  2126. failureRequest(response, error, request.redirectedServer);
  2127. }];
  2128. }
  2129. #pragma mark - Trash
  2130. - (void)listingTrash:(NSString *)path depth:(NSString *)depth onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2131. {
  2132. OCWebDAVClient *request = [OCWebDAVClient new];
  2133. request = [self getRequestWithCredentials:request];
  2134. path = [path encodeString:NSUTF8StringEncoding];
  2135. [request listTrash:path depth:depth onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  2136. OCXMLParser *parser = [OCXMLParser new];
  2137. [parser initParserWithData:responseObject];
  2138. NSMutableArray *list = [parser.directoryList mutableCopy];
  2139. successRequest(response, list, request.redirectedServer);
  2140. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error) {
  2141. failureRequest(response, error, request.redirectedServer);
  2142. }];
  2143. }
  2144. - (void)emptyTrash:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2145. {
  2146. OCWebDAVClient *request = [OCWebDAVClient new];
  2147. request = [self getRequestWithCredentials:request];
  2148. [request emptyTrash:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  2149. successRequest(response, request.redirectedServer);
  2150. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error) {
  2151. failureRequest(response, error, request.redirectedServer);
  2152. }];
  2153. }
  2154. #pragma mark - Third Parts
  2155. - (void)getHCUserProfile:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2156. {
  2157. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2158. OCWebDAVClient *request = [OCWebDAVClient new];
  2159. request = [self getRequestWithCredentials:request];
  2160. [request getHCUserProfile:serverPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response) {
  2161. NSData *responseData = (NSData*) response;
  2162. OCUserProfile *userProfile = [OCUserProfile new];
  2163. //Parse
  2164. NSError *error;
  2165. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2166. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  2167. if (jsongParsed && jsongParsed.allKeys > 0) {
  2168. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2169. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2170. NSDictionary *data = [ocs valueForKey:@"data"];
  2171. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2172. data = [data valueForKey:@"data"];
  2173. if (statusCode == kOCUserProfileAPISuccessful) {
  2174. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  2175. userProfile.address = [data valueForKey:@"address"];
  2176. if ([data valueForKey:@"displayname"] && ![[data valueForKey:@"displayname"] isKindOfClass:[NSNull class]])
  2177. userProfile.displayName = [data valueForKey:@"displayname"];
  2178. if ([data valueForKey:@"businesssize"] && ![[data valueForKey:@"businesssize"] isKindOfClass:[NSNull class]]) {
  2179. switch ([[data valueForKey:@"businesssize"] integerValue]) {
  2180. case 1:
  2181. userProfile.businessSize = @"1-4";
  2182. break;
  2183. case 5:
  2184. userProfile.businessSize = @"5-9";
  2185. break;
  2186. case 10:
  2187. userProfile.businessSize = @"10-19";
  2188. break;
  2189. case 20:
  2190. userProfile.businessSize = @"20-49";
  2191. break;
  2192. case 50:
  2193. userProfile.businessSize = @"50-99";
  2194. break;
  2195. case 100:
  2196. userProfile.businessSize = @"100-249";
  2197. break;
  2198. case 250:
  2199. userProfile.businessSize = @"250-499";
  2200. break;
  2201. case 500:
  2202. userProfile.businessSize = @"500-999";
  2203. break;
  2204. case 1000:
  2205. userProfile.businessSize = @"1000+";
  2206. break;
  2207. default:
  2208. break;
  2209. }
  2210. }
  2211. if ([data valueForKey:@"businesstype"] && ![[data valueForKey:@"businesstype"] isKindOfClass:[NSNull class]])
  2212. userProfile.businessType = [data valueForKey:@"businesstype"];
  2213. if ([data valueForKey:@"city"] && ![[data valueForKey:@"city"] isKindOfClass:[NSNull class]])
  2214. userProfile.city = [data valueForKey:@"city"];
  2215. if ([data valueForKey:@"company"] && ![[data valueForKey:@"company"] isKindOfClass:[NSNull class]])
  2216. userProfile.company = [data valueForKey:@"company"];
  2217. if ([data valueForKey:@"country"] && ![[data valueForKey:@"country"] isKindOfClass:[NSNull class]])
  2218. userProfile.country = [data valueForKey:@"country"];
  2219. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  2220. userProfile.email = [data valueForKey:@"email"];
  2221. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  2222. userProfile.phone = [data valueForKey:@"phone"];
  2223. if ([data valueForKey:@"role"] && ![[data valueForKey:@"role"] isKindOfClass:[NSNull class]])
  2224. userProfile.role = [data valueForKey:@"role"];
  2225. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  2226. userProfile.twitter = [data valueForKey:@"twitter"];
  2227. if ([data valueForKey:@"website"] && ![[data valueForKey:@"website"] isKindOfClass:[NSNull class]])
  2228. userProfile.webpage = [data valueForKey:@"website"];
  2229. if ([data valueForKey:@"zip"] && ![[data valueForKey:@"zip"] isKindOfClass:[NSNull class]])
  2230. userProfile.zip = [data valueForKey:@"zip"];
  2231. successRequest(response, userProfile, request.redirectedServer);
  2232. } else {
  2233. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2234. if ([message isKindOfClass:[NSNull class]]) {
  2235. message = NSLocalizedString(@"_server_response_error_", nil);
  2236. }
  2237. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2238. }
  2239. } else {
  2240. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2241. }
  2242. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2243. failureRequest(response, error, request.redirectedServer);
  2244. }];
  2245. }
  2246. - (void)putHCUserProfile:(NSString*)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  2247. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2248. OCWebDAVClient *request = [[OCWebDAVClient alloc] init];
  2249. request = [self getRequestWithCredentials:request];
  2250. [request putHCUserProfile:serverPath data:data onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *operation, id response) {
  2251. successRequest(response, request.redirectedServer);
  2252. } failure:^(NSHTTPURLResponse *response, id responseObject, NSError *error) {
  2253. failureRequest(response, error, request.redirectedServer);
  2254. }];
  2255. }
  2256. - (void)getHCFeatures:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  2257. {
  2258. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  2259. OCWebDAVClient *request = [OCWebDAVClient new];
  2260. request = [self getRequestWithCredentials:request];
  2261. [request getHCUserProfile:serverPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response) {
  2262. NSData *responseData = (NSData*) response;
  2263. HCFeatures *features = [HCFeatures new];
  2264. //Parse
  2265. NSError *error;
  2266. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  2267. NSLog(@"[LOG] User Profile Features : %@",jsongParsed);
  2268. if (jsongParsed && jsongParsed.allKeys > 0) {
  2269. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  2270. NSDictionary *meta = [ocs valueForKey:@"meta"];
  2271. NSDictionary *data = [ocs valueForKey:@"data"];
  2272. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  2273. data = [data valueForKey:@"data"];
  2274. if (statusCode == kOCUserProfileAPISuccessful) {
  2275. if ([data valueForKey:@"is_trial"] && ![[data valueForKey:@"is_trial"] isKindOfClass:[NSNull class]])
  2276. features.isTrial = [[data valueForKey:@"is_trial"] boolValue];
  2277. if ([data valueForKey:@"trial_expired"] && ![[data valueForKey:@"trial_expired"] isKindOfClass:[NSNull class]])
  2278. features.trialExpired = [[data valueForKey:@"trial_expired"] boolValue];
  2279. if ([data valueForKey:@"trial_remaining_sec"] && ![[data valueForKey:@"trial_remaining_sec"] isKindOfClass:[NSNull class]])
  2280. features.trialRemainingSec = [[data valueForKey:@"trial_remaining_sec"] integerValue];
  2281. if ([data valueForKey:@"trial_end_time"] && ![[data valueForKey:@"trial_end_time"] isKindOfClass:[NSNull class]])
  2282. features.trialEndTime = [[data valueForKey:@"trial_end_time"] integerValue];
  2283. if ([data valueForKey:@"trial_end"] && ![[data valueForKey:@"trial_end"] isKindOfClass:[NSNull class]])
  2284. features.trialEnd = [data valueForKey:@"trial_end"];
  2285. if ([data valueForKey:@"account_remove_expired"] && ![[data valueForKey:@"account_remove_expired"] isKindOfClass:[NSNull class]])
  2286. features.accountRemoveExpired = [[data valueForKey:@"account_remove_expired"] boolValue];
  2287. if ([data valueForKey:@"account_remove_remaining_sec"] && ![[data valueForKey:@"account_remove_remaining_sec"] isKindOfClass:[NSNull class]])
  2288. features.accountRemoveRemainingSec = [[data valueForKey:@"account_remove_remaining_sec"] integerValue];
  2289. if ([data valueForKey:@"account_remove_time"] && ![[data valueForKey:@"account_remove_time"] isKindOfClass:[NSNull class]])
  2290. features.accountRemoveTime = [[data valueForKey:@"account_remove_time"] integerValue];
  2291. if ([data valueForKey:@"account_remove"] && ![[data valueForKey:@"account_remove"] isKindOfClass:[NSNull class]])
  2292. features.accountRemove = [data valueForKey:@"account_remove"];
  2293. NSDictionary *nextGroupExpirationDic = [data valueForKey:@"next_group_expiration"];
  2294. if (nextGroupExpirationDic) {
  2295. if ([nextGroupExpirationDic valueForKey:@"group"] && ![[nextGroupExpirationDic valueForKey:@"group"] isKindOfClass:[NSNull class]])
  2296. features.nextGroupExpirationGroup = [nextGroupExpirationDic valueForKey:@"group"];
  2297. if ([nextGroupExpirationDic valueForKey:@"group_expired"] && ![[nextGroupExpirationDic valueForKey:@"group_expired"] isKindOfClass:[NSNull class]])
  2298. features.nextGroupExpirationGroupExpired = [[nextGroupExpirationDic valueForKey:@"group_expired"] boolValue];
  2299. if ([nextGroupExpirationDic valueForKey:@"expires_time"] && ![[nextGroupExpirationDic valueForKey:@"expires_time"] isKindOfClass:[NSNull class]])
  2300. features.nextGroupExpirationExpiresTime = [[nextGroupExpirationDic valueForKey:@"expires_time"] integerValue];
  2301. if ([nextGroupExpirationDic valueForKey:@"expires"] && ![[nextGroupExpirationDic valueForKey:@"expires"] isKindOfClass:[NSNull class]])
  2302. features.nextGroupExpirationExpires = [nextGroupExpirationDic valueForKey:@"expires"];
  2303. }
  2304. successRequest(response, features, request.redirectedServer);
  2305. } else {
  2306. NSString *message = (NSString *)[meta objectForKey:@"message"];
  2307. if ([message isKindOfClass:[NSNull class]]) {
  2308. message = NSLocalizedString(@"_server_response_error_", nil);
  2309. }
  2310. failureRequest(response, [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message], request.redirectedServer);
  2311. }
  2312. } else {
  2313. failureRequest(response, [UtilsFramework getErrorWithCode:k_CCErrorWebdavResponseError andCustomMessageFromTheServer:NSLocalizedString(@"_server_response_error_", nil)], request.redirectedServer);
  2314. }
  2315. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  2316. failureRequest(response, error, request.redirectedServer);
  2317. }];
  2318. }
  2319. #pragma mark - Manage Mobile Editor OCS API
  2320. - (void)eraseURLCache
  2321. {
  2322. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  2323. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  2324. }
  2325. #pragma mark - Utils
  2326. - (void) addUserItemOfType:(NSInteger) shareeType fromArray:(NSArray*) usersArray ToList: (NSMutableArray *) itemList
  2327. {
  2328. for (NSDictionary *userFound in usersArray) {
  2329. OCShareUser *user = [OCShareUser new];
  2330. if ([[userFound valueForKey:@"label"] isKindOfClass:[NSNumber class]]) {
  2331. NSNumber *number = [userFound valueForKey:@"label"];
  2332. user.displayName = [NSString stringWithFormat:@"%ld", number.longValue];
  2333. }else{
  2334. user.displayName = [userFound valueForKey:@"label"];
  2335. }
  2336. NSDictionary *userValues = [userFound valueForKey:@"value"];
  2337. if ([[userValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  2338. NSNumber *number = [userValues valueForKey:@"shareWith"];
  2339. user.name = [NSString stringWithFormat:@"%ld", number.longValue];
  2340. }else{
  2341. user.name = [userValues valueForKey:@"shareWith"];
  2342. }
  2343. user.shareeType = shareeType;
  2344. user.server = [userValues valueForKey:@"server"];
  2345. [itemList addObject:user];
  2346. }
  2347. }
  2348. - (void) addGroupItemFromArray:(NSArray*) groupsArray ToList: (NSMutableArray *) itemList
  2349. {
  2350. for (NSDictionary *groupFound in groupsArray) {
  2351. OCShareUser *group = [OCShareUser new];
  2352. NSDictionary *groupValues = [groupFound valueForKey:@"value"];
  2353. if ([[groupValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  2354. NSNumber *number = [groupValues valueForKey:@"shareWith"];
  2355. group.name = [NSString stringWithFormat:@"%ld", number.longValue];
  2356. }else{
  2357. group.name = [groupValues valueForKey:@"shareWith"];
  2358. }
  2359. group.shareeType = shareTypeGroup;
  2360. [itemList addObject:group];
  2361. }
  2362. }
  2363. @end