@@ -29,14 +29,14 @@ |
||
29 | 29 | |
30 | 30 | class Errorlog implements IWriter { |
31 | 31 | |
32 | - /** |
|
33 | - * write a message in the log |
|
34 | - * @param string $app |
|
35 | - * @param string $message |
|
36 | - * @param int $level |
|
37 | - */ |
|
38 | - public function write(string $app, $message, int $level) { |
|
39 | - error_log('[owncloud]['.$app.']['.$level.'] '.$message); |
|
40 | - } |
|
32 | + /** |
|
33 | + * write a message in the log |
|
34 | + * @param string $app |
|
35 | + * @param string $message |
|
36 | + * @param int $level |
|
37 | + */ |
|
38 | + public function write(string $app, $message, int $level) { |
|
39 | + error_log('[owncloud]['.$app.']['.$level.'] '.$message); |
|
40 | + } |
|
41 | 41 | } |
42 | 42 |
@@ -32,17 +32,17 @@ |
||
32 | 32 | * @since 14.0.0 |
33 | 33 | */ |
34 | 34 | interface ILogFactory { |
35 | - /** |
|
36 | - * @param string $type - one of: file, errorlog, syslog |
|
37 | - * @return IWriter |
|
38 | - * @since 14.0.0 |
|
39 | - */ |
|
40 | - public function get(string $type): IWriter; |
|
35 | + /** |
|
36 | + * @param string $type - one of: file, errorlog, syslog |
|
37 | + * @return IWriter |
|
38 | + * @since 14.0.0 |
|
39 | + */ |
|
40 | + public function get(string $type): IWriter; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param string $path |
|
44 | - * @return ILogger |
|
45 | - * @since 14.0.0 |
|
46 | - */ |
|
47 | - public function getCustomLogger(string $path): ILogger; |
|
42 | + /** |
|
43 | + * @param string $path |
|
44 | + * @return ILogger |
|
45 | + * @since 14.0.0 |
|
46 | + */ |
|
47 | + public function getCustomLogger(string $path): ILogger; |
|
48 | 48 | } |
@@ -30,8 +30,8 @@ |
||
30 | 30 | * @since 14.0.0 |
31 | 31 | */ |
32 | 32 | interface IWriter { |
33 | - /** |
|
34 | - * @since 14.0.0 |
|
35 | - */ |
|
36 | - public function write(string $app, $message, int $level); |
|
33 | + /** |
|
34 | + * @since 14.0.0 |
|
35 | + */ |
|
36 | + public function write(string $app, $message, int $level); |
|
37 | 37 | } |
@@ -58,492 +58,492 @@ |
||
58 | 58 | */ |
59 | 59 | interface IServerContainer extends IContainer { |
60 | 60 | |
61 | - /** |
|
62 | - * The calendar manager will act as a broker between consumers for calendar information and |
|
63 | - * providers which actual deliver the calendar information. |
|
64 | - * |
|
65 | - * @return \OCP\Calendar\IManager |
|
66 | - * @since 13.0.0 |
|
67 | - */ |
|
68 | - public function getCalendarManager(); |
|
69 | - |
|
70 | - /** |
|
71 | - * The contacts manager will act as a broker between consumers for contacts information and |
|
72 | - * providers which actual deliver the contact information. |
|
73 | - * |
|
74 | - * @return \OCP\Contacts\IManager |
|
75 | - * @since 6.0.0 |
|
76 | - */ |
|
77 | - public function getContactsManager(); |
|
78 | - |
|
79 | - /** |
|
80 | - * The current request object holding all information about the request currently being processed |
|
81 | - * is returned from this method. |
|
82 | - * In case the current execution was not initiated by a web request null is returned |
|
83 | - * |
|
84 | - * @return \OCP\IRequest |
|
85 | - * @since 6.0.0 |
|
86 | - */ |
|
87 | - public function getRequest(); |
|
88 | - |
|
89 | - /** |
|
90 | - * Returns the preview manager which can create preview images for a given file |
|
91 | - * |
|
92 | - * @return \OCP\IPreview |
|
93 | - * @since 6.0.0 |
|
94 | - */ |
|
95 | - public function getPreviewManager(); |
|
96 | - |
|
97 | - /** |
|
98 | - * Returns the tag manager which can get and set tags for different object types |
|
99 | - * |
|
100 | - * @see \OCP\ITagManager::load() |
|
101 | - * @return \OCP\ITagManager |
|
102 | - * @since 6.0.0 |
|
103 | - */ |
|
104 | - public function getTagManager(); |
|
105 | - |
|
106 | - /** |
|
107 | - * Returns the root folder of ownCloud's data directory |
|
108 | - * |
|
109 | - * @return \OCP\Files\IRootFolder |
|
110 | - * @since 6.0.0 - between 6.0.0 and 8.0.0 this returned \OCP\Files\Folder |
|
111 | - */ |
|
112 | - public function getRootFolder(); |
|
113 | - |
|
114 | - /** |
|
115 | - * Returns a view to ownCloud's files folder |
|
116 | - * |
|
117 | - * @param string $userId user ID |
|
118 | - * @return \OCP\Files\Folder |
|
119 | - * @since 6.0.0 - parameter $userId was added in 8.0.0 |
|
120 | - * @see getUserFolder in \OCP\Files\IRootFolder |
|
121 | - */ |
|
122 | - public function getUserFolder($userId = null); |
|
123 | - |
|
124 | - /** |
|
125 | - * Returns an app-specific view in ownClouds data directory |
|
126 | - * |
|
127 | - * @return \OCP\Files\Folder |
|
128 | - * @since 6.0.0 |
|
129 | - * @deprecated 9.2.0 use IAppData |
|
130 | - */ |
|
131 | - public function getAppFolder(); |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns a user manager |
|
135 | - * |
|
136 | - * @return \OCP\IUserManager |
|
137 | - * @since 8.0.0 |
|
138 | - */ |
|
139 | - public function getUserManager(); |
|
140 | - |
|
141 | - /** |
|
142 | - * Returns a group manager |
|
143 | - * |
|
144 | - * @return \OCP\IGroupManager |
|
145 | - * @since 8.0.0 |
|
146 | - */ |
|
147 | - public function getGroupManager(); |
|
148 | - |
|
149 | - /** |
|
150 | - * Returns the user session |
|
151 | - * |
|
152 | - * @return \OCP\IUserSession |
|
153 | - * @since 6.0.0 |
|
154 | - */ |
|
155 | - public function getUserSession(); |
|
156 | - |
|
157 | - /** |
|
158 | - * Returns the navigation manager |
|
159 | - * |
|
160 | - * @return \OCP\INavigationManager |
|
161 | - * @since 6.0.0 |
|
162 | - */ |
|
163 | - public function getNavigationManager(); |
|
164 | - |
|
165 | - /** |
|
166 | - * Returns the config manager |
|
167 | - * |
|
168 | - * @return \OCP\IConfig |
|
169 | - * @since 6.0.0 |
|
170 | - */ |
|
171 | - public function getConfig(); |
|
172 | - |
|
173 | - /** |
|
174 | - * Returns a Crypto instance |
|
175 | - * |
|
176 | - * @return \OCP\Security\ICrypto |
|
177 | - * @since 8.0.0 |
|
178 | - */ |
|
179 | - public function getCrypto(); |
|
180 | - |
|
181 | - /** |
|
182 | - * Returns a Hasher instance |
|
183 | - * |
|
184 | - * @return \OCP\Security\IHasher |
|
185 | - * @since 8.0.0 |
|
186 | - */ |
|
187 | - public function getHasher(); |
|
188 | - |
|
189 | - /** |
|
190 | - * Returns a SecureRandom instance |
|
191 | - * |
|
192 | - * @return \OCP\Security\ISecureRandom |
|
193 | - * @since 8.1.0 |
|
194 | - */ |
|
195 | - public function getSecureRandom(); |
|
196 | - |
|
197 | - /** |
|
198 | - * Returns a CredentialsManager instance |
|
199 | - * |
|
200 | - * @return \OCP\Security\ICredentialsManager |
|
201 | - * @since 9.0.0 |
|
202 | - */ |
|
203 | - public function getCredentialsManager(); |
|
204 | - |
|
205 | - /** |
|
206 | - * Returns the app config manager |
|
207 | - * |
|
208 | - * @return \OCP\IAppConfig |
|
209 | - * @since 7.0.0 |
|
210 | - */ |
|
211 | - public function getAppConfig(); |
|
212 | - |
|
213 | - /** |
|
214 | - * @return \OCP\L10N\IFactory |
|
215 | - * @since 8.2.0 |
|
216 | - */ |
|
217 | - public function getL10NFactory(); |
|
218 | - |
|
219 | - /** |
|
220 | - * get an L10N instance |
|
221 | - * @param string $app appid |
|
222 | - * @param string $lang |
|
223 | - * @return \OCP\IL10N |
|
224 | - * @since 6.0.0 - parameter $lang was added in 8.0.0 |
|
225 | - */ |
|
226 | - public function getL10N($app, $lang = null); |
|
227 | - |
|
228 | - /** |
|
229 | - * @return \OC\Encryption\Manager |
|
230 | - * @since 8.1.0 |
|
231 | - */ |
|
232 | - public function getEncryptionManager(); |
|
233 | - |
|
234 | - /** |
|
235 | - * @return \OC\Encryption\File |
|
236 | - * @since 8.1.0 |
|
237 | - */ |
|
238 | - public function getEncryptionFilesHelper(); |
|
239 | - |
|
240 | - /** |
|
241 | - * @return \OCP\Encryption\Keys\IStorage |
|
242 | - * @since 8.1.0 |
|
243 | - */ |
|
244 | - public function getEncryptionKeyStorage(); |
|
245 | - |
|
246 | - /** |
|
247 | - * Returns the URL generator |
|
248 | - * |
|
249 | - * @return \OCP\IURLGenerator |
|
250 | - * @since 6.0.0 |
|
251 | - */ |
|
252 | - public function getURLGenerator(); |
|
253 | - |
|
254 | - /** |
|
255 | - * Returns an ICache instance |
|
256 | - * |
|
257 | - * @return \OCP\ICache |
|
258 | - * @since 6.0.0 |
|
259 | - */ |
|
260 | - public function getCache(); |
|
261 | - |
|
262 | - /** |
|
263 | - * Returns an \OCP\CacheFactory instance |
|
264 | - * |
|
265 | - * @return \OCP\ICacheFactory |
|
266 | - * @since 7.0.0 |
|
267 | - */ |
|
268 | - public function getMemCacheFactory(); |
|
269 | - |
|
270 | - /** |
|
271 | - * Returns the current session |
|
272 | - * |
|
273 | - * @return \OCP\ISession |
|
274 | - * @since 6.0.0 |
|
275 | - */ |
|
276 | - public function getSession(); |
|
277 | - |
|
278 | - /** |
|
279 | - * Returns the activity manager |
|
280 | - * |
|
281 | - * @return \OCP\Activity\IManager |
|
282 | - * @since 6.0.0 |
|
283 | - */ |
|
284 | - public function getActivityManager(); |
|
285 | - |
|
286 | - /** |
|
287 | - * Returns the current session |
|
288 | - * |
|
289 | - * @return \OCP\IDBConnection |
|
290 | - * @since 6.0.0 |
|
291 | - */ |
|
292 | - public function getDatabaseConnection(); |
|
293 | - |
|
294 | - /** |
|
295 | - * Returns an avatar manager, used for avatar functionality |
|
296 | - * |
|
297 | - * @return \OCP\IAvatarManager |
|
298 | - * @since 6.0.0 |
|
299 | - */ |
|
300 | - public function getAvatarManager(); |
|
301 | - |
|
302 | - /** |
|
303 | - * Returns an job list for controlling background jobs |
|
304 | - * |
|
305 | - * @return \OCP\BackgroundJob\IJobList |
|
306 | - * @since 7.0.0 |
|
307 | - */ |
|
308 | - public function getJobList(); |
|
309 | - |
|
310 | - /** |
|
311 | - * Returns a logger instance |
|
312 | - * |
|
313 | - * @return \OCP\ILogger |
|
314 | - * @since 8.0.0 |
|
315 | - */ |
|
316 | - public function getLogger(); |
|
317 | - |
|
318 | - /** |
|
319 | - * returns a log factory instance |
|
320 | - * |
|
321 | - * @return ILogFactory |
|
322 | - * @since 14.0.0 |
|
323 | - */ |
|
324 | - public function getLogFactory(); |
|
325 | - |
|
326 | - /** |
|
327 | - * Returns a router for generating and matching urls |
|
328 | - * |
|
329 | - * @return \OCP\Route\IRouter |
|
330 | - * @since 7.0.0 |
|
331 | - */ |
|
332 | - public function getRouter(); |
|
333 | - |
|
334 | - /** |
|
335 | - * Returns a search instance |
|
336 | - * |
|
337 | - * @return \OCP\ISearch |
|
338 | - * @since 7.0.0 |
|
339 | - */ |
|
340 | - public function getSearch(); |
|
341 | - |
|
342 | - /** |
|
343 | - * Get the certificate manager for the user |
|
344 | - * |
|
345 | - * @param string $userId (optional) if not specified the current loggedin user is used, use null to get the system certificate manager |
|
346 | - * @return \OCP\ICertificateManager | null if $userId is null and no user is logged in |
|
347 | - * @since 8.0.0 |
|
348 | - */ |
|
349 | - public function getCertificateManager($userId = null); |
|
350 | - |
|
351 | - /** |
|
352 | - * Create a new event source |
|
353 | - * |
|
354 | - * @return \OCP\IEventSource |
|
355 | - * @since 8.0.0 |
|
356 | - */ |
|
357 | - public function createEventSource(); |
|
358 | - |
|
359 | - /** |
|
360 | - * Returns an instance of the HTTP client service |
|
361 | - * |
|
362 | - * @return \OCP\Http\Client\IClientService |
|
363 | - * @since 8.1.0 |
|
364 | - */ |
|
365 | - public function getHTTPClientService(); |
|
366 | - |
|
367 | - /** |
|
368 | - * Get the active event logger |
|
369 | - * |
|
370 | - * @return \OCP\Diagnostics\IEventLogger |
|
371 | - * @since 8.0.0 |
|
372 | - */ |
|
373 | - public function getEventLogger(); |
|
374 | - |
|
375 | - /** |
|
376 | - * Get the active query logger |
|
377 | - * |
|
378 | - * The returned logger only logs data when debug mode is enabled |
|
379 | - * |
|
380 | - * @return \OCP\Diagnostics\IQueryLogger |
|
381 | - * @since 8.0.0 |
|
382 | - */ |
|
383 | - public function getQueryLogger(); |
|
384 | - |
|
385 | - /** |
|
386 | - * Get the manager for temporary files and folders |
|
387 | - * |
|
388 | - * @return \OCP\ITempManager |
|
389 | - * @since 8.0.0 |
|
390 | - */ |
|
391 | - public function getTempManager(); |
|
392 | - |
|
393 | - /** |
|
394 | - * Get the app manager |
|
395 | - * |
|
396 | - * @return \OCP\App\IAppManager |
|
397 | - * @since 8.0.0 |
|
398 | - */ |
|
399 | - public function getAppManager(); |
|
400 | - |
|
401 | - /** |
|
402 | - * Get the webroot |
|
403 | - * |
|
404 | - * @return string |
|
405 | - * @since 8.0.0 |
|
406 | - */ |
|
407 | - public function getWebRoot(); |
|
408 | - |
|
409 | - /** |
|
410 | - * @return \OCP\Files\Config\IMountProviderCollection |
|
411 | - * @since 8.0.0 |
|
412 | - */ |
|
413 | - public function getMountProviderCollection(); |
|
414 | - |
|
415 | - /** |
|
416 | - * Get the IniWrapper |
|
417 | - * |
|
418 | - * @return \bantu\IniGetWrapper\IniGetWrapper |
|
419 | - * @since 8.0.0 |
|
420 | - */ |
|
421 | - public function getIniWrapper(); |
|
422 | - /** |
|
423 | - * @return \OCP\Command\IBus |
|
424 | - * @since 8.1.0 |
|
425 | - */ |
|
426 | - public function getCommandBus(); |
|
427 | - |
|
428 | - /** |
|
429 | - * Creates a new mailer |
|
430 | - * |
|
431 | - * @return \OCP\Mail\IMailer |
|
432 | - * @since 8.1.0 |
|
433 | - */ |
|
434 | - public function getMailer(); |
|
435 | - |
|
436 | - /** |
|
437 | - * Get the locking provider |
|
438 | - * |
|
439 | - * @return \OCP\Lock\ILockingProvider |
|
440 | - * @since 8.1.0 |
|
441 | - */ |
|
442 | - public function getLockingProvider(); |
|
443 | - |
|
444 | - /** |
|
445 | - * @return \OCP\Files\Mount\IMountManager |
|
446 | - * @since 8.2.0 |
|
447 | - */ |
|
448 | - public function getMountManager(); |
|
449 | - |
|
450 | - /** |
|
451 | - * Get the MimeTypeDetector |
|
452 | - * |
|
453 | - * @return \OCP\Files\IMimeTypeDetector |
|
454 | - * @since 8.2.0 |
|
455 | - */ |
|
456 | - public function getMimeTypeDetector(); |
|
457 | - |
|
458 | - /** |
|
459 | - * Get the MimeTypeLoader |
|
460 | - * |
|
461 | - * @return \OCP\Files\IMimeTypeLoader |
|
462 | - * @since 8.2.0 |
|
463 | - */ |
|
464 | - public function getMimeTypeLoader(); |
|
465 | - |
|
466 | - /** |
|
467 | - * Get the EventDispatcher |
|
468 | - * |
|
469 | - * @return EventDispatcherInterface |
|
470 | - * @since 8.2.0 |
|
471 | - */ |
|
472 | - public function getEventDispatcher(); |
|
473 | - |
|
474 | - /** |
|
475 | - * Get the Notification Manager |
|
476 | - * |
|
477 | - * @return \OCP\Notification\IManager |
|
478 | - * @since 9.0.0 |
|
479 | - */ |
|
480 | - public function getNotificationManager(); |
|
481 | - |
|
482 | - /** |
|
483 | - * @return \OCP\Comments\ICommentsManager |
|
484 | - * @since 9.0.0 |
|
485 | - */ |
|
486 | - public function getCommentsManager(); |
|
487 | - |
|
488 | - /** |
|
489 | - * Returns the system-tag manager |
|
490 | - * |
|
491 | - * @return \OCP\SystemTag\ISystemTagManager |
|
492 | - * |
|
493 | - * @since 9.0.0 |
|
494 | - */ |
|
495 | - public function getSystemTagManager(); |
|
496 | - |
|
497 | - /** |
|
498 | - * Returns the system-tag object mapper |
|
499 | - * |
|
500 | - * @return \OCP\SystemTag\ISystemTagObjectMapper |
|
501 | - * |
|
502 | - * @since 9.0.0 |
|
503 | - */ |
|
504 | - public function getSystemTagObjectMapper(); |
|
505 | - |
|
506 | - /** |
|
507 | - * Returns the share manager |
|
508 | - * |
|
509 | - * @return \OCP\Share\IManager |
|
510 | - * @since 9.0.0 |
|
511 | - */ |
|
512 | - public function getShareManager(); |
|
513 | - |
|
514 | - /** |
|
515 | - * @return IContentSecurityPolicyManager |
|
516 | - * @since 9.0.0 |
|
517 | - */ |
|
518 | - public function getContentSecurityPolicyManager(); |
|
519 | - |
|
520 | - /** |
|
521 | - * @return \OCP\IDateTimeZone |
|
522 | - * @since 8.0.0 |
|
523 | - */ |
|
524 | - public function getDateTimeZone(); |
|
525 | - |
|
526 | - /** |
|
527 | - * @return \OCP\IDateTimeFormatter |
|
528 | - * @since 8.0.0 |
|
529 | - */ |
|
530 | - public function getDateTimeFormatter(); |
|
531 | - |
|
532 | - /** |
|
533 | - * @return \OCP\Federation\ICloudIdManager |
|
534 | - * @since 12.0.0 |
|
535 | - */ |
|
536 | - public function getCloudIdManager(); |
|
537 | - |
|
538 | - /** |
|
539 | - * @return \OCP\Remote\Api\IApiFactory |
|
540 | - * @since 13.0.0 |
|
541 | - */ |
|
542 | - public function getRemoteApiFactory(); |
|
543 | - |
|
544 | - /** |
|
545 | - * @return \OCP\Remote\IInstanceFactory |
|
546 | - * @since 13.0.0 |
|
547 | - */ |
|
548 | - public function getRemoteInstanceFactory(); |
|
61 | + /** |
|
62 | + * The calendar manager will act as a broker between consumers for calendar information and |
|
63 | + * providers which actual deliver the calendar information. |
|
64 | + * |
|
65 | + * @return \OCP\Calendar\IManager |
|
66 | + * @since 13.0.0 |
|
67 | + */ |
|
68 | + public function getCalendarManager(); |
|
69 | + |
|
70 | + /** |
|
71 | + * The contacts manager will act as a broker between consumers for contacts information and |
|
72 | + * providers which actual deliver the contact information. |
|
73 | + * |
|
74 | + * @return \OCP\Contacts\IManager |
|
75 | + * @since 6.0.0 |
|
76 | + */ |
|
77 | + public function getContactsManager(); |
|
78 | + |
|
79 | + /** |
|
80 | + * The current request object holding all information about the request currently being processed |
|
81 | + * is returned from this method. |
|
82 | + * In case the current execution was not initiated by a web request null is returned |
|
83 | + * |
|
84 | + * @return \OCP\IRequest |
|
85 | + * @since 6.0.0 |
|
86 | + */ |
|
87 | + public function getRequest(); |
|
88 | + |
|
89 | + /** |
|
90 | + * Returns the preview manager which can create preview images for a given file |
|
91 | + * |
|
92 | + * @return \OCP\IPreview |
|
93 | + * @since 6.0.0 |
|
94 | + */ |
|
95 | + public function getPreviewManager(); |
|
96 | + |
|
97 | + /** |
|
98 | + * Returns the tag manager which can get and set tags for different object types |
|
99 | + * |
|
100 | + * @see \OCP\ITagManager::load() |
|
101 | + * @return \OCP\ITagManager |
|
102 | + * @since 6.0.0 |
|
103 | + */ |
|
104 | + public function getTagManager(); |
|
105 | + |
|
106 | + /** |
|
107 | + * Returns the root folder of ownCloud's data directory |
|
108 | + * |
|
109 | + * @return \OCP\Files\IRootFolder |
|
110 | + * @since 6.0.0 - between 6.0.0 and 8.0.0 this returned \OCP\Files\Folder |
|
111 | + */ |
|
112 | + public function getRootFolder(); |
|
113 | + |
|
114 | + /** |
|
115 | + * Returns a view to ownCloud's files folder |
|
116 | + * |
|
117 | + * @param string $userId user ID |
|
118 | + * @return \OCP\Files\Folder |
|
119 | + * @since 6.0.0 - parameter $userId was added in 8.0.0 |
|
120 | + * @see getUserFolder in \OCP\Files\IRootFolder |
|
121 | + */ |
|
122 | + public function getUserFolder($userId = null); |
|
123 | + |
|
124 | + /** |
|
125 | + * Returns an app-specific view in ownClouds data directory |
|
126 | + * |
|
127 | + * @return \OCP\Files\Folder |
|
128 | + * @since 6.0.0 |
|
129 | + * @deprecated 9.2.0 use IAppData |
|
130 | + */ |
|
131 | + public function getAppFolder(); |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns a user manager |
|
135 | + * |
|
136 | + * @return \OCP\IUserManager |
|
137 | + * @since 8.0.0 |
|
138 | + */ |
|
139 | + public function getUserManager(); |
|
140 | + |
|
141 | + /** |
|
142 | + * Returns a group manager |
|
143 | + * |
|
144 | + * @return \OCP\IGroupManager |
|
145 | + * @since 8.0.0 |
|
146 | + */ |
|
147 | + public function getGroupManager(); |
|
148 | + |
|
149 | + /** |
|
150 | + * Returns the user session |
|
151 | + * |
|
152 | + * @return \OCP\IUserSession |
|
153 | + * @since 6.0.0 |
|
154 | + */ |
|
155 | + public function getUserSession(); |
|
156 | + |
|
157 | + /** |
|
158 | + * Returns the navigation manager |
|
159 | + * |
|
160 | + * @return \OCP\INavigationManager |
|
161 | + * @since 6.0.0 |
|
162 | + */ |
|
163 | + public function getNavigationManager(); |
|
164 | + |
|
165 | + /** |
|
166 | + * Returns the config manager |
|
167 | + * |
|
168 | + * @return \OCP\IConfig |
|
169 | + * @since 6.0.0 |
|
170 | + */ |
|
171 | + public function getConfig(); |
|
172 | + |
|
173 | + /** |
|
174 | + * Returns a Crypto instance |
|
175 | + * |
|
176 | + * @return \OCP\Security\ICrypto |
|
177 | + * @since 8.0.0 |
|
178 | + */ |
|
179 | + public function getCrypto(); |
|
180 | + |
|
181 | + /** |
|
182 | + * Returns a Hasher instance |
|
183 | + * |
|
184 | + * @return \OCP\Security\IHasher |
|
185 | + * @since 8.0.0 |
|
186 | + */ |
|
187 | + public function getHasher(); |
|
188 | + |
|
189 | + /** |
|
190 | + * Returns a SecureRandom instance |
|
191 | + * |
|
192 | + * @return \OCP\Security\ISecureRandom |
|
193 | + * @since 8.1.0 |
|
194 | + */ |
|
195 | + public function getSecureRandom(); |
|
196 | + |
|
197 | + /** |
|
198 | + * Returns a CredentialsManager instance |
|
199 | + * |
|
200 | + * @return \OCP\Security\ICredentialsManager |
|
201 | + * @since 9.0.0 |
|
202 | + */ |
|
203 | + public function getCredentialsManager(); |
|
204 | + |
|
205 | + /** |
|
206 | + * Returns the app config manager |
|
207 | + * |
|
208 | + * @return \OCP\IAppConfig |
|
209 | + * @since 7.0.0 |
|
210 | + */ |
|
211 | + public function getAppConfig(); |
|
212 | + |
|
213 | + /** |
|
214 | + * @return \OCP\L10N\IFactory |
|
215 | + * @since 8.2.0 |
|
216 | + */ |
|
217 | + public function getL10NFactory(); |
|
218 | + |
|
219 | + /** |
|
220 | + * get an L10N instance |
|
221 | + * @param string $app appid |
|
222 | + * @param string $lang |
|
223 | + * @return \OCP\IL10N |
|
224 | + * @since 6.0.0 - parameter $lang was added in 8.0.0 |
|
225 | + */ |
|
226 | + public function getL10N($app, $lang = null); |
|
227 | + |
|
228 | + /** |
|
229 | + * @return \OC\Encryption\Manager |
|
230 | + * @since 8.1.0 |
|
231 | + */ |
|
232 | + public function getEncryptionManager(); |
|
233 | + |
|
234 | + /** |
|
235 | + * @return \OC\Encryption\File |
|
236 | + * @since 8.1.0 |
|
237 | + */ |
|
238 | + public function getEncryptionFilesHelper(); |
|
239 | + |
|
240 | + /** |
|
241 | + * @return \OCP\Encryption\Keys\IStorage |
|
242 | + * @since 8.1.0 |
|
243 | + */ |
|
244 | + public function getEncryptionKeyStorage(); |
|
245 | + |
|
246 | + /** |
|
247 | + * Returns the URL generator |
|
248 | + * |
|
249 | + * @return \OCP\IURLGenerator |
|
250 | + * @since 6.0.0 |
|
251 | + */ |
|
252 | + public function getURLGenerator(); |
|
253 | + |
|
254 | + /** |
|
255 | + * Returns an ICache instance |
|
256 | + * |
|
257 | + * @return \OCP\ICache |
|
258 | + * @since 6.0.0 |
|
259 | + */ |
|
260 | + public function getCache(); |
|
261 | + |
|
262 | + /** |
|
263 | + * Returns an \OCP\CacheFactory instance |
|
264 | + * |
|
265 | + * @return \OCP\ICacheFactory |
|
266 | + * @since 7.0.0 |
|
267 | + */ |
|
268 | + public function getMemCacheFactory(); |
|
269 | + |
|
270 | + /** |
|
271 | + * Returns the current session |
|
272 | + * |
|
273 | + * @return \OCP\ISession |
|
274 | + * @since 6.0.0 |
|
275 | + */ |
|
276 | + public function getSession(); |
|
277 | + |
|
278 | + /** |
|
279 | + * Returns the activity manager |
|
280 | + * |
|
281 | + * @return \OCP\Activity\IManager |
|
282 | + * @since 6.0.0 |
|
283 | + */ |
|
284 | + public function getActivityManager(); |
|
285 | + |
|
286 | + /** |
|
287 | + * Returns the current session |
|
288 | + * |
|
289 | + * @return \OCP\IDBConnection |
|
290 | + * @since 6.0.0 |
|
291 | + */ |
|
292 | + public function getDatabaseConnection(); |
|
293 | + |
|
294 | + /** |
|
295 | + * Returns an avatar manager, used for avatar functionality |
|
296 | + * |
|
297 | + * @return \OCP\IAvatarManager |
|
298 | + * @since 6.0.0 |
|
299 | + */ |
|
300 | + public function getAvatarManager(); |
|
301 | + |
|
302 | + /** |
|
303 | + * Returns an job list for controlling background jobs |
|
304 | + * |
|
305 | + * @return \OCP\BackgroundJob\IJobList |
|
306 | + * @since 7.0.0 |
|
307 | + */ |
|
308 | + public function getJobList(); |
|
309 | + |
|
310 | + /** |
|
311 | + * Returns a logger instance |
|
312 | + * |
|
313 | + * @return \OCP\ILogger |
|
314 | + * @since 8.0.0 |
|
315 | + */ |
|
316 | + public function getLogger(); |
|
317 | + |
|
318 | + /** |
|
319 | + * returns a log factory instance |
|
320 | + * |
|
321 | + * @return ILogFactory |
|
322 | + * @since 14.0.0 |
|
323 | + */ |
|
324 | + public function getLogFactory(); |
|
325 | + |
|
326 | + /** |
|
327 | + * Returns a router for generating and matching urls |
|
328 | + * |
|
329 | + * @return \OCP\Route\IRouter |
|
330 | + * @since 7.0.0 |
|
331 | + */ |
|
332 | + public function getRouter(); |
|
333 | + |
|
334 | + /** |
|
335 | + * Returns a search instance |
|
336 | + * |
|
337 | + * @return \OCP\ISearch |
|
338 | + * @since 7.0.0 |
|
339 | + */ |
|
340 | + public function getSearch(); |
|
341 | + |
|
342 | + /** |
|
343 | + * Get the certificate manager for the user |
|
344 | + * |
|
345 | + * @param string $userId (optional) if not specified the current loggedin user is used, use null to get the system certificate manager |
|
346 | + * @return \OCP\ICertificateManager | null if $userId is null and no user is logged in |
|
347 | + * @since 8.0.0 |
|
348 | + */ |
|
349 | + public function getCertificateManager($userId = null); |
|
350 | + |
|
351 | + /** |
|
352 | + * Create a new event source |
|
353 | + * |
|
354 | + * @return \OCP\IEventSource |
|
355 | + * @since 8.0.0 |
|
356 | + */ |
|
357 | + public function createEventSource(); |
|
358 | + |
|
359 | + /** |
|
360 | + * Returns an instance of the HTTP client service |
|
361 | + * |
|
362 | + * @return \OCP\Http\Client\IClientService |
|
363 | + * @since 8.1.0 |
|
364 | + */ |
|
365 | + public function getHTTPClientService(); |
|
366 | + |
|
367 | + /** |
|
368 | + * Get the active event logger |
|
369 | + * |
|
370 | + * @return \OCP\Diagnostics\IEventLogger |
|
371 | + * @since 8.0.0 |
|
372 | + */ |
|
373 | + public function getEventLogger(); |
|
374 | + |
|
375 | + /** |
|
376 | + * Get the active query logger |
|
377 | + * |
|
378 | + * The returned logger only logs data when debug mode is enabled |
|
379 | + * |
|
380 | + * @return \OCP\Diagnostics\IQueryLogger |
|
381 | + * @since 8.0.0 |
|
382 | + */ |
|
383 | + public function getQueryLogger(); |
|
384 | + |
|
385 | + /** |
|
386 | + * Get the manager for temporary files and folders |
|
387 | + * |
|
388 | + * @return \OCP\ITempManager |
|
389 | + * @since 8.0.0 |
|
390 | + */ |
|
391 | + public function getTempManager(); |
|
392 | + |
|
393 | + /** |
|
394 | + * Get the app manager |
|
395 | + * |
|
396 | + * @return \OCP\App\IAppManager |
|
397 | + * @since 8.0.0 |
|
398 | + */ |
|
399 | + public function getAppManager(); |
|
400 | + |
|
401 | + /** |
|
402 | + * Get the webroot |
|
403 | + * |
|
404 | + * @return string |
|
405 | + * @since 8.0.0 |
|
406 | + */ |
|
407 | + public function getWebRoot(); |
|
408 | + |
|
409 | + /** |
|
410 | + * @return \OCP\Files\Config\IMountProviderCollection |
|
411 | + * @since 8.0.0 |
|
412 | + */ |
|
413 | + public function getMountProviderCollection(); |
|
414 | + |
|
415 | + /** |
|
416 | + * Get the IniWrapper |
|
417 | + * |
|
418 | + * @return \bantu\IniGetWrapper\IniGetWrapper |
|
419 | + * @since 8.0.0 |
|
420 | + */ |
|
421 | + public function getIniWrapper(); |
|
422 | + /** |
|
423 | + * @return \OCP\Command\IBus |
|
424 | + * @since 8.1.0 |
|
425 | + */ |
|
426 | + public function getCommandBus(); |
|
427 | + |
|
428 | + /** |
|
429 | + * Creates a new mailer |
|
430 | + * |
|
431 | + * @return \OCP\Mail\IMailer |
|
432 | + * @since 8.1.0 |
|
433 | + */ |
|
434 | + public function getMailer(); |
|
435 | + |
|
436 | + /** |
|
437 | + * Get the locking provider |
|
438 | + * |
|
439 | + * @return \OCP\Lock\ILockingProvider |
|
440 | + * @since 8.1.0 |
|
441 | + */ |
|
442 | + public function getLockingProvider(); |
|
443 | + |
|
444 | + /** |
|
445 | + * @return \OCP\Files\Mount\IMountManager |
|
446 | + * @since 8.2.0 |
|
447 | + */ |
|
448 | + public function getMountManager(); |
|
449 | + |
|
450 | + /** |
|
451 | + * Get the MimeTypeDetector |
|
452 | + * |
|
453 | + * @return \OCP\Files\IMimeTypeDetector |
|
454 | + * @since 8.2.0 |
|
455 | + */ |
|
456 | + public function getMimeTypeDetector(); |
|
457 | + |
|
458 | + /** |
|
459 | + * Get the MimeTypeLoader |
|
460 | + * |
|
461 | + * @return \OCP\Files\IMimeTypeLoader |
|
462 | + * @since 8.2.0 |
|
463 | + */ |
|
464 | + public function getMimeTypeLoader(); |
|
465 | + |
|
466 | + /** |
|
467 | + * Get the EventDispatcher |
|
468 | + * |
|
469 | + * @return EventDispatcherInterface |
|
470 | + * @since 8.2.0 |
|
471 | + */ |
|
472 | + public function getEventDispatcher(); |
|
473 | + |
|
474 | + /** |
|
475 | + * Get the Notification Manager |
|
476 | + * |
|
477 | + * @return \OCP\Notification\IManager |
|
478 | + * @since 9.0.0 |
|
479 | + */ |
|
480 | + public function getNotificationManager(); |
|
481 | + |
|
482 | + /** |
|
483 | + * @return \OCP\Comments\ICommentsManager |
|
484 | + * @since 9.0.0 |
|
485 | + */ |
|
486 | + public function getCommentsManager(); |
|
487 | + |
|
488 | + /** |
|
489 | + * Returns the system-tag manager |
|
490 | + * |
|
491 | + * @return \OCP\SystemTag\ISystemTagManager |
|
492 | + * |
|
493 | + * @since 9.0.0 |
|
494 | + */ |
|
495 | + public function getSystemTagManager(); |
|
496 | + |
|
497 | + /** |
|
498 | + * Returns the system-tag object mapper |
|
499 | + * |
|
500 | + * @return \OCP\SystemTag\ISystemTagObjectMapper |
|
501 | + * |
|
502 | + * @since 9.0.0 |
|
503 | + */ |
|
504 | + public function getSystemTagObjectMapper(); |
|
505 | + |
|
506 | + /** |
|
507 | + * Returns the share manager |
|
508 | + * |
|
509 | + * @return \OCP\Share\IManager |
|
510 | + * @since 9.0.0 |
|
511 | + */ |
|
512 | + public function getShareManager(); |
|
513 | + |
|
514 | + /** |
|
515 | + * @return IContentSecurityPolicyManager |
|
516 | + * @since 9.0.0 |
|
517 | + */ |
|
518 | + public function getContentSecurityPolicyManager(); |
|
519 | + |
|
520 | + /** |
|
521 | + * @return \OCP\IDateTimeZone |
|
522 | + * @since 8.0.0 |
|
523 | + */ |
|
524 | + public function getDateTimeZone(); |
|
525 | + |
|
526 | + /** |
|
527 | + * @return \OCP\IDateTimeFormatter |
|
528 | + * @since 8.0.0 |
|
529 | + */ |
|
530 | + public function getDateTimeFormatter(); |
|
531 | + |
|
532 | + /** |
|
533 | + * @return \OCP\Federation\ICloudIdManager |
|
534 | + * @since 12.0.0 |
|
535 | + */ |
|
536 | + public function getCloudIdManager(); |
|
537 | + |
|
538 | + /** |
|
539 | + * @return \OCP\Remote\Api\IApiFactory |
|
540 | + * @since 13.0.0 |
|
541 | + */ |
|
542 | + public function getRemoteApiFactory(); |
|
543 | + |
|
544 | + /** |
|
545 | + * @return \OCP\Remote\IInstanceFactory |
|
546 | + * @since 13.0.0 |
|
547 | + */ |
|
548 | + public function getRemoteInstanceFactory(); |
|
549 | 549 | } |
@@ -29,17 +29,13 @@ discard block |
||
29 | 29 | |
30 | 30 | namespace OCA\FederatedFileSharing\Controller; |
31 | 31 | |
32 | -use OC\Files\Filesystem; |
|
33 | 32 | use OC\HintException; |
34 | -use OC\Share\Helper; |
|
35 | 33 | use OCA\FederatedFileSharing\AddressHandler; |
36 | 34 | use OCA\FederatedFileSharing\FederatedShareProvider; |
37 | -use OCA\Files_Sharing\External\Manager; |
|
38 | 35 | use OCP\AppFramework\Controller; |
39 | 36 | use OCP\AppFramework\Http; |
40 | 37 | use OCP\AppFramework\Http\JSONResponse; |
41 | 38 | use OCP\Federation\ICloudIdManager; |
42 | -use OCP\Files\StorageInvalidException; |
|
43 | 39 | use OCP\Http\Client\IClientService; |
44 | 40 | use OCP\IL10N; |
45 | 41 | use OCP\ILogger; |
@@ -47,7 +43,6 @@ discard block |
||
47 | 43 | use OCP\ISession; |
48 | 44 | use OCP\IUserSession; |
49 | 45 | use OCP\Share\IManager; |
50 | -use OCP\Util; |
|
51 | 46 | |
52 | 47 | /** |
53 | 48 | * Class MountPublicLinkController |
@@ -58,177 +58,177 @@ |
||
58 | 58 | */ |
59 | 59 | class MountPublicLinkController extends Controller { |
60 | 60 | |
61 | - /** @var FederatedShareProvider */ |
|
62 | - private $federatedShareProvider; |
|
63 | - |
|
64 | - /** @var AddressHandler */ |
|
65 | - private $addressHandler; |
|
66 | - |
|
67 | - /** @var IManager */ |
|
68 | - private $shareManager; |
|
69 | - |
|
70 | - /** @var ISession */ |
|
71 | - private $session; |
|
72 | - |
|
73 | - /** @var IL10N */ |
|
74 | - private $l; |
|
75 | - |
|
76 | - /** @var IUserSession */ |
|
77 | - private $userSession; |
|
78 | - |
|
79 | - /** @var IClientService */ |
|
80 | - private $clientService; |
|
81 | - |
|
82 | - /** @var ICloudIdManager */ |
|
83 | - private $cloudIdManager; |
|
84 | - |
|
85 | - /** |
|
86 | - * MountPublicLinkController constructor. |
|
87 | - * |
|
88 | - * @param string $appName |
|
89 | - * @param IRequest $request |
|
90 | - * @param FederatedShareProvider $federatedShareProvider |
|
91 | - * @param IManager $shareManager |
|
92 | - * @param AddressHandler $addressHandler |
|
93 | - * @param ISession $session |
|
94 | - * @param IL10N $l |
|
95 | - * @param IUserSession $userSession |
|
96 | - * @param IClientService $clientService |
|
97 | - * @param ICloudIdManager $cloudIdManager |
|
98 | - */ |
|
99 | - public function __construct($appName, |
|
100 | - IRequest $request, |
|
101 | - FederatedShareProvider $federatedShareProvider, |
|
102 | - IManager $shareManager, |
|
103 | - AddressHandler $addressHandler, |
|
104 | - ISession $session, |
|
105 | - IL10N $l, |
|
106 | - IUserSession $userSession, |
|
107 | - IClientService $clientService, |
|
108 | - ICloudIdManager $cloudIdManager |
|
109 | - ) { |
|
110 | - parent::__construct($appName, $request); |
|
111 | - |
|
112 | - $this->federatedShareProvider = $federatedShareProvider; |
|
113 | - $this->shareManager = $shareManager; |
|
114 | - $this->addressHandler = $addressHandler; |
|
115 | - $this->session = $session; |
|
116 | - $this->l = $l; |
|
117 | - $this->userSession = $userSession; |
|
118 | - $this->clientService = $clientService; |
|
119 | - $this->cloudIdManager = $cloudIdManager; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * send federated share to a user of a public link |
|
124 | - * |
|
125 | - * @NoCSRFRequired |
|
126 | - * @PublicPage |
|
127 | - * @BruteForceProtection(action=publicLink2FederatedShare) |
|
128 | - * |
|
129 | - * @param string $shareWith |
|
130 | - * @param string $token |
|
131 | - * @param string $password |
|
132 | - * @return JSONResponse |
|
133 | - */ |
|
134 | - public function createFederatedShare($shareWith, $token, $password = '') { |
|
135 | - |
|
136 | - if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
|
137 | - return new JSONResponse( |
|
138 | - ['message' => 'This server doesn\'t support outgoing federated shares'], |
|
139 | - Http::STATUS_BAD_REQUEST |
|
140 | - ); |
|
141 | - } |
|
142 | - |
|
143 | - try { |
|
144 | - list(, $server) = $this->addressHandler->splitUserRemote($shareWith); |
|
145 | - $share = $this->shareManager->getShareByToken($token); |
|
146 | - } catch (HintException $e) { |
|
147 | - return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST); |
|
148 | - } |
|
149 | - |
|
150 | - // make sure that user is authenticated in case of a password protected link |
|
151 | - $storedPassword = $share->getPassword(); |
|
152 | - $authenticated = $this->session->get('public_link_authenticated') === $share->getId() || |
|
153 | - $this->shareManager->checkPassword($share, $password); |
|
154 | - if (!empty($storedPassword) && !$authenticated ) { |
|
155 | - $response = new JSONResponse( |
|
156 | - ['message' => 'No permission to access the share'], |
|
157 | - Http::STATUS_BAD_REQUEST |
|
158 | - ); |
|
159 | - $response->throttle(); |
|
160 | - return $response; |
|
161 | - } |
|
162 | - |
|
163 | - $share->setSharedWith($shareWith); |
|
164 | - |
|
165 | - try { |
|
166 | - $this->federatedShareProvider->create($share); |
|
167 | - } catch (\Exception $e) { |
|
168 | - \OC::$server->getLogger()->logException($e, [ |
|
169 | - 'level' => ILogger::WARN, |
|
170 | - 'app' => 'federatedfilesharing', |
|
171 | - ]); |
|
172 | - return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
173 | - } |
|
174 | - |
|
175 | - return new JSONResponse(['remoteUrl' => $server]); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * ask other server to get a federated share |
|
180 | - * |
|
181 | - * @NoAdminRequired |
|
182 | - * |
|
183 | - * @param string $token |
|
184 | - * @param string $remote |
|
185 | - * @param string $password |
|
186 | - * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink()) |
|
187 | - * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink()) |
|
188 | - * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink()) |
|
189 | - * @return JSONResponse |
|
190 | - */ |
|
191 | - public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') { |
|
192 | - // check if server admin allows to mount public links from other servers |
|
193 | - if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) { |
|
194 | - return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST); |
|
195 | - } |
|
196 | - |
|
197 | - $cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL()); |
|
198 | - |
|
199 | - $httpClient = $this->clientService->newClient(); |
|
200 | - |
|
201 | - try { |
|
202 | - $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare', |
|
203 | - [ |
|
204 | - 'body' => |
|
205 | - [ |
|
206 | - 'token' => $token, |
|
207 | - 'shareWith' => rtrim($cloudId->getId(), '/'), |
|
208 | - 'password' => $password |
|
209 | - ], |
|
210 | - 'connect_timeout' => 10, |
|
211 | - ] |
|
212 | - ); |
|
213 | - } catch (\Exception $e) { |
|
214 | - if (empty($password)) { |
|
215 | - $message = $this->l->t("Couldn't establish a federated share."); |
|
216 | - } else { |
|
217 | - $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong."); |
|
218 | - } |
|
219 | - return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
220 | - } |
|
221 | - |
|
222 | - $body = $response->getBody(); |
|
223 | - $result = json_decode($body, true); |
|
224 | - |
|
225 | - if (is_array($result) && isset($result['remoteUrl'])) { |
|
226 | - return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]); |
|
227 | - } |
|
228 | - |
|
229 | - // if we doesn't get the expected response we assume that we try to add |
|
230 | - // a federated share from a Nextcloud <= 9 server |
|
231 | - $message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9)."); |
|
232 | - return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
233 | - } |
|
61 | + /** @var FederatedShareProvider */ |
|
62 | + private $federatedShareProvider; |
|
63 | + |
|
64 | + /** @var AddressHandler */ |
|
65 | + private $addressHandler; |
|
66 | + |
|
67 | + /** @var IManager */ |
|
68 | + private $shareManager; |
|
69 | + |
|
70 | + /** @var ISession */ |
|
71 | + private $session; |
|
72 | + |
|
73 | + /** @var IL10N */ |
|
74 | + private $l; |
|
75 | + |
|
76 | + /** @var IUserSession */ |
|
77 | + private $userSession; |
|
78 | + |
|
79 | + /** @var IClientService */ |
|
80 | + private $clientService; |
|
81 | + |
|
82 | + /** @var ICloudIdManager */ |
|
83 | + private $cloudIdManager; |
|
84 | + |
|
85 | + /** |
|
86 | + * MountPublicLinkController constructor. |
|
87 | + * |
|
88 | + * @param string $appName |
|
89 | + * @param IRequest $request |
|
90 | + * @param FederatedShareProvider $federatedShareProvider |
|
91 | + * @param IManager $shareManager |
|
92 | + * @param AddressHandler $addressHandler |
|
93 | + * @param ISession $session |
|
94 | + * @param IL10N $l |
|
95 | + * @param IUserSession $userSession |
|
96 | + * @param IClientService $clientService |
|
97 | + * @param ICloudIdManager $cloudIdManager |
|
98 | + */ |
|
99 | + public function __construct($appName, |
|
100 | + IRequest $request, |
|
101 | + FederatedShareProvider $federatedShareProvider, |
|
102 | + IManager $shareManager, |
|
103 | + AddressHandler $addressHandler, |
|
104 | + ISession $session, |
|
105 | + IL10N $l, |
|
106 | + IUserSession $userSession, |
|
107 | + IClientService $clientService, |
|
108 | + ICloudIdManager $cloudIdManager |
|
109 | + ) { |
|
110 | + parent::__construct($appName, $request); |
|
111 | + |
|
112 | + $this->federatedShareProvider = $federatedShareProvider; |
|
113 | + $this->shareManager = $shareManager; |
|
114 | + $this->addressHandler = $addressHandler; |
|
115 | + $this->session = $session; |
|
116 | + $this->l = $l; |
|
117 | + $this->userSession = $userSession; |
|
118 | + $this->clientService = $clientService; |
|
119 | + $this->cloudIdManager = $cloudIdManager; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * send federated share to a user of a public link |
|
124 | + * |
|
125 | + * @NoCSRFRequired |
|
126 | + * @PublicPage |
|
127 | + * @BruteForceProtection(action=publicLink2FederatedShare) |
|
128 | + * |
|
129 | + * @param string $shareWith |
|
130 | + * @param string $token |
|
131 | + * @param string $password |
|
132 | + * @return JSONResponse |
|
133 | + */ |
|
134 | + public function createFederatedShare($shareWith, $token, $password = '') { |
|
135 | + |
|
136 | + if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
|
137 | + return new JSONResponse( |
|
138 | + ['message' => 'This server doesn\'t support outgoing federated shares'], |
|
139 | + Http::STATUS_BAD_REQUEST |
|
140 | + ); |
|
141 | + } |
|
142 | + |
|
143 | + try { |
|
144 | + list(, $server) = $this->addressHandler->splitUserRemote($shareWith); |
|
145 | + $share = $this->shareManager->getShareByToken($token); |
|
146 | + } catch (HintException $e) { |
|
147 | + return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST); |
|
148 | + } |
|
149 | + |
|
150 | + // make sure that user is authenticated in case of a password protected link |
|
151 | + $storedPassword = $share->getPassword(); |
|
152 | + $authenticated = $this->session->get('public_link_authenticated') === $share->getId() || |
|
153 | + $this->shareManager->checkPassword($share, $password); |
|
154 | + if (!empty($storedPassword) && !$authenticated ) { |
|
155 | + $response = new JSONResponse( |
|
156 | + ['message' => 'No permission to access the share'], |
|
157 | + Http::STATUS_BAD_REQUEST |
|
158 | + ); |
|
159 | + $response->throttle(); |
|
160 | + return $response; |
|
161 | + } |
|
162 | + |
|
163 | + $share->setSharedWith($shareWith); |
|
164 | + |
|
165 | + try { |
|
166 | + $this->federatedShareProvider->create($share); |
|
167 | + } catch (\Exception $e) { |
|
168 | + \OC::$server->getLogger()->logException($e, [ |
|
169 | + 'level' => ILogger::WARN, |
|
170 | + 'app' => 'federatedfilesharing', |
|
171 | + ]); |
|
172 | + return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
173 | + } |
|
174 | + |
|
175 | + return new JSONResponse(['remoteUrl' => $server]); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * ask other server to get a federated share |
|
180 | + * |
|
181 | + * @NoAdminRequired |
|
182 | + * |
|
183 | + * @param string $token |
|
184 | + * @param string $remote |
|
185 | + * @param string $password |
|
186 | + * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink()) |
|
187 | + * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink()) |
|
188 | + * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink()) |
|
189 | + * @return JSONResponse |
|
190 | + */ |
|
191 | + public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') { |
|
192 | + // check if server admin allows to mount public links from other servers |
|
193 | + if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) { |
|
194 | + return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST); |
|
195 | + } |
|
196 | + |
|
197 | + $cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL()); |
|
198 | + |
|
199 | + $httpClient = $this->clientService->newClient(); |
|
200 | + |
|
201 | + try { |
|
202 | + $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare', |
|
203 | + [ |
|
204 | + 'body' => |
|
205 | + [ |
|
206 | + 'token' => $token, |
|
207 | + 'shareWith' => rtrim($cloudId->getId(), '/'), |
|
208 | + 'password' => $password |
|
209 | + ], |
|
210 | + 'connect_timeout' => 10, |
|
211 | + ] |
|
212 | + ); |
|
213 | + } catch (\Exception $e) { |
|
214 | + if (empty($password)) { |
|
215 | + $message = $this->l->t("Couldn't establish a federated share."); |
|
216 | + } else { |
|
217 | + $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong."); |
|
218 | + } |
|
219 | + return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
220 | + } |
|
221 | + |
|
222 | + $body = $response->getBody(); |
|
223 | + $result = json_decode($body, true); |
|
224 | + |
|
225 | + if (is_array($result) && isset($result['remoteUrl'])) { |
|
226 | + return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]); |
|
227 | + } |
|
228 | + |
|
229 | + // if we doesn't get the expected response we assume that we try to add |
|
230 | + // a federated share from a Nextcloud <= 9 server |
|
231 | + $message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9)."); |
|
232 | + return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
233 | + } |
|
234 | 234 | } |
@@ -53,7 +53,6 @@ |
||
53 | 53 | use OCP\Files\Storage\INotifyStorage; |
54 | 54 | use OCP\Files\StorageNotAvailableException; |
55 | 55 | use OCP\ILogger; |
56 | -use OCP\Util; |
|
57 | 56 | |
58 | 57 | class SMB extends Common implements INotifyStorage { |
59 | 58 | /** |
@@ -56,484 +56,484 @@ |
||
56 | 56 | use OCP\Util; |
57 | 57 | |
58 | 58 | class SMB extends Common implements INotifyStorage { |
59 | - /** |
|
60 | - * @var \Icewind\SMB\Server |
|
61 | - */ |
|
62 | - protected $server; |
|
63 | - |
|
64 | - /** |
|
65 | - * @var \Icewind\SMB\Share |
|
66 | - */ |
|
67 | - protected $share; |
|
68 | - |
|
69 | - /** |
|
70 | - * @var string |
|
71 | - */ |
|
72 | - protected $root; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var \Icewind\SMB\FileInfo[] |
|
76 | - */ |
|
77 | - protected $statCache; |
|
78 | - |
|
79 | - public function __construct($params) { |
|
80 | - if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
81 | - if (Server::NativeAvailable()) { |
|
82 | - $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
|
83 | - } else { |
|
84 | - $this->server = new Server($params['host'], $params['user'], $params['password']); |
|
85 | - } |
|
86 | - $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
87 | - |
|
88 | - $this->root = $params['root'] ?? '/'; |
|
89 | - $this->root = '/' . ltrim($this->root, '/'); |
|
90 | - $this->root = rtrim($this->root, '/') . '/'; |
|
91 | - } else { |
|
92 | - throw new \Exception('Invalid configuration'); |
|
93 | - } |
|
94 | - $this->statCache = new CappedMemoryCache(); |
|
95 | - parent::__construct($params); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public function getId() { |
|
102 | - // FIXME: double slash to keep compatible with the old storage ids, |
|
103 | - // failure to do so will lead to creation of a new storage id and |
|
104 | - // loss of shares from the storage |
|
105 | - return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @param string $path |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - protected function buildPath($path) { |
|
113 | - return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
114 | - } |
|
115 | - |
|
116 | - protected function relativePath($fullPath) { |
|
117 | - if ($fullPath === $this->root) { |
|
118 | - return ''; |
|
119 | - } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
120 | - return substr($fullPath, strlen($this->root)); |
|
121 | - } else { |
|
122 | - return null; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param string $path |
|
128 | - * @return \Icewind\SMB\IFileInfo |
|
129 | - * @throws StorageNotAvailableException |
|
130 | - */ |
|
131 | - protected function getFileInfo($path) { |
|
132 | - try { |
|
133 | - $path = $this->buildPath($path); |
|
134 | - if (!isset($this->statCache[$path])) { |
|
135 | - $this->statCache[$path] = $this->share->stat($path); |
|
136 | - } |
|
137 | - return $this->statCache[$path]; |
|
138 | - } catch (ConnectException $e) { |
|
139 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @param string $path |
|
145 | - * @return \Icewind\SMB\IFileInfo[] |
|
146 | - * @throws StorageNotAvailableException |
|
147 | - */ |
|
148 | - protected function getFolderContents($path) { |
|
149 | - try { |
|
150 | - $path = $this->buildPath($path); |
|
151 | - $files = $this->share->dir($path); |
|
152 | - foreach ($files as $file) { |
|
153 | - $this->statCache[$path . '/' . $file->getName()] = $file; |
|
154 | - } |
|
155 | - return array_filter($files, function (IFileInfo $file) { |
|
156 | - try { |
|
157 | - return !$file->isHidden(); |
|
158 | - } catch (ForbiddenException $e) { |
|
159 | - return false; |
|
160 | - } catch (NotFoundException $e) { |
|
161 | - return false; |
|
162 | - } |
|
163 | - }); |
|
164 | - } catch (ConnectException $e) { |
|
165 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
166 | - } |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param \Icewind\SMB\IFileInfo $info |
|
171 | - * @return array |
|
172 | - */ |
|
173 | - protected function formatInfo($info) { |
|
174 | - $result = [ |
|
175 | - 'size' => $info->getSize(), |
|
176 | - 'mtime' => $info->getMTime(), |
|
177 | - ]; |
|
178 | - if ($info->isDirectory()) { |
|
179 | - $result['type'] = 'dir'; |
|
180 | - } else { |
|
181 | - $result['type'] = 'file'; |
|
182 | - } |
|
183 | - return $result; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Rename the files. If the source or the target is the root, the rename won't happen. |
|
188 | - * |
|
189 | - * @param string $source the old name of the path |
|
190 | - * @param string $target the new name of the path |
|
191 | - * @return bool true if the rename is successful, false otherwise |
|
192 | - */ |
|
193 | - public function rename($source, $target) { |
|
194 | - if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
195 | - return false; |
|
196 | - } |
|
197 | - |
|
198 | - $absoluteSource = $this->buildPath($source); |
|
199 | - $absoluteTarget = $this->buildPath($target); |
|
200 | - try { |
|
201 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
202 | - } catch (AlreadyExistsException $e) { |
|
203 | - $this->remove($target); |
|
204 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
205 | - } catch (\Exception $e) { |
|
206 | - \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
|
207 | - return false; |
|
208 | - } |
|
209 | - unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
210 | - return $result; |
|
211 | - } |
|
212 | - |
|
213 | - public function stat($path) { |
|
214 | - try { |
|
215 | - $result = $this->formatInfo($this->getFileInfo($path)); |
|
216 | - } catch (ForbiddenException $e) { |
|
217 | - return false; |
|
218 | - } catch (NotFoundException $e) { |
|
219 | - return false; |
|
220 | - } |
|
221 | - if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
222 | - $result['mtime'] = $this->shareMTime(); |
|
223 | - } |
|
224 | - return $result; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * get the best guess for the modification time of the share |
|
229 | - * |
|
230 | - * @return int |
|
231 | - */ |
|
232 | - private function shareMTime() { |
|
233 | - $highestMTime = 0; |
|
234 | - $files = $this->share->dir($this->root); |
|
235 | - foreach ($files as $fileInfo) { |
|
236 | - try { |
|
237 | - if ($fileInfo->getMTime() > $highestMTime) { |
|
238 | - $highestMTime = $fileInfo->getMTime(); |
|
239 | - } |
|
240 | - } catch (NotFoundException $e) { |
|
241 | - // Ignore this, can happen on unavailable DFS shares |
|
242 | - } |
|
243 | - } |
|
244 | - return $highestMTime; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Check if the path is our root dir (not the smb one) |
|
249 | - * |
|
250 | - * @param string $path the path |
|
251 | - * @return bool |
|
252 | - */ |
|
253 | - private function isRootDir($path) { |
|
254 | - return $path === '' || $path === '/' || $path === '.'; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Check if our root points to a smb share |
|
259 | - * |
|
260 | - * @return bool true if our root points to a share false otherwise |
|
261 | - */ |
|
262 | - private function remoteIsShare() { |
|
263 | - return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @param string $path |
|
268 | - * @return bool |
|
269 | - */ |
|
270 | - public function unlink($path) { |
|
271 | - if ($this->isRootDir($path)) { |
|
272 | - return false; |
|
273 | - } |
|
274 | - |
|
275 | - try { |
|
276 | - if ($this->is_dir($path)) { |
|
277 | - return $this->rmdir($path); |
|
278 | - } else { |
|
279 | - $path = $this->buildPath($path); |
|
280 | - unset($this->statCache[$path]); |
|
281 | - $this->share->del($path); |
|
282 | - return true; |
|
283 | - } |
|
284 | - } catch (NotFoundException $e) { |
|
285 | - return false; |
|
286 | - } catch (ForbiddenException $e) { |
|
287 | - return false; |
|
288 | - } catch (ConnectException $e) { |
|
289 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * check if a file or folder has been updated since $time |
|
295 | - * |
|
296 | - * @param string $path |
|
297 | - * @param int $time |
|
298 | - * @return bool |
|
299 | - */ |
|
300 | - public function hasUpdated($path, $time) { |
|
301 | - if (!$path and $this->root === '/') { |
|
302 | - // mtime doesn't work for shares, but giving the nature of the backend, |
|
303 | - // doing a full update is still just fast enough |
|
304 | - return true; |
|
305 | - } else { |
|
306 | - $actualTime = $this->filemtime($path); |
|
307 | - return $actualTime > $time; |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * @param string $path |
|
313 | - * @param string $mode |
|
314 | - * @return resource|false |
|
315 | - */ |
|
316 | - public function fopen($path, $mode) { |
|
317 | - $fullPath = $this->buildPath($path); |
|
318 | - try { |
|
319 | - switch ($mode) { |
|
320 | - case 'r': |
|
321 | - case 'rb': |
|
322 | - if (!$this->file_exists($path)) { |
|
323 | - return false; |
|
324 | - } |
|
325 | - return $this->share->read($fullPath); |
|
326 | - case 'w': |
|
327 | - case 'wb': |
|
328 | - $source = $this->share->write($fullPath); |
|
329 | - return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
330 | - unset($this->statCache[$fullPath]); |
|
331 | - }); |
|
332 | - case 'a': |
|
333 | - case 'ab': |
|
334 | - case 'r+': |
|
335 | - case 'w+': |
|
336 | - case 'wb+': |
|
337 | - case 'a+': |
|
338 | - case 'x': |
|
339 | - case 'x+': |
|
340 | - case 'c': |
|
341 | - case 'c+': |
|
342 | - //emulate these |
|
343 | - if (strrpos($path, '.') !== false) { |
|
344 | - $ext = substr($path, strrpos($path, '.')); |
|
345 | - } else { |
|
346 | - $ext = ''; |
|
347 | - } |
|
348 | - if ($this->file_exists($path)) { |
|
349 | - if (!$this->isUpdatable($path)) { |
|
350 | - return false; |
|
351 | - } |
|
352 | - $tmpFile = $this->getCachedFile($path); |
|
353 | - } else { |
|
354 | - if (!$this->isCreatable(dirname($path))) { |
|
355 | - return false; |
|
356 | - } |
|
357 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
358 | - } |
|
359 | - $source = fopen($tmpFile, $mode); |
|
360 | - $share = $this->share; |
|
361 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
362 | - unset($this->statCache[$fullPath]); |
|
363 | - $share->put($tmpFile, $fullPath); |
|
364 | - unlink($tmpFile); |
|
365 | - }); |
|
366 | - } |
|
367 | - return false; |
|
368 | - } catch (NotFoundException $e) { |
|
369 | - return false; |
|
370 | - } catch (ForbiddenException $e) { |
|
371 | - return false; |
|
372 | - } catch (ConnectException $e) { |
|
373 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - public function rmdir($path) { |
|
378 | - if ($this->isRootDir($path)) { |
|
379 | - return false; |
|
380 | - } |
|
381 | - |
|
382 | - try { |
|
383 | - $this->statCache = array(); |
|
384 | - $content = $this->share->dir($this->buildPath($path)); |
|
385 | - foreach ($content as $file) { |
|
386 | - if ($file->isDirectory()) { |
|
387 | - $this->rmdir($path . '/' . $file->getName()); |
|
388 | - } else { |
|
389 | - $this->share->del($file->getPath()); |
|
390 | - } |
|
391 | - } |
|
392 | - $this->share->rmdir($this->buildPath($path)); |
|
393 | - return true; |
|
394 | - } catch (NotFoundException $e) { |
|
395 | - return false; |
|
396 | - } catch (ForbiddenException $e) { |
|
397 | - return false; |
|
398 | - } catch (ConnectException $e) { |
|
399 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
400 | - } |
|
401 | - } |
|
402 | - |
|
403 | - public function touch($path, $time = null) { |
|
404 | - try { |
|
405 | - if (!$this->file_exists($path)) { |
|
406 | - $fh = $this->share->write($this->buildPath($path)); |
|
407 | - fclose($fh); |
|
408 | - return true; |
|
409 | - } |
|
410 | - return false; |
|
411 | - } catch (ConnectException $e) { |
|
412 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
413 | - } |
|
414 | - } |
|
415 | - |
|
416 | - public function opendir($path) { |
|
417 | - try { |
|
418 | - $files = $this->getFolderContents($path); |
|
419 | - } catch (NotFoundException $e) { |
|
420 | - return false; |
|
421 | - } catch (ForbiddenException $e) { |
|
422 | - return false; |
|
423 | - } |
|
424 | - $names = array_map(function ($info) { |
|
425 | - /** @var \Icewind\SMB\IFileInfo $info */ |
|
426 | - return $info->getName(); |
|
427 | - }, $files); |
|
428 | - return IteratorDirectory::wrap($names); |
|
429 | - } |
|
430 | - |
|
431 | - public function filetype($path) { |
|
432 | - try { |
|
433 | - return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
434 | - } catch (NotFoundException $e) { |
|
435 | - return false; |
|
436 | - } catch (ForbiddenException $e) { |
|
437 | - return false; |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - public function mkdir($path) { |
|
442 | - $path = $this->buildPath($path); |
|
443 | - try { |
|
444 | - $this->share->mkdir($path); |
|
445 | - return true; |
|
446 | - } catch (ConnectException $e) { |
|
447 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
448 | - } catch (Exception $e) { |
|
449 | - return false; |
|
450 | - } |
|
451 | - } |
|
452 | - |
|
453 | - public function file_exists($path) { |
|
454 | - try { |
|
455 | - $this->getFileInfo($path); |
|
456 | - return true; |
|
457 | - } catch (NotFoundException $e) { |
|
458 | - return false; |
|
459 | - } catch (ForbiddenException $e) { |
|
460 | - return false; |
|
461 | - } catch (ConnectException $e) { |
|
462 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
463 | - } |
|
464 | - } |
|
465 | - |
|
466 | - public function isReadable($path) { |
|
467 | - try { |
|
468 | - $info = $this->getFileInfo($path); |
|
469 | - return !$info->isHidden(); |
|
470 | - } catch (NotFoundException $e) { |
|
471 | - return false; |
|
472 | - } catch (ForbiddenException $e) { |
|
473 | - return false; |
|
474 | - } |
|
475 | - } |
|
476 | - |
|
477 | - public function isUpdatable($path) { |
|
478 | - try { |
|
479 | - $info = $this->getFileInfo($path); |
|
480 | - // following windows behaviour for read-only folders: they can be written into |
|
481 | - // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
482 | - return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
483 | - } catch (NotFoundException $e) { |
|
484 | - return false; |
|
485 | - } catch (ForbiddenException $e) { |
|
486 | - return false; |
|
487 | - } |
|
488 | - } |
|
489 | - |
|
490 | - public function isDeletable($path) { |
|
491 | - try { |
|
492 | - $info = $this->getFileInfo($path); |
|
493 | - return !$info->isHidden() && !$info->isReadOnly(); |
|
494 | - } catch (NotFoundException $e) { |
|
495 | - return false; |
|
496 | - } catch (ForbiddenException $e) { |
|
497 | - return false; |
|
498 | - } |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * check if smbclient is installed |
|
503 | - */ |
|
504 | - public static function checkDependencies() { |
|
505 | - return ( |
|
506 | - (bool)\OC_Helper::findBinaryPath('smbclient') |
|
507 | - || Server::NativeAvailable() |
|
508 | - ) ? true : ['smbclient']; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Test a storage for availability |
|
513 | - * |
|
514 | - * @return bool |
|
515 | - */ |
|
516 | - public function test() { |
|
517 | - try { |
|
518 | - return parent::test(); |
|
519 | - } catch (Exception $e) { |
|
520 | - return false; |
|
521 | - } |
|
522 | - } |
|
523 | - |
|
524 | - public function listen($path, callable $callback) { |
|
525 | - $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
526 | - if ($change instanceof IRenameChange) { |
|
527 | - return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
528 | - } else { |
|
529 | - return $callback($change->getType(), $change->getPath()); |
|
530 | - } |
|
531 | - }); |
|
532 | - } |
|
533 | - |
|
534 | - public function notify($path) { |
|
535 | - $path = '/' . ltrim($path, '/'); |
|
536 | - $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
537 | - return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
538 | - } |
|
59 | + /** |
|
60 | + * @var \Icewind\SMB\Server |
|
61 | + */ |
|
62 | + protected $server; |
|
63 | + |
|
64 | + /** |
|
65 | + * @var \Icewind\SMB\Share |
|
66 | + */ |
|
67 | + protected $share; |
|
68 | + |
|
69 | + /** |
|
70 | + * @var string |
|
71 | + */ |
|
72 | + protected $root; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var \Icewind\SMB\FileInfo[] |
|
76 | + */ |
|
77 | + protected $statCache; |
|
78 | + |
|
79 | + public function __construct($params) { |
|
80 | + if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
81 | + if (Server::NativeAvailable()) { |
|
82 | + $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
|
83 | + } else { |
|
84 | + $this->server = new Server($params['host'], $params['user'], $params['password']); |
|
85 | + } |
|
86 | + $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
87 | + |
|
88 | + $this->root = $params['root'] ?? '/'; |
|
89 | + $this->root = '/' . ltrim($this->root, '/'); |
|
90 | + $this->root = rtrim($this->root, '/') . '/'; |
|
91 | + } else { |
|
92 | + throw new \Exception('Invalid configuration'); |
|
93 | + } |
|
94 | + $this->statCache = new CappedMemoryCache(); |
|
95 | + parent::__construct($params); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public function getId() { |
|
102 | + // FIXME: double slash to keep compatible with the old storage ids, |
|
103 | + // failure to do so will lead to creation of a new storage id and |
|
104 | + // loss of shares from the storage |
|
105 | + return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @param string $path |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + protected function buildPath($path) { |
|
113 | + return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
114 | + } |
|
115 | + |
|
116 | + protected function relativePath($fullPath) { |
|
117 | + if ($fullPath === $this->root) { |
|
118 | + return ''; |
|
119 | + } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
120 | + return substr($fullPath, strlen($this->root)); |
|
121 | + } else { |
|
122 | + return null; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param string $path |
|
128 | + * @return \Icewind\SMB\IFileInfo |
|
129 | + * @throws StorageNotAvailableException |
|
130 | + */ |
|
131 | + protected function getFileInfo($path) { |
|
132 | + try { |
|
133 | + $path = $this->buildPath($path); |
|
134 | + if (!isset($this->statCache[$path])) { |
|
135 | + $this->statCache[$path] = $this->share->stat($path); |
|
136 | + } |
|
137 | + return $this->statCache[$path]; |
|
138 | + } catch (ConnectException $e) { |
|
139 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @param string $path |
|
145 | + * @return \Icewind\SMB\IFileInfo[] |
|
146 | + * @throws StorageNotAvailableException |
|
147 | + */ |
|
148 | + protected function getFolderContents($path) { |
|
149 | + try { |
|
150 | + $path = $this->buildPath($path); |
|
151 | + $files = $this->share->dir($path); |
|
152 | + foreach ($files as $file) { |
|
153 | + $this->statCache[$path . '/' . $file->getName()] = $file; |
|
154 | + } |
|
155 | + return array_filter($files, function (IFileInfo $file) { |
|
156 | + try { |
|
157 | + return !$file->isHidden(); |
|
158 | + } catch (ForbiddenException $e) { |
|
159 | + return false; |
|
160 | + } catch (NotFoundException $e) { |
|
161 | + return false; |
|
162 | + } |
|
163 | + }); |
|
164 | + } catch (ConnectException $e) { |
|
165 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
166 | + } |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param \Icewind\SMB\IFileInfo $info |
|
171 | + * @return array |
|
172 | + */ |
|
173 | + protected function formatInfo($info) { |
|
174 | + $result = [ |
|
175 | + 'size' => $info->getSize(), |
|
176 | + 'mtime' => $info->getMTime(), |
|
177 | + ]; |
|
178 | + if ($info->isDirectory()) { |
|
179 | + $result['type'] = 'dir'; |
|
180 | + } else { |
|
181 | + $result['type'] = 'file'; |
|
182 | + } |
|
183 | + return $result; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Rename the files. If the source or the target is the root, the rename won't happen. |
|
188 | + * |
|
189 | + * @param string $source the old name of the path |
|
190 | + * @param string $target the new name of the path |
|
191 | + * @return bool true if the rename is successful, false otherwise |
|
192 | + */ |
|
193 | + public function rename($source, $target) { |
|
194 | + if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
195 | + return false; |
|
196 | + } |
|
197 | + |
|
198 | + $absoluteSource = $this->buildPath($source); |
|
199 | + $absoluteTarget = $this->buildPath($target); |
|
200 | + try { |
|
201 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
202 | + } catch (AlreadyExistsException $e) { |
|
203 | + $this->remove($target); |
|
204 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
205 | + } catch (\Exception $e) { |
|
206 | + \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
|
207 | + return false; |
|
208 | + } |
|
209 | + unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
210 | + return $result; |
|
211 | + } |
|
212 | + |
|
213 | + public function stat($path) { |
|
214 | + try { |
|
215 | + $result = $this->formatInfo($this->getFileInfo($path)); |
|
216 | + } catch (ForbiddenException $e) { |
|
217 | + return false; |
|
218 | + } catch (NotFoundException $e) { |
|
219 | + return false; |
|
220 | + } |
|
221 | + if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
222 | + $result['mtime'] = $this->shareMTime(); |
|
223 | + } |
|
224 | + return $result; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * get the best guess for the modification time of the share |
|
229 | + * |
|
230 | + * @return int |
|
231 | + */ |
|
232 | + private function shareMTime() { |
|
233 | + $highestMTime = 0; |
|
234 | + $files = $this->share->dir($this->root); |
|
235 | + foreach ($files as $fileInfo) { |
|
236 | + try { |
|
237 | + if ($fileInfo->getMTime() > $highestMTime) { |
|
238 | + $highestMTime = $fileInfo->getMTime(); |
|
239 | + } |
|
240 | + } catch (NotFoundException $e) { |
|
241 | + // Ignore this, can happen on unavailable DFS shares |
|
242 | + } |
|
243 | + } |
|
244 | + return $highestMTime; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Check if the path is our root dir (not the smb one) |
|
249 | + * |
|
250 | + * @param string $path the path |
|
251 | + * @return bool |
|
252 | + */ |
|
253 | + private function isRootDir($path) { |
|
254 | + return $path === '' || $path === '/' || $path === '.'; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Check if our root points to a smb share |
|
259 | + * |
|
260 | + * @return bool true if our root points to a share false otherwise |
|
261 | + */ |
|
262 | + private function remoteIsShare() { |
|
263 | + return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @param string $path |
|
268 | + * @return bool |
|
269 | + */ |
|
270 | + public function unlink($path) { |
|
271 | + if ($this->isRootDir($path)) { |
|
272 | + return false; |
|
273 | + } |
|
274 | + |
|
275 | + try { |
|
276 | + if ($this->is_dir($path)) { |
|
277 | + return $this->rmdir($path); |
|
278 | + } else { |
|
279 | + $path = $this->buildPath($path); |
|
280 | + unset($this->statCache[$path]); |
|
281 | + $this->share->del($path); |
|
282 | + return true; |
|
283 | + } |
|
284 | + } catch (NotFoundException $e) { |
|
285 | + return false; |
|
286 | + } catch (ForbiddenException $e) { |
|
287 | + return false; |
|
288 | + } catch (ConnectException $e) { |
|
289 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * check if a file or folder has been updated since $time |
|
295 | + * |
|
296 | + * @param string $path |
|
297 | + * @param int $time |
|
298 | + * @return bool |
|
299 | + */ |
|
300 | + public function hasUpdated($path, $time) { |
|
301 | + if (!$path and $this->root === '/') { |
|
302 | + // mtime doesn't work for shares, but giving the nature of the backend, |
|
303 | + // doing a full update is still just fast enough |
|
304 | + return true; |
|
305 | + } else { |
|
306 | + $actualTime = $this->filemtime($path); |
|
307 | + return $actualTime > $time; |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * @param string $path |
|
313 | + * @param string $mode |
|
314 | + * @return resource|false |
|
315 | + */ |
|
316 | + public function fopen($path, $mode) { |
|
317 | + $fullPath = $this->buildPath($path); |
|
318 | + try { |
|
319 | + switch ($mode) { |
|
320 | + case 'r': |
|
321 | + case 'rb': |
|
322 | + if (!$this->file_exists($path)) { |
|
323 | + return false; |
|
324 | + } |
|
325 | + return $this->share->read($fullPath); |
|
326 | + case 'w': |
|
327 | + case 'wb': |
|
328 | + $source = $this->share->write($fullPath); |
|
329 | + return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
330 | + unset($this->statCache[$fullPath]); |
|
331 | + }); |
|
332 | + case 'a': |
|
333 | + case 'ab': |
|
334 | + case 'r+': |
|
335 | + case 'w+': |
|
336 | + case 'wb+': |
|
337 | + case 'a+': |
|
338 | + case 'x': |
|
339 | + case 'x+': |
|
340 | + case 'c': |
|
341 | + case 'c+': |
|
342 | + //emulate these |
|
343 | + if (strrpos($path, '.') !== false) { |
|
344 | + $ext = substr($path, strrpos($path, '.')); |
|
345 | + } else { |
|
346 | + $ext = ''; |
|
347 | + } |
|
348 | + if ($this->file_exists($path)) { |
|
349 | + if (!$this->isUpdatable($path)) { |
|
350 | + return false; |
|
351 | + } |
|
352 | + $tmpFile = $this->getCachedFile($path); |
|
353 | + } else { |
|
354 | + if (!$this->isCreatable(dirname($path))) { |
|
355 | + return false; |
|
356 | + } |
|
357 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
358 | + } |
|
359 | + $source = fopen($tmpFile, $mode); |
|
360 | + $share = $this->share; |
|
361 | + return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
362 | + unset($this->statCache[$fullPath]); |
|
363 | + $share->put($tmpFile, $fullPath); |
|
364 | + unlink($tmpFile); |
|
365 | + }); |
|
366 | + } |
|
367 | + return false; |
|
368 | + } catch (NotFoundException $e) { |
|
369 | + return false; |
|
370 | + } catch (ForbiddenException $e) { |
|
371 | + return false; |
|
372 | + } catch (ConnectException $e) { |
|
373 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + public function rmdir($path) { |
|
378 | + if ($this->isRootDir($path)) { |
|
379 | + return false; |
|
380 | + } |
|
381 | + |
|
382 | + try { |
|
383 | + $this->statCache = array(); |
|
384 | + $content = $this->share->dir($this->buildPath($path)); |
|
385 | + foreach ($content as $file) { |
|
386 | + if ($file->isDirectory()) { |
|
387 | + $this->rmdir($path . '/' . $file->getName()); |
|
388 | + } else { |
|
389 | + $this->share->del($file->getPath()); |
|
390 | + } |
|
391 | + } |
|
392 | + $this->share->rmdir($this->buildPath($path)); |
|
393 | + return true; |
|
394 | + } catch (NotFoundException $e) { |
|
395 | + return false; |
|
396 | + } catch (ForbiddenException $e) { |
|
397 | + return false; |
|
398 | + } catch (ConnectException $e) { |
|
399 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
400 | + } |
|
401 | + } |
|
402 | + |
|
403 | + public function touch($path, $time = null) { |
|
404 | + try { |
|
405 | + if (!$this->file_exists($path)) { |
|
406 | + $fh = $this->share->write($this->buildPath($path)); |
|
407 | + fclose($fh); |
|
408 | + return true; |
|
409 | + } |
|
410 | + return false; |
|
411 | + } catch (ConnectException $e) { |
|
412 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
413 | + } |
|
414 | + } |
|
415 | + |
|
416 | + public function opendir($path) { |
|
417 | + try { |
|
418 | + $files = $this->getFolderContents($path); |
|
419 | + } catch (NotFoundException $e) { |
|
420 | + return false; |
|
421 | + } catch (ForbiddenException $e) { |
|
422 | + return false; |
|
423 | + } |
|
424 | + $names = array_map(function ($info) { |
|
425 | + /** @var \Icewind\SMB\IFileInfo $info */ |
|
426 | + return $info->getName(); |
|
427 | + }, $files); |
|
428 | + return IteratorDirectory::wrap($names); |
|
429 | + } |
|
430 | + |
|
431 | + public function filetype($path) { |
|
432 | + try { |
|
433 | + return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
434 | + } catch (NotFoundException $e) { |
|
435 | + return false; |
|
436 | + } catch (ForbiddenException $e) { |
|
437 | + return false; |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + public function mkdir($path) { |
|
442 | + $path = $this->buildPath($path); |
|
443 | + try { |
|
444 | + $this->share->mkdir($path); |
|
445 | + return true; |
|
446 | + } catch (ConnectException $e) { |
|
447 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
448 | + } catch (Exception $e) { |
|
449 | + return false; |
|
450 | + } |
|
451 | + } |
|
452 | + |
|
453 | + public function file_exists($path) { |
|
454 | + try { |
|
455 | + $this->getFileInfo($path); |
|
456 | + return true; |
|
457 | + } catch (NotFoundException $e) { |
|
458 | + return false; |
|
459 | + } catch (ForbiddenException $e) { |
|
460 | + return false; |
|
461 | + } catch (ConnectException $e) { |
|
462 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
463 | + } |
|
464 | + } |
|
465 | + |
|
466 | + public function isReadable($path) { |
|
467 | + try { |
|
468 | + $info = $this->getFileInfo($path); |
|
469 | + return !$info->isHidden(); |
|
470 | + } catch (NotFoundException $e) { |
|
471 | + return false; |
|
472 | + } catch (ForbiddenException $e) { |
|
473 | + return false; |
|
474 | + } |
|
475 | + } |
|
476 | + |
|
477 | + public function isUpdatable($path) { |
|
478 | + try { |
|
479 | + $info = $this->getFileInfo($path); |
|
480 | + // following windows behaviour for read-only folders: they can be written into |
|
481 | + // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
482 | + return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
483 | + } catch (NotFoundException $e) { |
|
484 | + return false; |
|
485 | + } catch (ForbiddenException $e) { |
|
486 | + return false; |
|
487 | + } |
|
488 | + } |
|
489 | + |
|
490 | + public function isDeletable($path) { |
|
491 | + try { |
|
492 | + $info = $this->getFileInfo($path); |
|
493 | + return !$info->isHidden() && !$info->isReadOnly(); |
|
494 | + } catch (NotFoundException $e) { |
|
495 | + return false; |
|
496 | + } catch (ForbiddenException $e) { |
|
497 | + return false; |
|
498 | + } |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * check if smbclient is installed |
|
503 | + */ |
|
504 | + public static function checkDependencies() { |
|
505 | + return ( |
|
506 | + (bool)\OC_Helper::findBinaryPath('smbclient') |
|
507 | + || Server::NativeAvailable() |
|
508 | + ) ? true : ['smbclient']; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Test a storage for availability |
|
513 | + * |
|
514 | + * @return bool |
|
515 | + */ |
|
516 | + public function test() { |
|
517 | + try { |
|
518 | + return parent::test(); |
|
519 | + } catch (Exception $e) { |
|
520 | + return false; |
|
521 | + } |
|
522 | + } |
|
523 | + |
|
524 | + public function listen($path, callable $callback) { |
|
525 | + $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
526 | + if ($change instanceof IRenameChange) { |
|
527 | + return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
528 | + } else { |
|
529 | + return $callback($change->getType(), $change->getPath()); |
|
530 | + } |
|
531 | + }); |
|
532 | + } |
|
533 | + |
|
534 | + public function notify($path) { |
|
535 | + $path = '/' . ltrim($path, '/'); |
|
536 | + $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
537 | + return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
538 | + } |
|
539 | 539 | } |
@@ -36,7 +36,6 @@ |
||
36 | 36 | use OCP\Http\Client\IClientService; |
37 | 37 | use OCP\IConfig; |
38 | 38 | use OCP\ILogger; |
39 | -use OCP\Util; |
|
40 | 39 | |
41 | 40 | abstract class Fetcher { |
42 | 41 | const INVALIDATE_AFTER_SECONDS = 300; |
@@ -39,162 +39,162 @@ |
||
39 | 39 | use OCP\Util; |
40 | 40 | |
41 | 41 | abstract class Fetcher { |
42 | - const INVALIDATE_AFTER_SECONDS = 300; |
|
43 | - |
|
44 | - /** @var IAppData */ |
|
45 | - protected $appData; |
|
46 | - /** @var IClientService */ |
|
47 | - protected $clientService; |
|
48 | - /** @var ITimeFactory */ |
|
49 | - protected $timeFactory; |
|
50 | - /** @var IConfig */ |
|
51 | - protected $config; |
|
52 | - /** @var Ilogger */ |
|
53 | - protected $logger; |
|
54 | - /** @var string */ |
|
55 | - protected $fileName; |
|
56 | - /** @var string */ |
|
57 | - protected $endpointUrl; |
|
58 | - /** @var string */ |
|
59 | - protected $version; |
|
60 | - |
|
61 | - /** |
|
62 | - * @param Factory $appDataFactory |
|
63 | - * @param IClientService $clientService |
|
64 | - * @param ITimeFactory $timeFactory |
|
65 | - * @param IConfig $config |
|
66 | - * @param ILogger $logger |
|
67 | - */ |
|
68 | - public function __construct(Factory $appDataFactory, |
|
69 | - IClientService $clientService, |
|
70 | - ITimeFactory $timeFactory, |
|
71 | - IConfig $config, |
|
72 | - ILogger $logger) { |
|
73 | - $this->appData = $appDataFactory->get('appstore'); |
|
74 | - $this->clientService = $clientService; |
|
75 | - $this->timeFactory = $timeFactory; |
|
76 | - $this->config = $config; |
|
77 | - $this->logger = $logger; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Fetches the response from the server |
|
82 | - * |
|
83 | - * @param string $ETag |
|
84 | - * @param string $content |
|
85 | - * |
|
86 | - * @return array |
|
87 | - */ |
|
88 | - protected function fetch($ETag, $content) { |
|
89 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
90 | - |
|
91 | - if (!$appstoreenabled) { |
|
92 | - return []; |
|
93 | - } |
|
94 | - |
|
95 | - $options = [ |
|
96 | - 'timeout' => 10, |
|
97 | - ]; |
|
98 | - |
|
99 | - if ($ETag !== '') { |
|
100 | - $options['headers'] = [ |
|
101 | - 'If-None-Match' => $ETag, |
|
102 | - ]; |
|
103 | - } |
|
104 | - |
|
105 | - $client = $this->clientService->newClient(); |
|
106 | - $response = $client->get($this->endpointUrl, $options); |
|
107 | - |
|
108 | - $responseJson = []; |
|
109 | - if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
110 | - $responseJson['data'] = json_decode($content, true); |
|
111 | - } else { |
|
112 | - $responseJson['data'] = json_decode($response->getBody(), true); |
|
113 | - $ETag = $response->getHeader('ETag'); |
|
114 | - } |
|
115 | - |
|
116 | - $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
117 | - $responseJson['ncversion'] = $this->getVersion(); |
|
118 | - if ($ETag !== '') { |
|
119 | - $responseJson['ETag'] = $ETag; |
|
120 | - } |
|
121 | - |
|
122 | - return $responseJson; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns the array with the categories on the appstore server |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function get() { |
|
131 | - $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
132 | - $internetavailable = $this->config->getSystemValue('has_internet_connection', true); |
|
133 | - |
|
134 | - if (!$appstoreenabled || !$internetavailable) { |
|
135 | - return []; |
|
136 | - } |
|
137 | - |
|
138 | - $rootFolder = $this->appData->getFolder('/'); |
|
139 | - |
|
140 | - $ETag = ''; |
|
141 | - $content = ''; |
|
142 | - |
|
143 | - try { |
|
144 | - // File does already exists |
|
145 | - $file = $rootFolder->getFile($this->fileName); |
|
146 | - $jsonBlob = json_decode($file->getContent(), true); |
|
147 | - if (is_array($jsonBlob)) { |
|
148 | - |
|
149 | - // No caching when the version has been updated |
|
150 | - if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
151 | - |
|
152 | - // If the timestamp is older than 300 seconds request the files new |
|
153 | - if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
154 | - return $jsonBlob['data']; |
|
155 | - } |
|
156 | - |
|
157 | - if (isset($jsonBlob['ETag'])) { |
|
158 | - $ETag = $jsonBlob['ETag']; |
|
159 | - $content = json_encode($jsonBlob['data']); |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - } catch (NotFoundException $e) { |
|
164 | - // File does not already exists |
|
165 | - $file = $rootFolder->newFile($this->fileName); |
|
166 | - } |
|
167 | - |
|
168 | - // Refresh the file content |
|
169 | - try { |
|
170 | - $responseJson = $this->fetch($ETag, $content); |
|
171 | - $file->putContent(json_encode($responseJson)); |
|
172 | - return json_decode($file->getContent(), true)['data']; |
|
173 | - } catch (ConnectException $e) { |
|
174 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']); |
|
175 | - return []; |
|
176 | - } catch (\Exception $e) { |
|
177 | - $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]); |
|
178 | - return []; |
|
179 | - } |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Get the currently Nextcloud version |
|
184 | - * @return string |
|
185 | - */ |
|
186 | - protected function getVersion() { |
|
187 | - if ($this->version === null) { |
|
188 | - $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
189 | - } |
|
190 | - return $this->version; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Set the current Nextcloud version |
|
195 | - * @param string $version |
|
196 | - */ |
|
197 | - public function setVersion(string $version) { |
|
198 | - $this->version = $version; |
|
199 | - } |
|
42 | + const INVALIDATE_AFTER_SECONDS = 300; |
|
43 | + |
|
44 | + /** @var IAppData */ |
|
45 | + protected $appData; |
|
46 | + /** @var IClientService */ |
|
47 | + protected $clientService; |
|
48 | + /** @var ITimeFactory */ |
|
49 | + protected $timeFactory; |
|
50 | + /** @var IConfig */ |
|
51 | + protected $config; |
|
52 | + /** @var Ilogger */ |
|
53 | + protected $logger; |
|
54 | + /** @var string */ |
|
55 | + protected $fileName; |
|
56 | + /** @var string */ |
|
57 | + protected $endpointUrl; |
|
58 | + /** @var string */ |
|
59 | + protected $version; |
|
60 | + |
|
61 | + /** |
|
62 | + * @param Factory $appDataFactory |
|
63 | + * @param IClientService $clientService |
|
64 | + * @param ITimeFactory $timeFactory |
|
65 | + * @param IConfig $config |
|
66 | + * @param ILogger $logger |
|
67 | + */ |
|
68 | + public function __construct(Factory $appDataFactory, |
|
69 | + IClientService $clientService, |
|
70 | + ITimeFactory $timeFactory, |
|
71 | + IConfig $config, |
|
72 | + ILogger $logger) { |
|
73 | + $this->appData = $appDataFactory->get('appstore'); |
|
74 | + $this->clientService = $clientService; |
|
75 | + $this->timeFactory = $timeFactory; |
|
76 | + $this->config = $config; |
|
77 | + $this->logger = $logger; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Fetches the response from the server |
|
82 | + * |
|
83 | + * @param string $ETag |
|
84 | + * @param string $content |
|
85 | + * |
|
86 | + * @return array |
|
87 | + */ |
|
88 | + protected function fetch($ETag, $content) { |
|
89 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
90 | + |
|
91 | + if (!$appstoreenabled) { |
|
92 | + return []; |
|
93 | + } |
|
94 | + |
|
95 | + $options = [ |
|
96 | + 'timeout' => 10, |
|
97 | + ]; |
|
98 | + |
|
99 | + if ($ETag !== '') { |
|
100 | + $options['headers'] = [ |
|
101 | + 'If-None-Match' => $ETag, |
|
102 | + ]; |
|
103 | + } |
|
104 | + |
|
105 | + $client = $this->clientService->newClient(); |
|
106 | + $response = $client->get($this->endpointUrl, $options); |
|
107 | + |
|
108 | + $responseJson = []; |
|
109 | + if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
|
110 | + $responseJson['data'] = json_decode($content, true); |
|
111 | + } else { |
|
112 | + $responseJson['data'] = json_decode($response->getBody(), true); |
|
113 | + $ETag = $response->getHeader('ETag'); |
|
114 | + } |
|
115 | + |
|
116 | + $responseJson['timestamp'] = $this->timeFactory->getTime(); |
|
117 | + $responseJson['ncversion'] = $this->getVersion(); |
|
118 | + if ($ETag !== '') { |
|
119 | + $responseJson['ETag'] = $ETag; |
|
120 | + } |
|
121 | + |
|
122 | + return $responseJson; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns the array with the categories on the appstore server |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function get() { |
|
131 | + $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
|
132 | + $internetavailable = $this->config->getSystemValue('has_internet_connection', true); |
|
133 | + |
|
134 | + if (!$appstoreenabled || !$internetavailable) { |
|
135 | + return []; |
|
136 | + } |
|
137 | + |
|
138 | + $rootFolder = $this->appData->getFolder('/'); |
|
139 | + |
|
140 | + $ETag = ''; |
|
141 | + $content = ''; |
|
142 | + |
|
143 | + try { |
|
144 | + // File does already exists |
|
145 | + $file = $rootFolder->getFile($this->fileName); |
|
146 | + $jsonBlob = json_decode($file->getContent(), true); |
|
147 | + if (is_array($jsonBlob)) { |
|
148 | + |
|
149 | + // No caching when the version has been updated |
|
150 | + if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
|
151 | + |
|
152 | + // If the timestamp is older than 300 seconds request the files new |
|
153 | + if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
|
154 | + return $jsonBlob['data']; |
|
155 | + } |
|
156 | + |
|
157 | + if (isset($jsonBlob['ETag'])) { |
|
158 | + $ETag = $jsonBlob['ETag']; |
|
159 | + $content = json_encode($jsonBlob['data']); |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + } catch (NotFoundException $e) { |
|
164 | + // File does not already exists |
|
165 | + $file = $rootFolder->newFile($this->fileName); |
|
166 | + } |
|
167 | + |
|
168 | + // Refresh the file content |
|
169 | + try { |
|
170 | + $responseJson = $this->fetch($ETag, $content); |
|
171 | + $file->putContent(json_encode($responseJson)); |
|
172 | + return json_decode($file->getContent(), true)['data']; |
|
173 | + } catch (ConnectException $e) { |
|
174 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']); |
|
175 | + return []; |
|
176 | + } catch (\Exception $e) { |
|
177 | + $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]); |
|
178 | + return []; |
|
179 | + } |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Get the currently Nextcloud version |
|
184 | + * @return string |
|
185 | + */ |
|
186 | + protected function getVersion() { |
|
187 | + if ($this->version === null) { |
|
188 | + $this->version = $this->config->getSystemValue('version', '0.0.0'); |
|
189 | + } |
|
190 | + return $this->version; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Set the current Nextcloud version |
|
195 | + * @param string $version |
|
196 | + */ |
|
197 | + public function setVersion(string $version) { |
|
198 | + $this->version = $version; |
|
199 | + } |
|
200 | 200 | } |
@@ -34,7 +34,6 @@ |
||
34 | 34 | namespace OC\Files\Storage; |
35 | 35 | |
36 | 36 | use Exception; |
37 | -use GuzzleHttp\Exception\RequestException; |
|
38 | 37 | use OCP\ILogger; |
39 | 38 | use Psr\Http\Message\ResponseInterface; |
40 | 39 | use Icewind\Streams\CallbackWrapper; |
@@ -58,801 +58,801 @@ |
||
58 | 58 | * @package OC\Files\Storage |
59 | 59 | */ |
60 | 60 | class DAV extends Common { |
61 | - /** @var string */ |
|
62 | - protected $password; |
|
63 | - /** @var string */ |
|
64 | - protected $user; |
|
65 | - /** @var string */ |
|
66 | - protected $authType; |
|
67 | - /** @var string */ |
|
68 | - protected $host; |
|
69 | - /** @var bool */ |
|
70 | - protected $secure; |
|
71 | - /** @var string */ |
|
72 | - protected $root; |
|
73 | - /** @var string */ |
|
74 | - protected $certPath; |
|
75 | - /** @var bool */ |
|
76 | - protected $ready; |
|
77 | - /** @var Client */ |
|
78 | - protected $client; |
|
79 | - /** @var ArrayCache */ |
|
80 | - protected $statCache; |
|
81 | - /** @var \OCP\Http\Client\IClientService */ |
|
82 | - protected $httpClientService; |
|
83 | - |
|
84 | - /** |
|
85 | - * @param array $params |
|
86 | - * @throws \Exception |
|
87 | - */ |
|
88 | - public function __construct($params) { |
|
89 | - $this->statCache = new ArrayCache(); |
|
90 | - $this->httpClientService = \OC::$server->getHTTPClientService(); |
|
91 | - if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
92 | - $host = $params['host']; |
|
93 | - //remove leading http[s], will be generated in createBaseUri() |
|
94 | - if (substr($host, 0, 8) == "https://") $host = substr($host, 8); |
|
95 | - else if (substr($host, 0, 7) == "http://") $host = substr($host, 7); |
|
96 | - $this->host = $host; |
|
97 | - $this->user = $params['user']; |
|
98 | - $this->password = $params['password']; |
|
99 | - if (isset($params['authType'])) { |
|
100 | - $this->authType = $params['authType']; |
|
101 | - } |
|
102 | - if (isset($params['secure'])) { |
|
103 | - if (is_string($params['secure'])) { |
|
104 | - $this->secure = ($params['secure'] === 'true'); |
|
105 | - } else { |
|
106 | - $this->secure = (bool)$params['secure']; |
|
107 | - } |
|
108 | - } else { |
|
109 | - $this->secure = false; |
|
110 | - } |
|
111 | - if ($this->secure === true) { |
|
112 | - // inject mock for testing |
|
113 | - $certManager = \OC::$server->getCertificateManager(); |
|
114 | - if (is_null($certManager)) { //no user |
|
115 | - $certManager = \OC::$server->getCertificateManager(null); |
|
116 | - } |
|
117 | - $certPath = $certManager->getAbsoluteBundlePath(); |
|
118 | - if (file_exists($certPath)) { |
|
119 | - $this->certPath = $certPath; |
|
120 | - } |
|
121 | - } |
|
122 | - $this->root = $params['root'] ?? '/'; |
|
123 | - $this->root = '/' . ltrim($this->root, '/'); |
|
124 | - $this->root = rtrim($this->root, '/') . '/'; |
|
125 | - } else { |
|
126 | - throw new \Exception('Invalid webdav storage configuration'); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - protected function init() { |
|
131 | - if ($this->ready) { |
|
132 | - return; |
|
133 | - } |
|
134 | - $this->ready = true; |
|
135 | - |
|
136 | - $settings = [ |
|
137 | - 'baseUri' => $this->createBaseUri(), |
|
138 | - 'userName' => $this->user, |
|
139 | - 'password' => $this->password, |
|
140 | - ]; |
|
141 | - if (isset($this->authType)) { |
|
142 | - $settings['authType'] = $this->authType; |
|
143 | - } |
|
144 | - |
|
145 | - $proxy = \OC::$server->getConfig()->getSystemValue('proxy', ''); |
|
146 | - if ($proxy !== '') { |
|
147 | - $settings['proxy'] = $proxy; |
|
148 | - } |
|
149 | - |
|
150 | - $this->client = new Client($settings); |
|
151 | - $this->client->setThrowExceptions(true); |
|
152 | - if ($this->secure === true && $this->certPath) { |
|
153 | - $this->client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Clear the stat cache |
|
159 | - */ |
|
160 | - public function clearStatCache() { |
|
161 | - $this->statCache->clear(); |
|
162 | - } |
|
163 | - |
|
164 | - /** {@inheritdoc} */ |
|
165 | - public function getId() { |
|
166 | - return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
167 | - } |
|
168 | - |
|
169 | - /** {@inheritdoc} */ |
|
170 | - public function createBaseUri() { |
|
171 | - $baseUri = 'http'; |
|
172 | - if ($this->secure) { |
|
173 | - $baseUri .= 's'; |
|
174 | - } |
|
175 | - $baseUri .= '://' . $this->host . $this->root; |
|
176 | - return $baseUri; |
|
177 | - } |
|
178 | - |
|
179 | - /** {@inheritdoc} */ |
|
180 | - public function mkdir($path) { |
|
181 | - $this->init(); |
|
182 | - $path = $this->cleanPath($path); |
|
183 | - $result = $this->simpleResponse('MKCOL', $path, null, 201); |
|
184 | - if ($result) { |
|
185 | - $this->statCache->set($path, true); |
|
186 | - } |
|
187 | - return $result; |
|
188 | - } |
|
189 | - |
|
190 | - /** {@inheritdoc} */ |
|
191 | - public function rmdir($path) { |
|
192 | - $this->init(); |
|
193 | - $path = $this->cleanPath($path); |
|
194 | - // FIXME: some WebDAV impl return 403 when trying to DELETE |
|
195 | - // a non-empty folder |
|
196 | - $result = $this->simpleResponse('DELETE', $path . '/', null, 204); |
|
197 | - $this->statCache->clear($path . '/'); |
|
198 | - $this->statCache->remove($path); |
|
199 | - return $result; |
|
200 | - } |
|
201 | - |
|
202 | - /** {@inheritdoc} */ |
|
203 | - public function opendir($path) { |
|
204 | - $this->init(); |
|
205 | - $path = $this->cleanPath($path); |
|
206 | - try { |
|
207 | - $response = $this->client->propFind( |
|
208 | - $this->encodePath($path), |
|
209 | - ['{DAV:}href'], |
|
210 | - 1 |
|
211 | - ); |
|
212 | - if ($response === false) { |
|
213 | - return false; |
|
214 | - } |
|
215 | - $content = []; |
|
216 | - $files = array_keys($response); |
|
217 | - array_shift($files); //the first entry is the current directory |
|
218 | - |
|
219 | - if (!$this->statCache->hasKey($path)) { |
|
220 | - $this->statCache->set($path, true); |
|
221 | - } |
|
222 | - foreach ($files as $file) { |
|
223 | - $file = urldecode($file); |
|
224 | - // do not store the real entry, we might not have all properties |
|
225 | - if (!$this->statCache->hasKey($path)) { |
|
226 | - $this->statCache->set($file, true); |
|
227 | - } |
|
228 | - $file = basename($file); |
|
229 | - $content[] = $file; |
|
230 | - } |
|
231 | - return IteratorDirectory::wrap($content); |
|
232 | - } catch (\Exception $e) { |
|
233 | - $this->convertException($e, $path); |
|
234 | - } |
|
235 | - return false; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Propfind call with cache handling. |
|
240 | - * |
|
241 | - * First checks if information is cached. |
|
242 | - * If not, request it from the server then store to cache. |
|
243 | - * |
|
244 | - * @param string $path path to propfind |
|
245 | - * |
|
246 | - * @return array|boolean propfind response or false if the entry was not found |
|
247 | - * |
|
248 | - * @throws ClientHttpException |
|
249 | - */ |
|
250 | - protected function propfind($path) { |
|
251 | - $path = $this->cleanPath($path); |
|
252 | - $cachedResponse = $this->statCache->get($path); |
|
253 | - // we either don't know it, or we know it exists but need more details |
|
254 | - if (is_null($cachedResponse) || $cachedResponse === true) { |
|
255 | - $this->init(); |
|
256 | - try { |
|
257 | - $response = $this->client->propFind( |
|
258 | - $this->encodePath($path), |
|
259 | - array( |
|
260 | - '{DAV:}getlastmodified', |
|
261 | - '{DAV:}getcontentlength', |
|
262 | - '{DAV:}getcontenttype', |
|
263 | - '{http://owncloud.org/ns}permissions', |
|
264 | - '{http://open-collaboration-services.org/ns}share-permissions', |
|
265 | - '{DAV:}resourcetype', |
|
266 | - '{DAV:}getetag', |
|
267 | - ) |
|
268 | - ); |
|
269 | - $this->statCache->set($path, $response); |
|
270 | - } catch (ClientHttpException $e) { |
|
271 | - if ($e->getHttpStatus() === 404) { |
|
272 | - $this->statCache->clear($path . '/'); |
|
273 | - $this->statCache->set($path, false); |
|
274 | - return false; |
|
275 | - } |
|
276 | - $this->convertException($e, $path); |
|
277 | - } catch (\Exception $e) { |
|
278 | - $this->convertException($e, $path); |
|
279 | - } |
|
280 | - } else { |
|
281 | - $response = $cachedResponse; |
|
282 | - } |
|
283 | - return $response; |
|
284 | - } |
|
285 | - |
|
286 | - /** {@inheritdoc} */ |
|
287 | - public function filetype($path) { |
|
288 | - try { |
|
289 | - $response = $this->propfind($path); |
|
290 | - if ($response === false) { |
|
291 | - return false; |
|
292 | - } |
|
293 | - $responseType = []; |
|
294 | - if (isset($response["{DAV:}resourcetype"])) { |
|
295 | - /** @var ResourceType[] $response */ |
|
296 | - $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
297 | - } |
|
298 | - return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
299 | - } catch (\Exception $e) { |
|
300 | - $this->convertException($e, $path); |
|
301 | - } |
|
302 | - return false; |
|
303 | - } |
|
304 | - |
|
305 | - /** {@inheritdoc} */ |
|
306 | - public function file_exists($path) { |
|
307 | - try { |
|
308 | - $path = $this->cleanPath($path); |
|
309 | - $cachedState = $this->statCache->get($path); |
|
310 | - if ($cachedState === false) { |
|
311 | - // we know the file doesn't exist |
|
312 | - return false; |
|
313 | - } else if (!is_null($cachedState)) { |
|
314 | - return true; |
|
315 | - } |
|
316 | - // need to get from server |
|
317 | - return ($this->propfind($path) !== false); |
|
318 | - } catch (\Exception $e) { |
|
319 | - $this->convertException($e, $path); |
|
320 | - } |
|
321 | - return false; |
|
322 | - } |
|
323 | - |
|
324 | - /** {@inheritdoc} */ |
|
325 | - public function unlink($path) { |
|
326 | - $this->init(); |
|
327 | - $path = $this->cleanPath($path); |
|
328 | - $result = $this->simpleResponse('DELETE', $path, null, 204); |
|
329 | - $this->statCache->clear($path . '/'); |
|
330 | - $this->statCache->remove($path); |
|
331 | - return $result; |
|
332 | - } |
|
333 | - |
|
334 | - /** {@inheritdoc} */ |
|
335 | - public function fopen($path, $mode) { |
|
336 | - $this->init(); |
|
337 | - $path = $this->cleanPath($path); |
|
338 | - switch ($mode) { |
|
339 | - case 'r': |
|
340 | - case 'rb': |
|
341 | - try { |
|
342 | - $response = $this->httpClientService |
|
343 | - ->newClient() |
|
344 | - ->get($this->createBaseUri() . $this->encodePath($path), [ |
|
345 | - 'auth' => [$this->user, $this->password], |
|
346 | - 'stream' => true |
|
347 | - ]); |
|
348 | - } catch (\GuzzleHttp\Exception\ClientException $e) { |
|
349 | - if ($e->getResponse() instanceof ResponseInterface |
|
350 | - && $e->getResponse()->getStatusCode() === 404) { |
|
351 | - return false; |
|
352 | - } else { |
|
353 | - throw $e; |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - if ($response->getStatusCode() !== Http::STATUS_OK) { |
|
358 | - if ($response->getStatusCode() === Http::STATUS_LOCKED) { |
|
359 | - throw new \OCP\Lock\LockedException($path); |
|
360 | - } else { |
|
361 | - Util::writeLog("webdav client", 'Guzzle get returned status code ' . $response->getStatusCode(), ILogger::ERROR); |
|
362 | - } |
|
363 | - } |
|
364 | - |
|
365 | - return $response->getBody(); |
|
366 | - case 'w': |
|
367 | - case 'wb': |
|
368 | - case 'a': |
|
369 | - case 'ab': |
|
370 | - case 'r+': |
|
371 | - case 'w+': |
|
372 | - case 'wb+': |
|
373 | - case 'a+': |
|
374 | - case 'x': |
|
375 | - case 'x+': |
|
376 | - case 'c': |
|
377 | - case 'c+': |
|
378 | - //emulate these |
|
379 | - $tempManager = \OC::$server->getTempManager(); |
|
380 | - if (strrpos($path, '.') !== false) { |
|
381 | - $ext = substr($path, strrpos($path, '.')); |
|
382 | - } else { |
|
383 | - $ext = ''; |
|
384 | - } |
|
385 | - if ($this->file_exists($path)) { |
|
386 | - if (!$this->isUpdatable($path)) { |
|
387 | - return false; |
|
388 | - } |
|
389 | - if ($mode === 'w' or $mode === 'w+') { |
|
390 | - $tmpFile = $tempManager->getTemporaryFile($ext); |
|
391 | - } else { |
|
392 | - $tmpFile = $this->getCachedFile($path); |
|
393 | - } |
|
394 | - } else { |
|
395 | - if (!$this->isCreatable(dirname($path))) { |
|
396 | - return false; |
|
397 | - } |
|
398 | - $tmpFile = $tempManager->getTemporaryFile($ext); |
|
399 | - } |
|
400 | - $handle = fopen($tmpFile, $mode); |
|
401 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
402 | - $this->writeBack($tmpFile, $path); |
|
403 | - }); |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * @param string $tmpFile |
|
409 | - */ |
|
410 | - public function writeBack($tmpFile, $path) { |
|
411 | - $this->uploadFile($tmpFile, $path); |
|
412 | - unlink($tmpFile); |
|
413 | - } |
|
414 | - |
|
415 | - /** {@inheritdoc} */ |
|
416 | - public function free_space($path) { |
|
417 | - $this->init(); |
|
418 | - $path = $this->cleanPath($path); |
|
419 | - try { |
|
420 | - // TODO: cacheable ? |
|
421 | - $response = $this->client->propfind($this->encodePath($path), ['{DAV:}quota-available-bytes']); |
|
422 | - if ($response === false) { |
|
423 | - return FileInfo::SPACE_UNKNOWN; |
|
424 | - } |
|
425 | - if (isset($response['{DAV:}quota-available-bytes'])) { |
|
426 | - return (int)$response['{DAV:}quota-available-bytes']; |
|
427 | - } else { |
|
428 | - return FileInfo::SPACE_UNKNOWN; |
|
429 | - } |
|
430 | - } catch (\Exception $e) { |
|
431 | - return FileInfo::SPACE_UNKNOWN; |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - /** {@inheritdoc} */ |
|
436 | - public function touch($path, $mtime = null) { |
|
437 | - $this->init(); |
|
438 | - if (is_null($mtime)) { |
|
439 | - $mtime = time(); |
|
440 | - } |
|
441 | - $path = $this->cleanPath($path); |
|
442 | - |
|
443 | - // if file exists, update the mtime, else create a new empty file |
|
444 | - if ($this->file_exists($path)) { |
|
445 | - try { |
|
446 | - $this->statCache->remove($path); |
|
447 | - $this->client->proppatch($this->encodePath($path), ['{DAV:}lastmodified' => $mtime]); |
|
448 | - // non-owncloud clients might not have accepted the property, need to recheck it |
|
449 | - $response = $this->client->propfind($this->encodePath($path), ['{DAV:}getlastmodified'], 0); |
|
450 | - if ($response === false) { |
|
451 | - return false; |
|
452 | - } |
|
453 | - if (isset($response['{DAV:}getlastmodified'])) { |
|
454 | - $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
455 | - if ($remoteMtime !== $mtime) { |
|
456 | - // server has not accepted the mtime |
|
457 | - return false; |
|
458 | - } |
|
459 | - } |
|
460 | - } catch (ClientHttpException $e) { |
|
461 | - if ($e->getHttpStatus() === 501) { |
|
462 | - return false; |
|
463 | - } |
|
464 | - $this->convertException($e, $path); |
|
465 | - return false; |
|
466 | - } catch (\Exception $e) { |
|
467 | - $this->convertException($e, $path); |
|
468 | - return false; |
|
469 | - } |
|
470 | - } else { |
|
471 | - $this->file_put_contents($path, ''); |
|
472 | - } |
|
473 | - return true; |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * @param string $path |
|
478 | - * @param string $data |
|
479 | - * @return int |
|
480 | - */ |
|
481 | - public function file_put_contents($path, $data) { |
|
482 | - $path = $this->cleanPath($path); |
|
483 | - $result = parent::file_put_contents($path, $data); |
|
484 | - $this->statCache->remove($path); |
|
485 | - return $result; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * @param string $path |
|
490 | - * @param string $target |
|
491 | - */ |
|
492 | - protected function uploadFile($path, $target) { |
|
493 | - $this->init(); |
|
494 | - |
|
495 | - // invalidate |
|
496 | - $target = $this->cleanPath($target); |
|
497 | - $this->statCache->remove($target); |
|
498 | - $source = fopen($path, 'r'); |
|
499 | - |
|
500 | - $this->httpClientService |
|
501 | - ->newClient() |
|
502 | - ->put($this->createBaseUri() . $this->encodePath($target), [ |
|
503 | - 'body' => $source, |
|
504 | - 'auth' => [$this->user, $this->password] |
|
505 | - ]); |
|
506 | - |
|
507 | - $this->removeCachedFile($target); |
|
508 | - } |
|
509 | - |
|
510 | - /** {@inheritdoc} */ |
|
511 | - public function rename($path1, $path2) { |
|
512 | - $this->init(); |
|
513 | - $path1 = $this->cleanPath($path1); |
|
514 | - $path2 = $this->cleanPath($path2); |
|
515 | - try { |
|
516 | - // overwrite directory ? |
|
517 | - if ($this->is_dir($path2)) { |
|
518 | - // needs trailing slash in destination |
|
519 | - $path2 = rtrim($path2, '/') . '/'; |
|
520 | - } |
|
521 | - $this->client->request( |
|
522 | - 'MOVE', |
|
523 | - $this->encodePath($path1), |
|
524 | - null, |
|
525 | - [ |
|
526 | - 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
527 | - ] |
|
528 | - ); |
|
529 | - $this->statCache->clear($path1 . '/'); |
|
530 | - $this->statCache->clear($path2 . '/'); |
|
531 | - $this->statCache->set($path1, false); |
|
532 | - $this->statCache->set($path2, true); |
|
533 | - $this->removeCachedFile($path1); |
|
534 | - $this->removeCachedFile($path2); |
|
535 | - return true; |
|
536 | - } catch (\Exception $e) { |
|
537 | - $this->convertException($e); |
|
538 | - } |
|
539 | - return false; |
|
540 | - } |
|
541 | - |
|
542 | - /** {@inheritdoc} */ |
|
543 | - public function copy($path1, $path2) { |
|
544 | - $this->init(); |
|
545 | - $path1 = $this->cleanPath($path1); |
|
546 | - $path2 = $this->cleanPath($path2); |
|
547 | - try { |
|
548 | - // overwrite directory ? |
|
549 | - if ($this->is_dir($path2)) { |
|
550 | - // needs trailing slash in destination |
|
551 | - $path2 = rtrim($path2, '/') . '/'; |
|
552 | - } |
|
553 | - $this->client->request( |
|
554 | - 'COPY', |
|
555 | - $this->encodePath($path1), |
|
556 | - null, |
|
557 | - [ |
|
558 | - 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
559 | - ] |
|
560 | - ); |
|
561 | - $this->statCache->clear($path2 . '/'); |
|
562 | - $this->statCache->set($path2, true); |
|
563 | - $this->removeCachedFile($path2); |
|
564 | - return true; |
|
565 | - } catch (\Exception $e) { |
|
566 | - $this->convertException($e); |
|
567 | - } |
|
568 | - return false; |
|
569 | - } |
|
570 | - |
|
571 | - /** {@inheritdoc} */ |
|
572 | - public function stat($path) { |
|
573 | - try { |
|
574 | - $response = $this->propfind($path); |
|
575 | - if (!$response) { |
|
576 | - return false; |
|
577 | - } |
|
578 | - return [ |
|
579 | - 'mtime' => strtotime($response['{DAV:}getlastmodified']), |
|
580 | - 'size' => (int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0, |
|
581 | - ]; |
|
582 | - } catch (\Exception $e) { |
|
583 | - $this->convertException($e, $path); |
|
584 | - } |
|
585 | - return array(); |
|
586 | - } |
|
587 | - |
|
588 | - /** {@inheritdoc} */ |
|
589 | - public function getMimeType($path) { |
|
590 | - $remoteMimetype = $this->getMimeTypeFromRemote($path); |
|
591 | - if ($remoteMimetype === 'application/octet-stream') { |
|
592 | - return \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
593 | - } else { |
|
594 | - return $remoteMimetype; |
|
595 | - } |
|
596 | - } |
|
597 | - |
|
598 | - public function getMimeTypeFromRemote($path) { |
|
599 | - try { |
|
600 | - $response = $this->propfind($path); |
|
601 | - if ($response === false) { |
|
602 | - return false; |
|
603 | - } |
|
604 | - $responseType = []; |
|
605 | - if (isset($response["{DAV:}resourcetype"])) { |
|
606 | - /** @var ResourceType[] $response */ |
|
607 | - $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
608 | - } |
|
609 | - $type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
610 | - if ($type == 'dir') { |
|
611 | - return 'httpd/unix-directory'; |
|
612 | - } elseif (isset($response['{DAV:}getcontenttype'])) { |
|
613 | - return $response['{DAV:}getcontenttype']; |
|
614 | - } else { |
|
615 | - return 'application/octet-stream'; |
|
616 | - } |
|
617 | - } catch (\Exception $e) { |
|
618 | - return false; |
|
619 | - } |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * @param string $path |
|
624 | - * @return string |
|
625 | - */ |
|
626 | - public function cleanPath($path) { |
|
627 | - if ($path === '') { |
|
628 | - return $path; |
|
629 | - } |
|
630 | - $path = Filesystem::normalizePath($path); |
|
631 | - // remove leading slash |
|
632 | - return substr($path, 1); |
|
633 | - } |
|
634 | - |
|
635 | - /** |
|
636 | - * URL encodes the given path but keeps the slashes |
|
637 | - * |
|
638 | - * @param string $path to encode |
|
639 | - * @return string encoded path |
|
640 | - */ |
|
641 | - protected function encodePath($path) { |
|
642 | - // slashes need to stay |
|
643 | - return str_replace('%2F', '/', rawurlencode($path)); |
|
644 | - } |
|
645 | - |
|
646 | - /** |
|
647 | - * @param string $method |
|
648 | - * @param string $path |
|
649 | - * @param string|resource|null $body |
|
650 | - * @param int $expected |
|
651 | - * @return bool |
|
652 | - * @throws StorageInvalidException |
|
653 | - * @throws StorageNotAvailableException |
|
654 | - */ |
|
655 | - protected function simpleResponse($method, $path, $body, $expected) { |
|
656 | - $path = $this->cleanPath($path); |
|
657 | - try { |
|
658 | - $response = $this->client->request($method, $this->encodePath($path), $body); |
|
659 | - return $response['statusCode'] == $expected; |
|
660 | - } catch (ClientHttpException $e) { |
|
661 | - if ($e->getHttpStatus() === 404 && $method === 'DELETE') { |
|
662 | - $this->statCache->clear($path . '/'); |
|
663 | - $this->statCache->set($path, false); |
|
664 | - return false; |
|
665 | - } |
|
666 | - |
|
667 | - $this->convertException($e, $path); |
|
668 | - } catch (\Exception $e) { |
|
669 | - $this->convertException($e, $path); |
|
670 | - } |
|
671 | - return false; |
|
672 | - } |
|
673 | - |
|
674 | - /** |
|
675 | - * check if curl is installed |
|
676 | - */ |
|
677 | - public static function checkDependencies() { |
|
678 | - return true; |
|
679 | - } |
|
680 | - |
|
681 | - /** {@inheritdoc} */ |
|
682 | - public function isUpdatable($path) { |
|
683 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE); |
|
684 | - } |
|
685 | - |
|
686 | - /** {@inheritdoc} */ |
|
687 | - public function isCreatable($path) { |
|
688 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE); |
|
689 | - } |
|
690 | - |
|
691 | - /** {@inheritdoc} */ |
|
692 | - public function isSharable($path) { |
|
693 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE); |
|
694 | - } |
|
695 | - |
|
696 | - /** {@inheritdoc} */ |
|
697 | - public function isDeletable($path) { |
|
698 | - return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE); |
|
699 | - } |
|
700 | - |
|
701 | - /** {@inheritdoc} */ |
|
702 | - public function getPermissions($path) { |
|
703 | - $this->init(); |
|
704 | - $path = $this->cleanPath($path); |
|
705 | - $response = $this->propfind($path); |
|
706 | - if ($response === false) { |
|
707 | - return 0; |
|
708 | - } |
|
709 | - if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
710 | - return $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
711 | - } else if ($this->is_dir($path)) { |
|
712 | - return Constants::PERMISSION_ALL; |
|
713 | - } else if ($this->file_exists($path)) { |
|
714 | - return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE; |
|
715 | - } else { |
|
716 | - return 0; |
|
717 | - } |
|
718 | - } |
|
719 | - |
|
720 | - /** {@inheritdoc} */ |
|
721 | - public function getETag($path) { |
|
722 | - $this->init(); |
|
723 | - $path = $this->cleanPath($path); |
|
724 | - $response = $this->propfind($path); |
|
725 | - if ($response === false) { |
|
726 | - return null; |
|
727 | - } |
|
728 | - if (isset($response['{DAV:}getetag'])) { |
|
729 | - return trim($response['{DAV:}getetag'], '"'); |
|
730 | - } |
|
731 | - return parent::getEtag($path); |
|
732 | - } |
|
733 | - |
|
734 | - /** |
|
735 | - * @param string $permissionsString |
|
736 | - * @return int |
|
737 | - */ |
|
738 | - protected function parsePermissions($permissionsString) { |
|
739 | - $permissions = Constants::PERMISSION_READ; |
|
740 | - if (strpos($permissionsString, 'R') !== false) { |
|
741 | - $permissions |= Constants::PERMISSION_SHARE; |
|
742 | - } |
|
743 | - if (strpos($permissionsString, 'D') !== false) { |
|
744 | - $permissions |= Constants::PERMISSION_DELETE; |
|
745 | - } |
|
746 | - if (strpos($permissionsString, 'W') !== false) { |
|
747 | - $permissions |= Constants::PERMISSION_UPDATE; |
|
748 | - } |
|
749 | - if (strpos($permissionsString, 'CK') !== false) { |
|
750 | - $permissions |= Constants::PERMISSION_CREATE; |
|
751 | - $permissions |= Constants::PERMISSION_UPDATE; |
|
752 | - } |
|
753 | - return $permissions; |
|
754 | - } |
|
755 | - |
|
756 | - /** |
|
757 | - * check if a file or folder has been updated since $time |
|
758 | - * |
|
759 | - * @param string $path |
|
760 | - * @param int $time |
|
761 | - * @throws \OCP\Files\StorageNotAvailableException |
|
762 | - * @return bool |
|
763 | - */ |
|
764 | - public function hasUpdated($path, $time) { |
|
765 | - $this->init(); |
|
766 | - $path = $this->cleanPath($path); |
|
767 | - try { |
|
768 | - // force refresh for $path |
|
769 | - $this->statCache->remove($path); |
|
770 | - $response = $this->propfind($path); |
|
771 | - if ($response === false) { |
|
772 | - if ($path === '') { |
|
773 | - // if root is gone it means the storage is not available |
|
774 | - throw new StorageNotAvailableException('root is gone'); |
|
775 | - } |
|
776 | - return false; |
|
777 | - } |
|
778 | - if (isset($response['{DAV:}getetag'])) { |
|
779 | - $cachedData = $this->getCache()->get($path); |
|
780 | - $etag = null; |
|
781 | - if (isset($response['{DAV:}getetag'])) { |
|
782 | - $etag = trim($response['{DAV:}getetag'], '"'); |
|
783 | - } |
|
784 | - if (!empty($etag) && $cachedData['etag'] !== $etag) { |
|
785 | - return true; |
|
786 | - } else if (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) { |
|
787 | - $sharePermissions = (int)$response['{http://open-collaboration-services.org/ns}share-permissions']; |
|
788 | - return $sharePermissions !== $cachedData['permissions']; |
|
789 | - } else if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
790 | - $permissions = $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
791 | - return $permissions !== $cachedData['permissions']; |
|
792 | - } else { |
|
793 | - return false; |
|
794 | - } |
|
795 | - } else { |
|
796 | - $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
797 | - return $remoteMtime > $time; |
|
798 | - } |
|
799 | - } catch (ClientHttpException $e) { |
|
800 | - if ($e->getHttpStatus() === 405) { |
|
801 | - if ($path === '') { |
|
802 | - // if root is gone it means the storage is not available |
|
803 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
804 | - } |
|
805 | - return false; |
|
806 | - } |
|
807 | - $this->convertException($e, $path); |
|
808 | - return false; |
|
809 | - } catch (\Exception $e) { |
|
810 | - $this->convertException($e, $path); |
|
811 | - return false; |
|
812 | - } |
|
813 | - } |
|
814 | - |
|
815 | - /** |
|
816 | - * Interpret the given exception and decide whether it is due to an |
|
817 | - * unavailable storage, invalid storage or other. |
|
818 | - * This will either throw StorageInvalidException, StorageNotAvailableException |
|
819 | - * or do nothing. |
|
820 | - * |
|
821 | - * @param Exception $e sabre exception |
|
822 | - * @param string $path optional path from the operation |
|
823 | - * |
|
824 | - * @throws StorageInvalidException if the storage is invalid, for example |
|
825 | - * when the authentication expired or is invalid |
|
826 | - * @throws StorageNotAvailableException if the storage is not available, |
|
827 | - * which might be temporary |
|
828 | - */ |
|
829 | - protected function convertException(Exception $e, $path = '') { |
|
830 | - \OC::$server->getLogger()->logException($e, ['app' => 'files_external']); |
|
831 | - if ($e instanceof ClientHttpException) { |
|
832 | - if ($e->getHttpStatus() === Http::STATUS_LOCKED) { |
|
833 | - throw new \OCP\Lock\LockedException($path); |
|
834 | - } |
|
835 | - if ($e->getHttpStatus() === Http::STATUS_UNAUTHORIZED) { |
|
836 | - // either password was changed or was invalid all along |
|
837 | - throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage()); |
|
838 | - } else if ($e->getHttpStatus() === Http::STATUS_METHOD_NOT_ALLOWED) { |
|
839 | - // ignore exception for MethodNotAllowed, false will be returned |
|
840 | - return; |
|
841 | - } |
|
842 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
843 | - } else if ($e instanceof ClientException) { |
|
844 | - // connection timeout or refused, server could be temporarily down |
|
845 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
846 | - } else if ($e instanceof \InvalidArgumentException) { |
|
847 | - // parse error because the server returned HTML instead of XML, |
|
848 | - // possibly temporarily down |
|
849 | - throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
850 | - } else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) { |
|
851 | - // rethrow |
|
852 | - throw $e; |
|
853 | - } |
|
854 | - |
|
855 | - // TODO: only log for now, but in the future need to wrap/rethrow exception |
|
856 | - } |
|
61 | + /** @var string */ |
|
62 | + protected $password; |
|
63 | + /** @var string */ |
|
64 | + protected $user; |
|
65 | + /** @var string */ |
|
66 | + protected $authType; |
|
67 | + /** @var string */ |
|
68 | + protected $host; |
|
69 | + /** @var bool */ |
|
70 | + protected $secure; |
|
71 | + /** @var string */ |
|
72 | + protected $root; |
|
73 | + /** @var string */ |
|
74 | + protected $certPath; |
|
75 | + /** @var bool */ |
|
76 | + protected $ready; |
|
77 | + /** @var Client */ |
|
78 | + protected $client; |
|
79 | + /** @var ArrayCache */ |
|
80 | + protected $statCache; |
|
81 | + /** @var \OCP\Http\Client\IClientService */ |
|
82 | + protected $httpClientService; |
|
83 | + |
|
84 | + /** |
|
85 | + * @param array $params |
|
86 | + * @throws \Exception |
|
87 | + */ |
|
88 | + public function __construct($params) { |
|
89 | + $this->statCache = new ArrayCache(); |
|
90 | + $this->httpClientService = \OC::$server->getHTTPClientService(); |
|
91 | + if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
92 | + $host = $params['host']; |
|
93 | + //remove leading http[s], will be generated in createBaseUri() |
|
94 | + if (substr($host, 0, 8) == "https://") $host = substr($host, 8); |
|
95 | + else if (substr($host, 0, 7) == "http://") $host = substr($host, 7); |
|
96 | + $this->host = $host; |
|
97 | + $this->user = $params['user']; |
|
98 | + $this->password = $params['password']; |
|
99 | + if (isset($params['authType'])) { |
|
100 | + $this->authType = $params['authType']; |
|
101 | + } |
|
102 | + if (isset($params['secure'])) { |
|
103 | + if (is_string($params['secure'])) { |
|
104 | + $this->secure = ($params['secure'] === 'true'); |
|
105 | + } else { |
|
106 | + $this->secure = (bool)$params['secure']; |
|
107 | + } |
|
108 | + } else { |
|
109 | + $this->secure = false; |
|
110 | + } |
|
111 | + if ($this->secure === true) { |
|
112 | + // inject mock for testing |
|
113 | + $certManager = \OC::$server->getCertificateManager(); |
|
114 | + if (is_null($certManager)) { //no user |
|
115 | + $certManager = \OC::$server->getCertificateManager(null); |
|
116 | + } |
|
117 | + $certPath = $certManager->getAbsoluteBundlePath(); |
|
118 | + if (file_exists($certPath)) { |
|
119 | + $this->certPath = $certPath; |
|
120 | + } |
|
121 | + } |
|
122 | + $this->root = $params['root'] ?? '/'; |
|
123 | + $this->root = '/' . ltrim($this->root, '/'); |
|
124 | + $this->root = rtrim($this->root, '/') . '/'; |
|
125 | + } else { |
|
126 | + throw new \Exception('Invalid webdav storage configuration'); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + protected function init() { |
|
131 | + if ($this->ready) { |
|
132 | + return; |
|
133 | + } |
|
134 | + $this->ready = true; |
|
135 | + |
|
136 | + $settings = [ |
|
137 | + 'baseUri' => $this->createBaseUri(), |
|
138 | + 'userName' => $this->user, |
|
139 | + 'password' => $this->password, |
|
140 | + ]; |
|
141 | + if (isset($this->authType)) { |
|
142 | + $settings['authType'] = $this->authType; |
|
143 | + } |
|
144 | + |
|
145 | + $proxy = \OC::$server->getConfig()->getSystemValue('proxy', ''); |
|
146 | + if ($proxy !== '') { |
|
147 | + $settings['proxy'] = $proxy; |
|
148 | + } |
|
149 | + |
|
150 | + $this->client = new Client($settings); |
|
151 | + $this->client->setThrowExceptions(true); |
|
152 | + if ($this->secure === true && $this->certPath) { |
|
153 | + $this->client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Clear the stat cache |
|
159 | + */ |
|
160 | + public function clearStatCache() { |
|
161 | + $this->statCache->clear(); |
|
162 | + } |
|
163 | + |
|
164 | + /** {@inheritdoc} */ |
|
165 | + public function getId() { |
|
166 | + return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
167 | + } |
|
168 | + |
|
169 | + /** {@inheritdoc} */ |
|
170 | + public function createBaseUri() { |
|
171 | + $baseUri = 'http'; |
|
172 | + if ($this->secure) { |
|
173 | + $baseUri .= 's'; |
|
174 | + } |
|
175 | + $baseUri .= '://' . $this->host . $this->root; |
|
176 | + return $baseUri; |
|
177 | + } |
|
178 | + |
|
179 | + /** {@inheritdoc} */ |
|
180 | + public function mkdir($path) { |
|
181 | + $this->init(); |
|
182 | + $path = $this->cleanPath($path); |
|
183 | + $result = $this->simpleResponse('MKCOL', $path, null, 201); |
|
184 | + if ($result) { |
|
185 | + $this->statCache->set($path, true); |
|
186 | + } |
|
187 | + return $result; |
|
188 | + } |
|
189 | + |
|
190 | + /** {@inheritdoc} */ |
|
191 | + public function rmdir($path) { |
|
192 | + $this->init(); |
|
193 | + $path = $this->cleanPath($path); |
|
194 | + // FIXME: some WebDAV impl return 403 when trying to DELETE |
|
195 | + // a non-empty folder |
|
196 | + $result = $this->simpleResponse('DELETE', $path . '/', null, 204); |
|
197 | + $this->statCache->clear($path . '/'); |
|
198 | + $this->statCache->remove($path); |
|
199 | + return $result; |
|
200 | + } |
|
201 | + |
|
202 | + /** {@inheritdoc} */ |
|
203 | + public function opendir($path) { |
|
204 | + $this->init(); |
|
205 | + $path = $this->cleanPath($path); |
|
206 | + try { |
|
207 | + $response = $this->client->propFind( |
|
208 | + $this->encodePath($path), |
|
209 | + ['{DAV:}href'], |
|
210 | + 1 |
|
211 | + ); |
|
212 | + if ($response === false) { |
|
213 | + return false; |
|
214 | + } |
|
215 | + $content = []; |
|
216 | + $files = array_keys($response); |
|
217 | + array_shift($files); //the first entry is the current directory |
|
218 | + |
|
219 | + if (!$this->statCache->hasKey($path)) { |
|
220 | + $this->statCache->set($path, true); |
|
221 | + } |
|
222 | + foreach ($files as $file) { |
|
223 | + $file = urldecode($file); |
|
224 | + // do not store the real entry, we might not have all properties |
|
225 | + if (!$this->statCache->hasKey($path)) { |
|
226 | + $this->statCache->set($file, true); |
|
227 | + } |
|
228 | + $file = basename($file); |
|
229 | + $content[] = $file; |
|
230 | + } |
|
231 | + return IteratorDirectory::wrap($content); |
|
232 | + } catch (\Exception $e) { |
|
233 | + $this->convertException($e, $path); |
|
234 | + } |
|
235 | + return false; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Propfind call with cache handling. |
|
240 | + * |
|
241 | + * First checks if information is cached. |
|
242 | + * If not, request it from the server then store to cache. |
|
243 | + * |
|
244 | + * @param string $path path to propfind |
|
245 | + * |
|
246 | + * @return array|boolean propfind response or false if the entry was not found |
|
247 | + * |
|
248 | + * @throws ClientHttpException |
|
249 | + */ |
|
250 | + protected function propfind($path) { |
|
251 | + $path = $this->cleanPath($path); |
|
252 | + $cachedResponse = $this->statCache->get($path); |
|
253 | + // we either don't know it, or we know it exists but need more details |
|
254 | + if (is_null($cachedResponse) || $cachedResponse === true) { |
|
255 | + $this->init(); |
|
256 | + try { |
|
257 | + $response = $this->client->propFind( |
|
258 | + $this->encodePath($path), |
|
259 | + array( |
|
260 | + '{DAV:}getlastmodified', |
|
261 | + '{DAV:}getcontentlength', |
|
262 | + '{DAV:}getcontenttype', |
|
263 | + '{http://owncloud.org/ns}permissions', |
|
264 | + '{http://open-collaboration-services.org/ns}share-permissions', |
|
265 | + '{DAV:}resourcetype', |
|
266 | + '{DAV:}getetag', |
|
267 | + ) |
|
268 | + ); |
|
269 | + $this->statCache->set($path, $response); |
|
270 | + } catch (ClientHttpException $e) { |
|
271 | + if ($e->getHttpStatus() === 404) { |
|
272 | + $this->statCache->clear($path . '/'); |
|
273 | + $this->statCache->set($path, false); |
|
274 | + return false; |
|
275 | + } |
|
276 | + $this->convertException($e, $path); |
|
277 | + } catch (\Exception $e) { |
|
278 | + $this->convertException($e, $path); |
|
279 | + } |
|
280 | + } else { |
|
281 | + $response = $cachedResponse; |
|
282 | + } |
|
283 | + return $response; |
|
284 | + } |
|
285 | + |
|
286 | + /** {@inheritdoc} */ |
|
287 | + public function filetype($path) { |
|
288 | + try { |
|
289 | + $response = $this->propfind($path); |
|
290 | + if ($response === false) { |
|
291 | + return false; |
|
292 | + } |
|
293 | + $responseType = []; |
|
294 | + if (isset($response["{DAV:}resourcetype"])) { |
|
295 | + /** @var ResourceType[] $response */ |
|
296 | + $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
297 | + } |
|
298 | + return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
299 | + } catch (\Exception $e) { |
|
300 | + $this->convertException($e, $path); |
|
301 | + } |
|
302 | + return false; |
|
303 | + } |
|
304 | + |
|
305 | + /** {@inheritdoc} */ |
|
306 | + public function file_exists($path) { |
|
307 | + try { |
|
308 | + $path = $this->cleanPath($path); |
|
309 | + $cachedState = $this->statCache->get($path); |
|
310 | + if ($cachedState === false) { |
|
311 | + // we know the file doesn't exist |
|
312 | + return false; |
|
313 | + } else if (!is_null($cachedState)) { |
|
314 | + return true; |
|
315 | + } |
|
316 | + // need to get from server |
|
317 | + return ($this->propfind($path) !== false); |
|
318 | + } catch (\Exception $e) { |
|
319 | + $this->convertException($e, $path); |
|
320 | + } |
|
321 | + return false; |
|
322 | + } |
|
323 | + |
|
324 | + /** {@inheritdoc} */ |
|
325 | + public function unlink($path) { |
|
326 | + $this->init(); |
|
327 | + $path = $this->cleanPath($path); |
|
328 | + $result = $this->simpleResponse('DELETE', $path, null, 204); |
|
329 | + $this->statCache->clear($path . '/'); |
|
330 | + $this->statCache->remove($path); |
|
331 | + return $result; |
|
332 | + } |
|
333 | + |
|
334 | + /** {@inheritdoc} */ |
|
335 | + public function fopen($path, $mode) { |
|
336 | + $this->init(); |
|
337 | + $path = $this->cleanPath($path); |
|
338 | + switch ($mode) { |
|
339 | + case 'r': |
|
340 | + case 'rb': |
|
341 | + try { |
|
342 | + $response = $this->httpClientService |
|
343 | + ->newClient() |
|
344 | + ->get($this->createBaseUri() . $this->encodePath($path), [ |
|
345 | + 'auth' => [$this->user, $this->password], |
|
346 | + 'stream' => true |
|
347 | + ]); |
|
348 | + } catch (\GuzzleHttp\Exception\ClientException $e) { |
|
349 | + if ($e->getResponse() instanceof ResponseInterface |
|
350 | + && $e->getResponse()->getStatusCode() === 404) { |
|
351 | + return false; |
|
352 | + } else { |
|
353 | + throw $e; |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + if ($response->getStatusCode() !== Http::STATUS_OK) { |
|
358 | + if ($response->getStatusCode() === Http::STATUS_LOCKED) { |
|
359 | + throw new \OCP\Lock\LockedException($path); |
|
360 | + } else { |
|
361 | + Util::writeLog("webdav client", 'Guzzle get returned status code ' . $response->getStatusCode(), ILogger::ERROR); |
|
362 | + } |
|
363 | + } |
|
364 | + |
|
365 | + return $response->getBody(); |
|
366 | + case 'w': |
|
367 | + case 'wb': |
|
368 | + case 'a': |
|
369 | + case 'ab': |
|
370 | + case 'r+': |
|
371 | + case 'w+': |
|
372 | + case 'wb+': |
|
373 | + case 'a+': |
|
374 | + case 'x': |
|
375 | + case 'x+': |
|
376 | + case 'c': |
|
377 | + case 'c+': |
|
378 | + //emulate these |
|
379 | + $tempManager = \OC::$server->getTempManager(); |
|
380 | + if (strrpos($path, '.') !== false) { |
|
381 | + $ext = substr($path, strrpos($path, '.')); |
|
382 | + } else { |
|
383 | + $ext = ''; |
|
384 | + } |
|
385 | + if ($this->file_exists($path)) { |
|
386 | + if (!$this->isUpdatable($path)) { |
|
387 | + return false; |
|
388 | + } |
|
389 | + if ($mode === 'w' or $mode === 'w+') { |
|
390 | + $tmpFile = $tempManager->getTemporaryFile($ext); |
|
391 | + } else { |
|
392 | + $tmpFile = $this->getCachedFile($path); |
|
393 | + } |
|
394 | + } else { |
|
395 | + if (!$this->isCreatable(dirname($path))) { |
|
396 | + return false; |
|
397 | + } |
|
398 | + $tmpFile = $tempManager->getTemporaryFile($ext); |
|
399 | + } |
|
400 | + $handle = fopen($tmpFile, $mode); |
|
401 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
402 | + $this->writeBack($tmpFile, $path); |
|
403 | + }); |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * @param string $tmpFile |
|
409 | + */ |
|
410 | + public function writeBack($tmpFile, $path) { |
|
411 | + $this->uploadFile($tmpFile, $path); |
|
412 | + unlink($tmpFile); |
|
413 | + } |
|
414 | + |
|
415 | + /** {@inheritdoc} */ |
|
416 | + public function free_space($path) { |
|
417 | + $this->init(); |
|
418 | + $path = $this->cleanPath($path); |
|
419 | + try { |
|
420 | + // TODO: cacheable ? |
|
421 | + $response = $this->client->propfind($this->encodePath($path), ['{DAV:}quota-available-bytes']); |
|
422 | + if ($response === false) { |
|
423 | + return FileInfo::SPACE_UNKNOWN; |
|
424 | + } |
|
425 | + if (isset($response['{DAV:}quota-available-bytes'])) { |
|
426 | + return (int)$response['{DAV:}quota-available-bytes']; |
|
427 | + } else { |
|
428 | + return FileInfo::SPACE_UNKNOWN; |
|
429 | + } |
|
430 | + } catch (\Exception $e) { |
|
431 | + return FileInfo::SPACE_UNKNOWN; |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + /** {@inheritdoc} */ |
|
436 | + public function touch($path, $mtime = null) { |
|
437 | + $this->init(); |
|
438 | + if (is_null($mtime)) { |
|
439 | + $mtime = time(); |
|
440 | + } |
|
441 | + $path = $this->cleanPath($path); |
|
442 | + |
|
443 | + // if file exists, update the mtime, else create a new empty file |
|
444 | + if ($this->file_exists($path)) { |
|
445 | + try { |
|
446 | + $this->statCache->remove($path); |
|
447 | + $this->client->proppatch($this->encodePath($path), ['{DAV:}lastmodified' => $mtime]); |
|
448 | + // non-owncloud clients might not have accepted the property, need to recheck it |
|
449 | + $response = $this->client->propfind($this->encodePath($path), ['{DAV:}getlastmodified'], 0); |
|
450 | + if ($response === false) { |
|
451 | + return false; |
|
452 | + } |
|
453 | + if (isset($response['{DAV:}getlastmodified'])) { |
|
454 | + $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
455 | + if ($remoteMtime !== $mtime) { |
|
456 | + // server has not accepted the mtime |
|
457 | + return false; |
|
458 | + } |
|
459 | + } |
|
460 | + } catch (ClientHttpException $e) { |
|
461 | + if ($e->getHttpStatus() === 501) { |
|
462 | + return false; |
|
463 | + } |
|
464 | + $this->convertException($e, $path); |
|
465 | + return false; |
|
466 | + } catch (\Exception $e) { |
|
467 | + $this->convertException($e, $path); |
|
468 | + return false; |
|
469 | + } |
|
470 | + } else { |
|
471 | + $this->file_put_contents($path, ''); |
|
472 | + } |
|
473 | + return true; |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * @param string $path |
|
478 | + * @param string $data |
|
479 | + * @return int |
|
480 | + */ |
|
481 | + public function file_put_contents($path, $data) { |
|
482 | + $path = $this->cleanPath($path); |
|
483 | + $result = parent::file_put_contents($path, $data); |
|
484 | + $this->statCache->remove($path); |
|
485 | + return $result; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * @param string $path |
|
490 | + * @param string $target |
|
491 | + */ |
|
492 | + protected function uploadFile($path, $target) { |
|
493 | + $this->init(); |
|
494 | + |
|
495 | + // invalidate |
|
496 | + $target = $this->cleanPath($target); |
|
497 | + $this->statCache->remove($target); |
|
498 | + $source = fopen($path, 'r'); |
|
499 | + |
|
500 | + $this->httpClientService |
|
501 | + ->newClient() |
|
502 | + ->put($this->createBaseUri() . $this->encodePath($target), [ |
|
503 | + 'body' => $source, |
|
504 | + 'auth' => [$this->user, $this->password] |
|
505 | + ]); |
|
506 | + |
|
507 | + $this->removeCachedFile($target); |
|
508 | + } |
|
509 | + |
|
510 | + /** {@inheritdoc} */ |
|
511 | + public function rename($path1, $path2) { |
|
512 | + $this->init(); |
|
513 | + $path1 = $this->cleanPath($path1); |
|
514 | + $path2 = $this->cleanPath($path2); |
|
515 | + try { |
|
516 | + // overwrite directory ? |
|
517 | + if ($this->is_dir($path2)) { |
|
518 | + // needs trailing slash in destination |
|
519 | + $path2 = rtrim($path2, '/') . '/'; |
|
520 | + } |
|
521 | + $this->client->request( |
|
522 | + 'MOVE', |
|
523 | + $this->encodePath($path1), |
|
524 | + null, |
|
525 | + [ |
|
526 | + 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
527 | + ] |
|
528 | + ); |
|
529 | + $this->statCache->clear($path1 . '/'); |
|
530 | + $this->statCache->clear($path2 . '/'); |
|
531 | + $this->statCache->set($path1, false); |
|
532 | + $this->statCache->set($path2, true); |
|
533 | + $this->removeCachedFile($path1); |
|
534 | + $this->removeCachedFile($path2); |
|
535 | + return true; |
|
536 | + } catch (\Exception $e) { |
|
537 | + $this->convertException($e); |
|
538 | + } |
|
539 | + return false; |
|
540 | + } |
|
541 | + |
|
542 | + /** {@inheritdoc} */ |
|
543 | + public function copy($path1, $path2) { |
|
544 | + $this->init(); |
|
545 | + $path1 = $this->cleanPath($path1); |
|
546 | + $path2 = $this->cleanPath($path2); |
|
547 | + try { |
|
548 | + // overwrite directory ? |
|
549 | + if ($this->is_dir($path2)) { |
|
550 | + // needs trailing slash in destination |
|
551 | + $path2 = rtrim($path2, '/') . '/'; |
|
552 | + } |
|
553 | + $this->client->request( |
|
554 | + 'COPY', |
|
555 | + $this->encodePath($path1), |
|
556 | + null, |
|
557 | + [ |
|
558 | + 'Destination' => $this->createBaseUri() . $this->encodePath($path2), |
|
559 | + ] |
|
560 | + ); |
|
561 | + $this->statCache->clear($path2 . '/'); |
|
562 | + $this->statCache->set($path2, true); |
|
563 | + $this->removeCachedFile($path2); |
|
564 | + return true; |
|
565 | + } catch (\Exception $e) { |
|
566 | + $this->convertException($e); |
|
567 | + } |
|
568 | + return false; |
|
569 | + } |
|
570 | + |
|
571 | + /** {@inheritdoc} */ |
|
572 | + public function stat($path) { |
|
573 | + try { |
|
574 | + $response = $this->propfind($path); |
|
575 | + if (!$response) { |
|
576 | + return false; |
|
577 | + } |
|
578 | + return [ |
|
579 | + 'mtime' => strtotime($response['{DAV:}getlastmodified']), |
|
580 | + 'size' => (int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0, |
|
581 | + ]; |
|
582 | + } catch (\Exception $e) { |
|
583 | + $this->convertException($e, $path); |
|
584 | + } |
|
585 | + return array(); |
|
586 | + } |
|
587 | + |
|
588 | + /** {@inheritdoc} */ |
|
589 | + public function getMimeType($path) { |
|
590 | + $remoteMimetype = $this->getMimeTypeFromRemote($path); |
|
591 | + if ($remoteMimetype === 'application/octet-stream') { |
|
592 | + return \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
593 | + } else { |
|
594 | + return $remoteMimetype; |
|
595 | + } |
|
596 | + } |
|
597 | + |
|
598 | + public function getMimeTypeFromRemote($path) { |
|
599 | + try { |
|
600 | + $response = $this->propfind($path); |
|
601 | + if ($response === false) { |
|
602 | + return false; |
|
603 | + } |
|
604 | + $responseType = []; |
|
605 | + if (isset($response["{DAV:}resourcetype"])) { |
|
606 | + /** @var ResourceType[] $response */ |
|
607 | + $responseType = $response["{DAV:}resourcetype"]->getValue(); |
|
608 | + } |
|
609 | + $type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file'; |
|
610 | + if ($type == 'dir') { |
|
611 | + return 'httpd/unix-directory'; |
|
612 | + } elseif (isset($response['{DAV:}getcontenttype'])) { |
|
613 | + return $response['{DAV:}getcontenttype']; |
|
614 | + } else { |
|
615 | + return 'application/octet-stream'; |
|
616 | + } |
|
617 | + } catch (\Exception $e) { |
|
618 | + return false; |
|
619 | + } |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * @param string $path |
|
624 | + * @return string |
|
625 | + */ |
|
626 | + public function cleanPath($path) { |
|
627 | + if ($path === '') { |
|
628 | + return $path; |
|
629 | + } |
|
630 | + $path = Filesystem::normalizePath($path); |
|
631 | + // remove leading slash |
|
632 | + return substr($path, 1); |
|
633 | + } |
|
634 | + |
|
635 | + /** |
|
636 | + * URL encodes the given path but keeps the slashes |
|
637 | + * |
|
638 | + * @param string $path to encode |
|
639 | + * @return string encoded path |
|
640 | + */ |
|
641 | + protected function encodePath($path) { |
|
642 | + // slashes need to stay |
|
643 | + return str_replace('%2F', '/', rawurlencode($path)); |
|
644 | + } |
|
645 | + |
|
646 | + /** |
|
647 | + * @param string $method |
|
648 | + * @param string $path |
|
649 | + * @param string|resource|null $body |
|
650 | + * @param int $expected |
|
651 | + * @return bool |
|
652 | + * @throws StorageInvalidException |
|
653 | + * @throws StorageNotAvailableException |
|
654 | + */ |
|
655 | + protected function simpleResponse($method, $path, $body, $expected) { |
|
656 | + $path = $this->cleanPath($path); |
|
657 | + try { |
|
658 | + $response = $this->client->request($method, $this->encodePath($path), $body); |
|
659 | + return $response['statusCode'] == $expected; |
|
660 | + } catch (ClientHttpException $e) { |
|
661 | + if ($e->getHttpStatus() === 404 && $method === 'DELETE') { |
|
662 | + $this->statCache->clear($path . '/'); |
|
663 | + $this->statCache->set($path, false); |
|
664 | + return false; |
|
665 | + } |
|
666 | + |
|
667 | + $this->convertException($e, $path); |
|
668 | + } catch (\Exception $e) { |
|
669 | + $this->convertException($e, $path); |
|
670 | + } |
|
671 | + return false; |
|
672 | + } |
|
673 | + |
|
674 | + /** |
|
675 | + * check if curl is installed |
|
676 | + */ |
|
677 | + public static function checkDependencies() { |
|
678 | + return true; |
|
679 | + } |
|
680 | + |
|
681 | + /** {@inheritdoc} */ |
|
682 | + public function isUpdatable($path) { |
|
683 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE); |
|
684 | + } |
|
685 | + |
|
686 | + /** {@inheritdoc} */ |
|
687 | + public function isCreatable($path) { |
|
688 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE); |
|
689 | + } |
|
690 | + |
|
691 | + /** {@inheritdoc} */ |
|
692 | + public function isSharable($path) { |
|
693 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE); |
|
694 | + } |
|
695 | + |
|
696 | + /** {@inheritdoc} */ |
|
697 | + public function isDeletable($path) { |
|
698 | + return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE); |
|
699 | + } |
|
700 | + |
|
701 | + /** {@inheritdoc} */ |
|
702 | + public function getPermissions($path) { |
|
703 | + $this->init(); |
|
704 | + $path = $this->cleanPath($path); |
|
705 | + $response = $this->propfind($path); |
|
706 | + if ($response === false) { |
|
707 | + return 0; |
|
708 | + } |
|
709 | + if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
710 | + return $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
711 | + } else if ($this->is_dir($path)) { |
|
712 | + return Constants::PERMISSION_ALL; |
|
713 | + } else if ($this->file_exists($path)) { |
|
714 | + return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE; |
|
715 | + } else { |
|
716 | + return 0; |
|
717 | + } |
|
718 | + } |
|
719 | + |
|
720 | + /** {@inheritdoc} */ |
|
721 | + public function getETag($path) { |
|
722 | + $this->init(); |
|
723 | + $path = $this->cleanPath($path); |
|
724 | + $response = $this->propfind($path); |
|
725 | + if ($response === false) { |
|
726 | + return null; |
|
727 | + } |
|
728 | + if (isset($response['{DAV:}getetag'])) { |
|
729 | + return trim($response['{DAV:}getetag'], '"'); |
|
730 | + } |
|
731 | + return parent::getEtag($path); |
|
732 | + } |
|
733 | + |
|
734 | + /** |
|
735 | + * @param string $permissionsString |
|
736 | + * @return int |
|
737 | + */ |
|
738 | + protected function parsePermissions($permissionsString) { |
|
739 | + $permissions = Constants::PERMISSION_READ; |
|
740 | + if (strpos($permissionsString, 'R') !== false) { |
|
741 | + $permissions |= Constants::PERMISSION_SHARE; |
|
742 | + } |
|
743 | + if (strpos($permissionsString, 'D') !== false) { |
|
744 | + $permissions |= Constants::PERMISSION_DELETE; |
|
745 | + } |
|
746 | + if (strpos($permissionsString, 'W') !== false) { |
|
747 | + $permissions |= Constants::PERMISSION_UPDATE; |
|
748 | + } |
|
749 | + if (strpos($permissionsString, 'CK') !== false) { |
|
750 | + $permissions |= Constants::PERMISSION_CREATE; |
|
751 | + $permissions |= Constants::PERMISSION_UPDATE; |
|
752 | + } |
|
753 | + return $permissions; |
|
754 | + } |
|
755 | + |
|
756 | + /** |
|
757 | + * check if a file or folder has been updated since $time |
|
758 | + * |
|
759 | + * @param string $path |
|
760 | + * @param int $time |
|
761 | + * @throws \OCP\Files\StorageNotAvailableException |
|
762 | + * @return bool |
|
763 | + */ |
|
764 | + public function hasUpdated($path, $time) { |
|
765 | + $this->init(); |
|
766 | + $path = $this->cleanPath($path); |
|
767 | + try { |
|
768 | + // force refresh for $path |
|
769 | + $this->statCache->remove($path); |
|
770 | + $response = $this->propfind($path); |
|
771 | + if ($response === false) { |
|
772 | + if ($path === '') { |
|
773 | + // if root is gone it means the storage is not available |
|
774 | + throw new StorageNotAvailableException('root is gone'); |
|
775 | + } |
|
776 | + return false; |
|
777 | + } |
|
778 | + if (isset($response['{DAV:}getetag'])) { |
|
779 | + $cachedData = $this->getCache()->get($path); |
|
780 | + $etag = null; |
|
781 | + if (isset($response['{DAV:}getetag'])) { |
|
782 | + $etag = trim($response['{DAV:}getetag'], '"'); |
|
783 | + } |
|
784 | + if (!empty($etag) && $cachedData['etag'] !== $etag) { |
|
785 | + return true; |
|
786 | + } else if (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) { |
|
787 | + $sharePermissions = (int)$response['{http://open-collaboration-services.org/ns}share-permissions']; |
|
788 | + return $sharePermissions !== $cachedData['permissions']; |
|
789 | + } else if (isset($response['{http://owncloud.org/ns}permissions'])) { |
|
790 | + $permissions = $this->parsePermissions($response['{http://owncloud.org/ns}permissions']); |
|
791 | + return $permissions !== $cachedData['permissions']; |
|
792 | + } else { |
|
793 | + return false; |
|
794 | + } |
|
795 | + } else { |
|
796 | + $remoteMtime = strtotime($response['{DAV:}getlastmodified']); |
|
797 | + return $remoteMtime > $time; |
|
798 | + } |
|
799 | + } catch (ClientHttpException $e) { |
|
800 | + if ($e->getHttpStatus() === 405) { |
|
801 | + if ($path === '') { |
|
802 | + // if root is gone it means the storage is not available |
|
803 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
804 | + } |
|
805 | + return false; |
|
806 | + } |
|
807 | + $this->convertException($e, $path); |
|
808 | + return false; |
|
809 | + } catch (\Exception $e) { |
|
810 | + $this->convertException($e, $path); |
|
811 | + return false; |
|
812 | + } |
|
813 | + } |
|
814 | + |
|
815 | + /** |
|
816 | + * Interpret the given exception and decide whether it is due to an |
|
817 | + * unavailable storage, invalid storage or other. |
|
818 | + * This will either throw StorageInvalidException, StorageNotAvailableException |
|
819 | + * or do nothing. |
|
820 | + * |
|
821 | + * @param Exception $e sabre exception |
|
822 | + * @param string $path optional path from the operation |
|
823 | + * |
|
824 | + * @throws StorageInvalidException if the storage is invalid, for example |
|
825 | + * when the authentication expired or is invalid |
|
826 | + * @throws StorageNotAvailableException if the storage is not available, |
|
827 | + * which might be temporary |
|
828 | + */ |
|
829 | + protected function convertException(Exception $e, $path = '') { |
|
830 | + \OC::$server->getLogger()->logException($e, ['app' => 'files_external']); |
|
831 | + if ($e instanceof ClientHttpException) { |
|
832 | + if ($e->getHttpStatus() === Http::STATUS_LOCKED) { |
|
833 | + throw new \OCP\Lock\LockedException($path); |
|
834 | + } |
|
835 | + if ($e->getHttpStatus() === Http::STATUS_UNAUTHORIZED) { |
|
836 | + // either password was changed or was invalid all along |
|
837 | + throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage()); |
|
838 | + } else if ($e->getHttpStatus() === Http::STATUS_METHOD_NOT_ALLOWED) { |
|
839 | + // ignore exception for MethodNotAllowed, false will be returned |
|
840 | + return; |
|
841 | + } |
|
842 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
843 | + } else if ($e instanceof ClientException) { |
|
844 | + // connection timeout or refused, server could be temporarily down |
|
845 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
846 | + } else if ($e instanceof \InvalidArgumentException) { |
|
847 | + // parse error because the server returned HTML instead of XML, |
|
848 | + // possibly temporarily down |
|
849 | + throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); |
|
850 | + } else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) { |
|
851 | + // rethrow |
|
852 | + throw $e; |
|
853 | + } |
|
854 | + |
|
855 | + // TODO: only log for now, but in the future need to wrap/rethrow exception |
|
856 | + } |
|
857 | 857 | } |
858 | 858 |
@@ -36,13 +36,11 @@ |
||
36 | 36 | namespace OC; |
37 | 37 | |
38 | 38 | use InterfaSys\LogNormalizer\Normalizer; |
39 | - |
|
40 | 39 | use OC\Log\ExceptionSerializer; |
41 | 40 | use OCP\Log\IFileBased; |
42 | 41 | use OCP\Log\IWriter; |
43 | 42 | use OCP\ILogger; |
44 | 43 | use OCP\Support\CrashReport\IRegistry; |
45 | -use OCP\Util; |
|
46 | 44 | |
47 | 45 | /** |
48 | 46 | * logging utilities |
@@ -55,271 +55,271 @@ |
||
55 | 55 | */ |
56 | 56 | class Log implements ILogger { |
57 | 57 | |
58 | - /** @var IWriter */ |
|
59 | - private $logger; |
|
60 | - |
|
61 | - /** @var SystemConfig */ |
|
62 | - private $config; |
|
63 | - |
|
64 | - /** @var boolean|null cache the result of the log condition check for the request */ |
|
65 | - private $logConditionSatisfied = null; |
|
66 | - |
|
67 | - /** @var Normalizer */ |
|
68 | - private $normalizer; |
|
69 | - |
|
70 | - /** @var IRegistry */ |
|
71 | - private $crashReporters; |
|
72 | - |
|
73 | - /** |
|
74 | - * @param IWriter $logger The logger that should be used |
|
75 | - * @param SystemConfig $config the system config object |
|
76 | - * @param Normalizer|null $normalizer |
|
77 | - * @param IRegistry|null $registry |
|
78 | - */ |
|
79 | - public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
80 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
81 | - if ($config === null) { |
|
82 | - $config = \OC::$server->getSystemConfig(); |
|
83 | - } |
|
84 | - |
|
85 | - $this->config = $config; |
|
86 | - $this->logger = $logger; |
|
87 | - if ($normalizer === null) { |
|
88 | - $this->normalizer = new Normalizer(); |
|
89 | - } else { |
|
90 | - $this->normalizer = $normalizer; |
|
91 | - } |
|
92 | - $this->crashReporters = $registry; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * System is unusable. |
|
97 | - * |
|
98 | - * @param string $message |
|
99 | - * @param array $context |
|
100 | - * @return void |
|
101 | - */ |
|
102 | - public function emergency(string $message, array $context = []) { |
|
103 | - $this->log(ILogger::FATAL, $message, $context); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Action must be taken immediately. |
|
108 | - * |
|
109 | - * Example: Entire website down, database unavailable, etc. This should |
|
110 | - * trigger the SMS alerts and wake you up. |
|
111 | - * |
|
112 | - * @param string $message |
|
113 | - * @param array $context |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - public function alert(string $message, array $context = []) { |
|
117 | - $this->log(ILogger::ERROR, $message, $context); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Critical conditions. |
|
122 | - * |
|
123 | - * Example: Application component unavailable, unexpected exception. |
|
124 | - * |
|
125 | - * @param string $message |
|
126 | - * @param array $context |
|
127 | - * @return void |
|
128 | - */ |
|
129 | - public function critical(string $message, array $context = []) { |
|
130 | - $this->log(ILogger::ERROR, $message, $context); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Runtime errors that do not require immediate action but should typically |
|
135 | - * be logged and monitored. |
|
136 | - * |
|
137 | - * @param string $message |
|
138 | - * @param array $context |
|
139 | - * @return void |
|
140 | - */ |
|
141 | - public function error(string $message, array $context = []) { |
|
142 | - $this->log(ILogger::ERROR, $message, $context); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Exceptional occurrences that are not errors. |
|
147 | - * |
|
148 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
149 | - * that are not necessarily wrong. |
|
150 | - * |
|
151 | - * @param string $message |
|
152 | - * @param array $context |
|
153 | - * @return void |
|
154 | - */ |
|
155 | - public function warning(string $message, array $context = []) { |
|
156 | - $this->log(ILogger::WARN, $message, $context); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Normal but significant events. |
|
161 | - * |
|
162 | - * @param string $message |
|
163 | - * @param array $context |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public function notice(string $message, array $context = []) { |
|
167 | - $this->log(ILogger::INFO, $message, $context); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Interesting events. |
|
172 | - * |
|
173 | - * Example: User logs in, SQL logs. |
|
174 | - * |
|
175 | - * @param string $message |
|
176 | - * @param array $context |
|
177 | - * @return void |
|
178 | - */ |
|
179 | - public function info(string $message, array $context = []) { |
|
180 | - $this->log(ILogger::INFO, $message, $context); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Detailed debug information. |
|
185 | - * |
|
186 | - * @param string $message |
|
187 | - * @param array $context |
|
188 | - * @return void |
|
189 | - */ |
|
190 | - public function debug(string $message, array $context = []) { |
|
191 | - $this->log(ILogger::DEBUG, $message, $context); |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * Logs with an arbitrary level. |
|
197 | - * |
|
198 | - * @param int $level |
|
199 | - * @param string $message |
|
200 | - * @param array $context |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - public function log(int $level, string $message, array $context = []) { |
|
204 | - $minLevel = $this->getLogLevel($context); |
|
205 | - |
|
206 | - array_walk($context, [$this->normalizer, 'format']); |
|
207 | - |
|
208 | - $app = $context['app'] ?? 'no app in context'; |
|
209 | - |
|
210 | - // interpolate $message as defined in PSR-3 |
|
211 | - $replace = []; |
|
212 | - foreach ($context as $key => $val) { |
|
213 | - $replace['{' . $key . '}'] = $val; |
|
214 | - } |
|
215 | - $message = strtr($message, $replace); |
|
216 | - |
|
217 | - if ($level >= $minLevel) { |
|
218 | - $this->writeLog($app, $message, $level); |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - private function getLogLevel($context) { |
|
223 | - /** |
|
224 | - * check for a special log condition - this enables an increased log on |
|
225 | - * a per request/user base |
|
226 | - */ |
|
227 | - if ($this->logConditionSatisfied === null) { |
|
228 | - // default to false to just process this once per request |
|
229 | - $this->logConditionSatisfied = false; |
|
230 | - if (!empty($logCondition)) { |
|
231 | - |
|
232 | - // check for secret token in the request |
|
233 | - if (isset($logCondition['shared_secret'])) { |
|
234 | - $request = \OC::$server->getRequest(); |
|
235 | - |
|
236 | - // if token is found in the request change set the log condition to satisfied |
|
237 | - if ($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) { |
|
238 | - $this->logConditionSatisfied = true; |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - // check for user |
|
243 | - if (isset($logCondition['users'])) { |
|
244 | - $user = \OC::$server->getUserSession()->getUser(); |
|
245 | - |
|
246 | - // if the user matches set the log condition to satisfied |
|
247 | - if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
248 | - $this->logConditionSatisfied = true; |
|
249 | - } |
|
250 | - } |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - // if log condition is satisfied change the required log level to DEBUG |
|
255 | - if ($this->logConditionSatisfied) { |
|
256 | - return ILogger::DEBUG; |
|
257 | - } |
|
258 | - |
|
259 | - if (isset($context['app'])) { |
|
260 | - $logCondition = $this->config->getValue('log.condition', []); |
|
261 | - $app = $context['app']; |
|
262 | - |
|
263 | - /** |
|
264 | - * check log condition based on the context of each log message |
|
265 | - * once this is met -> change the required log level to debug |
|
266 | - */ |
|
267 | - if (!empty($logCondition) |
|
268 | - && isset($logCondition['apps']) |
|
269 | - && in_array($app, $logCondition['apps'], true)) { |
|
270 | - return ILogger::DEBUG; |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Logs an exception very detailed |
|
279 | - * |
|
280 | - * @param \Exception|\Throwable $exception |
|
281 | - * @param array $context |
|
282 | - * @return void |
|
283 | - * @since 8.2.0 |
|
284 | - */ |
|
285 | - public function logException(\Throwable $exception, array $context = []) { |
|
286 | - $app = $context['app'] ?? 'no app in context'; |
|
287 | - $level = $context['level'] ?? ILogger::ERROR; |
|
288 | - |
|
289 | - $serializer = new ExceptionSerializer(); |
|
290 | - $data = $serializer->serializeException($exception); |
|
291 | - $data['CustomMessage'] = $context['message'] ?? '--'; |
|
292 | - |
|
293 | - $minLevel = $this->getLogLevel($context); |
|
294 | - |
|
295 | - array_walk($context, [$this->normalizer, 'format']); |
|
296 | - |
|
297 | - if ($level >= $minLevel) { |
|
298 | - if (!$this->logger instanceof IFileBased) { |
|
299 | - $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
300 | - } |
|
301 | - $this->writeLog($app, $data, $level); |
|
302 | - } |
|
303 | - |
|
304 | - $context['level'] = $level; |
|
305 | - if (!is_null($this->crashReporters)) { |
|
306 | - $this->crashReporters->delegateReport($exception, $context); |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * @param string $app |
|
312 | - * @param string|array $entry |
|
313 | - * @param int $level |
|
314 | - */ |
|
315 | - protected function writeLog(string $app, $entry, int $level) { |
|
316 | - $this->logger->write($app, $entry, $level); |
|
317 | - } |
|
318 | - |
|
319 | - public function getLogPath():string { |
|
320 | - if($this->logger instanceof IFileBased) { |
|
321 | - return $this->logger->getLogFilePath(); |
|
322 | - } |
|
323 | - throw new \RuntimeException('Log implementation has no path'); |
|
324 | - } |
|
58 | + /** @var IWriter */ |
|
59 | + private $logger; |
|
60 | + |
|
61 | + /** @var SystemConfig */ |
|
62 | + private $config; |
|
63 | + |
|
64 | + /** @var boolean|null cache the result of the log condition check for the request */ |
|
65 | + private $logConditionSatisfied = null; |
|
66 | + |
|
67 | + /** @var Normalizer */ |
|
68 | + private $normalizer; |
|
69 | + |
|
70 | + /** @var IRegistry */ |
|
71 | + private $crashReporters; |
|
72 | + |
|
73 | + /** |
|
74 | + * @param IWriter $logger The logger that should be used |
|
75 | + * @param SystemConfig $config the system config object |
|
76 | + * @param Normalizer|null $normalizer |
|
77 | + * @param IRegistry|null $registry |
|
78 | + */ |
|
79 | + public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
80 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
81 | + if ($config === null) { |
|
82 | + $config = \OC::$server->getSystemConfig(); |
|
83 | + } |
|
84 | + |
|
85 | + $this->config = $config; |
|
86 | + $this->logger = $logger; |
|
87 | + if ($normalizer === null) { |
|
88 | + $this->normalizer = new Normalizer(); |
|
89 | + } else { |
|
90 | + $this->normalizer = $normalizer; |
|
91 | + } |
|
92 | + $this->crashReporters = $registry; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * System is unusable. |
|
97 | + * |
|
98 | + * @param string $message |
|
99 | + * @param array $context |
|
100 | + * @return void |
|
101 | + */ |
|
102 | + public function emergency(string $message, array $context = []) { |
|
103 | + $this->log(ILogger::FATAL, $message, $context); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Action must be taken immediately. |
|
108 | + * |
|
109 | + * Example: Entire website down, database unavailable, etc. This should |
|
110 | + * trigger the SMS alerts and wake you up. |
|
111 | + * |
|
112 | + * @param string $message |
|
113 | + * @param array $context |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + public function alert(string $message, array $context = []) { |
|
117 | + $this->log(ILogger::ERROR, $message, $context); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Critical conditions. |
|
122 | + * |
|
123 | + * Example: Application component unavailable, unexpected exception. |
|
124 | + * |
|
125 | + * @param string $message |
|
126 | + * @param array $context |
|
127 | + * @return void |
|
128 | + */ |
|
129 | + public function critical(string $message, array $context = []) { |
|
130 | + $this->log(ILogger::ERROR, $message, $context); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Runtime errors that do not require immediate action but should typically |
|
135 | + * be logged and monitored. |
|
136 | + * |
|
137 | + * @param string $message |
|
138 | + * @param array $context |
|
139 | + * @return void |
|
140 | + */ |
|
141 | + public function error(string $message, array $context = []) { |
|
142 | + $this->log(ILogger::ERROR, $message, $context); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Exceptional occurrences that are not errors. |
|
147 | + * |
|
148 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
149 | + * that are not necessarily wrong. |
|
150 | + * |
|
151 | + * @param string $message |
|
152 | + * @param array $context |
|
153 | + * @return void |
|
154 | + */ |
|
155 | + public function warning(string $message, array $context = []) { |
|
156 | + $this->log(ILogger::WARN, $message, $context); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Normal but significant events. |
|
161 | + * |
|
162 | + * @param string $message |
|
163 | + * @param array $context |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public function notice(string $message, array $context = []) { |
|
167 | + $this->log(ILogger::INFO, $message, $context); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Interesting events. |
|
172 | + * |
|
173 | + * Example: User logs in, SQL logs. |
|
174 | + * |
|
175 | + * @param string $message |
|
176 | + * @param array $context |
|
177 | + * @return void |
|
178 | + */ |
|
179 | + public function info(string $message, array $context = []) { |
|
180 | + $this->log(ILogger::INFO, $message, $context); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Detailed debug information. |
|
185 | + * |
|
186 | + * @param string $message |
|
187 | + * @param array $context |
|
188 | + * @return void |
|
189 | + */ |
|
190 | + public function debug(string $message, array $context = []) { |
|
191 | + $this->log(ILogger::DEBUG, $message, $context); |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * Logs with an arbitrary level. |
|
197 | + * |
|
198 | + * @param int $level |
|
199 | + * @param string $message |
|
200 | + * @param array $context |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + public function log(int $level, string $message, array $context = []) { |
|
204 | + $minLevel = $this->getLogLevel($context); |
|
205 | + |
|
206 | + array_walk($context, [$this->normalizer, 'format']); |
|
207 | + |
|
208 | + $app = $context['app'] ?? 'no app in context'; |
|
209 | + |
|
210 | + // interpolate $message as defined in PSR-3 |
|
211 | + $replace = []; |
|
212 | + foreach ($context as $key => $val) { |
|
213 | + $replace['{' . $key . '}'] = $val; |
|
214 | + } |
|
215 | + $message = strtr($message, $replace); |
|
216 | + |
|
217 | + if ($level >= $minLevel) { |
|
218 | + $this->writeLog($app, $message, $level); |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + private function getLogLevel($context) { |
|
223 | + /** |
|
224 | + * check for a special log condition - this enables an increased log on |
|
225 | + * a per request/user base |
|
226 | + */ |
|
227 | + if ($this->logConditionSatisfied === null) { |
|
228 | + // default to false to just process this once per request |
|
229 | + $this->logConditionSatisfied = false; |
|
230 | + if (!empty($logCondition)) { |
|
231 | + |
|
232 | + // check for secret token in the request |
|
233 | + if (isset($logCondition['shared_secret'])) { |
|
234 | + $request = \OC::$server->getRequest(); |
|
235 | + |
|
236 | + // if token is found in the request change set the log condition to satisfied |
|
237 | + if ($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) { |
|
238 | + $this->logConditionSatisfied = true; |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + // check for user |
|
243 | + if (isset($logCondition['users'])) { |
|
244 | + $user = \OC::$server->getUserSession()->getUser(); |
|
245 | + |
|
246 | + // if the user matches set the log condition to satisfied |
|
247 | + if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
248 | + $this->logConditionSatisfied = true; |
|
249 | + } |
|
250 | + } |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + // if log condition is satisfied change the required log level to DEBUG |
|
255 | + if ($this->logConditionSatisfied) { |
|
256 | + return ILogger::DEBUG; |
|
257 | + } |
|
258 | + |
|
259 | + if (isset($context['app'])) { |
|
260 | + $logCondition = $this->config->getValue('log.condition', []); |
|
261 | + $app = $context['app']; |
|
262 | + |
|
263 | + /** |
|
264 | + * check log condition based on the context of each log message |
|
265 | + * once this is met -> change the required log level to debug |
|
266 | + */ |
|
267 | + if (!empty($logCondition) |
|
268 | + && isset($logCondition['apps']) |
|
269 | + && in_array($app, $logCondition['apps'], true)) { |
|
270 | + return ILogger::DEBUG; |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Logs an exception very detailed |
|
279 | + * |
|
280 | + * @param \Exception|\Throwable $exception |
|
281 | + * @param array $context |
|
282 | + * @return void |
|
283 | + * @since 8.2.0 |
|
284 | + */ |
|
285 | + public function logException(\Throwable $exception, array $context = []) { |
|
286 | + $app = $context['app'] ?? 'no app in context'; |
|
287 | + $level = $context['level'] ?? ILogger::ERROR; |
|
288 | + |
|
289 | + $serializer = new ExceptionSerializer(); |
|
290 | + $data = $serializer->serializeException($exception); |
|
291 | + $data['CustomMessage'] = $context['message'] ?? '--'; |
|
292 | + |
|
293 | + $minLevel = $this->getLogLevel($context); |
|
294 | + |
|
295 | + array_walk($context, [$this->normalizer, 'format']); |
|
296 | + |
|
297 | + if ($level >= $minLevel) { |
|
298 | + if (!$this->logger instanceof IFileBased) { |
|
299 | + $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
300 | + } |
|
301 | + $this->writeLog($app, $data, $level); |
|
302 | + } |
|
303 | + |
|
304 | + $context['level'] = $level; |
|
305 | + if (!is_null($this->crashReporters)) { |
|
306 | + $this->crashReporters->delegateReport($exception, $context); |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * @param string $app |
|
312 | + * @param string|array $entry |
|
313 | + * @param int $level |
|
314 | + */ |
|
315 | + protected function writeLog(string $app, $entry, int $level) { |
|
316 | + $this->logger->write($app, $entry, $level); |
|
317 | + } |
|
318 | + |
|
319 | + public function getLogPath():string { |
|
320 | + if($this->logger instanceof IFileBased) { |
|
321 | + return $this->logger->getLogFilePath(); |
|
322 | + } |
|
323 | + throw new \RuntimeException('Log implementation has no path'); |
|
324 | + } |
|
325 | 325 | } |