Completed
Push — master ( 220cc9...7cdafd )
by Joas
97:57 queued 96:09
created

StorageWrapper::getCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
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\Constants;
26
use OCP\Files\ForbiddenException;
27
use OCP\Files\Storage\IStorage;
28
29
class StorageWrapper extends Wrapper {
30
31
	/** @var Operation */
32
	protected $operation;
33
34
	/** @var string */
35
	public $mountPoint;
36
	/** @var int */
37
	protected $mask;
38
39
	/**
40
	 * @param array $parameters
41
	 */
42 38
	public function __construct($parameters) {
43 38
		parent::__construct($parameters);
44 38
		$this->operation = $parameters['operation'];
45 38
		$this->mountPoint = $parameters['mountPoint'];
46
47 38
		$this->mask = Constants::PERMISSION_ALL;
48 38
		$this->mask &= ~Constants::PERMISSION_READ;
49 38
		$this->mask &= ~Constants::PERMISSION_CREATE;
50 38
		$this->mask &= ~Constants::PERMISSION_UPDATE;
51 38
		$this->mask &= ~Constants::PERMISSION_DELETE;
52 38
	}
53
54
	/**
55
	 * @param string $path
56
	 */
57 2
	protected function checkFileAccess($path) {
58 2
		$this->operation->checkFileAccess($this, $path);
59 2
	}
60
61
	/*
62
	 * Storage wrapper methods
63
	 */
64
65
	/**
66
	 * see http://php.net/manual/en/function.mkdir.php
67
	 *
68
	 * @param string $path
69
	 * @return bool
70
	 */
71 4
	public function mkdir($path) {
72 4
		$this->checkFileAccess($path);
73 2
		return $this->storage->mkdir($path);
74
	}
75
76
	/**
77
	 * see http://php.net/manual/en/function.rmdir.php
78
	 *
79
	 * @param string $path
80
	 * @return bool
81
	 */
82 4
	public function rmdir($path) {
83 4
		$this->checkFileAccess($path);
84 2
		return $this->storage->rmdir($path);
85
	}
86
87
//	/**
88
//	 * see http://php.net/manual/en/function.opendir.php
89
//	 *
90
//	 * @param string $path
91
//	 * @return resource
92
//	 */
93
//	public function opendir($path) {
94
//		return $this->storage->opendir($path);
95
//	}
96
//
97
//	/**
98
//	 * see http://php.net/manual/en/function.is_dir.php
99
//	 *
100
//	 * @param string $path
101
//	 * @return bool
102
//	 */
103
//	public function is_dir($path) {
104
//		return $this->storage->is_dir($path);
105
//	}
106
//
107
//	/**
108
//	 * see http://php.net/manual/en/function.is_file.php
109
//	 *
110
//	 * @param string $path
111
//	 * @return bool
112
//	 */
113
//	public function is_file($path) {
114
//		return $this->storage->is_file($path);
115
//	}
116
//
117
//	/**
118
//	 * see http://php.net/manual/en/function.stat.php
119
//	 * only the following keys are required in the result: size and mtime
120
//	 *
121
//	 * @param string $path
122
//	 * @return array
123
//	 */
124
//	public function stat($path) {
125
//		return $this->storage->stat($path);
126
//	}
127
//
128
//	/**
129
//	 * see http://php.net/manual/en/function.filetype.php
130
//	 *
131
//	 * @param string $path
132
//	 * @return bool
133
//	 */
134
//	public function filetype($path) {
135
//		return $this->storage->filetype($path);
136
//	}
137
//
138
//	/**
139
//	 * see http://php.net/manual/en/function.filesize.php
140
//	 * The result for filesize when called on a folder is required to be 0
141
//	 *
142
//	 * @param string $path
143
//	 * @return int
144
//	 */
145
//	public function filesize($path) {
146
//		return $this->storage->filesize($path);
147
//	}
148
149
	/**
150
	 * check if a file can be created in $path
151
	 *
152
	 * @param string $path
153
	 * @return bool
154
	 */
155 4
	public function isCreatable($path) {
156
		try {
157 4
			$this->checkFileAccess($path);
158 2
		} 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...
159 2
			return false;
160
		}
161 2
		return $this->storage->isCreatable($path);
162
	}
163
164
	/**
165
	 * check if a file can be read
166
	 *
167
	 * @param string $path
168
	 * @return bool
169
	 */
170 4
	public function isReadable($path) {
171
		try {
172 4
			$this->checkFileAccess($path);
173 2
		} 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...
174 2
			return false;
175
		}
176 2
		return $this->storage->isReadable($path);
177
	}
178
179
	/**
180
	 * check if a file can be written to
181
	 *
182
	 * @param string $path
183
	 * @return bool
184
	 */
185 4
	public function isUpdatable($path) {
186
		try {
187 4
			$this->checkFileAccess($path);
188 2
		} 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...
189 2
			return false;
190
		}
191 2
		return $this->storage->isUpdatable($path);
192
	}
193
194
	/**
195
	 * check if a file can be deleted
196
	 *
197
	 * @param string $path
198
	 * @return bool
199
	 */
200 4
	public function isDeletable($path) {
201
		try {
202 4
			$this->checkFileAccess($path);
203 2
		} 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...
204 2
			return false;
205
		}
206 2
		return $this->storage->isDeletable($path);
207
	}
208
209
//	/**
210
//	 * check if a file can be shared
211
//	 *
212
//	 * @param string $path
213
//	 * @return bool
214
//	 */
215
//	public function isSharable($path) {
216
//		return $this->storage->isSharable($path);
217
//	}
218
//
219
//	/**
220
//	 * get the full permissions of a path.
221
//	 * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
222
//	 *
223
//	 * @param string $path
224
//	 * @return int
225
//	 */
226
	public function getPermissions($path) {
227
		try {
228
			$this->checkFileAccess($path);
229
		} 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...
230
			return $this->mask;
231
		}
232
		return $this->storage->getPermissions($path);
233
	}
234
//
235
//	/**
236
//	 * see http://php.net/manual/en/function.file_exists.php
237
//	 *
238
//	 * @param string $path
239
//	 * @return bool
240
//	 */
241
//	public function file_exists($path) {
242
//		return $this->storage->file_exists($path);
243
//	}
244
//
245
//	/**
246
//	 * see http://php.net/manual/en/function.filemtime.php
247
//	 *
248
//	 * @param string $path
249
//	 * @return int
250
//	 */
251
//	public function filemtime($path) {
252
//		return $this->storage->filemtime($path);
253
//	}
254
255
	/**
256
	 * see http://php.net/manual/en/function.file_get_contents.php
257
	 *
258
	 * @param string $path
259
	 * @return string
260
	 */
261 4
	public function file_get_contents($path) {
262 4
		$this->checkFileAccess($path);
263 2
		return $this->storage->file_get_contents($path);
264
	}
265
266
	/**
267
	 * see http://php.net/manual/en/function.file_put_contents.php
268
	 *
269
	 * @param string $path
270
	 * @param string $data
271
	 * @return bool
272
	 */
273
	public function file_put_contents($path, $data) {
274
		$this->checkFileAccess($path);
275
		return $this->storage->file_put_contents($path, $data);
276
	}
277
278
	/**
279
	 * see http://php.net/manual/en/function.unlink.php
280
	 *
281
	 * @param string $path
282
	 * @return bool
283
	 */
284 4
	public function unlink($path) {
285 4
		$this->checkFileAccess($path);
286 2
		return $this->storage->unlink($path);
287
	}
288
289
	/**
290
	 * see http://php.net/manual/en/function.rename.php
291
	 *
292
	 * @param string $path1
293
	 * @param string $path2
294
	 * @return bool
295
	 */
296
	public function rename($path1, $path2) {
297
		$this->checkFileAccess($path1);
298
		$this->checkFileAccess($path2);
299
		return $this->storage->rename($path1, $path2);
300
	}
301
302
	/**
303
	 * see http://php.net/manual/en/function.copy.php
304
	 *
305
	 * @param string $path1
306
	 * @param string $path2
307
	 * @return bool
308
	 */
309
	public function copy($path1, $path2) {
310
		$this->checkFileAccess($path1);
311
		$this->checkFileAccess($path2);
312
		return $this->storage->copy($path1, $path2);
313
	}
314
315
	/**
316
	 * see http://php.net/manual/en/function.fopen.php
317
	 *
318
	 * @param string $path
319
	 * @param string $mode
320
	 * @return resource
321
	 */
322
	public function fopen($path, $mode) {
323
		$this->checkFileAccess($path);
324
		return $this->storage->fopen($path, $mode);
325
	}
326
327
//	/**
328
//	 * get the mimetype for a file or folder
329
//	 * The mimetype for a folder is required to be "httpd/unix-directory"
330
//	 *
331
//	 * @param string $path
332
//	 * @return string
333
//	 */
334
//	public function getMimeType($path) {
335
//		return $this->storage->getMimeType($path);
336
//	}
337
//
338
//	/**
339
//	 * see http://php.net/manual/en/function.hash.php
340
//	 *
341
//	 * @param string $type
342
//	 * @param string $path
343
//	 * @param bool $raw
344
//	 * @return string
345
//	 */
346
//	public function hash($type, $path, $raw = false) {
347
//		return $this->storage->hash($type, $path, $raw);
348
//	}
349
//
350
//	/**
351
//	 * see http://php.net/manual/en/function.free_space.php
352
//	 *
353
//	 * @param string $path
354
//	 * @return int
355
//	 */
356
//	public function free_space($path) {
357
//		return $this->storage->free_space($path);
358
//	}
359
//
360
//	/**
361
//	 * search for occurrences of $query in file names
362
//	 *
363
//	 * @param string $query
364
//	 * @return array
365
//	 */
366
//	public function search($query) {
367
//		return $this->storage->search($query);
368
//	}
369
370
	/**
371
	 * see http://php.net/manual/en/function.touch.php
372
	 * If the backend does not support the operation, false should be returned
373
	 *
374
	 * @param string $path
375
	 * @param int $mtime
376
	 * @return bool
377
	 */
378
	public function touch($path, $mtime = null) {
379
		$this->checkFileAccess($path);
380
		return $this->storage->touch($path, $mtime);
381
	}
382
383
//	/**
384
//	 * get the path to a local version of the file.
385
//	 * The local version of the file can be temporary and doesn't have to be persistent across requests
386
//	 *
387
//	 * @param string $path
388
//	 * @return string
389
//	 */
390
//	public function getLocalFile($path) {
391
//		return $this->storage->getLocalFile($path);
392
//	}
393
//
394
//	/**
395
//	 * check if a file or folder has been updated since $time
396
//	 *
397
//	 * @param string $path
398
//	 * @param int $time
399
//	 * @return bool
400
//	 *
401
//	 * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
402
//	 * returning true for other changes in the folder is optional
403
//	 */
404
//	public function hasUpdated($path, $time) {
405
//		return $this->storage->hasUpdated($path, $time);
406
//	}
407
408
	/**
409
	 * get a cache instance for the storage
410
	 *
411
	 * @param string $path
412
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
413
	 * @return \OC\Files\Cache\Cache
414
	 */
415
	public function getCache($path = '', $storage = null) {
416
		if (!$storage) {
417
			$storage = $this;
418
		}
419
		$cache = $this->storage->getCache($path, $storage);
420
		return new CacheWrapper($cache, $storage, $this->operation);
421
	}
422
423
//	/**
424
//	 * get a scanner instance for the storage
425
//	 *
426
//	 * @param string $path
427
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
428
//	 * @return \OC\Files\Cache\Scanner
429
//	 */
430
//	public function getScanner($path = '', $storage = null) {
431
//		if (!$storage) {
432
//			$storage = $this;
433
//		}
434
//		return $this->storage->getScanner($path, $storage);
435
//	}
436
//
437
//
438
//	/**
439
//	 * get the user id of the owner of a file or folder
440
//	 *
441
//	 * @param string $path
442
//	 * @return string
443
//	 */
444
//	public function getOwner($path) {
445
//		return $this->storage->getOwner($path);
446
//	}
447
//
448
//	/**
449
//	 * get a watcher instance for the cache
450
//	 *
451
//	 * @param string $path
452
//	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
453
//	 * @return \OC\Files\Cache\Watcher
454
//	 */
455
//	public function getWatcher($path = '', $storage = null) {
456
//		if (!$storage) {
457
//			$storage = $this;
458
//		}
459
//		return $this->storage->getWatcher($path, $storage);
460
//	}
461
//
462
//	public function getPropagator($storage = null) {
463
//		if (!$storage) {
464
//			$storage = $this;
465
//		}
466
//		return $this->storage->getPropagator($storage);
467
//	}
468
//
469
//	public function getUpdater($storage = null) {
470
//		if (!$storage) {
471
//			$storage = $this;
472
//		}
473
//		return $this->storage->getUpdater($storage);
474
//	}
475
//
476
//	/**
477
//	 * @return \OC\Files\Cache\Storage
478
//	 */
479
//	public function getStorageCache() {
480
//		return $this->storage->getStorageCache();
481
//	}
482
//
483
//	/**
484
//	 * get the ETag for a file or folder
485
//	 *
486
//	 * @param string $path
487
//	 * @return string
488
//	 */
489
//	public function getETag($path) {
490
//		return $this->storage->getETag($path);
491
//	}
492
//
493
//	/**
494
//	 * Returns true
495
//	 *
496
//	 * @return true
497
//	 */
498
//	public function test() {
499
//		return $this->storage->test();
500
//	}
501
//
502
//	/**
503
//	 * Returns the wrapped storage's value for isLocal()
504
//	 *
505
//	 * @return bool wrapped storage's isLocal() value
506
//	 */
507
//	public function isLocal() {
508
//		return $this->storage->isLocal();
509
//	}
510
//
511
//	/**
512
//	 * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
513
//	 *
514
//	 * @param string $class
515
//	 * @return bool
516
//	 */
517
//	public function instanceOfStorage($class) {
518
//		return is_a($this, $class) or $this->storage->instanceOfStorage($class);
519
//	}
520
//
521
//	/**
522
//	 * Pass any methods custom to specific storage implementations to the wrapped storage
523
//	 *
524
//	 * @param string $method
525
//	 * @param array $args
526
//	 * @return mixed
527
//	 */
528
//	public function __call($method, $args) {
529
//		return call_user_func_array(array($this->storage, $method), $args);
530
//	}
531
532
	/**
533
	 * A custom storage implementation can return an url for direct download of a give file.
534
	 *
535
	 * For now the returned array can hold the parameter url - in future more attributes might follow.
536
	 *
537
	 * @param string $path
538
	 * @return array
539
	 */
540 4
	public function getDirectDownload($path) {
541 4
		$this->checkFileAccess($path);
542 2
		return $this->storage->getDirectDownload($path);
543
	}
544
545
//	/**
546
//	 * Get availability of the storage
547
//	 *
548
//	 * @return array [ available, last_checked ]
549
//	 */
550
//	public function getAvailability() {
551
//		return $this->storage->getAvailability();
552
//	}
553
//
554
//	/**
555
//	 * Set availability of the storage
556
//	 *
557
//	 * @param bool $isAvailable
558
//	 */
559
//	public function setAvailability($isAvailable) {
560
//		$this->storage->setAvailability($isAvailable);
561
//	}
562
//
563
//	/**
564
//	 * @param string $path the path of the target folder
565
//	 * @param string $fileName the name of the file itself
566
//	 * @return void
567
//	 * @throws InvalidPathException
568
//	 */
569
//	public function verifyPath($path, $fileName) {
570
//		$this->storage->verifyPath($path, $fileName);
571
//	}
572
573
	/**
574
	 * @param IStorage $sourceStorage
575
	 * @param string $sourceInternalPath
576
	 * @param string $targetInternalPath
577
	 * @return bool
578
	 */
579
	public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
580
		if ($sourceStorage === $this) {
581
			return $this->copy($sourceInternalPath, $targetInternalPath);
582
		}
583
584
		$this->checkFileAccess($targetInternalPath);
585
		return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
586
	}
587
588
	/**
589
	 * @param IStorage $sourceStorage
590
	 * @param string $sourceInternalPath
591
	 * @param string $targetInternalPath
592
	 * @return bool
593
	 */
594
	public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
595
		if ($sourceStorage === $this) {
596
			return $this->rename($sourceInternalPath, $targetInternalPath);
597
		}
598
599
		$this->checkFileAccess($targetInternalPath);
600
		return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
601
	}
602
603
//	/**
604
//	 * @param string $path
605
//	 * @return array
606
//	 */
607
//	public function getMetaData($path) {
608
//		return $this->storage->getMetaData($path);
609
//	}
610
//
611
//	/**
612
//	 * @param string $path
613
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
614
//	 * @param \OCP\Lock\ILockingProvider $provider
615
//	 * @throws \OCP\Lock\LockedException
616
//	 */
617
//	public function acquireLock($path, $type, ILockingProvider $provider) {
618
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
619
//			$this->storage->acquireLock($path, $type, $provider);
620
//		}
621
//	}
622
//
623
//	/**
624
//	 * @param string $path
625
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
626
//	 * @param \OCP\Lock\ILockingProvider $provider
627
//	 */
628
//	public function releaseLock($path, $type, ILockingProvider $provider) {
629
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
630
//			$this->storage->releaseLock($path, $type, $provider);
631
//		}
632
//	}
633
//
634
//	/**
635
//	 * @param string $path
636
//	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
637
//	 * @param \OCP\Lock\ILockingProvider $provider
638
//	 */
639
//	public function changeLock($path, $type, ILockingProvider $provider) {
640
//		if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
641
//			$this->storage->changeLock($path, $type, $provider);
642
//		}
643
//	}
644
}
645