Completed
Pull Request — master (#5715)
by Morris
51:53 queued 29:36
created

Jail::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Morris Jobke <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Files\Storage\Wrapper;
26
27
use OC\Files\Cache\Wrapper\CacheJail;
28
use OC\Files\Cache\Wrapper\JailPropagator;
29
use OCP\Lock\ILockingProvider;
30
31
/**
32
 * Jail to a subdirectory of the wrapped storage
33
 *
34
 * This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
35
 */
36
class Jail extends Wrapper {
37
	/**
38
	 * @var string
39
	 */
40
	protected $rootPath;
41
42
	/**
43
	 * @param array $arguments ['storage' => $storage, 'mask' => $root]
44
	 *
45
	 * $storage: The storage that will be wrapper
46
	 * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
47
	 */
48
	public function __construct($arguments) {
49
		parent::__construct($arguments);
50
		$this->rootPath = $arguments['root'];
51
	}
52
53
	public function getUnjailedPath($path) {
54
		if ($path === '') {
55
			return $this->rootPath;
56
		} else {
57
			return $this->rootPath . '/' . $path;
58
		}
59
	}
60
61
	public function getId() {
62
		return parent::getId();
63
	}
64
65
	/**
66
	 * see http://php.net/manual/en/function.mkdir.php
67
	 *
68
	 * @param string $path
69
	 * @return bool
70
	 */
71
	public function mkdir($path) {
72
		return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path));
73
	}
74
75
	/**
76
	 * see http://php.net/manual/en/function.rmdir.php
77
	 *
78
	 * @param string $path
79
	 * @return bool
80
	 */
81
	public function rmdir($path) {
82
		return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
83
	}
84
85
	/**
86
	 * see http://php.net/manual/en/function.opendir.php
87
	 *
88
	 * @param string $path
89
	 * @return resource
90
	 */
91
	public function opendir($path) {
92
		return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path));
93
	}
94
95
	/**
96
	 * see http://php.net/manual/en/function.is_dir.php
97
	 *
98
	 * @param string $path
99
	 * @return bool
100
	 */
101
	public function is_dir($path) {
102
		return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path));
103
	}
104
105
	/**
106
	 * see http://php.net/manual/en/function.is_file.php
107
	 *
108
	 * @param string $path
109
	 * @return bool
110
	 */
111
	public function is_file($path) {
112
		return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path));
113
	}
114
115
	/**
116
	 * see http://php.net/manual/en/function.stat.php
117
	 * only the following keys are required in the result: size and mtime
118
	 *
119
	 * @param string $path
120
	 * @return array
121
	 */
122
	public function stat($path) {
123
		return $this->getWrapperStorage()->stat($this->getUnjailedPath($path));
124
	}
125
126
	/**
127
	 * see http://php.net/manual/en/function.filetype.php
128
	 *
129
	 * @param string $path
130
	 * @return bool
131
	 */
132
	public function filetype($path) {
133
		return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
134
	}
135
136
	/**
137
	 * see http://php.net/manual/en/function.filesize.php
138
	 * The result for filesize when called on a folder is required to be 0
139
	 *
140
	 * @param string $path
141
	 * @return int
142
	 */
143
	public function filesize($path) {
144
		return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path));
145
	}
146
147
	/**
148
	 * check if a file can be created in $path
149
	 *
150
	 * @param string $path
151
	 * @return bool
152
	 */
153
	public function isCreatable($path) {
154
		return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
155
	}
156
157
	/**
158
	 * check if a file can be read
159
	 *
160
	 * @param string $path
161
	 * @return bool
162
	 */
163
	public function isReadable($path) {
164
		return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
165
	}
166
167
	/**
168
	 * check if a file can be written to
169
	 *
170
	 * @param string $path
171
	 * @return bool
172
	 */
173
	public function isUpdatable($path) {
174
		return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
175
	}
176
177
	/**
178
	 * check if a file can be deleted
179
	 *
180
	 * @param string $path
181
	 * @return bool
182
	 */
183
	public function isDeletable($path) {
184
		return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
185
	}
186
187
	/**
188
	 * check if a file can be shared
189
	 *
190
	 * @param string $path
191
	 * @return bool
192
	 */
193
	public function isSharable($path) {
194
		return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path));
195
	}
196
197
	/**
198
	 * get the full permissions of a path.
199
	 * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
200
	 *
201
	 * @param string $path
202
	 * @return int
203
	 */
204
	public function getPermissions($path) {
205
		return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path));
206
	}
207
208
	/**
209
	 * see http://php.net/manual/en/function.file_exists.php
210
	 *
211
	 * @param string $path
212
	 * @return bool
213
	 */
214
	public function file_exists($path) {
215
		return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path));
216
	}
217
218
	/**
219
	 * see http://php.net/manual/en/function.filemtime.php
220
	 *
221
	 * @param string $path
222
	 * @return int
223
	 */
224
	public function filemtime($path) {
225
		return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
226
	}
227
228
	/**
229
	 * see http://php.net/manual/en/function.file_get_contents.php
230
	 *
231
	 * @param string $path
232
	 * @return string
233
	 */
234
	public function file_get_contents($path) {
235
		return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
236
	}
237
238
	/**
239
	 * see http://php.net/manual/en/function.file_put_contents.php
240
	 *
241
	 * @param string $path
242
	 * @param string $data
243
	 * @return bool
244
	 */
245
	public function file_put_contents($path, $data) {
246
		return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data);
247
	}
248
249
	/**
250
	 * see http://php.net/manual/en/function.unlink.php
251
	 *
252
	 * @param string $path
253
	 * @return bool
254
	 */
255
	public function unlink($path) {
256
		return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
257
	}
258
259
	/**
260
	 * see http://php.net/manual/en/function.rename.php
261
	 *
262
	 * @param string $path1
263
	 * @param string $path2
264
	 * @return bool
265
	 */
266
	public function rename($path1, $path2) {
267
		return $this->getWrapperStorage()->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
268
	}
269
270
	/**
271
	 * see http://php.net/manual/en/function.copy.php
272
	 *
273
	 * @param string $path1
274
	 * @param string $path2
275
	 * @return bool
276
	 */
277
	public function copy($path1, $path2) {
278
		return $this->getWrapperStorage()->copy($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
279
	}
280
281
	/**
282
	 * see http://php.net/manual/en/function.fopen.php
283
	 *
284
	 * @param string $path
285
	 * @param string $mode
286
	 * @return resource
287
	 */
288
	public function fopen($path, $mode) {
289
		return $this->getWrapperStorage()->fopen($this->getUnjailedPath($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->getWrapperStorage()->getMimeType($this->getUnjailedPath($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->getWrapperStorage()->hash($type, $this->getUnjailedPath($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->getWrapperStorage()->free_space($this->getUnjailedPath($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->getWrapperStorage()->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
		return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
345
	}
346
347
	/**
348
	 * get the path to a local version of the file.
349
	 * The local version of the file can be temporary and doesn't have to be persistent across requests
350
	 *
351
	 * @param string $path
352
	 * @return string
353
	 */
354
	public function getLocalFile($path) {
355
		return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path));
356
	}
357
358
	/**
359
	 * check if a file or folder has been updated since $time
360
	 *
361
	 * @param string $path
362
	 * @param int $time
363
	 * @return bool
364
	 *
365
	 * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
366
	 * returning true for other changes in the folder is optional
367
	 */
368
	public function hasUpdated($path, $time) {
369
		return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time);
370
	}
371
372
	/**
373
	 * get a cache instance for the storage
374
	 *
375
	 * @param string $path
376
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
377
	 * @return \OC\Files\Cache\Cache
378
	 */
379
	public function getCache($path = '', $storage = null) {
380
		if (!$storage) {
381
			$storage = $this->getWrapperStorage();
382
		}
383
		$sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path), $storage);
384
		return new CacheJail($sourceCache, $this->rootPath);
385
	}
386
387
	/**
388
	 * get the user id of the owner of a file or folder
389
	 *
390
	 * @param string $path
391
	 * @return string
392
	 */
393
	public function getOwner($path) {
394
		return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path));
395
	}
396
397
	/**
398
	 * get a watcher instance for the cache
399
	 *
400
	 * @param string $path
401
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
402
	 * @return \OC\Files\Cache\Watcher
403
	 */
404
	public function getWatcher($path = '', $storage = null) {
405
		if (!$storage) {
406
			$storage = $this;
407
		}
408
		return $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $storage);
409
	}
410
411
	/**
412
	 * get the ETag for a file or folder
413
	 *
414
	 * @param string $path
415
	 * @return string
416
	 */
417
	public function getETag($path) {
418
		return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
419
	}
420
421
	/**
422
	 * @param string $path
423
	 * @return array
424
	 */
425
	public function getMetaData($path) {
426
		return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path));
427
	}
428
429
	/**
430
	 * @param string $path
431
	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
432
	 * @param \OCP\Lock\ILockingProvider $provider
433
	 * @throws \OCP\Lock\LockedException
434
	 */
435
	public function acquireLock($path, $type, ILockingProvider $provider) {
436
		$this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider);
437
	}
438
439
	/**
440
	 * @param string $path
441
	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
442
	 * @param \OCP\Lock\ILockingProvider $provider
443
	 */
444
	public function releaseLock($path, $type, ILockingProvider $provider) {
445
		$this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider);
446
	}
447
448
	/**
449
	 * @param string $path
450
	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
451
	 * @param \OCP\Lock\ILockingProvider $provider
452
	 */
453
	public function changeLock($path, $type, ILockingProvider $provider) {
454
		$this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider);
455
	}
456
457
	/**
458
	 * Resolve the path for the source of the share
459
	 *
460
	 * @param string $path
461
	 * @return array
462
	 */
463
	public function resolvePath($path) {
464
		return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
465
	}
466
467
	/**
468
	 * @param \OCP\Files\Storage $sourceStorage
469
	 * @param string $sourceInternalPath
470
	 * @param string $targetInternalPath
471
	 * @return bool
472
	 */
473
	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
474
		if ($sourceStorage === $this) {
475
			return $this->copy($sourceInternalPath, $targetInternalPath);
476
		}
477
		return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
478
	}
479
480
	/**
481
	 * @param \OCP\Files\Storage $sourceStorage
482
	 * @param string $sourceInternalPath
483
	 * @param string $targetInternalPath
484
	 * @return bool
485
	 */
486
	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
487
		if ($sourceStorage === $this) {
488
			return $this->rename($sourceInternalPath, $targetInternalPath);
489
		}
490
		return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
491
	}
492
493 View Code Duplication
	public function getPropagator($storage = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
494
		if (isset($this->propagator)) {
495
			return $this->propagator;
496
		}
497
498
		if (!$storage) {
499
			$storage = $this;
500
		}
501
		$this->propagator = new JailPropagator($storage, \OC::$server->getDatabaseConnection());
502
		return $this->propagator;
503
	}
504
}
505