Completed
Push — master ( dccb89...31024b )
by Morris
12:39
created

Jail::filemtime()   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 1
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 OCP\Lock\ILockingProvider;
29
30
/**
31
 * Jail to a subdirectory of the wrapped storage
32
 *
33
 * This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
34
 */
35
class Jail extends Wrapper {
36
	/**
37
	 * @var string
38
	 */
39
	protected $rootPath;
40
41
	/**
42
	 * @param array $arguments ['storage' => $storage, 'mask' => $root]
43
	 *
44
	 * $storage: The storage that will be wrapper
45
	 * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
46
	 */
47
	public function __construct($arguments) {
48
		parent::__construct($arguments);
49
		$this->rootPath = $arguments['root'];
50
	}
51
52
	public function getUnjailedPath($path) {
53
		if ($path === '') {
54
			return $this->rootPath;
55
		} else {
56
			return $this->rootPath . '/' . $path;
57
		}
58
	}
59
60
	public function getId() {
61
		return parent::getId();
62
	}
63
64
	/**
65
	 * see http://php.net/manual/en/function.mkdir.php
66
	 *
67
	 * @param string $path
68
	 * @return bool
69
	 */
70
	public function mkdir($path) {
71
		return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path));
72
	}
73
74
	/**
75
	 * see http://php.net/manual/en/function.rmdir.php
76
	 *
77
	 * @param string $path
78
	 * @return bool
79
	 */
80
	public function rmdir($path) {
81
		return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
82
	}
83
84
	/**
85
	 * see http://php.net/manual/en/function.opendir.php
86
	 *
87
	 * @param string $path
88
	 * @return resource
89
	 */
90
	public function opendir($path) {
91
		return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path));
92
	}
93
94
	/**
95
	 * see http://php.net/manual/en/function.is_dir.php
96
	 *
97
	 * @param string $path
98
	 * @return bool
99
	 */
100
	public function is_dir($path) {
101
		return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path));
102
	}
103
104
	/**
105
	 * see http://php.net/manual/en/function.is_file.php
106
	 *
107
	 * @param string $path
108
	 * @return bool
109
	 */
110
	public function is_file($path) {
111
		return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path));
112
	}
113
114
	/**
115
	 * see http://php.net/manual/en/function.stat.php
116
	 * only the following keys are required in the result: size and mtime
117
	 *
118
	 * @param string $path
119
	 * @return array
120
	 */
121
	public function stat($path) {
122
		return $this->getWrapperStorage()->stat($this->getUnjailedPath($path));
123
	}
124
125
	/**
126
	 * see http://php.net/manual/en/function.filetype.php
127
	 *
128
	 * @param string $path
129
	 * @return bool
130
	 */
131
	public function filetype($path) {
132
		return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
133
	}
134
135
	/**
136
	 * see http://php.net/manual/en/function.filesize.php
137
	 * The result for filesize when called on a folder is required to be 0
138
	 *
139
	 * @param string $path
140
	 * @return int
141
	 */
142
	public function filesize($path) {
143
		return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path));
144
	}
145
146
	/**
147
	 * check if a file can be created in $path
148
	 *
149
	 * @param string $path
150
	 * @return bool
151
	 */
152
	public function isCreatable($path) {
153
		return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
154
	}
155
156
	/**
157
	 * check if a file can be read
158
	 *
159
	 * @param string $path
160
	 * @return bool
161
	 */
162
	public function isReadable($path) {
163
		return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
164
	}
165
166
	/**
167
	 * check if a file can be written to
168
	 *
169
	 * @param string $path
170
	 * @return bool
171
	 */
172
	public function isUpdatable($path) {
173
		return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
174
	}
175
176
	/**
177
	 * check if a file can be deleted
178
	 *
179
	 * @param string $path
180
	 * @return bool
181
	 */
182
	public function isDeletable($path) {
183
		return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
184
	}
185
186
	/**
187
	 * check if a file can be shared
188
	 *
189
	 * @param string $path
190
	 * @return bool
191
	 */
192
	public function isSharable($path) {
193
		return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path));
194
	}
195
196
	/**
197
	 * get the full permissions of a path.
198
	 * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
199
	 *
200
	 * @param string $path
201
	 * @return int
202
	 */
203
	public function getPermissions($path) {
204
		return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path));
205
	}
206
207
	/**
208
	 * see http://php.net/manual/en/function.file_exists.php
209
	 *
210
	 * @param string $path
211
	 * @return bool
212
	 */
213
	public function file_exists($path) {
214
		return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path));
215
	}
216
217
	/**
218
	 * see http://php.net/manual/en/function.filemtime.php
219
	 *
220
	 * @param string $path
221
	 * @return int
222
	 */
223
	public function filemtime($path) {
224
		return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
225
	}
226
227
	/**
228
	 * see http://php.net/manual/en/function.file_get_contents.php
229
	 *
230
	 * @param string $path
231
	 * @return string
232
	 */
233
	public function file_get_contents($path) {
234
		return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
235
	}
236
237
	/**
238
	 * see http://php.net/manual/en/function.file_put_contents.php
239
	 *
240
	 * @param string $path
241
	 * @param string $data
242
	 * @return bool
243
	 */
244
	public function file_put_contents($path, $data) {
245
		return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data);
246
	}
247
248
	/**
249
	 * see http://php.net/manual/en/function.unlink.php
250
	 *
251
	 * @param string $path
252
	 * @return bool
253
	 */
254
	public function unlink($path) {
255
		return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
256
	}
257
258
	/**
259
	 * see http://php.net/manual/en/function.rename.php
260
	 *
261
	 * @param string $path1
262
	 * @param string $path2
263
	 * @return bool
264
	 */
265
	public function rename($path1, $path2) {
266
		return $this->getWrapperStorage()->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
267
	}
268
269
	/**
270
	 * see http://php.net/manual/en/function.copy.php
271
	 *
272
	 * @param string $path1
273
	 * @param string $path2
274
	 * @return bool
275
	 */
276
	public function copy($path1, $path2) {
277
		return $this->getWrapperStorage()->copy($this->getUnjailedPath($path1), $this->getUnjailedPath($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
		return $this->getWrapperStorage()->fopen($this->getUnjailedPath($path), $mode);
289
	}
290
291
	/**
292
	 * get the mimetype for a file or folder
293
	 * The mimetype for a folder is required to be "httpd/unix-directory"
294
	 *
295
	 * @param string $path
296
	 * @return string
297
	 */
298
	public function getMimeType($path) {
299
		return $this->getWrapperStorage()->getMimeType($this->getUnjailedPath($path));
300
	}
301
302
	/**
303
	 * see http://php.net/manual/en/function.hash.php
304
	 *
305
	 * @param string $type
306
	 * @param string $path
307
	 * @param bool $raw
308
	 * @return string
309
	 */
310
	public function hash($type, $path, $raw = false) {
311
		return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw);
312
	}
313
314
	/**
315
	 * see http://php.net/manual/en/function.free_space.php
316
	 *
317
	 * @param string $path
318
	 * @return int
319
	 */
320
	public function free_space($path) {
321
		return $this->getWrapperStorage()->free_space($this->getUnjailedPath($path));
322
	}
323
324
	/**
325
	 * search for occurrences of $query in file names
326
	 *
327
	 * @param string $query
328
	 * @return array
329
	 */
330
	public function search($query) {
331
		return $this->getWrapperStorage()->search($query);
332
	}
333
334
	/**
335
	 * see http://php.net/manual/en/function.touch.php
336
	 * If the backend does not support the operation, false should be returned
337
	 *
338
	 * @param string $path
339
	 * @param int $mtime
340
	 * @return bool
341
	 */
342
	public function touch($path, $mtime = null) {
343
		return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
344
	}
345
346
	/**
347
	 * get the path to a local version of the file.
348
	 * The local version of the file can be temporary and doesn't have to be persistent across requests
349
	 *
350
	 * @param string $path
351
	 * @return string
352
	 */
353
	public function getLocalFile($path) {
354
		return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path));
355
	}
356
357
	/**
358
	 * check if a file or folder has been updated since $time
359
	 *
360
	 * @param string $path
361
	 * @param int $time
362
	 * @return bool
363
	 *
364
	 * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
365
	 * returning true for other changes in the folder is optional
366
	 */
367
	public function hasUpdated($path, $time) {
368
		return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time);
369
	}
370
371
	/**
372
	 * get a cache instance for the storage
373
	 *
374
	 * @param string $path
375
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
376
	 * @return \OC\Files\Cache\Cache
377
	 */
378
	public function getCache($path = '', $storage = null) {
379
		if (!$storage) {
380
			$storage = $this->getWrapperStorage();
381
		}
382
		$sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path), $storage);
383
		return new CacheJail($sourceCache, $this->rootPath);
384
	}
385
386
	/**
387
	 * get the user id of the owner of a file or folder
388
	 *
389
	 * @param string $path
390
	 * @return string
391
	 */
392
	public function getOwner($path) {
393
		return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path));
394
	}
395
396
	/**
397
	 * get a watcher instance for the cache
398
	 *
399
	 * @param string $path
400
	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
401
	 * @return \OC\Files\Cache\Watcher
402
	 */
403
	public function getWatcher($path = '', $storage = null) {
404
		if (!$storage) {
405
			$storage = $this;
406
		}
407
		return $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $storage);
408
	}
409
410
	/**
411
	 * get the ETag for a file or folder
412
	 *
413
	 * @param string $path
414
	 * @return string
415
	 */
416
	public function getETag($path) {
417
		return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
418
	}
419
420
	/**
421
	 * @param string $path
422
	 * @return array
423
	 */
424
	public function getMetaData($path) {
425
		return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path));
426
	}
427
428
	/**
429
	 * @param string $path
430
	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
431
	 * @param \OCP\Lock\ILockingProvider $provider
432
	 * @throws \OCP\Lock\LockedException
433
	 */
434
	public function acquireLock($path, $type, ILockingProvider $provider) {
435
		$this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider);
436
	}
437
438
	/**
439
	 * @param string $path
440
	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
441
	 * @param \OCP\Lock\ILockingProvider $provider
442
	 */
443
	public function releaseLock($path, $type, ILockingProvider $provider) {
444
		$this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider);
445
	}
446
447
	/**
448
	 * @param string $path
449
	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
450
	 * @param \OCP\Lock\ILockingProvider $provider
451
	 */
452
	public function changeLock($path, $type, ILockingProvider $provider) {
453
		$this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider);
454
	}
455
456
	/**
457
	 * Resolve the path for the source of the share
458
	 *
459
	 * @param string $path
460
	 * @return array
461
	 */
462
	public function resolvePath($path) {
463
		return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
464
	}
465
466
	/**
467
	 * @param \OCP\Files\Storage $sourceStorage
468
	 * @param string $sourceInternalPath
469
	 * @param string $targetInternalPath
470
	 * @return bool
471
	 */
472
	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
473
		if ($sourceStorage === $this) {
474
			return $this->copy($sourceInternalPath, $targetInternalPath);
475
		}
476
		return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
477
	}
478
479
	/**
480
	 * @param \OCP\Files\Storage $sourceStorage
481
	 * @param string $sourceInternalPath
482
	 * @param string $targetInternalPath
483
	 * @return bool
484
	 */
485
	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
486
		if ($sourceStorage === $this) {
487
			return $this->rename($sourceInternalPath, $targetInternalPath);
488
		}
489
		return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
490
	}
491
}
492