Completed
Push — master ( 4ff76f...bfd2a0 )
by Morris
13s
created

lib/StorageWrapper.php (4 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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