Completed
Pull Request — master (#11)
by Joas
02:50
created

StorageWrapper::emitEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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