@@ -35,398 +35,398 @@ |
||
35 | 35 | |
36 | 36 | // FIXME: this class really should be abstract |
37 | 37 | class Node implements \OCP\Files\Node { |
38 | - /** |
|
39 | - * @var \OC\Files\View $view |
|
40 | - */ |
|
41 | - protected $view; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var \OC\Files\Node\Root $root |
|
45 | - */ |
|
46 | - protected $root; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var string $path |
|
50 | - */ |
|
51 | - protected $path; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var \OCP\Files\FileInfo |
|
55 | - */ |
|
56 | - protected $fileInfo; |
|
57 | - |
|
58 | - /** |
|
59 | - * @param \OC\Files\View $view |
|
60 | - * @param \OCP\Files\IRootFolder $root |
|
61 | - * @param string $path |
|
62 | - * @param FileInfo $fileInfo |
|
63 | - */ |
|
64 | - public function __construct($root, $view, $path, $fileInfo = null) { |
|
65 | - $this->view = $view; |
|
66 | - $this->root = $root; |
|
67 | - $this->path = $path; |
|
68 | - $this->fileInfo = $fileInfo; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Creates a Node of the same type that represents a non-existing path |
|
73 | - * |
|
74 | - * @param string $path path |
|
75 | - * @return string non-existing node class |
|
76 | - */ |
|
77 | - protected function createNonExistingNode($path) { |
|
78 | - throw new \Exception('Must be implemented by subclasses'); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Returns the matching file info |
|
83 | - * |
|
84 | - * @return FileInfo |
|
85 | - * @throws InvalidPathException |
|
86 | - * @throws NotFoundException |
|
87 | - */ |
|
88 | - public function getFileInfo() { |
|
89 | - if (!Filesystem::isValidPath($this->path)) { |
|
90 | - throw new InvalidPathException(); |
|
91 | - } |
|
92 | - if (!$this->fileInfo) { |
|
93 | - $fileInfo = $this->view->getFileInfo($this->path); |
|
94 | - if ($fileInfo instanceof FileInfo) { |
|
95 | - $this->fileInfo = $fileInfo; |
|
96 | - } else { |
|
97 | - throw new NotFoundException(); |
|
98 | - } |
|
99 | - } |
|
100 | - return $this->fileInfo; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string[] $hooks |
|
105 | - */ |
|
106 | - protected function sendHooks($hooks) { |
|
107 | - foreach ($hooks as $hook) { |
|
108 | - $this->root->emit('\OC\Files', $hook, array($this)); |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param int $permissions |
|
114 | - * @return bool |
|
115 | - */ |
|
116 | - protected function checkPermissions($permissions) { |
|
117 | - return ($this->getPermissions() & $permissions) === $permissions; |
|
118 | - } |
|
119 | - |
|
120 | - public function delete() { |
|
121 | - return; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param int $mtime |
|
126 | - * @throws \OCP\Files\NotPermittedException |
|
127 | - */ |
|
128 | - public function touch($mtime = null) { |
|
129 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
130 | - $this->sendHooks(array('preTouch')); |
|
131 | - $this->view->touch($this->path, $mtime); |
|
132 | - $this->sendHooks(array('postTouch')); |
|
133 | - if ($this->fileInfo) { |
|
134 | - if (is_null($mtime)) { |
|
135 | - $mtime = time(); |
|
136 | - } |
|
137 | - $this->fileInfo['mtime'] = $mtime; |
|
138 | - } |
|
139 | - } else { |
|
140 | - throw new NotPermittedException(); |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @return \OC\Files\Storage\Storage |
|
146 | - * @throws \OCP\Files\NotFoundException |
|
147 | - */ |
|
148 | - public function getStorage() { |
|
149 | - list($storage,) = $this->view->resolvePath($this->path); |
|
150 | - return $storage; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - public function getPath() { |
|
157 | - return $this->path; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @return string |
|
162 | - */ |
|
163 | - public function getInternalPath() { |
|
164 | - list(, $internalPath) = $this->view->resolvePath($this->path); |
|
165 | - return $internalPath; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @return int |
|
170 | - * @throws InvalidPathException |
|
171 | - * @throws NotFoundException |
|
172 | - */ |
|
173 | - public function getId() { |
|
174 | - return $this->getFileInfo()->getId(); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - public function stat() { |
|
181 | - return $this->view->stat($this->path); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * @return int |
|
186 | - * @throws InvalidPathException |
|
187 | - * @throws NotFoundException |
|
188 | - */ |
|
189 | - public function getMTime() { |
|
190 | - return $this->getFileInfo()->getMTime(); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @return int |
|
195 | - * @throws InvalidPathException |
|
196 | - * @throws NotFoundException |
|
197 | - */ |
|
198 | - public function getSize() { |
|
199 | - return $this->getFileInfo()->getSize(); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @return string |
|
204 | - * @throws InvalidPathException |
|
205 | - * @throws NotFoundException |
|
206 | - */ |
|
207 | - public function getEtag() { |
|
208 | - return $this->getFileInfo()->getEtag(); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @return int |
|
213 | - * @throws InvalidPathException |
|
214 | - * @throws NotFoundException |
|
215 | - */ |
|
216 | - public function getPermissions() { |
|
217 | - return $this->getFileInfo()->getPermissions(); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @return bool |
|
222 | - * @throws InvalidPathException |
|
223 | - * @throws NotFoundException |
|
224 | - */ |
|
225 | - public function isReadable() { |
|
226 | - return $this->getFileInfo()->isReadable(); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * @return bool |
|
231 | - * @throws InvalidPathException |
|
232 | - * @throws NotFoundException |
|
233 | - */ |
|
234 | - public function isUpdateable() { |
|
235 | - return $this->getFileInfo()->isUpdateable(); |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @return bool |
|
240 | - * @throws InvalidPathException |
|
241 | - * @throws NotFoundException |
|
242 | - */ |
|
243 | - public function isDeletable() { |
|
244 | - return $this->getFileInfo()->isDeletable(); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @return bool |
|
249 | - * @throws InvalidPathException |
|
250 | - * @throws NotFoundException |
|
251 | - */ |
|
252 | - public function isShareable() { |
|
253 | - return $this->getFileInfo()->isShareable(); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @return bool |
|
258 | - * @throws InvalidPathException |
|
259 | - * @throws NotFoundException |
|
260 | - */ |
|
261 | - public function isCreatable() { |
|
262 | - return $this->getFileInfo()->isCreatable(); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * @return Node |
|
267 | - */ |
|
268 | - public function getParent() { |
|
269 | - $newPath = dirname($this->path); |
|
270 | - if ($newPath === '' || $newPath === '.' || $newPath === '/') { |
|
271 | - return $this->root; |
|
272 | - } |
|
273 | - return $this->root->get($newPath); |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * @return string |
|
278 | - */ |
|
279 | - public function getName() { |
|
280 | - return basename($this->path); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @param string $path |
|
285 | - * @return string |
|
286 | - */ |
|
287 | - protected function normalizePath($path) { |
|
288 | - if ($path === '' or $path === '/') { |
|
289 | - return '/'; |
|
290 | - } |
|
291 | - //no windows style slashes |
|
292 | - $path = str_replace('\\', '/', $path); |
|
293 | - //add leading slash |
|
294 | - if ($path[0] !== '/') { |
|
295 | - $path = '/' . $path; |
|
296 | - } |
|
297 | - //remove duplicate slashes |
|
298 | - while (strpos($path, '//') !== false) { |
|
299 | - $path = str_replace('//', '/', $path); |
|
300 | - } |
|
301 | - //remove trailing slash |
|
302 | - $path = rtrim($path, '/'); |
|
303 | - |
|
304 | - return $path; |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * check if the requested path is valid |
|
309 | - * |
|
310 | - * @param string $path |
|
311 | - * @return bool |
|
312 | - */ |
|
313 | - public function isValidPath($path) { |
|
314 | - if (!$path || $path[0] !== '/') { |
|
315 | - $path = '/' . $path; |
|
316 | - } |
|
317 | - if (strstr($path, '/../') || strrchr($path, '/') === '/..') { |
|
318 | - return false; |
|
319 | - } |
|
320 | - return true; |
|
321 | - } |
|
322 | - |
|
323 | - public function isMounted() { |
|
324 | - return $this->getFileInfo()->isMounted(); |
|
325 | - } |
|
326 | - |
|
327 | - public function isShared() { |
|
328 | - return $this->getFileInfo()->isShared(); |
|
329 | - } |
|
330 | - |
|
331 | - public function getMimeType() { |
|
332 | - return $this->getFileInfo()->getMimetype(); |
|
333 | - } |
|
334 | - |
|
335 | - public function getMimePart() { |
|
336 | - return $this->getFileInfo()->getMimePart(); |
|
337 | - } |
|
338 | - |
|
339 | - public function getType() { |
|
340 | - return $this->getFileInfo()->getType(); |
|
341 | - } |
|
342 | - |
|
343 | - public function isEncrypted() { |
|
344 | - return $this->getFileInfo()->isEncrypted(); |
|
345 | - } |
|
346 | - |
|
347 | - public function getMountPoint() { |
|
348 | - return $this->getFileInfo()->getMountPoint(); |
|
349 | - } |
|
350 | - |
|
351 | - public function getOwner() { |
|
352 | - return $this->getFileInfo()->getOwner(); |
|
353 | - } |
|
354 | - |
|
355 | - public function getChecksum() { |
|
356 | - return; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
361 | - * @throws \OCP\Lock\LockedException |
|
362 | - */ |
|
363 | - public function lock($type) { |
|
364 | - $this->view->lockFile($this->path, $type); |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
369 | - * @throws \OCP\Lock\LockedException |
|
370 | - */ |
|
371 | - public function changeLock($type) { |
|
372 | - $this->view->changeLock($this->path, $type); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
377 | - * @throws \OCP\Lock\LockedException |
|
378 | - */ |
|
379 | - public function unlock($type) { |
|
380 | - $this->view->unlockFile($this->path, $type); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * @param string $targetPath |
|
385 | - * @throws \OCP\Files\NotPermittedException if copy not allowed or failed |
|
386 | - * @return \OC\Files\Node\Node |
|
387 | - */ |
|
388 | - public function copy($targetPath) { |
|
389 | - $targetPath = $this->normalizePath($targetPath); |
|
390 | - $parent = $this->root->get(dirname($targetPath)); |
|
391 | - if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
392 | - $nonExisting = $this->createNonExistingNode($targetPath); |
|
393 | - $this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]); |
|
394 | - $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
395 | - if (!$this->view->copy($this->path, $targetPath)) { |
|
396 | - throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath); |
|
397 | - } |
|
398 | - $targetNode = $this->root->get($targetPath); |
|
399 | - $this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]); |
|
400 | - $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
401 | - return $targetNode; |
|
402 | - } else { |
|
403 | - throw new NotPermittedException('No permission to copy to path ' . $targetPath); |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * @param string $targetPath |
|
409 | - * @throws \OCP\Files\NotPermittedException if move not allowed or failed |
|
410 | - * @return \OC\Files\Node\Node |
|
411 | - */ |
|
412 | - public function move($targetPath) { |
|
413 | - $targetPath = $this->normalizePath($targetPath); |
|
414 | - $parent = $this->root->get(dirname($targetPath)); |
|
415 | - if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
416 | - $nonExisting = $this->createNonExistingNode($targetPath); |
|
417 | - $this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]); |
|
418 | - $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
419 | - if (!$this->view->rename($this->path, $targetPath)) { |
|
420 | - throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath); |
|
421 | - } |
|
422 | - $targetNode = $this->root->get($targetPath); |
|
423 | - $this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]); |
|
424 | - $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
425 | - $this->path = $targetPath; |
|
426 | - return $targetNode; |
|
427 | - } else { |
|
428 | - throw new NotPermittedException('No permission to move to path ' . $targetPath); |
|
429 | - } |
|
430 | - } |
|
38 | + /** |
|
39 | + * @var \OC\Files\View $view |
|
40 | + */ |
|
41 | + protected $view; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var \OC\Files\Node\Root $root |
|
45 | + */ |
|
46 | + protected $root; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var string $path |
|
50 | + */ |
|
51 | + protected $path; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var \OCP\Files\FileInfo |
|
55 | + */ |
|
56 | + protected $fileInfo; |
|
57 | + |
|
58 | + /** |
|
59 | + * @param \OC\Files\View $view |
|
60 | + * @param \OCP\Files\IRootFolder $root |
|
61 | + * @param string $path |
|
62 | + * @param FileInfo $fileInfo |
|
63 | + */ |
|
64 | + public function __construct($root, $view, $path, $fileInfo = null) { |
|
65 | + $this->view = $view; |
|
66 | + $this->root = $root; |
|
67 | + $this->path = $path; |
|
68 | + $this->fileInfo = $fileInfo; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Creates a Node of the same type that represents a non-existing path |
|
73 | + * |
|
74 | + * @param string $path path |
|
75 | + * @return string non-existing node class |
|
76 | + */ |
|
77 | + protected function createNonExistingNode($path) { |
|
78 | + throw new \Exception('Must be implemented by subclasses'); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Returns the matching file info |
|
83 | + * |
|
84 | + * @return FileInfo |
|
85 | + * @throws InvalidPathException |
|
86 | + * @throws NotFoundException |
|
87 | + */ |
|
88 | + public function getFileInfo() { |
|
89 | + if (!Filesystem::isValidPath($this->path)) { |
|
90 | + throw new InvalidPathException(); |
|
91 | + } |
|
92 | + if (!$this->fileInfo) { |
|
93 | + $fileInfo = $this->view->getFileInfo($this->path); |
|
94 | + if ($fileInfo instanceof FileInfo) { |
|
95 | + $this->fileInfo = $fileInfo; |
|
96 | + } else { |
|
97 | + throw new NotFoundException(); |
|
98 | + } |
|
99 | + } |
|
100 | + return $this->fileInfo; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string[] $hooks |
|
105 | + */ |
|
106 | + protected function sendHooks($hooks) { |
|
107 | + foreach ($hooks as $hook) { |
|
108 | + $this->root->emit('\OC\Files', $hook, array($this)); |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param int $permissions |
|
114 | + * @return bool |
|
115 | + */ |
|
116 | + protected function checkPermissions($permissions) { |
|
117 | + return ($this->getPermissions() & $permissions) === $permissions; |
|
118 | + } |
|
119 | + |
|
120 | + public function delete() { |
|
121 | + return; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param int $mtime |
|
126 | + * @throws \OCP\Files\NotPermittedException |
|
127 | + */ |
|
128 | + public function touch($mtime = null) { |
|
129 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
130 | + $this->sendHooks(array('preTouch')); |
|
131 | + $this->view->touch($this->path, $mtime); |
|
132 | + $this->sendHooks(array('postTouch')); |
|
133 | + if ($this->fileInfo) { |
|
134 | + if (is_null($mtime)) { |
|
135 | + $mtime = time(); |
|
136 | + } |
|
137 | + $this->fileInfo['mtime'] = $mtime; |
|
138 | + } |
|
139 | + } else { |
|
140 | + throw new NotPermittedException(); |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @return \OC\Files\Storage\Storage |
|
146 | + * @throws \OCP\Files\NotFoundException |
|
147 | + */ |
|
148 | + public function getStorage() { |
|
149 | + list($storage,) = $this->view->resolvePath($this->path); |
|
150 | + return $storage; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + public function getPath() { |
|
157 | + return $this->path; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @return string |
|
162 | + */ |
|
163 | + public function getInternalPath() { |
|
164 | + list(, $internalPath) = $this->view->resolvePath($this->path); |
|
165 | + return $internalPath; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @return int |
|
170 | + * @throws InvalidPathException |
|
171 | + * @throws NotFoundException |
|
172 | + */ |
|
173 | + public function getId() { |
|
174 | + return $this->getFileInfo()->getId(); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + public function stat() { |
|
181 | + return $this->view->stat($this->path); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * @return int |
|
186 | + * @throws InvalidPathException |
|
187 | + * @throws NotFoundException |
|
188 | + */ |
|
189 | + public function getMTime() { |
|
190 | + return $this->getFileInfo()->getMTime(); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @return int |
|
195 | + * @throws InvalidPathException |
|
196 | + * @throws NotFoundException |
|
197 | + */ |
|
198 | + public function getSize() { |
|
199 | + return $this->getFileInfo()->getSize(); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @return string |
|
204 | + * @throws InvalidPathException |
|
205 | + * @throws NotFoundException |
|
206 | + */ |
|
207 | + public function getEtag() { |
|
208 | + return $this->getFileInfo()->getEtag(); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @return int |
|
213 | + * @throws InvalidPathException |
|
214 | + * @throws NotFoundException |
|
215 | + */ |
|
216 | + public function getPermissions() { |
|
217 | + return $this->getFileInfo()->getPermissions(); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @return bool |
|
222 | + * @throws InvalidPathException |
|
223 | + * @throws NotFoundException |
|
224 | + */ |
|
225 | + public function isReadable() { |
|
226 | + return $this->getFileInfo()->isReadable(); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * @return bool |
|
231 | + * @throws InvalidPathException |
|
232 | + * @throws NotFoundException |
|
233 | + */ |
|
234 | + public function isUpdateable() { |
|
235 | + return $this->getFileInfo()->isUpdateable(); |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @return bool |
|
240 | + * @throws InvalidPathException |
|
241 | + * @throws NotFoundException |
|
242 | + */ |
|
243 | + public function isDeletable() { |
|
244 | + return $this->getFileInfo()->isDeletable(); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @return bool |
|
249 | + * @throws InvalidPathException |
|
250 | + * @throws NotFoundException |
|
251 | + */ |
|
252 | + public function isShareable() { |
|
253 | + return $this->getFileInfo()->isShareable(); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @return bool |
|
258 | + * @throws InvalidPathException |
|
259 | + * @throws NotFoundException |
|
260 | + */ |
|
261 | + public function isCreatable() { |
|
262 | + return $this->getFileInfo()->isCreatable(); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * @return Node |
|
267 | + */ |
|
268 | + public function getParent() { |
|
269 | + $newPath = dirname($this->path); |
|
270 | + if ($newPath === '' || $newPath === '.' || $newPath === '/') { |
|
271 | + return $this->root; |
|
272 | + } |
|
273 | + return $this->root->get($newPath); |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * @return string |
|
278 | + */ |
|
279 | + public function getName() { |
|
280 | + return basename($this->path); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @param string $path |
|
285 | + * @return string |
|
286 | + */ |
|
287 | + protected function normalizePath($path) { |
|
288 | + if ($path === '' or $path === '/') { |
|
289 | + return '/'; |
|
290 | + } |
|
291 | + //no windows style slashes |
|
292 | + $path = str_replace('\\', '/', $path); |
|
293 | + //add leading slash |
|
294 | + if ($path[0] !== '/') { |
|
295 | + $path = '/' . $path; |
|
296 | + } |
|
297 | + //remove duplicate slashes |
|
298 | + while (strpos($path, '//') !== false) { |
|
299 | + $path = str_replace('//', '/', $path); |
|
300 | + } |
|
301 | + //remove trailing slash |
|
302 | + $path = rtrim($path, '/'); |
|
303 | + |
|
304 | + return $path; |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * check if the requested path is valid |
|
309 | + * |
|
310 | + * @param string $path |
|
311 | + * @return bool |
|
312 | + */ |
|
313 | + public function isValidPath($path) { |
|
314 | + if (!$path || $path[0] !== '/') { |
|
315 | + $path = '/' . $path; |
|
316 | + } |
|
317 | + if (strstr($path, '/../') || strrchr($path, '/') === '/..') { |
|
318 | + return false; |
|
319 | + } |
|
320 | + return true; |
|
321 | + } |
|
322 | + |
|
323 | + public function isMounted() { |
|
324 | + return $this->getFileInfo()->isMounted(); |
|
325 | + } |
|
326 | + |
|
327 | + public function isShared() { |
|
328 | + return $this->getFileInfo()->isShared(); |
|
329 | + } |
|
330 | + |
|
331 | + public function getMimeType() { |
|
332 | + return $this->getFileInfo()->getMimetype(); |
|
333 | + } |
|
334 | + |
|
335 | + public function getMimePart() { |
|
336 | + return $this->getFileInfo()->getMimePart(); |
|
337 | + } |
|
338 | + |
|
339 | + public function getType() { |
|
340 | + return $this->getFileInfo()->getType(); |
|
341 | + } |
|
342 | + |
|
343 | + public function isEncrypted() { |
|
344 | + return $this->getFileInfo()->isEncrypted(); |
|
345 | + } |
|
346 | + |
|
347 | + public function getMountPoint() { |
|
348 | + return $this->getFileInfo()->getMountPoint(); |
|
349 | + } |
|
350 | + |
|
351 | + public function getOwner() { |
|
352 | + return $this->getFileInfo()->getOwner(); |
|
353 | + } |
|
354 | + |
|
355 | + public function getChecksum() { |
|
356 | + return; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
361 | + * @throws \OCP\Lock\LockedException |
|
362 | + */ |
|
363 | + public function lock($type) { |
|
364 | + $this->view->lockFile($this->path, $type); |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
369 | + * @throws \OCP\Lock\LockedException |
|
370 | + */ |
|
371 | + public function changeLock($type) { |
|
372 | + $this->view->changeLock($this->path, $type); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
377 | + * @throws \OCP\Lock\LockedException |
|
378 | + */ |
|
379 | + public function unlock($type) { |
|
380 | + $this->view->unlockFile($this->path, $type); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * @param string $targetPath |
|
385 | + * @throws \OCP\Files\NotPermittedException if copy not allowed or failed |
|
386 | + * @return \OC\Files\Node\Node |
|
387 | + */ |
|
388 | + public function copy($targetPath) { |
|
389 | + $targetPath = $this->normalizePath($targetPath); |
|
390 | + $parent = $this->root->get(dirname($targetPath)); |
|
391 | + if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
392 | + $nonExisting = $this->createNonExistingNode($targetPath); |
|
393 | + $this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]); |
|
394 | + $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
395 | + if (!$this->view->copy($this->path, $targetPath)) { |
|
396 | + throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath); |
|
397 | + } |
|
398 | + $targetNode = $this->root->get($targetPath); |
|
399 | + $this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]); |
|
400 | + $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
401 | + return $targetNode; |
|
402 | + } else { |
|
403 | + throw new NotPermittedException('No permission to copy to path ' . $targetPath); |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * @param string $targetPath |
|
409 | + * @throws \OCP\Files\NotPermittedException if move not allowed or failed |
|
410 | + * @return \OC\Files\Node\Node |
|
411 | + */ |
|
412 | + public function move($targetPath) { |
|
413 | + $targetPath = $this->normalizePath($targetPath); |
|
414 | + $parent = $this->root->get(dirname($targetPath)); |
|
415 | + if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { |
|
416 | + $nonExisting = $this->createNonExistingNode($targetPath); |
|
417 | + $this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]); |
|
418 | + $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); |
|
419 | + if (!$this->view->rename($this->path, $targetPath)) { |
|
420 | + throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath); |
|
421 | + } |
|
422 | + $targetNode = $this->root->get($targetPath); |
|
423 | + $this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]); |
|
424 | + $this->root->emit('\OC\Files', 'postWrite', [$targetNode]); |
|
425 | + $this->path = $targetPath; |
|
426 | + return $targetNode; |
|
427 | + } else { |
|
428 | + throw new NotPermittedException('No permission to move to path ' . $targetPath); |
|
429 | + } |
|
430 | + } |
|
431 | 431 | |
432 | 432 | } |