Completed
Push — master ( d4fe99...100c17 )
by
unknown
23:17
created

StorageWrapper::getCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Morris Jobke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\FilesAccessControl;
23
24
use OC\Files\Storage\Wrapper\Wrapper;
25
use OCP\Files\ForbiddenException;
26
use OCP\Files\Storage\IStorage;
27
28
class StorageWrapper extends Wrapper {
29
30
	/** @var Operation */
31
	protected $operation;
32
33
	/** @var string */
34
	public $mountPoint;
35
36
	/**
37
	 * @param array $parameters
38
	 */
39 38
	public function __construct($parameters) {
40 38
		parent::__construct($parameters);
41 38
		$this->operation = $parameters['operation'];
42 38
		$this->mountPoint = $parameters['mountPoint'];
43 38
	}
44
45
	/**
46
	 * @param string $path
47
	 */
48 2
	protected function checkFileAccess($path) {
49 2
		$this->operation->checkFileAccess($this, $path);
50 2
	}
51
52
	/*
53
	 * Storage wrapper methods
54
	 */
55
56
	/**
57
	 * see http://php.net/manual/en/function.mkdir.php
58
	 *
59
	 * @param string $path
60
	 * @return bool
61
	 */
62 4
	public function mkdir($path) {
63 4
		$this->checkFileAccess($path);
64 2
		return $this->storage->mkdir($path);
65
	}
66
67
	/**
68
	 * see http://php.net/manual/en/function.rmdir.php
69
	 *
70
	 * @param string $path
71
	 * @return bool
72
	 */
73 4
	public function rmdir($path) {
74 4
		$this->checkFileAccess($path);
75 2
		return $this->storage->rmdir($path);
76
	}
77
78
//	/**
79
//	 * see http://php.net/manual/en/function.opendir.php
80
//	 *
81
//	 * @param string $path
82
//	 * @return resource
83
//	 */
84
//	public function opendir($path) {
85
//		return $this->storage->opendir($path);
86
//	}
87
//
88
//	/**
89
//	 * see http://php.net/manual/en/function.is_dir.php
90
//	 *
91
//	 * @param string $path
92
//	 * @return bool
93
//	 */
94
//	public function is_dir($path) {
95
//		return $this->storage->is_dir($path);
96
//	}
97
//
98
//	/**
99
//	 * see http://php.net/manual/en/function.is_file.php
100
//	 *
101
//	 * @param string $path
102
//	 * @return bool
103
//	 */
104
//	public function is_file($path) {
105
//		return $this->storage->is_file($path);
106
//	}
107
//
108
//	/**
109
//	 * see http://php.net/manual/en/function.stat.php
110
//	 * only the following keys are required in the result: size and mtime
111
//	 *
112
//	 * @param string $path
113
//	 * @return array
114
//	 */
115
//	public function stat($path) {
116
//		return $this->storage->stat($path);
117
//	}
118
//
119
//	/**
120
//	 * see http://php.net/manual/en/function.filetype.php
121
//	 *
122
//	 * @param string $path
123
//	 * @return bool
124
//	 */
125
//	public function filetype($path) {
126
//		return $this->storage->filetype($path);
127
//	}
128
//
129
//	/**
130
//	 * see http://php.net/manual/en/function.filesize.php
131
//	 * The result for filesize when called on a folder is required to be 0
132
//	 *
133
//	 * @param string $path
134
//	 * @return int
135
//	 */
136
//	public function filesize($path) {
137
//		return $this->storage->filesize($path);
138
//	}
139
140
	/**
141
	 * check if a file can be created in $path
142
	 *
143
	 * @param string $path
144
	 * @return bool
145
	 */
146 4
	public function isCreatable($path) {
147
		try {
148 4
			$this->checkFileAccess($path);
149 4
		} catch (ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
150 2
			return false;
151
		}
152 2
		return $this->storage->isCreatable($path);
153
	}
154
155
	/**
156
	 * check if a file can be read
157
	 *
158
	 * @param string $path
159
	 * @return bool
160
	 */
161 4
	public function isReadable($path) {
162
		try {
163 4
			$this->checkFileAccess($path);
164 4
		} catch (ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
165 2
			return false;
166
		}
167 2
		return $this->storage->isReadable($path);
168
	}
169
170
	/**
171
	 * check if a file can be written to
172
	 *
173
	 * @param string $path
174
	 * @return bool
175
	 */
176 4
	public function isUpdatable($path) {
177
		try {
178 4
			$this->checkFileAccess($path);
179 4
		} catch (ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
180 2
			return false;
181
		}
182 2
		return $this->storage->isUpdatable($path);
183
	}
184
185
	/**
186
	 * check if a file can be deleted
187
	 *
188
	 * @param string $path
189
	 * @return bool
190
	 */
191 4
	public function isDeletable($path) {
192
		try {
193 4
			$this->checkFileAccess($path);
194 4
		} catch (ForbiddenException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\ForbiddenException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
195 2
			return false;
196
		}
197 2
		return $this->storage->isDeletable($path);
198
	}
199
200
//	/**
201
//	 * check if a file can be shared
202
//	 *
203
//	 * @param string $path
204
//	 * @return bool
205
//	 */
206
//	public function isSharable($path) {
207
//		return $this->storage->isSharable($path);
208
//	}
209
//
210
//	/**
211
//	 * get the full permissions of a path.
212
//	 * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
213
//	 *
214
//	 * @param string $path
215
//	 * @return int
216
//	 */
217
//	public function getPermissions($path) {
218
//		return $this->storage->getPermissions($path);
219
//	}
220
//
221
//	/**
222
//	 * see http://php.net/manual/en/function.file_exists.php
223
//	 *
224
//	 * @param string $path
225
//	 * @return bool
226
//	 */
227
//	public function file_exists($path) {
228
//		return $this->storage->file_exists($path);
229
//	}
230
//
231
//	/**
232
//	 * see http://php.net/manual/en/function.filemtime.php
233
//	 *
234
//	 * @param string $path
235
//	 * @return int
236
//	 */
237
//	public function filemtime($path) {
238
//		return $this->storage->filemtime($path);
239
//	}
240
241
	/**
242
	 * see http://php.net/manual/en/function.file_get_contents.php
243
	 *
244
	 * @param string $path
245
	 * @return string
246
	 */
247 4
	public function file_get_contents($path) {
248 4
		$this->checkFileAccess($path);
249 2
		return $this->storage->file_get_contents($path);
250
	}
251
252
	/**
253
	 * see http://php.net/manual/en/function.file_put_contents.php
254
	 *
255
	 * @param string $path
256
	 * @param string $data
257
	 * @return bool
258
	 */
259
	public function file_put_contents($path, $data) {
260
		$this->checkFileAccess($path);
261
		return $this->storage->file_put_contents($path, $data);
262
	}
263
264
	/**
265
	 * see http://php.net/manual/en/function.unlink.php
266
	 *
267
	 * @param string $path
268
	 * @return bool
269
	 */
270 4
	public function unlink($path) {
271 4
		$this->checkFileAccess($path);
272 2
		return $this->storage->unlink($path);
273
	}
274
275
	/**
276
	 * see http://php.net/manual/en/function.rename.php
277
	 *
278
	 * @param string $path1
279
	 * @param string $path2
280
	 * @return bool
281
	 */
282
	public function rename($path1, $path2) {
283
		$this->checkFileAccess($path1);
284
		$this->checkFileAccess($path2);
285
		return $this->storage->rename($path1, $path2);
286
	}
287
288
	/**
289
	 * see http://php.net/manual/en/function.copy.php
290
	 *
291
	 * @param string $path1
292
	 * @param string $path2
293
	 * @return bool
294
	 */
295
	public function copy($path1, $path2) {
296
		$this->checkFileAccess($path1);
297
		$this->checkFileAccess($path2);
298
		return $this->storage->copy($path1, $path2);
299
	}
300
301
	/**
302
	 * see http://php.net/manual/en/function.fopen.php
303
	 *
304
	 * @param string $path
305
	 * @param string $mode
306
	 * @return resource
307
	 */
308
	public function fopen($path, $mode) {
309
		$this->checkFileAccess($path);
310
		return $this->storage->fopen($path, $mode);
311
	}
312
313
//	/**
314
//	 * get the mimetype for a file or folder
315
//	 * The mimetype for a folder is required to be "httpd/unix-directory"
316
//	 *
317
//	 * @param string $path
318
//	 * @return string
319
//	 */
320
//	public function getMimeType($path) {
321
//		return $this->storage->getMimeType($path);
322
//	}
323
//
324
//	/**
325
//	 * see http://php.net/manual/en/function.hash.php
326
//	 *
327
//	 * @param string $type
328
//	 * @param string $path
329
//	 * @param bool $raw
330
//	 * @return string
331
//	 */
332
//	public function hash($type, $path, $raw = false) {
333
//		return $this->storage->hash($type, $path, $raw);
334
//	}
335
//
336
//	/**
337
//	 * see http://php.net/manual/en/function.free_space.php
338
//	 *
339
//	 * @param string $path
340
//	 * @return int
341
//	 */
342
//	public function free_space($path) {
343
//		return $this->storage->free_space($path);
344
//	}
345
//
346
//	/**
347
//	 * search for occurrences of $query in file names
348
//	 *
349
//	 * @param string $query
350
//	 * @return array
351
//	 */
352
//	public function search($query) {
353
//		return $this->storage->search($query);
354
//	}
355
356
	/**
357
	 * see http://php.net/manual/en/function.touch.php
358
	 * If the backend does not support the operation, false should be returned
359
	 *
360
	 * @param string $path
361
	 * @param int $mtime
362
	 * @return bool
363
	 */
364
	public function touch($path, $mtime = null) {
365
		$this->checkFileAccess($path);
366
		return $this->storage->touch($path, $mtime);
367
	}
368
369
//	/**
370
//	 * get the path to a local version of the file.
371
//	 * The local version of the file can be temporary and doesn't have to be persistent across requests
372
//	 *
373
//	 * @param string $path
374
//	 * @return string
375
//	 */
376
//	public function getLocalFile($path) {
377
//		return $this->storage->getLocalFile($path);
378
//	}
379
//
380
//	/**
381
//	 * check if a file or folder has been updated since $time
382
//	 *
383
//	 * @param string $path
384
//	 * @param int $time
385
//	 * @return bool
386
//	 *
387
//	 * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
388
//	 * returning true for other changes in the folder is optional
389
//	 */
390
//	public function hasUpdated($path, $time) {
391
//		return $this->storage->hasUpdated($path, $time);
392
//	}
393
394
	/**
395
	 * get a cache instance for the storage
396
	 *
397
	 * @param string $path
398
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
399
	 * @return \OC\Files\Cache\Cache
400
	 */
401
	public function getCache($path = '', $storage = null) {
402
		if (!$storage) {
403
			$storage = $this;
404
		}
405
		$cache = $this->storage->getCache($path, $storage);
406
		return new CacheWrapper($cache, $storage, $this->operation);
407
	}
408
409
//	/**
410
//	 * get a scanner instance for the storage
411
//	 *
412
//	 * @param string $path
413
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
414
//	 * @return \OC\Files\Cache\Scanner
415
//	 */
416
//	public function getScanner($path = '', $storage = null) {
417
//		if (!$storage) {
418
//			$storage = $this;
419
//		}
420
//		return $this->storage->getScanner($path, $storage);
421
//	}
422
//
423
//
424
//	/**
425
//	 * get the user id of the owner of a file or folder
426
//	 *
427
//	 * @param string $path
428
//	 * @return string
429
//	 */
430
//	public function getOwner($path) {
431
//		return $this->storage->getOwner($path);
432
//	}
433
//
434
//	/**
435
//	 * get a watcher instance for the cache
436
//	 *
437
//	 * @param string $path
438
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
439
//	 * @return \OC\Files\Cache\Watcher
440
//	 */
441
//	public function getWatcher($path = '', $storage = null) {
442
//		if (!$storage) {
443
//			$storage = $this;
444
//		}
445
//		return $this->storage->getWatcher($path, $storage);
446
//	}
447
//
448
//	public function getPropagator($storage = null) {
449
//		if (!$storage) {
450
//			$storage = $this;
451
//		}
452
//		return $this->storage->getPropagator($storage);
453
//	}
454
//
455
//	public function getUpdater($storage = null) {
456
//		if (!$storage) {
457
//			$storage = $this;
458
//		}
459
//		return $this->storage->getUpdater($storage);
460
//	}
461
//
462
//	/**
463
//	 * @return \OC\Files\Cache\Storage
464
//	 */
465
//	public function getStorageCache() {
466
//		return $this->storage->getStorageCache();
467
//	}
468
//
469
//	/**
470
//	 * get the ETag for a file or folder
471
//	 *
472
//	 * @param string $path
473
//	 * @return string
474
//	 */
475
//	public function getETag($path) {
476
//		return $this->storage->getETag($path);
477
//	}
478
//
479
//	/**
480
//	 * Returns true
481
//	 *
482
//	 * @return true
483
//	 */
484
//	public function test() {
485
//		return $this->storage->test();
486
//	}
487
//
488
//	/**
489
//	 * Returns the wrapped storage's value for isLocal()
490
//	 *
491
//	 * @return bool wrapped storage's isLocal() value
492
//	 */
493
//	public function isLocal() {
494
//		return $this->storage->isLocal();
495
//	}
496
//
497
//	/**
498
//	 * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
499
//	 *
500
//	 * @param string $class
501
//	 * @return bool
502
//	 */
503
//	public function instanceOfStorage($class) {
504
//		return is_a($this, $class) or $this->storage->instanceOfStorage($class);
505
//	}
506
//
507
//	/**
508
//	 * Pass any methods custom to specific storage implementations to the wrapped storage
509
//	 *
510
//	 * @param string $method
511
//	 * @param array $args
512
//	 * @return mixed
513
//	 */
514
//	public function __call($method, $args) {
515
//		return call_user_func_array(array($this->storage, $method), $args);
516
//	}
517
518
	/**
519
	 * A custom storage implementation can return an url for direct download of a give file.
520
	 *
521
	 * For now the returned array can hold the parameter url - in future more attributes might follow.
522
	 *
523
	 * @param string $path
524
	 * @return array
525
	 */
526 4
	public function getDirectDownload($path) {
527 4
		$this->checkFileAccess($path);
528 2
		return $this->storage->getDirectDownload($path);
529
	}
530
531
//	/**
532
//	 * Get availability of the storage
533
//	 *
534
//	 * @return array [ available, last_checked ]
535
//	 */
536
//	public function getAvailability() {
537
//		return $this->storage->getAvailability();
538
//	}
539
//
540
//	/**
541
//	 * Set availability of the storage
542
//	 *
543
//	 * @param bool $isAvailable
544
//	 */
545
//	public function setAvailability($isAvailable) {
546
//		$this->storage->setAvailability($isAvailable);
547
//	}
548
//
549
//	/**
550
//	 * @param string $path the path of the target folder
551
//	 * @param string $fileName the name of the file itself
552
//	 * @return void
553
//	 * @throws InvalidPathException
554
//	 */
555
//	public function verifyPath($path, $fileName) {
556
//		$this->storage->verifyPath($path, $fileName);
557
//	}
558
559
	/**
560
	 * @param IStorage $sourceStorage
561
	 * @param string $sourceInternalPath
562
	 * @param string $targetInternalPath
563
	 * @return bool
564
	 */
565
	public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
566
		if ($sourceStorage === $this) {
567
			return $this->copy($sourceInternalPath, $targetInternalPath);
568
		}
569
570
		$this->checkFileAccess($targetInternalPath);
571
		return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
572
	}
573
574
	/**
575
	 * @param IStorage $sourceStorage
576
	 * @param string $sourceInternalPath
577
	 * @param string $targetInternalPath
578
	 * @return bool
579
	 */
580
	public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
581
		if ($sourceStorage === $this) {
582
			return $this->rename($sourceInternalPath, $targetInternalPath);
583
		}
584
585
		$this->checkFileAccess($targetInternalPath);
586
		return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
587
	}
588
589
//	/**
590
//	 * @param string $path
591
//	 * @return array
592
//	 */
593
//	public function getMetaData($path) {
594
//		return $this->storage->getMetaData($path);
595
//	}
596
//
597
//	/**
598
//	 * @param string $path
599
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
600
//	 * @param \OCP\Lock\ILockingProvider $provider
601
//	 * @throws \OCP\Lock\LockedException
602
//	 */
603
//	public function acquireLock($path, $type, ILockingProvider $provider) {
604
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
605
//			$this->storage->acquireLock($path, $type, $provider);
606
//		}
607
//	}
608
//
609
//	/**
610
//	 * @param string $path
611
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
612
//	 * @param \OCP\Lock\ILockingProvider $provider
613
//	 */
614
//	public function releaseLock($path, $type, ILockingProvider $provider) {
615
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
616
//			$this->storage->releaseLock($path, $type, $provider);
617
//		}
618
//	}
619
//
620
//	/**
621
//	 * @param string $path
622
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
623
//	 * @param \OCP\Lock\ILockingProvider $provider
624
//	 */
625
//	public function changeLock($path, $type, ILockingProvider $provider) {
626
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
627
//			$this->storage->changeLock($path, $type, $provider);
628
//		}
629
//	}
630
}
631