Completed
Pull Request — master (#5685)
by Robin
34:26 queued 18:08
created
lib/private/Files/Storage/Wrapper/Jail.php 1 patch
Indentation   +467 added lines, -467 removed lines patch added patch discarded remove patch
@@ -34,471 +34,471 @@
 block discarded – undo
34 34
  * This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
35 35
  */
36 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
-	public function getPropagator($storage = null) {
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
-	}
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
+    public function getPropagator($storage = null) {
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 504
 }
Please login to merge, or discard this patch.
lib/private/Files/Cache/Wrapper/JailPropagator.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -26,19 +26,19 @@
 block discarded – undo
26 26
 use OC\Files\Storage\Wrapper\Jail;
27 27
 
28 28
 class JailPropagator extends Propagator {
29
-	/**
30
-	 * @var Jail
31
-	 */
32
-	protected $storage;
29
+    /**
30
+     * @var Jail
31
+     */
32
+    protected $storage;
33 33
 
34
-	/**
35
-	 * @param string $internalPath
36
-	 * @param int $time
37
-	 * @param int $sizeDifference
38
-	 */
39
-	public function propagateChange($internalPath, $time, $sizeDifference = 0) {
40
-		/** @var \OC\Files\Storage\Storage $storage */
41
-		list($storage, $sourceInternalPath) = $this->storage->resolvePath($internalPath);
42
-		$storage->getPropagator()->propagateChange($sourceInternalPath, $time, $sizeDifference);
43
-	}
34
+    /**
35
+     * @param string $internalPath
36
+     * @param int $time
37
+     * @param int $sizeDifference
38
+     */
39
+    public function propagateChange($internalPath, $time, $sizeDifference = 0) {
40
+        /** @var \OC\Files\Storage\Storage $storage */
41
+        list($storage, $sourceInternalPath) = $this->storage->resolvePath($internalPath);
42
+        $storage->getPropagator()->propagateChange($sourceInternalPath, $time, $sizeDifference);
43
+    }
44 44
 }
Please login to merge, or discard this patch.
apps/files_sharing/lib/SharedStorage.php 1 patch
Indentation   +438 added lines, -438 removed lines patch added patch discarded remove patch
@@ -47,442 +47,442 @@
 block discarded – undo
47 47
  */
48 48
 class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage {
49 49
 
50
-	/** @var \OCP\Share\IShare */
51
-	private $superShare;
52
-
53
-	/** @var \OCP\Share\IShare[] */
54
-	private $groupedShares;
55
-
56
-	/**
57
-	 * @var \OC\Files\View
58
-	 */
59
-	private $ownerView;
60
-
61
-	private $initialized = false;
62
-
63
-	/**
64
-	 * @var ICacheEntry
65
-	 */
66
-	private $sourceRootInfo;
67
-
68
-	/** @var string */
69
-	private $user;
70
-
71
-	/**
72
-	 * @var \OCP\ILogger
73
-	 */
74
-	private $logger;
75
-
76
-	/** @var  IStorage */
77
-	private $nonMaskedStorage;
78
-
79
-	private $options;
80
-
81
-	public function __construct($arguments) {
82
-		$this->ownerView = $arguments['ownerView'];
83
-		$this->logger = \OC::$server->getLogger();
84
-
85
-		$this->superShare = $arguments['superShare'];
86
-		$this->groupedShares = $arguments['groupedShares'];
87
-
88
-		$this->user = $arguments['user'];
89
-
90
-		parent::__construct([
91
-			'storage' => null,
92
-			'root' => null,
93
-		]);
94
-	}
95
-
96
-	/**
97
-	 * @return ICacheEntry
98
-	 */
99
-	private function getSourceRootInfo() {
100
-		if (is_null($this->sourceRootInfo)) {
101
-			if (is_null($this->superShare->getNodeCacheEntry())) {
102
-				$this->init();
103
-				$this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
104
-			} else {
105
-				$this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
106
-			}
107
-		}
108
-		return $this->sourceRootInfo;
109
-	}
110
-
111
-	private function init() {
112
-		if ($this->initialized) {
113
-			return;
114
-		}
115
-		$this->initialized = true;
116
-		try {
117
-			Filesystem::initMountPoints($this->superShare->getShareOwner());
118
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
119
-			list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
120
-			$this->storage = new PermissionsMask([
121
-				'storage' => $this->nonMaskedStorage,
122
-				'mask' => $this->superShare->getPermissions()
123
-			]);
124
-		} catch (NotFoundException $e) {
125
-			// original file not accessible or deleted, set FailedStorage
126
-			$this->storage = new FailedStorage(['exception' => $e]);
127
-			$this->cache = new FailedCache();
128
-			$this->rootPath = '';
129
-		} catch (NoUserException $e) {
130
-			// sharer user deleted, set FailedStorage
131
-			$this->storage = new FailedStorage(['exception' => $e]);
132
-			$this->cache = new FailedCache();
133
-			$this->rootPath = '';
134
-		} catch (\Exception $e) {
135
-			$this->storage = new FailedStorage(['exception' => $e]);
136
-			$this->cache = new FailedCache();
137
-			$this->rootPath = '';
138
-			$this->logger->logException($e);
139
-		}
140
-
141
-		if (!$this->nonMaskedStorage) {
142
-			$this->nonMaskedStorage = $this->storage;
143
-		}
144
-	}
145
-
146
-	/**
147
-	 * @inheritdoc
148
-	 */
149
-	public function instanceOfStorage($class) {
150
-		if ($class === '\OC\Files\Storage\Common') {
151
-			return true;
152
-		}
153
-		if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
154
-			return false;
155
-		}
156
-		return parent::instanceOfStorage($class);
157
-	}
158
-
159
-	/**
160
-	 * @return string
161
-	 */
162
-	public function getShareId() {
163
-		return $this->superShare->getId();
164
-	}
165
-
166
-	private function isValid() {
167
-		return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
168
-	}
169
-
170
-	/**
171
-	 * get id of the mount point
172
-	 *
173
-	 * @return string
174
-	 */
175
-	public function getId() {
176
-		return 'shared::' . $this->getMountPoint();
177
-	}
178
-
179
-	/**
180
-	 * Get the permissions granted for a shared file
181
-	 *
182
-	 * @param string $target Shared target file path
183
-	 * @return int CRUDS permissions granted
184
-	 */
185
-	public function getPermissions($target = '') {
186
-		if (!$this->isValid()) {
187
-			return 0;
188
-		}
189
-		$permissions = $this->superShare->getPermissions();
190
-		// part files and the mount point always have delete permissions
191
-		if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
192
-			$permissions |= \OCP\Constants::PERMISSION_DELETE;
193
-		}
194
-
195
-		if (\OCP\Util::isSharingDisabledForUser()) {
196
-			$permissions &= ~\OCP\Constants::PERMISSION_SHARE;
197
-		}
198
-
199
-		return $permissions;
200
-	}
201
-
202
-	public function isCreatable($path) {
203
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
204
-	}
205
-
206
-	public function isReadable($path) {
207
-		if (!$this->isValid()) {
208
-			return false;
209
-		}
210
-		if (!$this->file_exists($path)) {
211
-			return false;
212
-		}
213
-		/** @var IStorage $storage */
214
-		/** @var string $internalPath */
215
-		list($storage, $internalPath) = $this->resolvePath($path);
216
-		return $storage->isReadable($internalPath);
217
-	}
218
-
219
-	public function isUpdatable($path) {
220
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
221
-	}
222
-
223
-	public function isDeletable($path) {
224
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
225
-	}
226
-
227
-	public function isSharable($path) {
228
-		if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
229
-			return false;
230
-		}
231
-		return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
232
-	}
233
-
234
-	public function fopen($path, $mode) {
235
-		if ($source = $this->getUnjailedPath($path)) {
236
-			switch ($mode) {
237
-				case 'r+':
238
-				case 'rb+':
239
-				case 'w+':
240
-				case 'wb+':
241
-				case 'x+':
242
-				case 'xb+':
243
-				case 'a+':
244
-				case 'ab+':
245
-				case 'w':
246
-				case 'wb':
247
-				case 'x':
248
-				case 'xb':
249
-				case 'a':
250
-				case 'ab':
251
-					$creatable = $this->isCreatable($path);
252
-					$updatable = $this->isUpdatable($path);
253
-					// if neither permissions given, no need to continue
254
-					if (!$creatable && !$updatable) {
255
-						return false;
256
-					}
257
-
258
-					$exists = $this->file_exists($path);
259
-					// if a file exists, updatable permissions are required
260
-					if ($exists && !$updatable) {
261
-						return false;
262
-					}
263
-
264
-					// part file is allowed if !$creatable but the final file is $updatable
265
-					if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
266
-						if (!$exists && !$creatable) {
267
-							return false;
268
-						}
269
-					}
270
-			}
271
-			$info = array(
272
-				'target' => $this->getMountPoint() . $path,
273
-				'source' => $source,
274
-				'mode' => $mode,
275
-			);
276
-			\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
277
-			return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
278
-		}
279
-		return false;
280
-	}
281
-
282
-	/**
283
-	 * see http://php.net/manual/en/function.rename.php
284
-	 *
285
-	 * @param string $path1
286
-	 * @param string $path2
287
-	 * @return bool
288
-	 */
289
-	public function rename($path1, $path2) {
290
-		$this->init();
291
-		$isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
292
-		$targetExists = $this->file_exists($path2);
293
-		$sameFodler = dirname($path1) === dirname($path2);
294
-
295
-		if ($targetExists || ($sameFodler && !$isPartFile)) {
296
-			if (!$this->isUpdatable('')) {
297
-				return false;
298
-			}
299
-		} else {
300
-			if (!$this->isCreatable('')) {
301
-				return false;
302
-			}
303
-		}
304
-
305
-		return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
306
-	}
307
-
308
-	/**
309
-	 * return mount point of share, relative to data/user/files
310
-	 *
311
-	 * @return string
312
-	 */
313
-	public function getMountPoint() {
314
-		return $this->superShare->getTarget();
315
-	}
316
-
317
-	/**
318
-	 * @param string $path
319
-	 */
320
-	public function setMountPoint($path) {
321
-		$this->superShare->setTarget($path);
322
-
323
-		foreach ($this->groupedShares as $share) {
324
-			$share->setTarget($path);
325
-		}
326
-	}
327
-
328
-	/**
329
-	 * get the user who shared the file
330
-	 *
331
-	 * @return string
332
-	 */
333
-	public function getSharedFrom() {
334
-		return $this->superShare->getShareOwner();
335
-	}
336
-
337
-	/**
338
-	 * @return \OCP\Share\IShare
339
-	 */
340
-	public function getShare() {
341
-		return $this->superShare;
342
-	}
343
-
344
-	/**
345
-	 * return share type, can be "file" or "folder"
346
-	 *
347
-	 * @return string
348
-	 */
349
-	public function getItemType() {
350
-		return $this->superShare->getNodeType();
351
-	}
352
-
353
-	/**
354
-	 * @param string $path
355
-	 * @param null $storage
356
-	 * @return Cache
357
-	 */
358
-	public function getCache($path = '', $storage = null) {
359
-		if ($this->cache) {
360
-			return $this->cache;
361
-		}
362
-		if (!$storage) {
363
-			$storage = $this;
364
-		}
365
-		if ($this->storage instanceof FailedStorage) {
366
-			return new FailedCache();
367
-		}
368
-		$this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare);
369
-		return $this->cache;
370
-	}
371
-
372
-	public function getScanner($path = '', $storage = null) {
373
-		if (!$storage) {
374
-			$storage = $this;
375
-		}
376
-		return new \OCA\Files_Sharing\Scanner($storage);
377
-	}
378
-
379
-	public function getOwner($path) {
380
-		return $this->superShare->getShareOwner();
381
-	}
382
-
383
-	/**
384
-	 * unshare complete storage, also the grouped shares
385
-	 *
386
-	 * @return bool
387
-	 */
388
-	public function unshareStorage() {
389
-		foreach ($this->groupedShares as $share) {
390
-			\OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
391
-		}
392
-		return true;
393
-	}
394
-
395
-	/**
396
-	 * @param string $path
397
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
398
-	 * @param \OCP\Lock\ILockingProvider $provider
399
-	 * @throws \OCP\Lock\LockedException
400
-	 */
401
-	public function acquireLock($path, $type, ILockingProvider $provider) {
402
-		/** @var \OCP\Files\Storage $targetStorage */
403
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
404
-		$targetStorage->acquireLock($targetInternalPath, $type, $provider);
405
-		// lock the parent folders of the owner when locking the share as recipient
406
-		if ($path === '') {
407
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
408
-			$this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
409
-		}
410
-	}
411
-
412
-	/**
413
-	 * @param string $path
414
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
415
-	 * @param \OCP\Lock\ILockingProvider $provider
416
-	 */
417
-	public function releaseLock($path, $type, ILockingProvider $provider) {
418
-		/** @var \OCP\Files\Storage $targetStorage */
419
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
420
-		$targetStorage->releaseLock($targetInternalPath, $type, $provider);
421
-		// unlock the parent folders of the owner when unlocking the share as recipient
422
-		if ($path === '') {
423
-			$sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
424
-			$this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
425
-		}
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
-	 */
433
-	public function changeLock($path, $type, ILockingProvider $provider) {
434
-		/** @var \OCP\Files\Storage $targetStorage */
435
-		list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
436
-		$targetStorage->changeLock($targetInternalPath, $type, $provider);
437
-	}
438
-
439
-	/**
440
-	 * @return array [ available, last_checked ]
441
-	 */
442
-	public function getAvailability() {
443
-		// shares do not participate in availability logic
444
-		return [
445
-			'available' => true,
446
-			'last_checked' => 0
447
-		];
448
-	}
449
-
450
-	/**
451
-	 * @param bool $available
452
-	 */
453
-	public function setAvailability($available) {
454
-		// shares do not participate in availability logic
455
-	}
456
-
457
-	public function getSourceStorage() {
458
-		$this->init();
459
-		return $this->nonMaskedStorage;
460
-	}
461
-
462
-	public function getWrapperStorage() {
463
-		$this->init();
464
-		return $this->storage;
465
-	}
466
-
467
-	public function file_get_contents($path) {
468
-		$info = [
469
-			'target' => $this->getMountPoint() . '/' . $path,
470
-			'source' => $this->getUnjailedPath($path),
471
-		];
472
-		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
473
-		return parent::file_get_contents($path);
474
-	}
475
-
476
-	public function file_put_contents($path, $data) {
477
-		$info = [
478
-			'target' => $this->getMountPoint() . '/' . $path,
479
-			'source' => $this->getUnjailedPath($path),
480
-		];
481
-		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
482
-		return parent::file_put_contents($path, $data);
483
-	}
484
-
485
-	public function setMountOptions(array $options) {
486
-		$this->mountOptions = $options;
487
-	}
50
+    /** @var \OCP\Share\IShare */
51
+    private $superShare;
52
+
53
+    /** @var \OCP\Share\IShare[] */
54
+    private $groupedShares;
55
+
56
+    /**
57
+     * @var \OC\Files\View
58
+     */
59
+    private $ownerView;
60
+
61
+    private $initialized = false;
62
+
63
+    /**
64
+     * @var ICacheEntry
65
+     */
66
+    private $sourceRootInfo;
67
+
68
+    /** @var string */
69
+    private $user;
70
+
71
+    /**
72
+     * @var \OCP\ILogger
73
+     */
74
+    private $logger;
75
+
76
+    /** @var  IStorage */
77
+    private $nonMaskedStorage;
78
+
79
+    private $options;
80
+
81
+    public function __construct($arguments) {
82
+        $this->ownerView = $arguments['ownerView'];
83
+        $this->logger = \OC::$server->getLogger();
84
+
85
+        $this->superShare = $arguments['superShare'];
86
+        $this->groupedShares = $arguments['groupedShares'];
87
+
88
+        $this->user = $arguments['user'];
89
+
90
+        parent::__construct([
91
+            'storage' => null,
92
+            'root' => null,
93
+        ]);
94
+    }
95
+
96
+    /**
97
+     * @return ICacheEntry
98
+     */
99
+    private function getSourceRootInfo() {
100
+        if (is_null($this->sourceRootInfo)) {
101
+            if (is_null($this->superShare->getNodeCacheEntry())) {
102
+                $this->init();
103
+                $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath);
104
+            } else {
105
+                $this->sourceRootInfo = $this->superShare->getNodeCacheEntry();
106
+            }
107
+        }
108
+        return $this->sourceRootInfo;
109
+    }
110
+
111
+    private function init() {
112
+        if ($this->initialized) {
113
+            return;
114
+        }
115
+        $this->initialized = true;
116
+        try {
117
+            Filesystem::initMountPoints($this->superShare->getShareOwner());
118
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
119
+            list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath);
120
+            $this->storage = new PermissionsMask([
121
+                'storage' => $this->nonMaskedStorage,
122
+                'mask' => $this->superShare->getPermissions()
123
+            ]);
124
+        } catch (NotFoundException $e) {
125
+            // original file not accessible or deleted, set FailedStorage
126
+            $this->storage = new FailedStorage(['exception' => $e]);
127
+            $this->cache = new FailedCache();
128
+            $this->rootPath = '';
129
+        } catch (NoUserException $e) {
130
+            // sharer user deleted, set FailedStorage
131
+            $this->storage = new FailedStorage(['exception' => $e]);
132
+            $this->cache = new FailedCache();
133
+            $this->rootPath = '';
134
+        } catch (\Exception $e) {
135
+            $this->storage = new FailedStorage(['exception' => $e]);
136
+            $this->cache = new FailedCache();
137
+            $this->rootPath = '';
138
+            $this->logger->logException($e);
139
+        }
140
+
141
+        if (!$this->nonMaskedStorage) {
142
+            $this->nonMaskedStorage = $this->storage;
143
+        }
144
+    }
145
+
146
+    /**
147
+     * @inheritdoc
148
+     */
149
+    public function instanceOfStorage($class) {
150
+        if ($class === '\OC\Files\Storage\Common') {
151
+            return true;
152
+        }
153
+        if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) {
154
+            return false;
155
+        }
156
+        return parent::instanceOfStorage($class);
157
+    }
158
+
159
+    /**
160
+     * @return string
161
+     */
162
+    public function getShareId() {
163
+        return $this->superShare->getId();
164
+    }
165
+
166
+    private function isValid() {
167
+        return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
168
+    }
169
+
170
+    /**
171
+     * get id of the mount point
172
+     *
173
+     * @return string
174
+     */
175
+    public function getId() {
176
+        return 'shared::' . $this->getMountPoint();
177
+    }
178
+
179
+    /**
180
+     * Get the permissions granted for a shared file
181
+     *
182
+     * @param string $target Shared target file path
183
+     * @return int CRUDS permissions granted
184
+     */
185
+    public function getPermissions($target = '') {
186
+        if (!$this->isValid()) {
187
+            return 0;
188
+        }
189
+        $permissions = $this->superShare->getPermissions();
190
+        // part files and the mount point always have delete permissions
191
+        if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') {
192
+            $permissions |= \OCP\Constants::PERMISSION_DELETE;
193
+        }
194
+
195
+        if (\OCP\Util::isSharingDisabledForUser()) {
196
+            $permissions &= ~\OCP\Constants::PERMISSION_SHARE;
197
+        }
198
+
199
+        return $permissions;
200
+    }
201
+
202
+    public function isCreatable($path) {
203
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE);
204
+    }
205
+
206
+    public function isReadable($path) {
207
+        if (!$this->isValid()) {
208
+            return false;
209
+        }
210
+        if (!$this->file_exists($path)) {
211
+            return false;
212
+        }
213
+        /** @var IStorage $storage */
214
+        /** @var string $internalPath */
215
+        list($storage, $internalPath) = $this->resolvePath($path);
216
+        return $storage->isReadable($internalPath);
217
+    }
218
+
219
+    public function isUpdatable($path) {
220
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE);
221
+    }
222
+
223
+    public function isDeletable($path) {
224
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE);
225
+    }
226
+
227
+    public function isSharable($path) {
228
+        if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) {
229
+            return false;
230
+        }
231
+        return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE);
232
+    }
233
+
234
+    public function fopen($path, $mode) {
235
+        if ($source = $this->getUnjailedPath($path)) {
236
+            switch ($mode) {
237
+                case 'r+':
238
+                case 'rb+':
239
+                case 'w+':
240
+                case 'wb+':
241
+                case 'x+':
242
+                case 'xb+':
243
+                case 'a+':
244
+                case 'ab+':
245
+                case 'w':
246
+                case 'wb':
247
+                case 'x':
248
+                case 'xb':
249
+                case 'a':
250
+                case 'ab':
251
+                    $creatable = $this->isCreatable($path);
252
+                    $updatable = $this->isUpdatable($path);
253
+                    // if neither permissions given, no need to continue
254
+                    if (!$creatable && !$updatable) {
255
+                        return false;
256
+                    }
257
+
258
+                    $exists = $this->file_exists($path);
259
+                    // if a file exists, updatable permissions are required
260
+                    if ($exists && !$updatable) {
261
+                        return false;
262
+                    }
263
+
264
+                    // part file is allowed if !$creatable but the final file is $updatable
265
+                    if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') {
266
+                        if (!$exists && !$creatable) {
267
+                            return false;
268
+                        }
269
+                    }
270
+            }
271
+            $info = array(
272
+                'target' => $this->getMountPoint() . $path,
273
+                'source' => $source,
274
+                'mode' => $mode,
275
+            );
276
+            \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
277
+            return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode);
278
+        }
279
+        return false;
280
+    }
281
+
282
+    /**
283
+     * see http://php.net/manual/en/function.rename.php
284
+     *
285
+     * @param string $path1
286
+     * @param string $path2
287
+     * @return bool
288
+     */
289
+    public function rename($path1, $path2) {
290
+        $this->init();
291
+        $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part';
292
+        $targetExists = $this->file_exists($path2);
293
+        $sameFodler = dirname($path1) === dirname($path2);
294
+
295
+        if ($targetExists || ($sameFodler && !$isPartFile)) {
296
+            if (!$this->isUpdatable('')) {
297
+                return false;
298
+            }
299
+        } else {
300
+            if (!$this->isCreatable('')) {
301
+                return false;
302
+            }
303
+        }
304
+
305
+        return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2));
306
+    }
307
+
308
+    /**
309
+     * return mount point of share, relative to data/user/files
310
+     *
311
+     * @return string
312
+     */
313
+    public function getMountPoint() {
314
+        return $this->superShare->getTarget();
315
+    }
316
+
317
+    /**
318
+     * @param string $path
319
+     */
320
+    public function setMountPoint($path) {
321
+        $this->superShare->setTarget($path);
322
+
323
+        foreach ($this->groupedShares as $share) {
324
+            $share->setTarget($path);
325
+        }
326
+    }
327
+
328
+    /**
329
+     * get the user who shared the file
330
+     *
331
+     * @return string
332
+     */
333
+    public function getSharedFrom() {
334
+        return $this->superShare->getShareOwner();
335
+    }
336
+
337
+    /**
338
+     * @return \OCP\Share\IShare
339
+     */
340
+    public function getShare() {
341
+        return $this->superShare;
342
+    }
343
+
344
+    /**
345
+     * return share type, can be "file" or "folder"
346
+     *
347
+     * @return string
348
+     */
349
+    public function getItemType() {
350
+        return $this->superShare->getNodeType();
351
+    }
352
+
353
+    /**
354
+     * @param string $path
355
+     * @param null $storage
356
+     * @return Cache
357
+     */
358
+    public function getCache($path = '', $storage = null) {
359
+        if ($this->cache) {
360
+            return $this->cache;
361
+        }
362
+        if (!$storage) {
363
+            $storage = $this;
364
+        }
365
+        if ($this->storage instanceof FailedStorage) {
366
+            return new FailedCache();
367
+        }
368
+        $this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare);
369
+        return $this->cache;
370
+    }
371
+
372
+    public function getScanner($path = '', $storage = null) {
373
+        if (!$storage) {
374
+            $storage = $this;
375
+        }
376
+        return new \OCA\Files_Sharing\Scanner($storage);
377
+    }
378
+
379
+    public function getOwner($path) {
380
+        return $this->superShare->getShareOwner();
381
+    }
382
+
383
+    /**
384
+     * unshare complete storage, also the grouped shares
385
+     *
386
+     * @return bool
387
+     */
388
+    public function unshareStorage() {
389
+        foreach ($this->groupedShares as $share) {
390
+            \OC::$server->getShareManager()->deleteFromSelf($share, $this->user);
391
+        }
392
+        return true;
393
+    }
394
+
395
+    /**
396
+     * @param string $path
397
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
398
+     * @param \OCP\Lock\ILockingProvider $provider
399
+     * @throws \OCP\Lock\LockedException
400
+     */
401
+    public function acquireLock($path, $type, ILockingProvider $provider) {
402
+        /** @var \OCP\Files\Storage $targetStorage */
403
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
404
+        $targetStorage->acquireLock($targetInternalPath, $type, $provider);
405
+        // lock the parent folders of the owner when locking the share as recipient
406
+        if ($path === '') {
407
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
408
+            $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
409
+        }
410
+    }
411
+
412
+    /**
413
+     * @param string $path
414
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
415
+     * @param \OCP\Lock\ILockingProvider $provider
416
+     */
417
+    public function releaseLock($path, $type, ILockingProvider $provider) {
418
+        /** @var \OCP\Files\Storage $targetStorage */
419
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
420
+        $targetStorage->releaseLock($targetInternalPath, $type, $provider);
421
+        // unlock the parent folders of the owner when unlocking the share as recipient
422
+        if ($path === '') {
423
+            $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
424
+            $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
425
+        }
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
+     */
433
+    public function changeLock($path, $type, ILockingProvider $provider) {
434
+        /** @var \OCP\Files\Storage $targetStorage */
435
+        list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
436
+        $targetStorage->changeLock($targetInternalPath, $type, $provider);
437
+    }
438
+
439
+    /**
440
+     * @return array [ available, last_checked ]
441
+     */
442
+    public function getAvailability() {
443
+        // shares do not participate in availability logic
444
+        return [
445
+            'available' => true,
446
+            'last_checked' => 0
447
+        ];
448
+    }
449
+
450
+    /**
451
+     * @param bool $available
452
+     */
453
+    public function setAvailability($available) {
454
+        // shares do not participate in availability logic
455
+    }
456
+
457
+    public function getSourceStorage() {
458
+        $this->init();
459
+        return $this->nonMaskedStorage;
460
+    }
461
+
462
+    public function getWrapperStorage() {
463
+        $this->init();
464
+        return $this->storage;
465
+    }
466
+
467
+    public function file_get_contents($path) {
468
+        $info = [
469
+            'target' => $this->getMountPoint() . '/' . $path,
470
+            'source' => $this->getUnjailedPath($path),
471
+        ];
472
+        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
473
+        return parent::file_get_contents($path);
474
+    }
475
+
476
+    public function file_put_contents($path, $data) {
477
+        $info = [
478
+            'target' => $this->getMountPoint() . '/' . $path,
479
+            'source' => $this->getUnjailedPath($path),
480
+        ];
481
+        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
482
+        return parent::file_put_contents($path, $data);
483
+    }
484
+
485
+    public function setMountOptions(array $options) {
486
+        $this->mountOptions = $options;
487
+    }
488 488
 }
Please login to merge, or discard this patch.