@@ -162,6 +162,9 @@ discard block |
||
| 162 | 162 | return true; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | + /** |
|
| 166 | + * @param string $path |
|
| 167 | + */ |
|
| 165 | 168 | private function rmObjects($path) { |
| 166 | 169 | $children = $this->getCache()->getFolderContents($path); |
| 167 | 170 | foreach ($children as $child) { |
@@ -364,6 +367,9 @@ discard block |
||
| 364 | 367 | return true; |
| 365 | 368 | } |
| 366 | 369 | |
| 370 | + /** |
|
| 371 | + * @param string $path |
|
| 372 | + */ |
|
| 367 | 373 | public function writeBack($tmpFile, $path) { |
| 368 | 374 | $stat = $this->stat($path); |
| 369 | 375 | if (empty($stat)) { |
@@ -31,396 +31,396 @@ |
||
| 31 | 31 | use OCP\Files\ObjectStore\IObjectStore; |
| 32 | 32 | |
| 33 | 33 | class ObjectStoreStorage extends \OC\Files\Storage\Common { |
| 34 | - /** |
|
| 35 | - * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 36 | - */ |
|
| 37 | - protected $objectStore; |
|
| 38 | - /** |
|
| 39 | - * @var string $id |
|
| 40 | - */ |
|
| 41 | - protected $id; |
|
| 42 | - /** |
|
| 43 | - * @var \OC\User\User $user |
|
| 44 | - */ |
|
| 45 | - protected $user; |
|
| 46 | - |
|
| 47 | - private $objectPrefix = 'urn:oid:'; |
|
| 48 | - |
|
| 49 | - private $logger; |
|
| 50 | - |
|
| 51 | - public function __construct($params) { |
|
| 52 | - if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 53 | - $this->objectStore = $params['objectstore']; |
|
| 54 | - } else { |
|
| 55 | - throw new \Exception('missing IObjectStore instance'); |
|
| 56 | - } |
|
| 57 | - if (isset($params['storageid'])) { |
|
| 58 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 59 | - } else { |
|
| 60 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 61 | - } |
|
| 62 | - if (isset($params['objectPrefix'])) { |
|
| 63 | - $this->objectPrefix = $params['objectPrefix']; |
|
| 64 | - } |
|
| 65 | - //initialize cache with root directory in cache |
|
| 66 | - if (!$this->is_dir('/')) { |
|
| 67 | - $this->mkdir('/'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->logger = \OC::$server->getLogger(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function mkdir($path) { |
|
| 74 | - $path = $this->normalizePath($path); |
|
| 75 | - |
|
| 76 | - if ($this->file_exists($path)) { |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $mTime = time(); |
|
| 81 | - $data = [ |
|
| 82 | - 'mimetype' => 'httpd/unix-directory', |
|
| 83 | - 'size' => 0, |
|
| 84 | - 'mtime' => $mTime, |
|
| 85 | - 'storage_mtime' => $mTime, |
|
| 86 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 87 | - ]; |
|
| 88 | - if ($path === '') { |
|
| 89 | - //create root on the fly |
|
| 90 | - $data['etag'] = $this->getETag(''); |
|
| 91 | - $this->getCache()->put('', $data); |
|
| 92 | - return true; |
|
| 93 | - } else { |
|
| 94 | - // if parent does not exist, create it |
|
| 95 | - $parent = $this->normalizePath(dirname($path)); |
|
| 96 | - $parentType = $this->filetype($parent); |
|
| 97 | - if ($parentType === false) { |
|
| 98 | - if (!$this->mkdir($parent)) { |
|
| 99 | - // something went wrong |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - } else if ($parentType === 'file') { |
|
| 103 | - // parent is a file |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - // finally create the new dir |
|
| 107 | - $mTime = time(); // update mtime |
|
| 108 | - $data['mtime'] = $mTime; |
|
| 109 | - $data['storage_mtime'] = $mTime; |
|
| 110 | - $data['etag'] = $this->getETag($path); |
|
| 111 | - $this->getCache()->put($path, $data); |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param string $path |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - private function normalizePath($path) { |
|
| 121 | - $path = trim($path, '/'); |
|
| 122 | - //FIXME why do we sometimes get a path like 'files//username'? |
|
| 123 | - $path = str_replace('//', '/', $path); |
|
| 124 | - |
|
| 125 | - // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 126 | - if (!$path || $path === '.') { |
|
| 127 | - $path = ''; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - return $path; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 135 | - * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 136 | - * |
|
| 137 | - * @param string $path |
|
| 138 | - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 139 | - * @return \OC\Files\ObjectStore\NoopScanner |
|
| 140 | - */ |
|
| 141 | - public function getScanner($path = '', $storage = null) { |
|
| 142 | - if (!$storage) { |
|
| 143 | - $storage = $this; |
|
| 144 | - } |
|
| 145 | - if (!isset($this->scanner)) { |
|
| 146 | - $this->scanner = new NoopScanner($storage); |
|
| 147 | - } |
|
| 148 | - return $this->scanner; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - public function getId() { |
|
| 152 | - return $this->id; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - public function rmdir($path) { |
|
| 156 | - $path = $this->normalizePath($path); |
|
| 157 | - |
|
| 158 | - if (!$this->is_dir($path)) { |
|
| 159 | - return false; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $this->rmObjects($path); |
|
| 163 | - |
|
| 164 | - $this->getCache()->remove($path); |
|
| 165 | - |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - private function rmObjects($path) { |
|
| 170 | - $children = $this->getCache()->getFolderContents($path); |
|
| 171 | - foreach ($children as $child) { |
|
| 172 | - if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 173 | - $this->rmObjects($child['path']); |
|
| 174 | - } else { |
|
| 175 | - $this->unlink($child['path']); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - public function unlink($path) { |
|
| 181 | - $path = $this->normalizePath($path); |
|
| 182 | - $stat = $this->stat($path); |
|
| 183 | - |
|
| 184 | - if ($stat && isset($stat['fileid'])) { |
|
| 185 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 186 | - return $this->rmdir($path); |
|
| 187 | - } |
|
| 188 | - try { |
|
| 189 | - $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 190 | - } catch (\Exception $ex) { |
|
| 191 | - if ($ex->getCode() !== 404) { |
|
| 192 | - $this->logger->logException($ex, [ |
|
| 193 | - 'app' => 'objectstore', |
|
| 194 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 195 | - ]); |
|
| 196 | - return false; |
|
| 197 | - } |
|
| 198 | - //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | - } |
|
| 200 | - $this->getCache()->remove($path); |
|
| 201 | - return true; |
|
| 202 | - } |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - public function stat($path) { |
|
| 207 | - $path = $this->normalizePath($path); |
|
| 208 | - $cacheEntry = $this->getCache()->get($path); |
|
| 209 | - if ($cacheEntry instanceof CacheEntry) { |
|
| 210 | - return $cacheEntry->getData(); |
|
| 211 | - } else { |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 218 | - * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 219 | - * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 220 | - * |
|
| 221 | - * @param int $fileId the fileid |
|
| 222 | - * @return null|string the unified resource name used to identify the object |
|
| 223 | - */ |
|
| 224 | - protected function getURN($fileId) { |
|
| 225 | - if (is_numeric($fileId)) { |
|
| 226 | - return $this->objectPrefix . $fileId; |
|
| 227 | - } |
|
| 228 | - return null; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - public function opendir($path) { |
|
| 232 | - $path = $this->normalizePath($path); |
|
| 233 | - |
|
| 234 | - try { |
|
| 235 | - $files = array(); |
|
| 236 | - $folderContents = $this->getCache()->getFolderContents($path); |
|
| 237 | - foreach ($folderContents as $file) { |
|
| 238 | - $files[] = $file['name']; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - return IteratorDirectory::wrap($files); |
|
| 242 | - } catch (\Exception $e) { |
|
| 243 | - $this->logger->logException($e); |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - public function filetype($path) { |
|
| 249 | - $path = $this->normalizePath($path); |
|
| 250 | - $stat = $this->stat($path); |
|
| 251 | - if ($stat) { |
|
| 252 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 253 | - return 'dir'; |
|
| 254 | - } |
|
| 255 | - return 'file'; |
|
| 256 | - } else { |
|
| 257 | - return false; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - public function fopen($path, $mode) { |
|
| 262 | - $path = $this->normalizePath($path); |
|
| 263 | - |
|
| 264 | - switch ($mode) { |
|
| 265 | - case 'r': |
|
| 266 | - case 'rb': |
|
| 267 | - $stat = $this->stat($path); |
|
| 268 | - if (is_array($stat)) { |
|
| 269 | - try { |
|
| 270 | - return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 271 | - } catch (\Exception $ex) { |
|
| 272 | - $this->logger->logException($ex, [ |
|
| 273 | - 'app' => 'objectstore', |
|
| 274 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 275 | - ]); |
|
| 276 | - return false; |
|
| 277 | - } |
|
| 278 | - } else { |
|
| 279 | - return false; |
|
| 280 | - } |
|
| 281 | - case 'w': |
|
| 282 | - case 'wb': |
|
| 283 | - case 'a': |
|
| 284 | - case 'ab': |
|
| 285 | - case 'r+': |
|
| 286 | - case 'w+': |
|
| 287 | - case 'wb+': |
|
| 288 | - case 'a+': |
|
| 289 | - case 'x': |
|
| 290 | - case 'x+': |
|
| 291 | - case 'c': |
|
| 292 | - case 'c+': |
|
| 293 | - if (strrpos($path, '.') !== false) { |
|
| 294 | - $ext = substr($path, strrpos($path, '.')); |
|
| 295 | - } else { |
|
| 296 | - $ext = ''; |
|
| 297 | - } |
|
| 298 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 299 | - if ($this->file_exists($path)) { |
|
| 300 | - $source = $this->fopen($path, 'r'); |
|
| 301 | - file_put_contents($tmpFile, $source); |
|
| 302 | - } |
|
| 303 | - $handle = fopen($tmpFile, $mode); |
|
| 304 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 305 | - $this->writeBack($tmpFile, $path); |
|
| 306 | - }); |
|
| 307 | - } |
|
| 308 | - return false; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - public function file_exists($path) { |
|
| 312 | - $path = $this->normalizePath($path); |
|
| 313 | - return (bool)$this->stat($path); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - public function rename($source, $target) { |
|
| 317 | - $source = $this->normalizePath($source); |
|
| 318 | - $target = $this->normalizePath($target); |
|
| 319 | - $this->remove($target); |
|
| 320 | - $this->getCache()->move($source, $target); |
|
| 321 | - $this->touch(dirname($target)); |
|
| 322 | - return true; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - public function getMimeType($path) { |
|
| 326 | - $path = $this->normalizePath($path); |
|
| 327 | - $stat = $this->stat($path); |
|
| 328 | - if (is_array($stat)) { |
|
| 329 | - return $stat['mimetype']; |
|
| 330 | - } else { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - public function touch($path, $mtime = null) { |
|
| 336 | - if (is_null($mtime)) { |
|
| 337 | - $mtime = time(); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $path = $this->normalizePath($path); |
|
| 341 | - $dirName = dirname($path); |
|
| 342 | - $parentExists = $this->is_dir($dirName); |
|
| 343 | - if (!$parentExists) { |
|
| 344 | - return false; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - $stat = $this->stat($path); |
|
| 348 | - if (is_array($stat)) { |
|
| 349 | - // update existing mtime in db |
|
| 350 | - $stat['mtime'] = $mtime; |
|
| 351 | - $this->getCache()->update($stat['fileid'], $stat); |
|
| 352 | - } else { |
|
| 353 | - try { |
|
| 354 | - //create a empty file, need to have at least on char to make it |
|
| 355 | - // work with all object storage implementations |
|
| 356 | - $this->file_put_contents($path, ' '); |
|
| 357 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 358 | - $stat = array( |
|
| 359 | - 'etag' => $this->getETag($path), |
|
| 360 | - 'mimetype' => $mimeType, |
|
| 361 | - 'size' => 0, |
|
| 362 | - 'mtime' => $mtime, |
|
| 363 | - 'storage_mtime' => $mtime, |
|
| 364 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 365 | - ); |
|
| 366 | - $this->getCache()->put($path, $stat); |
|
| 367 | - } catch (\Exception $ex) { |
|
| 368 | - $this->logger->logException($ex, [ |
|
| 369 | - 'app' => 'objectstore', |
|
| 370 | - 'message' => 'Could not create object for ' . $path, |
|
| 371 | - ]); |
|
| 372 | - throw $ex; |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - return true; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - public function writeBack($tmpFile, $path) { |
|
| 379 | - $stat = $this->stat($path); |
|
| 380 | - if (empty($stat)) { |
|
| 381 | - // create new file |
|
| 382 | - $stat = array( |
|
| 383 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 384 | - ); |
|
| 385 | - } |
|
| 386 | - // update stat with new data |
|
| 387 | - $mTime = time(); |
|
| 388 | - $stat['size'] = filesize($tmpFile); |
|
| 389 | - $stat['mtime'] = $mTime; |
|
| 390 | - $stat['storage_mtime'] = $mTime; |
|
| 391 | - |
|
| 392 | - // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 393 | - $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 394 | - $mimetype = $mimetypeDetector->detectPath($path); |
|
| 395 | - if ($mimetype === 'application/octet-stream') { |
|
| 396 | - $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - $stat['mimetype'] = $mimetype; |
|
| 400 | - $stat['etag'] = $this->getETag($path); |
|
| 401 | - |
|
| 402 | - $fileId = $this->getCache()->put($path, $stat); |
|
| 403 | - try { |
|
| 404 | - //upload to object storage |
|
| 405 | - $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 406 | - } catch (\Exception $ex) { |
|
| 407 | - $this->getCache()->remove($path); |
|
| 408 | - $this->logger->logException($ex, [ |
|
| 409 | - 'app' => 'objectstore', |
|
| 410 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 411 | - ]); |
|
| 412 | - throw $ex; // make this bubble up |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * external changes are not supported, exclusive access to the object storage is assumed |
|
| 418 | - * |
|
| 419 | - * @param string $path |
|
| 420 | - * @param int $time |
|
| 421 | - * @return false |
|
| 422 | - */ |
|
| 423 | - public function hasUpdated($path, $time) { |
|
| 424 | - return false; |
|
| 425 | - } |
|
| 34 | + /** |
|
| 35 | + * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 36 | + */ |
|
| 37 | + protected $objectStore; |
|
| 38 | + /** |
|
| 39 | + * @var string $id |
|
| 40 | + */ |
|
| 41 | + protected $id; |
|
| 42 | + /** |
|
| 43 | + * @var \OC\User\User $user |
|
| 44 | + */ |
|
| 45 | + protected $user; |
|
| 46 | + |
|
| 47 | + private $objectPrefix = 'urn:oid:'; |
|
| 48 | + |
|
| 49 | + private $logger; |
|
| 50 | + |
|
| 51 | + public function __construct($params) { |
|
| 52 | + if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 53 | + $this->objectStore = $params['objectstore']; |
|
| 54 | + } else { |
|
| 55 | + throw new \Exception('missing IObjectStore instance'); |
|
| 56 | + } |
|
| 57 | + if (isset($params['storageid'])) { |
|
| 58 | + $this->id = 'object::store:' . $params['storageid']; |
|
| 59 | + } else { |
|
| 60 | + $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 61 | + } |
|
| 62 | + if (isset($params['objectPrefix'])) { |
|
| 63 | + $this->objectPrefix = $params['objectPrefix']; |
|
| 64 | + } |
|
| 65 | + //initialize cache with root directory in cache |
|
| 66 | + if (!$this->is_dir('/')) { |
|
| 67 | + $this->mkdir('/'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->logger = \OC::$server->getLogger(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function mkdir($path) { |
|
| 74 | + $path = $this->normalizePath($path); |
|
| 75 | + |
|
| 76 | + if ($this->file_exists($path)) { |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $mTime = time(); |
|
| 81 | + $data = [ |
|
| 82 | + 'mimetype' => 'httpd/unix-directory', |
|
| 83 | + 'size' => 0, |
|
| 84 | + 'mtime' => $mTime, |
|
| 85 | + 'storage_mtime' => $mTime, |
|
| 86 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 87 | + ]; |
|
| 88 | + if ($path === '') { |
|
| 89 | + //create root on the fly |
|
| 90 | + $data['etag'] = $this->getETag(''); |
|
| 91 | + $this->getCache()->put('', $data); |
|
| 92 | + return true; |
|
| 93 | + } else { |
|
| 94 | + // if parent does not exist, create it |
|
| 95 | + $parent = $this->normalizePath(dirname($path)); |
|
| 96 | + $parentType = $this->filetype($parent); |
|
| 97 | + if ($parentType === false) { |
|
| 98 | + if (!$this->mkdir($parent)) { |
|
| 99 | + // something went wrong |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + } else if ($parentType === 'file') { |
|
| 103 | + // parent is a file |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + // finally create the new dir |
|
| 107 | + $mTime = time(); // update mtime |
|
| 108 | + $data['mtime'] = $mTime; |
|
| 109 | + $data['storage_mtime'] = $mTime; |
|
| 110 | + $data['etag'] = $this->getETag($path); |
|
| 111 | + $this->getCache()->put($path, $data); |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param string $path |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + private function normalizePath($path) { |
|
| 121 | + $path = trim($path, '/'); |
|
| 122 | + //FIXME why do we sometimes get a path like 'files//username'? |
|
| 123 | + $path = str_replace('//', '/', $path); |
|
| 124 | + |
|
| 125 | + // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 126 | + if (!$path || $path === '.') { |
|
| 127 | + $path = ''; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + return $path; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 135 | + * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 136 | + * |
|
| 137 | + * @param string $path |
|
| 138 | + * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 139 | + * @return \OC\Files\ObjectStore\NoopScanner |
|
| 140 | + */ |
|
| 141 | + public function getScanner($path = '', $storage = null) { |
|
| 142 | + if (!$storage) { |
|
| 143 | + $storage = $this; |
|
| 144 | + } |
|
| 145 | + if (!isset($this->scanner)) { |
|
| 146 | + $this->scanner = new NoopScanner($storage); |
|
| 147 | + } |
|
| 148 | + return $this->scanner; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + public function getId() { |
|
| 152 | + return $this->id; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + public function rmdir($path) { |
|
| 156 | + $path = $this->normalizePath($path); |
|
| 157 | + |
|
| 158 | + if (!$this->is_dir($path)) { |
|
| 159 | + return false; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $this->rmObjects($path); |
|
| 163 | + |
|
| 164 | + $this->getCache()->remove($path); |
|
| 165 | + |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + private function rmObjects($path) { |
|
| 170 | + $children = $this->getCache()->getFolderContents($path); |
|
| 171 | + foreach ($children as $child) { |
|
| 172 | + if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 173 | + $this->rmObjects($child['path']); |
|
| 174 | + } else { |
|
| 175 | + $this->unlink($child['path']); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + public function unlink($path) { |
|
| 181 | + $path = $this->normalizePath($path); |
|
| 182 | + $stat = $this->stat($path); |
|
| 183 | + |
|
| 184 | + if ($stat && isset($stat['fileid'])) { |
|
| 185 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 186 | + return $this->rmdir($path); |
|
| 187 | + } |
|
| 188 | + try { |
|
| 189 | + $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 190 | + } catch (\Exception $ex) { |
|
| 191 | + if ($ex->getCode() !== 404) { |
|
| 192 | + $this->logger->logException($ex, [ |
|
| 193 | + 'app' => 'objectstore', |
|
| 194 | + 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 195 | + ]); |
|
| 196 | + return false; |
|
| 197 | + } |
|
| 198 | + //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | + } |
|
| 200 | + $this->getCache()->remove($path); |
|
| 201 | + return true; |
|
| 202 | + } |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + public function stat($path) { |
|
| 207 | + $path = $this->normalizePath($path); |
|
| 208 | + $cacheEntry = $this->getCache()->get($path); |
|
| 209 | + if ($cacheEntry instanceof CacheEntry) { |
|
| 210 | + return $cacheEntry->getData(); |
|
| 211 | + } else { |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 218 | + * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 219 | + * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 220 | + * |
|
| 221 | + * @param int $fileId the fileid |
|
| 222 | + * @return null|string the unified resource name used to identify the object |
|
| 223 | + */ |
|
| 224 | + protected function getURN($fileId) { |
|
| 225 | + if (is_numeric($fileId)) { |
|
| 226 | + return $this->objectPrefix . $fileId; |
|
| 227 | + } |
|
| 228 | + return null; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + public function opendir($path) { |
|
| 232 | + $path = $this->normalizePath($path); |
|
| 233 | + |
|
| 234 | + try { |
|
| 235 | + $files = array(); |
|
| 236 | + $folderContents = $this->getCache()->getFolderContents($path); |
|
| 237 | + foreach ($folderContents as $file) { |
|
| 238 | + $files[] = $file['name']; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + return IteratorDirectory::wrap($files); |
|
| 242 | + } catch (\Exception $e) { |
|
| 243 | + $this->logger->logException($e); |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + public function filetype($path) { |
|
| 249 | + $path = $this->normalizePath($path); |
|
| 250 | + $stat = $this->stat($path); |
|
| 251 | + if ($stat) { |
|
| 252 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 253 | + return 'dir'; |
|
| 254 | + } |
|
| 255 | + return 'file'; |
|
| 256 | + } else { |
|
| 257 | + return false; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + public function fopen($path, $mode) { |
|
| 262 | + $path = $this->normalizePath($path); |
|
| 263 | + |
|
| 264 | + switch ($mode) { |
|
| 265 | + case 'r': |
|
| 266 | + case 'rb': |
|
| 267 | + $stat = $this->stat($path); |
|
| 268 | + if (is_array($stat)) { |
|
| 269 | + try { |
|
| 270 | + return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 271 | + } catch (\Exception $ex) { |
|
| 272 | + $this->logger->logException($ex, [ |
|
| 273 | + 'app' => 'objectstore', |
|
| 274 | + 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 275 | + ]); |
|
| 276 | + return false; |
|
| 277 | + } |
|
| 278 | + } else { |
|
| 279 | + return false; |
|
| 280 | + } |
|
| 281 | + case 'w': |
|
| 282 | + case 'wb': |
|
| 283 | + case 'a': |
|
| 284 | + case 'ab': |
|
| 285 | + case 'r+': |
|
| 286 | + case 'w+': |
|
| 287 | + case 'wb+': |
|
| 288 | + case 'a+': |
|
| 289 | + case 'x': |
|
| 290 | + case 'x+': |
|
| 291 | + case 'c': |
|
| 292 | + case 'c+': |
|
| 293 | + if (strrpos($path, '.') !== false) { |
|
| 294 | + $ext = substr($path, strrpos($path, '.')); |
|
| 295 | + } else { |
|
| 296 | + $ext = ''; |
|
| 297 | + } |
|
| 298 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 299 | + if ($this->file_exists($path)) { |
|
| 300 | + $source = $this->fopen($path, 'r'); |
|
| 301 | + file_put_contents($tmpFile, $source); |
|
| 302 | + } |
|
| 303 | + $handle = fopen($tmpFile, $mode); |
|
| 304 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 305 | + $this->writeBack($tmpFile, $path); |
|
| 306 | + }); |
|
| 307 | + } |
|
| 308 | + return false; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + public function file_exists($path) { |
|
| 312 | + $path = $this->normalizePath($path); |
|
| 313 | + return (bool)$this->stat($path); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + public function rename($source, $target) { |
|
| 317 | + $source = $this->normalizePath($source); |
|
| 318 | + $target = $this->normalizePath($target); |
|
| 319 | + $this->remove($target); |
|
| 320 | + $this->getCache()->move($source, $target); |
|
| 321 | + $this->touch(dirname($target)); |
|
| 322 | + return true; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + public function getMimeType($path) { |
|
| 326 | + $path = $this->normalizePath($path); |
|
| 327 | + $stat = $this->stat($path); |
|
| 328 | + if (is_array($stat)) { |
|
| 329 | + return $stat['mimetype']; |
|
| 330 | + } else { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + public function touch($path, $mtime = null) { |
|
| 336 | + if (is_null($mtime)) { |
|
| 337 | + $mtime = time(); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + $path = $this->normalizePath($path); |
|
| 341 | + $dirName = dirname($path); |
|
| 342 | + $parentExists = $this->is_dir($dirName); |
|
| 343 | + if (!$parentExists) { |
|
| 344 | + return false; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + $stat = $this->stat($path); |
|
| 348 | + if (is_array($stat)) { |
|
| 349 | + // update existing mtime in db |
|
| 350 | + $stat['mtime'] = $mtime; |
|
| 351 | + $this->getCache()->update($stat['fileid'], $stat); |
|
| 352 | + } else { |
|
| 353 | + try { |
|
| 354 | + //create a empty file, need to have at least on char to make it |
|
| 355 | + // work with all object storage implementations |
|
| 356 | + $this->file_put_contents($path, ' '); |
|
| 357 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 358 | + $stat = array( |
|
| 359 | + 'etag' => $this->getETag($path), |
|
| 360 | + 'mimetype' => $mimeType, |
|
| 361 | + 'size' => 0, |
|
| 362 | + 'mtime' => $mtime, |
|
| 363 | + 'storage_mtime' => $mtime, |
|
| 364 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 365 | + ); |
|
| 366 | + $this->getCache()->put($path, $stat); |
|
| 367 | + } catch (\Exception $ex) { |
|
| 368 | + $this->logger->logException($ex, [ |
|
| 369 | + 'app' => 'objectstore', |
|
| 370 | + 'message' => 'Could not create object for ' . $path, |
|
| 371 | + ]); |
|
| 372 | + throw $ex; |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + return true; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + public function writeBack($tmpFile, $path) { |
|
| 379 | + $stat = $this->stat($path); |
|
| 380 | + if (empty($stat)) { |
|
| 381 | + // create new file |
|
| 382 | + $stat = array( |
|
| 383 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 384 | + ); |
|
| 385 | + } |
|
| 386 | + // update stat with new data |
|
| 387 | + $mTime = time(); |
|
| 388 | + $stat['size'] = filesize($tmpFile); |
|
| 389 | + $stat['mtime'] = $mTime; |
|
| 390 | + $stat['storage_mtime'] = $mTime; |
|
| 391 | + |
|
| 392 | + // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 393 | + $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 394 | + $mimetype = $mimetypeDetector->detectPath($path); |
|
| 395 | + if ($mimetype === 'application/octet-stream') { |
|
| 396 | + $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + $stat['mimetype'] = $mimetype; |
|
| 400 | + $stat['etag'] = $this->getETag($path); |
|
| 401 | + |
|
| 402 | + $fileId = $this->getCache()->put($path, $stat); |
|
| 403 | + try { |
|
| 404 | + //upload to object storage |
|
| 405 | + $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 406 | + } catch (\Exception $ex) { |
|
| 407 | + $this->getCache()->remove($path); |
|
| 408 | + $this->logger->logException($ex, [ |
|
| 409 | + 'app' => 'objectstore', |
|
| 410 | + 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 411 | + ]); |
|
| 412 | + throw $ex; // make this bubble up |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * external changes are not supported, exclusive access to the object storage is assumed |
|
| 418 | + * |
|
| 419 | + * @param string $path |
|
| 420 | + * @param int $time |
|
| 421 | + * @return false |
|
| 422 | + */ |
|
| 423 | + public function hasUpdated($path, $time) { |
|
| 424 | + return false; |
|
| 425 | + } |
|
| 426 | 426 | } |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | throw new \Exception('missing IObjectStore instance'); |
| 56 | 56 | } |
| 57 | 57 | if (isset($params['storageid'])) { |
| 58 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 58 | + $this->id = 'object::store:'.$params['storageid']; |
|
| 59 | 59 | } else { |
| 60 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 60 | + $this->id = 'object::store:'.$this->objectStore->getStorageId(); |
|
| 61 | 61 | } |
| 62 | 62 | if (isset($params['objectPrefix'])) { |
| 63 | 63 | $this->objectPrefix = $params['objectPrefix']; |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | if ($ex->getCode() !== 404) { |
| 192 | 192 | $this->logger->logException($ex, [ |
| 193 | 193 | 'app' => 'objectstore', |
| 194 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 194 | + 'message' => 'Could not delete object '.$this->getURN($stat['fileid']).' for '.$path, |
|
| 195 | 195 | ]); |
| 196 | 196 | return false; |
| 197 | 197 | } |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | */ |
| 224 | 224 | protected function getURN($fileId) { |
| 225 | 225 | if (is_numeric($fileId)) { |
| 226 | - return $this->objectPrefix . $fileId; |
|
| 226 | + return $this->objectPrefix.$fileId; |
|
| 227 | 227 | } |
| 228 | 228 | return null; |
| 229 | 229 | } |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | } catch (\Exception $ex) { |
| 272 | 272 | $this->logger->logException($ex, [ |
| 273 | 273 | 'app' => 'objectstore', |
| 274 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 274 | + 'message' => 'Count not get object '.$this->getURN($stat['fileid']).' for file '.$path, |
|
| 275 | 275 | ]); |
| 276 | 276 | return false; |
| 277 | 277 | } |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | file_put_contents($tmpFile, $source); |
| 302 | 302 | } |
| 303 | 303 | $handle = fopen($tmpFile, $mode); |
| 304 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 304 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 305 | 305 | $this->writeBack($tmpFile, $path); |
| 306 | 306 | }); |
| 307 | 307 | } |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | |
| 311 | 311 | public function file_exists($path) { |
| 312 | 312 | $path = $this->normalizePath($path); |
| 313 | - return (bool)$this->stat($path); |
|
| 313 | + return (bool) $this->stat($path); |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | public function rename($source, $target) { |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | } catch (\Exception $ex) { |
| 368 | 368 | $this->logger->logException($ex, [ |
| 369 | 369 | 'app' => 'objectstore', |
| 370 | - 'message' => 'Could not create object for ' . $path, |
|
| 370 | + 'message' => 'Could not create object for '.$path, |
|
| 371 | 371 | ]); |
| 372 | 372 | throw $ex; |
| 373 | 373 | } |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | $this->getCache()->remove($path); |
| 408 | 408 | $this->logger->logException($ex, [ |
| 409 | 409 | 'app' => 'objectstore', |
| 410 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 410 | + 'message' => 'Could not create object '.$this->getURN($fileId).' for '.$path, |
|
| 411 | 411 | ]); |
| 412 | 412 | throw $ex; // make this bubble up |
| 413 | 413 | } |
@@ -102,7 +102,7 @@ |
||
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | 104 | * @param \Icewind\SMB\Change $change |
| 105 | - * @return IChange|null |
|
| 105 | + * @return null|Change |
|
| 106 | 106 | */ |
| 107 | 107 | private function mapChange(\Icewind\SMB\Change $change) { |
| 108 | 108 | $path = $this->relativePath($change->getPath()); |
@@ -29,122 +29,122 @@ |
||
| 29 | 29 | use OCP\Files\Notify\INotifyHandler; |
| 30 | 30 | |
| 31 | 31 | class SMBNotifyHandler implements INotifyHandler { |
| 32 | - /** |
|
| 33 | - * @var \Icewind\SMB\INotifyHandler |
|
| 34 | - */ |
|
| 35 | - private $shareNotifyHandler; |
|
| 32 | + /** |
|
| 33 | + * @var \Icewind\SMB\INotifyHandler |
|
| 34 | + */ |
|
| 35 | + private $shareNotifyHandler; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - private $root; |
|
| 37 | + /** |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + private $root; |
|
| 41 | 41 | |
| 42 | - private $oldRenamePath = null; |
|
| 42 | + private $oldRenamePath = null; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * SMBNotifyHandler constructor. |
|
| 46 | - * |
|
| 47 | - * @param \Icewind\SMB\INotifyHandler $shareNotifyHandler |
|
| 48 | - * @param string $root |
|
| 49 | - */ |
|
| 50 | - public function __construct(\Icewind\SMB\INotifyHandler $shareNotifyHandler, $root) { |
|
| 51 | - $this->shareNotifyHandler = $shareNotifyHandler; |
|
| 52 | - $this->root = $root; |
|
| 53 | - } |
|
| 44 | + /** |
|
| 45 | + * SMBNotifyHandler constructor. |
|
| 46 | + * |
|
| 47 | + * @param \Icewind\SMB\INotifyHandler $shareNotifyHandler |
|
| 48 | + * @param string $root |
|
| 49 | + */ |
|
| 50 | + public function __construct(\Icewind\SMB\INotifyHandler $shareNotifyHandler, $root) { |
|
| 51 | + $this->shareNotifyHandler = $shareNotifyHandler; |
|
| 52 | + $this->root = $root; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - private function relativePath($fullPath) { |
|
| 56 | - if ($fullPath === $this->root) { |
|
| 57 | - return ''; |
|
| 58 | - } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 59 | - return substr($fullPath, strlen($this->root)); |
|
| 60 | - } else { |
|
| 61 | - return null; |
|
| 62 | - } |
|
| 63 | - } |
|
| 55 | + private function relativePath($fullPath) { |
|
| 56 | + if ($fullPath === $this->root) { |
|
| 57 | + return ''; |
|
| 58 | + } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 59 | + return substr($fullPath, strlen($this->root)); |
|
| 60 | + } else { |
|
| 61 | + return null; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public function listen(callable $callback) { |
|
| 66 | - $oldRenamePath = null; |
|
| 67 | - $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | - $change = $this->mapChange($shareChange); |
|
| 69 | - if (!is_null($change)) { |
|
| 70 | - return $callback($change); |
|
| 71 | - } else { |
|
| 72 | - return true; |
|
| 73 | - } |
|
| 74 | - }); |
|
| 75 | - } |
|
| 65 | + public function listen(callable $callback) { |
|
| 66 | + $oldRenamePath = null; |
|
| 67 | + $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | + $change = $this->mapChange($shareChange); |
|
| 69 | + if (!is_null($change)) { |
|
| 70 | + return $callback($change); |
|
| 71 | + } else { |
|
| 72 | + return true; |
|
| 73 | + } |
|
| 74 | + }); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Get all changes detected since the start of the notify process or the last call to getChanges |
|
| 79 | - * |
|
| 80 | - * @return IChange[] |
|
| 81 | - */ |
|
| 82 | - public function getChanges() { |
|
| 83 | - $shareChanges = $this->shareNotifyHandler->getChanges(); |
|
| 84 | - $changes = []; |
|
| 85 | - foreach ($shareChanges as $shareChange) { |
|
| 86 | - $change = $this->mapChange($shareChange); |
|
| 87 | - if ($change) { |
|
| 88 | - $changes[] = $change; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - return $changes; |
|
| 92 | - } |
|
| 77 | + /** |
|
| 78 | + * Get all changes detected since the start of the notify process or the last call to getChanges |
|
| 79 | + * |
|
| 80 | + * @return IChange[] |
|
| 81 | + */ |
|
| 82 | + public function getChanges() { |
|
| 83 | + $shareChanges = $this->shareNotifyHandler->getChanges(); |
|
| 84 | + $changes = []; |
|
| 85 | + foreach ($shareChanges as $shareChange) { |
|
| 86 | + $change = $this->mapChange($shareChange); |
|
| 87 | + if ($change) { |
|
| 88 | + $changes[] = $change; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + return $changes; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Stop listening for changes |
|
| 96 | - * |
|
| 97 | - * Note that any pending changes will be discarded |
|
| 98 | - */ |
|
| 99 | - public function stop() { |
|
| 100 | - $this->shareNotifyHandler->stop(); |
|
| 101 | - } |
|
| 94 | + /** |
|
| 95 | + * Stop listening for changes |
|
| 96 | + * |
|
| 97 | + * Note that any pending changes will be discarded |
|
| 98 | + */ |
|
| 99 | + public function stop() { |
|
| 100 | + $this->shareNotifyHandler->stop(); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @param \Icewind\SMB\Change $change |
|
| 105 | - * @return IChange|null |
|
| 106 | - */ |
|
| 107 | - private function mapChange(\Icewind\SMB\Change $change) { |
|
| 108 | - $path = $this->relativePath($change->getPath()); |
|
| 109 | - if (is_null($path)) { |
|
| 110 | - return null; |
|
| 111 | - } |
|
| 112 | - if ($change->getCode() === \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_OLD) { |
|
| 113 | - $this->oldRenamePath = $path; |
|
| 114 | - return null; |
|
| 115 | - } |
|
| 116 | - $type = $this->mapNotifyType($change->getCode()); |
|
| 117 | - if (is_null($type)) { |
|
| 118 | - return null; |
|
| 119 | - } |
|
| 120 | - if ($type === IChange::RENAMED) { |
|
| 121 | - if (!is_null($this->oldRenamePath)) { |
|
| 122 | - $result = new RenameChange($type, $this->oldRenamePath, $path); |
|
| 123 | - $this->oldRenamePath = null; |
|
| 124 | - } else { |
|
| 125 | - $result = null; |
|
| 126 | - } |
|
| 127 | - } else { |
|
| 128 | - $result = new Change($type, $path); |
|
| 129 | - } |
|
| 130 | - return $result; |
|
| 131 | - } |
|
| 103 | + /** |
|
| 104 | + * @param \Icewind\SMB\Change $change |
|
| 105 | + * @return IChange|null |
|
| 106 | + */ |
|
| 107 | + private function mapChange(\Icewind\SMB\Change $change) { |
|
| 108 | + $path = $this->relativePath($change->getPath()); |
|
| 109 | + if (is_null($path)) { |
|
| 110 | + return null; |
|
| 111 | + } |
|
| 112 | + if ($change->getCode() === \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_OLD) { |
|
| 113 | + $this->oldRenamePath = $path; |
|
| 114 | + return null; |
|
| 115 | + } |
|
| 116 | + $type = $this->mapNotifyType($change->getCode()); |
|
| 117 | + if (is_null($type)) { |
|
| 118 | + return null; |
|
| 119 | + } |
|
| 120 | + if ($type === IChange::RENAMED) { |
|
| 121 | + if (!is_null($this->oldRenamePath)) { |
|
| 122 | + $result = new RenameChange($type, $this->oldRenamePath, $path); |
|
| 123 | + $this->oldRenamePath = null; |
|
| 124 | + } else { |
|
| 125 | + $result = null; |
|
| 126 | + } |
|
| 127 | + } else { |
|
| 128 | + $result = new Change($type, $path); |
|
| 129 | + } |
|
| 130 | + return $result; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - private function mapNotifyType($smbType) { |
|
| 134 | - switch ($smbType) { |
|
| 135 | - case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED: |
|
| 136 | - return IChange::ADDED; |
|
| 137 | - case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED: |
|
| 138 | - return IChange::REMOVED; |
|
| 139 | - case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED: |
|
| 140 | - case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED_STREAM: |
|
| 141 | - case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED_STREAM: |
|
| 142 | - case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED_STREAM: |
|
| 143 | - return IChange::MODIFIED; |
|
| 144 | - case \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_NEW: |
|
| 145 | - return IChange::RENAMED; |
|
| 146 | - default: |
|
| 147 | - return null; |
|
| 148 | - } |
|
| 149 | - } |
|
| 133 | + private function mapNotifyType($smbType) { |
|
| 134 | + switch ($smbType) { |
|
| 135 | + case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED: |
|
| 136 | + return IChange::ADDED; |
|
| 137 | + case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED: |
|
| 138 | + return IChange::REMOVED; |
|
| 139 | + case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED: |
|
| 140 | + case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED_STREAM: |
|
| 141 | + case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED_STREAM: |
|
| 142 | + case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED_STREAM: |
|
| 143 | + return IChange::MODIFIED; |
|
| 144 | + case \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_NEW: |
|
| 145 | + return IChange::RENAMED; |
|
| 146 | + default: |
|
| 147 | + return null; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | 150 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | |
| 65 | 65 | public function listen(callable $callback) { |
| 66 | 66 | $oldRenamePath = null; |
| 67 | - $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 67 | + $this->shareNotifyHandler->listen(function(\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | 68 | $change = $this->mapChange($shareChange); |
| 69 | 69 | if (!is_null($change)) { |
| 70 | 70 | return $callback($change); |
@@ -31,7 +31,7 @@ |
||
| 31 | 31 | * Creates a Folder that represents a non-existing path |
| 32 | 32 | * |
| 33 | 33 | * @param string $path path |
| 34 | - * @return string non-existing node class |
|
| 34 | + * @return NonExistingFile non-existing node class |
|
| 35 | 35 | */ |
| 36 | 36 | protected function createNonExistingNode($path) { |
| 37 | 37 | return new NonExistingFile($this->root, $this->view, $path); |
@@ -29,113 +29,113 @@ |
||
| 29 | 29 | use OCP\Files\NotPermittedException; |
| 30 | 30 | |
| 31 | 31 | class File extends Node implements \OCP\Files\File { |
| 32 | - /** |
|
| 33 | - * Creates a Folder that represents a non-existing path |
|
| 34 | - * |
|
| 35 | - * @param string $path path |
|
| 36 | - * @return string non-existing node class |
|
| 37 | - */ |
|
| 38 | - protected function createNonExistingNode($path) { |
|
| 39 | - return new NonExistingFile($this->root, $this->view, $path); |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * Creates a Folder that represents a non-existing path |
|
| 34 | + * |
|
| 35 | + * @param string $path path |
|
| 36 | + * @return string non-existing node class |
|
| 37 | + */ |
|
| 38 | + protected function createNonExistingNode($path) { |
|
| 39 | + return new NonExistingFile($this->root, $this->view, $path); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return string |
|
| 44 | - * @throws \OCP\Files\NotPermittedException |
|
| 45 | - */ |
|
| 46 | - public function getContent() { |
|
| 47 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
|
| 48 | - /** |
|
| 49 | - * @var \OC\Files\Storage\Storage $storage; |
|
| 50 | - */ |
|
| 51 | - return $this->view->file_get_contents($this->path); |
|
| 52 | - } else { |
|
| 53 | - throw new NotPermittedException(); |
|
| 54 | - } |
|
| 55 | - } |
|
| 42 | + /** |
|
| 43 | + * @return string |
|
| 44 | + * @throws \OCP\Files\NotPermittedException |
|
| 45 | + */ |
|
| 46 | + public function getContent() { |
|
| 47 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
|
| 48 | + /** |
|
| 49 | + * @var \OC\Files\Storage\Storage $storage; |
|
| 50 | + */ |
|
| 51 | + return $this->view->file_get_contents($this->path); |
|
| 52 | + } else { |
|
| 53 | + throw new NotPermittedException(); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @param string $data |
|
| 59 | - * @throws \OCP\Files\NotPermittedException |
|
| 60 | - */ |
|
| 61 | - public function putContent($data) { |
|
| 62 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
| 63 | - $this->sendHooks(array('preWrite')); |
|
| 64 | - $this->view->file_put_contents($this->path, $data); |
|
| 65 | - $this->fileInfo = null; |
|
| 66 | - $this->sendHooks(array('postWrite')); |
|
| 67 | - } else { |
|
| 68 | - throw new NotPermittedException(); |
|
| 69 | - } |
|
| 70 | - } |
|
| 57 | + /** |
|
| 58 | + * @param string $data |
|
| 59 | + * @throws \OCP\Files\NotPermittedException |
|
| 60 | + */ |
|
| 61 | + public function putContent($data) { |
|
| 62 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
| 63 | + $this->sendHooks(array('preWrite')); |
|
| 64 | + $this->view->file_put_contents($this->path, $data); |
|
| 65 | + $this->fileInfo = null; |
|
| 66 | + $this->sendHooks(array('postWrite')); |
|
| 67 | + } else { |
|
| 68 | + throw new NotPermittedException(); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @param string $mode |
|
| 74 | - * @return resource |
|
| 75 | - * @throws \OCP\Files\NotPermittedException |
|
| 76 | - */ |
|
| 77 | - public function fopen($mode) { |
|
| 78 | - $preHooks = array(); |
|
| 79 | - $postHooks = array(); |
|
| 80 | - $requiredPermissions = \OCP\Constants::PERMISSION_READ; |
|
| 81 | - switch ($mode) { |
|
| 82 | - case 'r+': |
|
| 83 | - case 'rb+': |
|
| 84 | - case 'w+': |
|
| 85 | - case 'wb+': |
|
| 86 | - case 'x+': |
|
| 87 | - case 'xb+': |
|
| 88 | - case 'a+': |
|
| 89 | - case 'ab+': |
|
| 90 | - case 'w': |
|
| 91 | - case 'wb': |
|
| 92 | - case 'x': |
|
| 93 | - case 'xb': |
|
| 94 | - case 'a': |
|
| 95 | - case 'ab': |
|
| 96 | - $preHooks[] = 'preWrite'; |
|
| 97 | - $postHooks[] = 'postWrite'; |
|
| 98 | - $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
| 99 | - break; |
|
| 100 | - } |
|
| 72 | + /** |
|
| 73 | + * @param string $mode |
|
| 74 | + * @return resource |
|
| 75 | + * @throws \OCP\Files\NotPermittedException |
|
| 76 | + */ |
|
| 77 | + public function fopen($mode) { |
|
| 78 | + $preHooks = array(); |
|
| 79 | + $postHooks = array(); |
|
| 80 | + $requiredPermissions = \OCP\Constants::PERMISSION_READ; |
|
| 81 | + switch ($mode) { |
|
| 82 | + case 'r+': |
|
| 83 | + case 'rb+': |
|
| 84 | + case 'w+': |
|
| 85 | + case 'wb+': |
|
| 86 | + case 'x+': |
|
| 87 | + case 'xb+': |
|
| 88 | + case 'a+': |
|
| 89 | + case 'ab+': |
|
| 90 | + case 'w': |
|
| 91 | + case 'wb': |
|
| 92 | + case 'x': |
|
| 93 | + case 'xb': |
|
| 94 | + case 'a': |
|
| 95 | + case 'ab': |
|
| 96 | + $preHooks[] = 'preWrite'; |
|
| 97 | + $postHooks[] = 'postWrite'; |
|
| 98 | + $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
| 99 | + break; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - if ($this->checkPermissions($requiredPermissions)) { |
|
| 103 | - $this->sendHooks($preHooks); |
|
| 104 | - $result = $this->view->fopen($this->path, $mode); |
|
| 105 | - $this->sendHooks($postHooks); |
|
| 106 | - return $result; |
|
| 107 | - } else { |
|
| 108 | - throw new NotPermittedException(); |
|
| 109 | - } |
|
| 110 | - } |
|
| 102 | + if ($this->checkPermissions($requiredPermissions)) { |
|
| 103 | + $this->sendHooks($preHooks); |
|
| 104 | + $result = $this->view->fopen($this->path, $mode); |
|
| 105 | + $this->sendHooks($postHooks); |
|
| 106 | + return $result; |
|
| 107 | + } else { |
|
| 108 | + throw new NotPermittedException(); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - public function delete() { |
|
| 113 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 114 | - $this->sendHooks(array('preDelete')); |
|
| 115 | - $fileInfo = $this->getFileInfo(); |
|
| 116 | - $this->view->unlink($this->path); |
|
| 117 | - $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); |
|
| 118 | - $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 119 | - $this->exists = false; |
|
| 120 | - $this->fileInfo = null; |
|
| 121 | - } else { |
|
| 122 | - throw new NotPermittedException(); |
|
| 123 | - } |
|
| 124 | - } |
|
| 112 | + public function delete() { |
|
| 113 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 114 | + $this->sendHooks(array('preDelete')); |
|
| 115 | + $fileInfo = $this->getFileInfo(); |
|
| 116 | + $this->view->unlink($this->path); |
|
| 117 | + $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); |
|
| 118 | + $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 119 | + $this->exists = false; |
|
| 120 | + $this->fileInfo = null; |
|
| 121 | + } else { |
|
| 122 | + throw new NotPermittedException(); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * @param string $type |
|
| 128 | - * @param bool $raw |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - public function hash($type, $raw = false) { |
|
| 132 | - return $this->view->hash($type, $this->path, $raw); |
|
| 133 | - } |
|
| 126 | + /** |
|
| 127 | + * @param string $type |
|
| 128 | + * @param bool $raw |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + public function hash($type, $raw = false) { |
|
| 132 | + return $this->view->hash($type, $this->path, $raw); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * @inheritdoc |
|
| 137 | - */ |
|
| 138 | - public function getChecksum() { |
|
| 139 | - return $this->getFileInfo()->getChecksum(); |
|
| 140 | - } |
|
| 135 | + /** |
|
| 136 | + * @inheritdoc |
|
| 137 | + */ |
|
| 138 | + public function getChecksum() { |
|
| 139 | + return $this->getFileInfo()->getChecksum(); |
|
| 140 | + } |
|
| 141 | 141 | } |
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | * Creates a Folder that represents a non-existing path |
| 38 | 38 | * |
| 39 | 39 | * @param string $path path |
| 40 | - * @return string non-existing node class |
|
| 40 | + * @return NonExistingFolder non-existing node class |
|
| 41 | 41 | */ |
| 42 | 42 | protected function createNonExistingNode($path) { |
| 43 | 43 | return new NonExistingFolder($this->root, $this->view, $path); |
@@ -36,399 +36,399 @@ |
||
| 36 | 36 | use OCP\Files\Search\ISearchOperator; |
| 37 | 37 | |
| 38 | 38 | class Folder extends Node implements \OCP\Files\Folder { |
| 39 | - /** |
|
| 40 | - * Creates a Folder that represents a non-existing path |
|
| 41 | - * |
|
| 42 | - * @param string $path path |
|
| 43 | - * @return string non-existing node class |
|
| 44 | - */ |
|
| 45 | - protected function createNonExistingNode($path) { |
|
| 46 | - return new NonExistingFolder($this->root, $this->view, $path); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param string $path path relative to the folder |
|
| 51 | - * @return string |
|
| 52 | - * @throws \OCP\Files\NotPermittedException |
|
| 53 | - */ |
|
| 54 | - public function getFullPath($path) { |
|
| 55 | - if (!$this->isValidPath($path)) { |
|
| 56 | - throw new NotPermittedException('Invalid path'); |
|
| 57 | - } |
|
| 58 | - return $this->path . $this->normalizePath($path); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param string $path |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public function getRelativePath($path) { |
|
| 66 | - if ($this->path === '' or $this->path === '/') { |
|
| 67 | - return $this->normalizePath($path); |
|
| 68 | - } |
|
| 69 | - if ($path === $this->path) { |
|
| 70 | - return '/'; |
|
| 71 | - } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 72 | - return null; |
|
| 73 | - } else { |
|
| 74 | - $path = substr($path, strlen($this->path)); |
|
| 75 | - return $this->normalizePath($path); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * check if a node is a (grand-)child of the folder |
|
| 81 | - * |
|
| 82 | - * @param \OC\Files\Node\Node $node |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function isSubNode($node) { |
|
| 86 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * get the content of this directory |
|
| 91 | - * |
|
| 92 | - * @throws \OCP\Files\NotFoundException |
|
| 93 | - * @return Node[] |
|
| 94 | - */ |
|
| 95 | - public function getDirectoryListing() { |
|
| 96 | - $folderContent = $this->view->getDirectoryContent($this->path); |
|
| 97 | - |
|
| 98 | - return array_map(function (FileInfo $info) { |
|
| 99 | - if ($info->getMimetype() === 'httpd/unix-directory') { |
|
| 100 | - return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 101 | - } else { |
|
| 102 | - return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 103 | - } |
|
| 104 | - }, $folderContent); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param string $path |
|
| 109 | - * @param FileInfo $info |
|
| 110 | - * @return File|Folder |
|
| 111 | - */ |
|
| 112 | - protected function createNode($path, FileInfo $info = null) { |
|
| 113 | - if (is_null($info)) { |
|
| 114 | - $isDir = $this->view->is_dir($path); |
|
| 115 | - } else { |
|
| 116 | - $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 117 | - } |
|
| 118 | - if ($isDir) { |
|
| 119 | - return new Folder($this->root, $this->view, $path, $info); |
|
| 120 | - } else { |
|
| 121 | - return new File($this->root, $this->view, $path, $info); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Get the node at $path |
|
| 127 | - * |
|
| 128 | - * @param string $path |
|
| 129 | - * @return \OC\Files\Node\Node |
|
| 130 | - * @throws \OCP\Files\NotFoundException |
|
| 131 | - */ |
|
| 132 | - public function get($path) { |
|
| 133 | - return $this->root->get($this->getFullPath($path)); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @param string $path |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - public function nodeExists($path) { |
|
| 141 | - try { |
|
| 142 | - $this->get($path); |
|
| 143 | - return true; |
|
| 144 | - } catch (NotFoundException $e) { |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @param string $path |
|
| 151 | - * @return \OC\Files\Node\Folder |
|
| 152 | - * @throws \OCP\Files\NotPermittedException |
|
| 153 | - */ |
|
| 154 | - public function newFolder($path) { |
|
| 155 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 156 | - $fullPath = $this->getFullPath($path); |
|
| 157 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 158 | - $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 159 | - $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 160 | - $this->view->mkdir($fullPath); |
|
| 161 | - $node = new Folder($this->root, $this->view, $fullPath); |
|
| 162 | - $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 163 | - $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 164 | - return $node; |
|
| 165 | - } else { |
|
| 166 | - throw new NotPermittedException('No create permission for folder'); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param string $path |
|
| 172 | - * @return \OC\Files\Node\File |
|
| 173 | - * @throws \OCP\Files\NotPermittedException |
|
| 174 | - */ |
|
| 175 | - public function newFile($path) { |
|
| 176 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 177 | - $fullPath = $this->getFullPath($path); |
|
| 178 | - $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 179 | - $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 180 | - $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 181 | - $this->view->touch($fullPath); |
|
| 182 | - $node = new File($this->root, $this->view, $fullPath); |
|
| 183 | - $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 184 | - $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 185 | - return $node; |
|
| 186 | - } else { |
|
| 187 | - throw new NotPermittedException('No create permission for path'); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * search for files with the name matching $query |
|
| 193 | - * |
|
| 194 | - * @param string|ISearchOperator $query |
|
| 195 | - * @return \OC\Files\Node\Node[] |
|
| 196 | - */ |
|
| 197 | - public function search($query) { |
|
| 198 | - if (is_string($query)) { |
|
| 199 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 200 | - } else { |
|
| 201 | - return $this->searchCommon('searchQuery', array($query)); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * search for files by mimetype |
|
| 207 | - * |
|
| 208 | - * @param string $mimetype |
|
| 209 | - * @return Node[] |
|
| 210 | - */ |
|
| 211 | - public function searchByMime($mimetype) { |
|
| 212 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * search for files by tag |
|
| 217 | - * |
|
| 218 | - * @param string|int $tag name or tag id |
|
| 219 | - * @param string $userId owner of the tags |
|
| 220 | - * @return Node[] |
|
| 221 | - */ |
|
| 222 | - public function searchByTag($tag, $userId) { |
|
| 223 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param string $method cache method |
|
| 228 | - * @param array $args call args |
|
| 229 | - * @return \OC\Files\Node\Node[] |
|
| 230 | - */ |
|
| 231 | - private function searchCommon($method, $args) { |
|
| 232 | - $files = array(); |
|
| 233 | - $rootLength = strlen($this->path); |
|
| 234 | - $mount = $this->root->getMount($this->path); |
|
| 235 | - $storage = $mount->getStorage(); |
|
| 236 | - $internalPath = $mount->getInternalPath($this->path); |
|
| 237 | - $internalPath = rtrim($internalPath, '/'); |
|
| 238 | - if ($internalPath !== '') { |
|
| 239 | - $internalPath = $internalPath . '/'; |
|
| 240 | - } |
|
| 241 | - $internalRootLength = strlen($internalPath); |
|
| 242 | - |
|
| 243 | - $cache = $storage->getCache(''); |
|
| 244 | - |
|
| 245 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 246 | - foreach ($results as $result) { |
|
| 247 | - if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { |
|
| 248 | - $result['internalPath'] = $result['path']; |
|
| 249 | - $result['path'] = substr($result['path'], $internalRootLength); |
|
| 250 | - $result['storage'] = $storage; |
|
| 251 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 256 | - foreach ($mounts as $mount) { |
|
| 257 | - $storage = $mount->getStorage(); |
|
| 258 | - if ($storage) { |
|
| 259 | - $cache = $storage->getCache(''); |
|
| 260 | - |
|
| 261 | - $relativeMountPoint = substr($mount->getMountPoint(), $rootLength); |
|
| 262 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 263 | - foreach ($results as $result) { |
|
| 264 | - $result['internalPath'] = $result['path']; |
|
| 265 | - $result['path'] = $relativeMountPoint . $result['path']; |
|
| 266 | - $result['storage'] = $storage; |
|
| 267 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return array_map(function (FileInfo $file) { |
|
| 273 | - return $this->createNode($file->getPath(), $file); |
|
| 274 | - }, $files); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @param int $id |
|
| 279 | - * @return \OC\Files\Node\Node[] |
|
| 280 | - */ |
|
| 281 | - public function getById($id) { |
|
| 282 | - $mountCache = $this->root->getUserMountCache(); |
|
| 283 | - if (strpos($this->getPath(), '/', 1) > 0) { |
|
| 284 | - list(, $user) = explode('/', $this->getPath()); |
|
| 285 | - } else { |
|
| 286 | - $user = null; |
|
| 287 | - } |
|
| 288 | - $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 289 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 290 | - $mounts[] = $this->root->getMount($this->path); |
|
| 291 | - /** @var IMountPoint[] $folderMounts */ |
|
| 292 | - $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 293 | - return $mountPoint->getMountPoint(); |
|
| 294 | - }, $mounts), $mounts); |
|
| 295 | - |
|
| 296 | - /** @var ICachedMountInfo[] $mountsContainingFile */ |
|
| 297 | - $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 298 | - return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
|
| 299 | - })); |
|
| 300 | - |
|
| 301 | - if (count($mountsContainingFile) === 0) { |
|
| 302 | - return []; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - // we only need to get the cache info once, since all mounts we found point to the same storage |
|
| 306 | - |
|
| 307 | - $mount = $folderMounts[$mountsContainingFile[0]->getMountPoint()]; |
|
| 308 | - $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 309 | - if (!$cacheEntry) { |
|
| 310 | - return []; |
|
| 311 | - } |
|
| 312 | - // cache jails will hide the "true" internal path |
|
| 313 | - $internalPath = ltrim($mountsContainingFile[0]->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 314 | - |
|
| 315 | - $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($cacheEntry, $folderMounts, $internalPath) { |
|
| 316 | - $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
|
| 317 | - $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
|
| 318 | - $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 319 | - $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount; |
|
| 320 | - return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
|
| 321 | - $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
| 322 | - \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
| 323 | - )); |
|
| 324 | - }, $mountsContainingFile); |
|
| 325 | - |
|
| 326 | - return array_filter($nodes, function (Node $node) { |
|
| 327 | - return $this->getRelativePath($node->getPath()); |
|
| 328 | - }); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - public function getFreeSpace() { |
|
| 332 | - return $this->view->free_space($this->path); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - public function delete() { |
|
| 336 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 337 | - $this->sendHooks(array('preDelete')); |
|
| 338 | - $fileInfo = $this->getFileInfo(); |
|
| 339 | - $this->view->rmdir($this->path); |
|
| 340 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 341 | - $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 342 | - $this->exists = false; |
|
| 343 | - } else { |
|
| 344 | - throw new NotPermittedException('No delete permission for path'); |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Add a suffix to the name in case the file exists |
|
| 350 | - * |
|
| 351 | - * @param string $name |
|
| 352 | - * @return string |
|
| 353 | - * @throws NotPermittedException |
|
| 354 | - */ |
|
| 355 | - public function getNonExistingName($name) { |
|
| 356 | - $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 357 | - return trim($this->getRelativePath($uniqueName), '/'); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @param int $limit |
|
| 362 | - * @param int $offset |
|
| 363 | - * @return \OCP\Files\Node[] |
|
| 364 | - */ |
|
| 365 | - public function getRecent($limit, $offset = 0) { |
|
| 366 | - $mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 367 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 368 | - $mounts[] = $this->getMountPoint(); |
|
| 369 | - |
|
| 370 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 371 | - return $mount->getStorage(); |
|
| 372 | - }); |
|
| 373 | - $storageIds = array_map(function (IMountPoint $mount) { |
|
| 374 | - return $mount->getStorage()->getCache()->getNumericStorageId(); |
|
| 375 | - }, $mounts); |
|
| 376 | - /** @var IMountPoint[] $mountMap */ |
|
| 377 | - $mountMap = array_combine($storageIds, $mounts); |
|
| 378 | - $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER); |
|
| 379 | - |
|
| 380 | - //todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc) |
|
| 381 | - |
|
| 382 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 383 | - $query = $builder |
|
| 384 | - ->select('f.*') |
|
| 385 | - ->from('filecache', 'f') |
|
| 386 | - ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 387 | - ->andWhere($builder->expr()->orX( |
|
| 388 | - // handle non empty folders separate |
|
| 389 | - $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), |
|
| 390 | - $builder->expr()->eq('f.size', new Literal(0)) |
|
| 391 | - )) |
|
| 392 | - ->orderBy('f.mtime', 'DESC') |
|
| 393 | - ->setMaxResults($limit) |
|
| 394 | - ->setFirstResult($offset); |
|
| 395 | - |
|
| 396 | - $result = $query->execute()->fetchAll(); |
|
| 397 | - |
|
| 398 | - $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 399 | - $mount = $mountMap[$entry['storage']]; |
|
| 400 | - $entry['internalPath'] = $entry['path']; |
|
| 401 | - $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
|
| 402 | - $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']); |
|
| 403 | - $path = $this->getAbsolutePath($mount, $entry['path']); |
|
| 404 | - if (is_null($path)) { |
|
| 405 | - return null; |
|
| 406 | - } |
|
| 407 | - $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount); |
|
| 408 | - return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
|
| 409 | - }, $result)); |
|
| 410 | - |
|
| 411 | - return array_values(array_filter($files, function (Node $node) { |
|
| 412 | - $relative = $this->getRelativePath($node->getPath()); |
|
| 413 | - return $relative !== null && $relative !== '/'; |
|
| 414 | - })); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - private function getAbsolutePath(IMountPoint $mount, $path) { |
|
| 418 | - $storage = $mount->getStorage(); |
|
| 419 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) { |
|
| 420 | - /** @var \OC\Files\Storage\Wrapper\Jail $storage */ |
|
| 421 | - $jailRoot = $storage->getUnjailedPath(''); |
|
| 422 | - $rootLength = strlen($jailRoot) + 1; |
|
| 423 | - if ($path === $jailRoot) { |
|
| 424 | - return $mount->getMountPoint(); |
|
| 425 | - } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 426 | - return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 427 | - } else { |
|
| 428 | - return null; |
|
| 429 | - } |
|
| 430 | - } else { |
|
| 431 | - return $mount->getMountPoint() . $path; |
|
| 432 | - } |
|
| 433 | - } |
|
| 39 | + /** |
|
| 40 | + * Creates a Folder that represents a non-existing path |
|
| 41 | + * |
|
| 42 | + * @param string $path path |
|
| 43 | + * @return string non-existing node class |
|
| 44 | + */ |
|
| 45 | + protected function createNonExistingNode($path) { |
|
| 46 | + return new NonExistingFolder($this->root, $this->view, $path); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param string $path path relative to the folder |
|
| 51 | + * @return string |
|
| 52 | + * @throws \OCP\Files\NotPermittedException |
|
| 53 | + */ |
|
| 54 | + public function getFullPath($path) { |
|
| 55 | + if (!$this->isValidPath($path)) { |
|
| 56 | + throw new NotPermittedException('Invalid path'); |
|
| 57 | + } |
|
| 58 | + return $this->path . $this->normalizePath($path); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param string $path |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public function getRelativePath($path) { |
|
| 66 | + if ($this->path === '' or $this->path === '/') { |
|
| 67 | + return $this->normalizePath($path); |
|
| 68 | + } |
|
| 69 | + if ($path === $this->path) { |
|
| 70 | + return '/'; |
|
| 71 | + } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 72 | + return null; |
|
| 73 | + } else { |
|
| 74 | + $path = substr($path, strlen($this->path)); |
|
| 75 | + return $this->normalizePath($path); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * check if a node is a (grand-)child of the folder |
|
| 81 | + * |
|
| 82 | + * @param \OC\Files\Node\Node $node |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function isSubNode($node) { |
|
| 86 | + return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * get the content of this directory |
|
| 91 | + * |
|
| 92 | + * @throws \OCP\Files\NotFoundException |
|
| 93 | + * @return Node[] |
|
| 94 | + */ |
|
| 95 | + public function getDirectoryListing() { |
|
| 96 | + $folderContent = $this->view->getDirectoryContent($this->path); |
|
| 97 | + |
|
| 98 | + return array_map(function (FileInfo $info) { |
|
| 99 | + if ($info->getMimetype() === 'httpd/unix-directory') { |
|
| 100 | + return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 101 | + } else { |
|
| 102 | + return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 103 | + } |
|
| 104 | + }, $folderContent); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param string $path |
|
| 109 | + * @param FileInfo $info |
|
| 110 | + * @return File|Folder |
|
| 111 | + */ |
|
| 112 | + protected function createNode($path, FileInfo $info = null) { |
|
| 113 | + if (is_null($info)) { |
|
| 114 | + $isDir = $this->view->is_dir($path); |
|
| 115 | + } else { |
|
| 116 | + $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 117 | + } |
|
| 118 | + if ($isDir) { |
|
| 119 | + return new Folder($this->root, $this->view, $path, $info); |
|
| 120 | + } else { |
|
| 121 | + return new File($this->root, $this->view, $path, $info); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Get the node at $path |
|
| 127 | + * |
|
| 128 | + * @param string $path |
|
| 129 | + * @return \OC\Files\Node\Node |
|
| 130 | + * @throws \OCP\Files\NotFoundException |
|
| 131 | + */ |
|
| 132 | + public function get($path) { |
|
| 133 | + return $this->root->get($this->getFullPath($path)); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @param string $path |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + public function nodeExists($path) { |
|
| 141 | + try { |
|
| 142 | + $this->get($path); |
|
| 143 | + return true; |
|
| 144 | + } catch (NotFoundException $e) { |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @param string $path |
|
| 151 | + * @return \OC\Files\Node\Folder |
|
| 152 | + * @throws \OCP\Files\NotPermittedException |
|
| 153 | + */ |
|
| 154 | + public function newFolder($path) { |
|
| 155 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 156 | + $fullPath = $this->getFullPath($path); |
|
| 157 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 158 | + $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 159 | + $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 160 | + $this->view->mkdir($fullPath); |
|
| 161 | + $node = new Folder($this->root, $this->view, $fullPath); |
|
| 162 | + $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 163 | + $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 164 | + return $node; |
|
| 165 | + } else { |
|
| 166 | + throw new NotPermittedException('No create permission for folder'); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param string $path |
|
| 172 | + * @return \OC\Files\Node\File |
|
| 173 | + * @throws \OCP\Files\NotPermittedException |
|
| 174 | + */ |
|
| 175 | + public function newFile($path) { |
|
| 176 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 177 | + $fullPath = $this->getFullPath($path); |
|
| 178 | + $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 179 | + $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 180 | + $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 181 | + $this->view->touch($fullPath); |
|
| 182 | + $node = new File($this->root, $this->view, $fullPath); |
|
| 183 | + $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 184 | + $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 185 | + return $node; |
|
| 186 | + } else { |
|
| 187 | + throw new NotPermittedException('No create permission for path'); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * search for files with the name matching $query |
|
| 193 | + * |
|
| 194 | + * @param string|ISearchOperator $query |
|
| 195 | + * @return \OC\Files\Node\Node[] |
|
| 196 | + */ |
|
| 197 | + public function search($query) { |
|
| 198 | + if (is_string($query)) { |
|
| 199 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 200 | + } else { |
|
| 201 | + return $this->searchCommon('searchQuery', array($query)); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * search for files by mimetype |
|
| 207 | + * |
|
| 208 | + * @param string $mimetype |
|
| 209 | + * @return Node[] |
|
| 210 | + */ |
|
| 211 | + public function searchByMime($mimetype) { |
|
| 212 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * search for files by tag |
|
| 217 | + * |
|
| 218 | + * @param string|int $tag name or tag id |
|
| 219 | + * @param string $userId owner of the tags |
|
| 220 | + * @return Node[] |
|
| 221 | + */ |
|
| 222 | + public function searchByTag($tag, $userId) { |
|
| 223 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param string $method cache method |
|
| 228 | + * @param array $args call args |
|
| 229 | + * @return \OC\Files\Node\Node[] |
|
| 230 | + */ |
|
| 231 | + private function searchCommon($method, $args) { |
|
| 232 | + $files = array(); |
|
| 233 | + $rootLength = strlen($this->path); |
|
| 234 | + $mount = $this->root->getMount($this->path); |
|
| 235 | + $storage = $mount->getStorage(); |
|
| 236 | + $internalPath = $mount->getInternalPath($this->path); |
|
| 237 | + $internalPath = rtrim($internalPath, '/'); |
|
| 238 | + if ($internalPath !== '') { |
|
| 239 | + $internalPath = $internalPath . '/'; |
|
| 240 | + } |
|
| 241 | + $internalRootLength = strlen($internalPath); |
|
| 242 | + |
|
| 243 | + $cache = $storage->getCache(''); |
|
| 244 | + |
|
| 245 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 246 | + foreach ($results as $result) { |
|
| 247 | + if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { |
|
| 248 | + $result['internalPath'] = $result['path']; |
|
| 249 | + $result['path'] = substr($result['path'], $internalRootLength); |
|
| 250 | + $result['storage'] = $storage; |
|
| 251 | + $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 256 | + foreach ($mounts as $mount) { |
|
| 257 | + $storage = $mount->getStorage(); |
|
| 258 | + if ($storage) { |
|
| 259 | + $cache = $storage->getCache(''); |
|
| 260 | + |
|
| 261 | + $relativeMountPoint = substr($mount->getMountPoint(), $rootLength); |
|
| 262 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 263 | + foreach ($results as $result) { |
|
| 264 | + $result['internalPath'] = $result['path']; |
|
| 265 | + $result['path'] = $relativeMountPoint . $result['path']; |
|
| 266 | + $result['storage'] = $storage; |
|
| 267 | + $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return array_map(function (FileInfo $file) { |
|
| 273 | + return $this->createNode($file->getPath(), $file); |
|
| 274 | + }, $files); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @param int $id |
|
| 279 | + * @return \OC\Files\Node\Node[] |
|
| 280 | + */ |
|
| 281 | + public function getById($id) { |
|
| 282 | + $mountCache = $this->root->getUserMountCache(); |
|
| 283 | + if (strpos($this->getPath(), '/', 1) > 0) { |
|
| 284 | + list(, $user) = explode('/', $this->getPath()); |
|
| 285 | + } else { |
|
| 286 | + $user = null; |
|
| 287 | + } |
|
| 288 | + $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 289 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 290 | + $mounts[] = $this->root->getMount($this->path); |
|
| 291 | + /** @var IMountPoint[] $folderMounts */ |
|
| 292 | + $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 293 | + return $mountPoint->getMountPoint(); |
|
| 294 | + }, $mounts), $mounts); |
|
| 295 | + |
|
| 296 | + /** @var ICachedMountInfo[] $mountsContainingFile */ |
|
| 297 | + $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 298 | + return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
|
| 299 | + })); |
|
| 300 | + |
|
| 301 | + if (count($mountsContainingFile) === 0) { |
|
| 302 | + return []; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + // we only need to get the cache info once, since all mounts we found point to the same storage |
|
| 306 | + |
|
| 307 | + $mount = $folderMounts[$mountsContainingFile[0]->getMountPoint()]; |
|
| 308 | + $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 309 | + if (!$cacheEntry) { |
|
| 310 | + return []; |
|
| 311 | + } |
|
| 312 | + // cache jails will hide the "true" internal path |
|
| 313 | + $internalPath = ltrim($mountsContainingFile[0]->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 314 | + |
|
| 315 | + $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($cacheEntry, $folderMounts, $internalPath) { |
|
| 316 | + $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
|
| 317 | + $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
|
| 318 | + $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 319 | + $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount; |
|
| 320 | + return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
|
| 321 | + $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
| 322 | + \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
| 323 | + )); |
|
| 324 | + }, $mountsContainingFile); |
|
| 325 | + |
|
| 326 | + return array_filter($nodes, function (Node $node) { |
|
| 327 | + return $this->getRelativePath($node->getPath()); |
|
| 328 | + }); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + public function getFreeSpace() { |
|
| 332 | + return $this->view->free_space($this->path); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + public function delete() { |
|
| 336 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 337 | + $this->sendHooks(array('preDelete')); |
|
| 338 | + $fileInfo = $this->getFileInfo(); |
|
| 339 | + $this->view->rmdir($this->path); |
|
| 340 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 341 | + $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 342 | + $this->exists = false; |
|
| 343 | + } else { |
|
| 344 | + throw new NotPermittedException('No delete permission for path'); |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Add a suffix to the name in case the file exists |
|
| 350 | + * |
|
| 351 | + * @param string $name |
|
| 352 | + * @return string |
|
| 353 | + * @throws NotPermittedException |
|
| 354 | + */ |
|
| 355 | + public function getNonExistingName($name) { |
|
| 356 | + $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 357 | + return trim($this->getRelativePath($uniqueName), '/'); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @param int $limit |
|
| 362 | + * @param int $offset |
|
| 363 | + * @return \OCP\Files\Node[] |
|
| 364 | + */ |
|
| 365 | + public function getRecent($limit, $offset = 0) { |
|
| 366 | + $mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 367 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 368 | + $mounts[] = $this->getMountPoint(); |
|
| 369 | + |
|
| 370 | + $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 371 | + return $mount->getStorage(); |
|
| 372 | + }); |
|
| 373 | + $storageIds = array_map(function (IMountPoint $mount) { |
|
| 374 | + return $mount->getStorage()->getCache()->getNumericStorageId(); |
|
| 375 | + }, $mounts); |
|
| 376 | + /** @var IMountPoint[] $mountMap */ |
|
| 377 | + $mountMap = array_combine($storageIds, $mounts); |
|
| 378 | + $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER); |
|
| 379 | + |
|
| 380 | + //todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc) |
|
| 381 | + |
|
| 382 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 383 | + $query = $builder |
|
| 384 | + ->select('f.*') |
|
| 385 | + ->from('filecache', 'f') |
|
| 386 | + ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 387 | + ->andWhere($builder->expr()->orX( |
|
| 388 | + // handle non empty folders separate |
|
| 389 | + $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), |
|
| 390 | + $builder->expr()->eq('f.size', new Literal(0)) |
|
| 391 | + )) |
|
| 392 | + ->orderBy('f.mtime', 'DESC') |
|
| 393 | + ->setMaxResults($limit) |
|
| 394 | + ->setFirstResult($offset); |
|
| 395 | + |
|
| 396 | + $result = $query->execute()->fetchAll(); |
|
| 397 | + |
|
| 398 | + $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 399 | + $mount = $mountMap[$entry['storage']]; |
|
| 400 | + $entry['internalPath'] = $entry['path']; |
|
| 401 | + $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
|
| 402 | + $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']); |
|
| 403 | + $path = $this->getAbsolutePath($mount, $entry['path']); |
|
| 404 | + if (is_null($path)) { |
|
| 405 | + return null; |
|
| 406 | + } |
|
| 407 | + $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount); |
|
| 408 | + return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
|
| 409 | + }, $result)); |
|
| 410 | + |
|
| 411 | + return array_values(array_filter($files, function (Node $node) { |
|
| 412 | + $relative = $this->getRelativePath($node->getPath()); |
|
| 413 | + return $relative !== null && $relative !== '/'; |
|
| 414 | + })); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + private function getAbsolutePath(IMountPoint $mount, $path) { |
|
| 418 | + $storage = $mount->getStorage(); |
|
| 419 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) { |
|
| 420 | + /** @var \OC\Files\Storage\Wrapper\Jail $storage */ |
|
| 421 | + $jailRoot = $storage->getUnjailedPath(''); |
|
| 422 | + $rootLength = strlen($jailRoot) + 1; |
|
| 423 | + if ($path === $jailRoot) { |
|
| 424 | + return $mount->getMountPoint(); |
|
| 425 | + } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 426 | + return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 427 | + } else { |
|
| 428 | + return null; |
|
| 429 | + } |
|
| 430 | + } else { |
|
| 431 | + return $mount->getMountPoint() . $path; |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | 434 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | if (!$this->isValidPath($path)) { |
| 56 | 56 | throw new NotPermittedException('Invalid path'); |
| 57 | 57 | } |
| 58 | - return $this->path . $this->normalizePath($path); |
|
| 58 | + return $this->path.$this->normalizePath($path); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | if ($path === $this->path) { |
| 70 | 70 | return '/'; |
| 71 | - } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 71 | + } else if (strpos($path, $this->path.'/') !== 0) { |
|
| 72 | 72 | return null; |
| 73 | 73 | } else { |
| 74 | 74 | $path = substr($path, strlen($this->path)); |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * @return bool |
| 84 | 84 | */ |
| 85 | 85 | public function isSubNode($node) { |
| 86 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 86 | + return strpos($node->getPath(), $this->path.'/') === 0; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | public function getDirectoryListing() { |
| 96 | 96 | $folderContent = $this->view->getDirectoryContent($this->path); |
| 97 | 97 | |
| 98 | - return array_map(function (FileInfo $info) { |
|
| 98 | + return array_map(function(FileInfo $info) { |
|
| 99 | 99 | if ($info->getMimetype() === 'httpd/unix-directory') { |
| 100 | 100 | return new Folder($this->root, $this->view, $info->getPath(), $info); |
| 101 | 101 | } else { |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | public function search($query) { |
| 198 | 198 | if (is_string($query)) { |
| 199 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 199 | + return $this->searchCommon('search', array('%'.$query.'%')); |
|
| 200 | 200 | } else { |
| 201 | 201 | return $this->searchCommon('searchQuery', array($query)); |
| 202 | 202 | } |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | $internalPath = $mount->getInternalPath($this->path); |
| 237 | 237 | $internalPath = rtrim($internalPath, '/'); |
| 238 | 238 | if ($internalPath !== '') { |
| 239 | - $internalPath = $internalPath . '/'; |
|
| 239 | + $internalPath = $internalPath.'/'; |
|
| 240 | 240 | } |
| 241 | 241 | $internalRootLength = strlen($internalPath); |
| 242 | 242 | |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | $result['internalPath'] = $result['path']; |
| 249 | 249 | $result['path'] = substr($result['path'], $internalRootLength); |
| 250 | 250 | $result['storage'] = $storage; |
| 251 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 251 | + $files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
@@ -262,14 +262,14 @@ discard block |
||
| 262 | 262 | $results = call_user_func_array(array($cache, $method), $args); |
| 263 | 263 | foreach ($results as $result) { |
| 264 | 264 | $result['internalPath'] = $result['path']; |
| 265 | - $result['path'] = $relativeMountPoint . $result['path']; |
|
| 265 | + $result['path'] = $relativeMountPoint.$result['path']; |
|
| 266 | 266 | $result['storage'] = $storage; |
| 267 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 267 | + $files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 268 | 268 | } |
| 269 | 269 | } |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - return array_map(function (FileInfo $file) { |
|
| 272 | + return array_map(function(FileInfo $file) { |
|
| 273 | 273 | return $this->createNode($file->getPath(), $file); |
| 274 | 274 | }, $files); |
| 275 | 275 | } |
@@ -285,16 +285,16 @@ discard block |
||
| 285 | 285 | } else { |
| 286 | 286 | $user = null; |
| 287 | 287 | } |
| 288 | - $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 288 | + $mountsContainingFile = $mountCache->getMountsForFileId((int) $id, $user); |
|
| 289 | 289 | $mounts = $this->root->getMountsIn($this->path); |
| 290 | 290 | $mounts[] = $this->root->getMount($this->path); |
| 291 | 291 | /** @var IMountPoint[] $folderMounts */ |
| 292 | - $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 292 | + $folderMounts = array_combine(array_map(function(IMountPoint $mountPoint) { |
|
| 293 | 293 | return $mountPoint->getMountPoint(); |
| 294 | 294 | }, $mounts), $mounts); |
| 295 | 295 | |
| 296 | 296 | /** @var ICachedMountInfo[] $mountsContainingFile */ |
| 297 | - $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 297 | + $mountsContainingFile = array_values(array_filter($mountsContainingFile, function(ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 298 | 298 | return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
| 299 | 299 | })); |
| 300 | 300 | |
@@ -305,25 +305,25 @@ discard block |
||
| 305 | 305 | // we only need to get the cache info once, since all mounts we found point to the same storage |
| 306 | 306 | |
| 307 | 307 | $mount = $folderMounts[$mountsContainingFile[0]->getMountPoint()]; |
| 308 | - $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 308 | + $cacheEntry = $mount->getStorage()->getCache()->get((int) $id); |
|
| 309 | 309 | if (!$cacheEntry) { |
| 310 | 310 | return []; |
| 311 | 311 | } |
| 312 | 312 | // cache jails will hide the "true" internal path |
| 313 | - $internalPath = ltrim($mountsContainingFile[0]->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 313 | + $internalPath = ltrim($mountsContainingFile[0]->getRootInternalPath().'/'.$cacheEntry->getPath(), '/'); |
|
| 314 | 314 | |
| 315 | - $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($cacheEntry, $folderMounts, $internalPath) { |
|
| 315 | + $nodes = array_map(function(ICachedMountInfo $cachedMountInfo) use ($cacheEntry, $folderMounts, $internalPath) { |
|
| 316 | 316 | $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
| 317 | 317 | $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
| 318 | 318 | $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
| 319 | - $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount; |
|
| 319 | + $absolutePath = $cachedMountInfo->getMountPoint().$pathRelativeToMount; |
|
| 320 | 320 | return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
| 321 | 321 | $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
| 322 | 322 | \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
| 323 | 323 | )); |
| 324 | 324 | }, $mountsContainingFile); |
| 325 | 325 | |
| 326 | - return array_filter($nodes, function (Node $node) { |
|
| 326 | + return array_filter($nodes, function(Node $node) { |
|
| 327 | 327 | return $this->getRelativePath($node->getPath()); |
| 328 | 328 | }); |
| 329 | 329 | } |
@@ -367,10 +367,10 @@ discard block |
||
| 367 | 367 | $mounts = $this->root->getMountsIn($this->path); |
| 368 | 368 | $mounts[] = $this->getMountPoint(); |
| 369 | 369 | |
| 370 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 370 | + $mounts = array_filter($mounts, function(IMountPoint $mount) { |
|
| 371 | 371 | return $mount->getStorage(); |
| 372 | 372 | }); |
| 373 | - $storageIds = array_map(function (IMountPoint $mount) { |
|
| 373 | + $storageIds = array_map(function(IMountPoint $mount) { |
|
| 374 | 374 | return $mount->getStorage()->getCache()->getNumericStorageId(); |
| 375 | 375 | }, $mounts); |
| 376 | 376 | /** @var IMountPoint[] $mountMap */ |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | |
| 396 | 396 | $result = $query->execute()->fetchAll(); |
| 397 | 397 | |
| 398 | - $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 398 | + $files = array_filter(array_map(function(array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 399 | 399 | $mount = $mountMap[$entry['storage']]; |
| 400 | 400 | $entry['internalPath'] = $entry['path']; |
| 401 | 401 | $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
@@ -408,7 +408,7 @@ discard block |
||
| 408 | 408 | return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
| 409 | 409 | }, $result)); |
| 410 | 410 | |
| 411 | - return array_values(array_filter($files, function (Node $node) { |
|
| 411 | + return array_values(array_filter($files, function(Node $node) { |
|
| 412 | 412 | $relative = $this->getRelativePath($node->getPath()); |
| 413 | 413 | return $relative !== null && $relative !== '/'; |
| 414 | 414 | })); |
@@ -422,13 +422,13 @@ discard block |
||
| 422 | 422 | $rootLength = strlen($jailRoot) + 1; |
| 423 | 423 | if ($path === $jailRoot) { |
| 424 | 424 | return $mount->getMountPoint(); |
| 425 | - } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 426 | - return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 425 | + } else if (substr($path, 0, $rootLength) === $jailRoot.'/') { |
|
| 426 | + return $mount->getMountPoint().substr($path, $rootLength); |
|
| 427 | 427 | } else { |
| 428 | 428 | return null; |
| 429 | 429 | } |
| 430 | 430 | } else { |
| 431 | - return $mount->getMountPoint() . $path; |
|
| 431 | + return $mount->getMountPoint().$path; |
|
| 432 | 432 | } |
| 433 | 433 | } |
| 434 | 434 | } |
@@ -33,6 +33,7 @@ |
||
| 33 | 33 | * |
| 34 | 34 | * @returns string |
| 35 | 35 | * @since 12 |
| 36 | + * @return string |
|
| 36 | 37 | */ |
| 37 | 38 | public function getIcon(); |
| 38 | 39 | } |
@@ -27,12 +27,12 @@ |
||
| 27 | 27 | * @since 12 |
| 28 | 28 | */ |
| 29 | 29 | interface IIconSection extends ISection { |
| 30 | - /** |
|
| 31 | - * returns the relative path to an 16*16 icon describing the section. |
|
| 32 | - * e.g. '/core/img/places/files.svg' |
|
| 33 | - * |
|
| 34 | - * @returns string |
|
| 35 | - * @since 12 |
|
| 36 | - */ |
|
| 37 | - public function getIcon(); |
|
| 30 | + /** |
|
| 31 | + * returns the relative path to an 16*16 icon describing the section. |
|
| 32 | + * e.g. '/core/img/places/files.svg' |
|
| 33 | + * |
|
| 34 | + * @returns string |
|
| 35 | + * @since 12 |
|
| 36 | + */ |
|
| 37 | + public function getIcon(); |
|
| 38 | 38 | } |
@@ -139,6 +139,9 @@ |
||
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | + /** |
|
| 143 | + * @param string $path |
|
| 144 | + */ |
|
| 142 | 145 | public function writeBack($tmpFile, $path) { |
| 143 | 146 | $this->uploadFile($tmpFile, $path); |
| 144 | 147 | unlink($tmpFile); |
@@ -93,8 +93,7 @@ |
||
| 93 | 93 | public function unlink($path) { |
| 94 | 94 | if ($this->is_dir($path)) { |
| 95 | 95 | return $this->rmdir($path); |
| 96 | - } |
|
| 97 | - else { |
|
| 96 | + } else { |
|
| 98 | 97 | $url = $this->constructUrl($path); |
| 99 | 98 | $result = unlink($url); |
| 100 | 99 | clearstatcache(true, $url); |
@@ -36,28 +36,28 @@ discard block |
||
| 36 | 36 | use Icewind\Streams\CallbackWrapper; |
| 37 | 37 | use Icewind\Streams\RetryWrapper; |
| 38 | 38 | |
| 39 | -class FTP extends StreamWrapper{ |
|
| 39 | +class FTP extends StreamWrapper { |
|
| 40 | 40 | private $password; |
| 41 | 41 | private $user; |
| 42 | 42 | private $host; |
| 43 | 43 | private $secure; |
| 44 | 44 | private $root; |
| 45 | 45 | |
| 46 | - private static $tempFiles=array(); |
|
| 46 | + private static $tempFiles = array(); |
|
| 47 | 47 | |
| 48 | 48 | public function __construct($params) { |
| 49 | 49 | if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
| 50 | - $this->host=$params['host']; |
|
| 51 | - $this->user=$params['user']; |
|
| 52 | - $this->password=$params['password']; |
|
| 50 | + $this->host = $params['host']; |
|
| 51 | + $this->user = $params['user']; |
|
| 52 | + $this->password = $params['password']; |
|
| 53 | 53 | if (isset($params['secure'])) { |
| 54 | 54 | $this->secure = $params['secure']; |
| 55 | 55 | } else { |
| 56 | 56 | $this->secure = false; |
| 57 | 57 | } |
| 58 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | - if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | - $this->root='/'.$this->root; |
|
| 58 | + $this->root = isset($params['root']) ? $params['root'] : '/'; |
|
| 59 | + if (!$this->root || $this->root[0] !== '/') { |
|
| 60 | + $this->root = '/'.$this->root; |
|
| 61 | 61 | } |
| 62 | 62 | if (substr($this->root, -1) !== '/') { |
| 63 | 63 | $this->root .= '/'; |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function getId(){ |
|
| 72 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 71 | + public function getId() { |
|
| 72 | + return 'ftp::'.$this->user.'@'.$this->host.'/'.$this->root; |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -78,11 +78,11 @@ discard block |
||
| 78 | 78 | * @return string |
| 79 | 79 | */ |
| 80 | 80 | public function constructUrl($path) { |
| 81 | - $url='ftp'; |
|
| 81 | + $url = 'ftp'; |
|
| 82 | 82 | if ($this->secure) { |
| 83 | - $url.='s'; |
|
| 83 | + $url .= 's'; |
|
| 84 | 84 | } |
| 85 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 85 | + $url .= '://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | 86 | return $url; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | return $result; |
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | - public function fopen($path,$mode) { |
|
| 105 | - switch($mode) { |
|
| 104 | + public function fopen($path, $mode) { |
|
| 105 | + switch ($mode) { |
|
| 106 | 106 | case 'r': |
| 107 | 107 | case 'rb': |
| 108 | 108 | case 'w': |
@@ -122,17 +122,17 @@ discard block |
||
| 122 | 122 | case 'c': |
| 123 | 123 | case 'c+': |
| 124 | 124 | //emulate these |
| 125 | - if (strrpos($path, '.')!==false) { |
|
| 126 | - $ext=substr($path, strrpos($path, '.')); |
|
| 125 | + if (strrpos($path, '.') !== false) { |
|
| 126 | + $ext = substr($path, strrpos($path, '.')); |
|
| 127 | 127 | } else { |
| 128 | - $ext=''; |
|
| 128 | + $ext = ''; |
|
| 129 | 129 | } |
| 130 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 130 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 131 | 131 | if ($this->file_exists($path)) { |
| 132 | 132 | $this->getFile($path, $tmpFile); |
| 133 | 133 | } |
| 134 | 134 | $handle = fopen($tmpFile, $mode); |
| 135 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 135 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 136 | 136 | $this->writeBack($tmpFile, $path); |
| 137 | 137 | }); |
| 138 | 138 | } |
@@ -38,122 +38,122 @@ |
||
| 38 | 38 | use Icewind\Streams\RetryWrapper; |
| 39 | 39 | |
| 40 | 40 | class FTP extends StreamWrapper{ |
| 41 | - private $password; |
|
| 42 | - private $user; |
|
| 43 | - private $host; |
|
| 44 | - private $secure; |
|
| 45 | - private $root; |
|
| 41 | + private $password; |
|
| 42 | + private $user; |
|
| 43 | + private $host; |
|
| 44 | + private $secure; |
|
| 45 | + private $root; |
|
| 46 | 46 | |
| 47 | - private static $tempFiles=array(); |
|
| 47 | + private static $tempFiles=array(); |
|
| 48 | 48 | |
| 49 | - public function __construct($params) { |
|
| 50 | - if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 51 | - $this->host=$params['host']; |
|
| 52 | - $this->user=$params['user']; |
|
| 53 | - $this->password=$params['password']; |
|
| 54 | - if (isset($params['secure'])) { |
|
| 55 | - $this->secure = $params['secure']; |
|
| 56 | - } else { |
|
| 57 | - $this->secure = false; |
|
| 58 | - } |
|
| 59 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 60 | - if ( ! $this->root || $this->root[0]!=='/') { |
|
| 61 | - $this->root='/'.$this->root; |
|
| 62 | - } |
|
| 63 | - if (substr($this->root, -1) !== '/') { |
|
| 64 | - $this->root .= '/'; |
|
| 65 | - } |
|
| 66 | - } else { |
|
| 67 | - throw new \Exception('Creating FTP storage failed'); |
|
| 68 | - } |
|
| 49 | + public function __construct($params) { |
|
| 50 | + if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 51 | + $this->host=$params['host']; |
|
| 52 | + $this->user=$params['user']; |
|
| 53 | + $this->password=$params['password']; |
|
| 54 | + if (isset($params['secure'])) { |
|
| 55 | + $this->secure = $params['secure']; |
|
| 56 | + } else { |
|
| 57 | + $this->secure = false; |
|
| 58 | + } |
|
| 59 | + $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 60 | + if ( ! $this->root || $this->root[0]!=='/') { |
|
| 61 | + $this->root='/'.$this->root; |
|
| 62 | + } |
|
| 63 | + if (substr($this->root, -1) !== '/') { |
|
| 64 | + $this->root .= '/'; |
|
| 65 | + } |
|
| 66 | + } else { |
|
| 67 | + throw new \Exception('Creating FTP storage failed'); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - public function getId(){ |
|
| 73 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 74 | - } |
|
| 72 | + public function getId(){ |
|
| 73 | + return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * construct the ftp url |
|
| 78 | - * @param string $path |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function constructUrl($path) { |
|
| 82 | - $url='ftp'; |
|
| 83 | - if ($this->secure) { |
|
| 84 | - $url.='s'; |
|
| 85 | - } |
|
| 86 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 87 | - return $url; |
|
| 88 | - } |
|
| 76 | + /** |
|
| 77 | + * construct the ftp url |
|
| 78 | + * @param string $path |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function constructUrl($path) { |
|
| 82 | + $url='ftp'; |
|
| 83 | + if ($this->secure) { |
|
| 84 | + $url.='s'; |
|
| 85 | + } |
|
| 86 | + $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 87 | + return $url; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Unlinks file or directory |
|
| 92 | - * @param string $path |
|
| 93 | - */ |
|
| 94 | - public function unlink($path) { |
|
| 95 | - if ($this->is_dir($path)) { |
|
| 96 | - return $this->rmdir($path); |
|
| 97 | - } |
|
| 98 | - else { |
|
| 99 | - $url = $this->constructUrl($path); |
|
| 100 | - $result = unlink($url); |
|
| 101 | - clearstatcache(true, $url); |
|
| 102 | - return $result; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - public function fopen($path,$mode) { |
|
| 106 | - switch($mode) { |
|
| 107 | - case 'r': |
|
| 108 | - case 'rb': |
|
| 109 | - case 'w': |
|
| 110 | - case 'wb': |
|
| 111 | - case 'a': |
|
| 112 | - case 'ab': |
|
| 113 | - //these are supported by the wrapper |
|
| 114 | - $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 115 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 116 | - return RetryWrapper::wrap($handle); |
|
| 117 | - case 'r+': |
|
| 118 | - case 'w+': |
|
| 119 | - case 'wb+': |
|
| 120 | - case 'a+': |
|
| 121 | - case 'x': |
|
| 122 | - case 'x+': |
|
| 123 | - case 'c': |
|
| 124 | - case 'c+': |
|
| 125 | - //emulate these |
|
| 126 | - if (strrpos($path, '.')!==false) { |
|
| 127 | - $ext=substr($path, strrpos($path, '.')); |
|
| 128 | - } else { |
|
| 129 | - $ext=''; |
|
| 130 | - } |
|
| 131 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 132 | - if ($this->file_exists($path)) { |
|
| 133 | - $this->getFile($path, $tmpFile); |
|
| 134 | - } |
|
| 135 | - $handle = fopen($tmpFile, $mode); |
|
| 136 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 137 | - $this->writeBack($tmpFile, $path); |
|
| 138 | - }); |
|
| 139 | - } |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 90 | + /** |
|
| 91 | + * Unlinks file or directory |
|
| 92 | + * @param string $path |
|
| 93 | + */ |
|
| 94 | + public function unlink($path) { |
|
| 95 | + if ($this->is_dir($path)) { |
|
| 96 | + return $this->rmdir($path); |
|
| 97 | + } |
|
| 98 | + else { |
|
| 99 | + $url = $this->constructUrl($path); |
|
| 100 | + $result = unlink($url); |
|
| 101 | + clearstatcache(true, $url); |
|
| 102 | + return $result; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + public function fopen($path,$mode) { |
|
| 106 | + switch($mode) { |
|
| 107 | + case 'r': |
|
| 108 | + case 'rb': |
|
| 109 | + case 'w': |
|
| 110 | + case 'wb': |
|
| 111 | + case 'a': |
|
| 112 | + case 'ab': |
|
| 113 | + //these are supported by the wrapper |
|
| 114 | + $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 115 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 116 | + return RetryWrapper::wrap($handle); |
|
| 117 | + case 'r+': |
|
| 118 | + case 'w+': |
|
| 119 | + case 'wb+': |
|
| 120 | + case 'a+': |
|
| 121 | + case 'x': |
|
| 122 | + case 'x+': |
|
| 123 | + case 'c': |
|
| 124 | + case 'c+': |
|
| 125 | + //emulate these |
|
| 126 | + if (strrpos($path, '.')!==false) { |
|
| 127 | + $ext=substr($path, strrpos($path, '.')); |
|
| 128 | + } else { |
|
| 129 | + $ext=''; |
|
| 130 | + } |
|
| 131 | + $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 132 | + if ($this->file_exists($path)) { |
|
| 133 | + $this->getFile($path, $tmpFile); |
|
| 134 | + } |
|
| 135 | + $handle = fopen($tmpFile, $mode); |
|
| 136 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 137 | + $this->writeBack($tmpFile, $path); |
|
| 138 | + }); |
|
| 139 | + } |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - public function writeBack($tmpFile, $path) { |
|
| 144 | - $this->uploadFile($tmpFile, $path); |
|
| 145 | - unlink($tmpFile); |
|
| 146 | - } |
|
| 143 | + public function writeBack($tmpFile, $path) { |
|
| 144 | + $this->uploadFile($tmpFile, $path); |
|
| 145 | + unlink($tmpFile); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * check if php-ftp is installed |
|
| 150 | - */ |
|
| 151 | - public static function checkDependencies() { |
|
| 152 | - if (function_exists('ftp_login')) { |
|
| 153 | - return true; |
|
| 154 | - } else { |
|
| 155 | - return array('ftp'); |
|
| 156 | - } |
|
| 157 | - } |
|
| 148 | + /** |
|
| 149 | + * check if php-ftp is installed |
|
| 150 | + */ |
|
| 151 | + public static function checkDependencies() { |
|
| 152 | + if (function_exists('ftp_login')) { |
|
| 153 | + return true; |
|
| 154 | + } else { |
|
| 155 | + return array('ftp'); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | 159 | } |
@@ -597,7 +597,7 @@ |
||
| 597 | 597 | * publish activity |
| 598 | 598 | * |
| 599 | 599 | * @param string $subject |
| 600 | - * @param array $parameters |
|
| 600 | + * @param string[] $parameters |
|
| 601 | 601 | * @param string $affectedUser |
| 602 | 602 | * @param int $fileId |
| 603 | 603 | * @param string $filePath |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | public function showAuthenticate($token) { |
| 151 | 151 | $share = $this->shareManager->getShareByToken($token); |
| 152 | 152 | |
| 153 | - if($this->linkShareAuth($share)) { |
|
| 153 | + if ($this->linkShareAuth($share)) { |
|
| 154 | 154 | return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
| 155 | 155 | } |
| 156 | 156 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | |
| 179 | 179 | $authenticate = $this->linkShareAuth($share, $password); |
| 180 | 180 | |
| 181 | - if($authenticate === true) { |
|
| 181 | + if ($authenticate === true) { |
|
| 182 | 182 | return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -201,15 +201,15 @@ discard block |
||
| 201 | 201 | private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
| 202 | 202 | if ($password !== null) { |
| 203 | 203 | if ($this->shareManager->checkPassword($share, $password)) { |
| 204 | - $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 204 | + $this->session->set('public_link_authenticated', (string) $share->getId()); |
|
| 205 | 205 | } else { |
| 206 | 206 | $this->emitAccessShareHook($share, 403, 'Wrong password'); |
| 207 | 207 | return false; |
| 208 | 208 | } |
| 209 | 209 | } else { |
| 210 | 210 | // not authenticated ? |
| 211 | - if ( ! $this->session->exists('public_link_authenticated') |
|
| 212 | - || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 211 | + if (!$this->session->exists('public_link_authenticated') |
|
| 212 | + || $this->session->get('public_link_authenticated') !== (string) $share->getId()) { |
|
| 213 | 213 | return false; |
| 214 | 214 | } |
| 215 | 215 | } |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | $itemType = $itemSource = $uidOwner = ''; |
| 231 | 231 | $token = $share; |
| 232 | 232 | $exception = null; |
| 233 | - if($share instanceof \OCP\Share\IShare) { |
|
| 233 | + if ($share instanceof \OCP\Share\IShare) { |
|
| 234 | 234 | try { |
| 235 | 235 | $token = $share->getToken(); |
| 236 | 236 | $uidOwner = $share->getSharedBy(); |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | 'errorCode' => $errorCode, |
| 250 | 250 | 'errorMessage' => $errorMessage, |
| 251 | 251 | ]); |
| 252 | - if(!is_null($exception)) { |
|
| 252 | + if (!is_null($exception)) { |
|
| 253 | 253 | throw $exception; |
| 254 | 254 | } |
| 255 | 255 | } |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
| 376 | 376 | $ogPreview = ''; |
| 377 | 377 | if ($shareTmpl['previewSupported']) { |
| 378 | - $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 378 | + $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', |
|
| 379 | 379 | ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
| 380 | 380 | $ogPreview = $shareTmpl['previewImage']; |
| 381 | 381 | |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | |
| 413 | 413 | // OpenGraph Support: http://ogp.me/ |
| 414 | 414 | \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]); |
| 415 | - \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 415 | + \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName().($this->defaults->getSlogan() !== '' ? ' - '.$this->defaults->getSlogan() : '')]); |
|
| 416 | 416 | \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
| 417 | 417 | \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
| 418 | 418 | \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | |
| 447 | 447 | $share = $this->shareManager->getShareByToken($token); |
| 448 | 448 | |
| 449 | - if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 449 | + if (!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 450 | 450 | return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
| 451 | 451 | } |
| 452 | 452 | |
@@ -528,7 +528,7 @@ discard block |
||
| 528 | 528 | |
| 529 | 529 | $this->emitAccessShareHook($share); |
| 530 | 530 | |
| 531 | - $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 531 | + $server_params = array('head' => $this->request->getMethod() === 'HEAD'); |
|
| 532 | 532 | |
| 533 | 533 | /** |
| 534 | 534 | * Http range requests support |
@@ -67,583 +67,583 @@ |
||
| 67 | 67 | */ |
| 68 | 68 | class ShareController extends Controller { |
| 69 | 69 | |
| 70 | - /** @var IConfig */ |
|
| 71 | - protected $config; |
|
| 72 | - /** @var IURLGenerator */ |
|
| 73 | - protected $urlGenerator; |
|
| 74 | - /** @var IUserManager */ |
|
| 75 | - protected $userManager; |
|
| 76 | - /** @var ILogger */ |
|
| 77 | - protected $logger; |
|
| 78 | - /** @var \OCP\Activity\IManager */ |
|
| 79 | - protected $activityManager; |
|
| 80 | - /** @var \OCP\Share\IManager */ |
|
| 81 | - protected $shareManager; |
|
| 82 | - /** @var ISession */ |
|
| 83 | - protected $session; |
|
| 84 | - /** @var IPreview */ |
|
| 85 | - protected $previewManager; |
|
| 86 | - /** @var IRootFolder */ |
|
| 87 | - protected $rootFolder; |
|
| 88 | - /** @var FederatedShareProvider */ |
|
| 89 | - protected $federatedShareProvider; |
|
| 90 | - /** @var EventDispatcherInterface */ |
|
| 91 | - protected $eventDispatcher; |
|
| 92 | - /** @var IL10N */ |
|
| 93 | - protected $l10n; |
|
| 94 | - /** @var Defaults */ |
|
| 95 | - protected $defaults; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @param string $appName |
|
| 99 | - * @param IRequest $request |
|
| 100 | - * @param IConfig $config |
|
| 101 | - * @param IURLGenerator $urlGenerator |
|
| 102 | - * @param IUserManager $userManager |
|
| 103 | - * @param ILogger $logger |
|
| 104 | - * @param \OCP\Activity\IManager $activityManager |
|
| 105 | - * @param \OCP\Share\IManager $shareManager |
|
| 106 | - * @param ISession $session |
|
| 107 | - * @param IPreview $previewManager |
|
| 108 | - * @param IRootFolder $rootFolder |
|
| 109 | - * @param FederatedShareProvider $federatedShareProvider |
|
| 110 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 111 | - * @param IL10N $l10n |
|
| 112 | - * @param Defaults $defaults |
|
| 113 | - */ |
|
| 114 | - public function __construct($appName, |
|
| 115 | - IRequest $request, |
|
| 116 | - IConfig $config, |
|
| 117 | - IURLGenerator $urlGenerator, |
|
| 118 | - IUserManager $userManager, |
|
| 119 | - ILogger $logger, |
|
| 120 | - \OCP\Activity\IManager $activityManager, |
|
| 121 | - \OCP\Share\IManager $shareManager, |
|
| 122 | - ISession $session, |
|
| 123 | - IPreview $previewManager, |
|
| 124 | - IRootFolder $rootFolder, |
|
| 125 | - FederatedShareProvider $federatedShareProvider, |
|
| 126 | - EventDispatcherInterface $eventDispatcher, |
|
| 127 | - IL10N $l10n, |
|
| 128 | - Defaults $defaults) { |
|
| 129 | - parent::__construct($appName, $request); |
|
| 130 | - |
|
| 131 | - $this->config = $config; |
|
| 132 | - $this->urlGenerator = $urlGenerator; |
|
| 133 | - $this->userManager = $userManager; |
|
| 134 | - $this->logger = $logger; |
|
| 135 | - $this->activityManager = $activityManager; |
|
| 136 | - $this->shareManager = $shareManager; |
|
| 137 | - $this->session = $session; |
|
| 138 | - $this->previewManager = $previewManager; |
|
| 139 | - $this->rootFolder = $rootFolder; |
|
| 140 | - $this->federatedShareProvider = $federatedShareProvider; |
|
| 141 | - $this->eventDispatcher = $eventDispatcher; |
|
| 142 | - $this->l10n = $l10n; |
|
| 143 | - $this->defaults = $defaults; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @PublicPage |
|
| 148 | - * @NoCSRFRequired |
|
| 149 | - * |
|
| 150 | - * @param string $token |
|
| 151 | - * @return TemplateResponse|RedirectResponse |
|
| 152 | - */ |
|
| 153 | - public function showAuthenticate($token) { |
|
| 154 | - $share = $this->shareManager->getShareByToken($token); |
|
| 155 | - |
|
| 156 | - if($this->linkShareAuth($share)) { |
|
| 157 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return new TemplateResponse($this->appName, 'authenticate', array(), 'guest'); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @PublicPage |
|
| 165 | - * @UseSession |
|
| 166 | - * @BruteForceProtection(action=publicLinkAuth) |
|
| 167 | - * |
|
| 168 | - * Authenticates against password-protected shares |
|
| 169 | - * @param string $token |
|
| 170 | - * @param string $password |
|
| 171 | - * @return RedirectResponse|TemplateResponse|NotFoundResponse |
|
| 172 | - */ |
|
| 173 | - public function authenticate($token, $password = '') { |
|
| 174 | - |
|
| 175 | - // Check whether share exists |
|
| 176 | - try { |
|
| 177 | - $share = $this->shareManager->getShareByToken($token); |
|
| 178 | - } catch (ShareNotFound $e) { |
|
| 179 | - return new NotFoundResponse(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $authenticate = $this->linkShareAuth($share, $password); |
|
| 183 | - |
|
| 184 | - if($authenticate === true) { |
|
| 185 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $response = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest'); |
|
| 189 | - $response->throttle(); |
|
| 190 | - return $response; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Authenticate a link item with the given password. |
|
| 195 | - * Or use the session if no password is provided. |
|
| 196 | - * |
|
| 197 | - * This is a modified version of Helper::authenticate |
|
| 198 | - * TODO: Try to merge back eventually with Helper::authenticate |
|
| 199 | - * |
|
| 200 | - * @param \OCP\Share\IShare $share |
|
| 201 | - * @param string|null $password |
|
| 202 | - * @return bool |
|
| 203 | - */ |
|
| 204 | - private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
|
| 205 | - if ($password !== null) { |
|
| 206 | - if ($this->shareManager->checkPassword($share, $password)) { |
|
| 207 | - $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 208 | - } else { |
|
| 209 | - $this->emitAccessShareHook($share, 403, 'Wrong password'); |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - } else { |
|
| 213 | - // not authenticated ? |
|
| 214 | - if ( ! $this->session->exists('public_link_authenticated') |
|
| 215 | - || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 216 | - return false; |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - return true; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * throws hooks when a share is attempted to be accessed |
|
| 224 | - * |
|
| 225 | - * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 226 | - * otherwise token |
|
| 227 | - * @param int $errorCode |
|
| 228 | - * @param string $errorMessage |
|
| 229 | - * @throws \OC\HintException |
|
| 230 | - * @throws \OC\ServerNotAvailableException |
|
| 231 | - */ |
|
| 232 | - protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 233 | - $itemType = $itemSource = $uidOwner = ''; |
|
| 234 | - $token = $share; |
|
| 235 | - $exception = null; |
|
| 236 | - if($share instanceof \OCP\Share\IShare) { |
|
| 237 | - try { |
|
| 238 | - $token = $share->getToken(); |
|
| 239 | - $uidOwner = $share->getSharedBy(); |
|
| 240 | - $itemType = $share->getNodeType(); |
|
| 241 | - $itemSource = $share->getNodeId(); |
|
| 242 | - } catch (\Exception $e) { |
|
| 243 | - // we log what we know and pass on the exception afterwards |
|
| 244 | - $exception = $e; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - \OC_Hook::emit(Share::class, 'share_link_access', [ |
|
| 248 | - 'itemType' => $itemType, |
|
| 249 | - 'itemSource' => $itemSource, |
|
| 250 | - 'uidOwner' => $uidOwner, |
|
| 251 | - 'token' => $token, |
|
| 252 | - 'errorCode' => $errorCode, |
|
| 253 | - 'errorMessage' => $errorMessage, |
|
| 254 | - ]); |
|
| 255 | - if(!is_null($exception)) { |
|
| 256 | - throw $exception; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Validate the permissions of the share |
|
| 262 | - * |
|
| 263 | - * @param Share\IShare $share |
|
| 264 | - * @return bool |
|
| 265 | - */ |
|
| 266 | - private function validateShare(\OCP\Share\IShare $share) { |
|
| 267 | - return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @PublicPage |
|
| 272 | - * @NoCSRFRequired |
|
| 273 | - * |
|
| 274 | - * @param string $token |
|
| 275 | - * @param string $path |
|
| 276 | - * @return TemplateResponse|RedirectResponse|NotFoundResponse |
|
| 277 | - * @throws NotFoundException |
|
| 278 | - * @throws \Exception |
|
| 279 | - */ |
|
| 280 | - public function showShare($token, $path = '') { |
|
| 281 | - \OC_User::setIncognitoMode(true); |
|
| 282 | - |
|
| 283 | - // Check whether share exists |
|
| 284 | - try { |
|
| 285 | - $share = $this->shareManager->getShareByToken($token); |
|
| 286 | - } catch (ShareNotFound $e) { |
|
| 287 | - $this->emitAccessShareHook($token, 404, 'Share not found'); |
|
| 288 | - return new NotFoundResponse(); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // Share is password protected - check whether the user is permitted to access the share |
|
| 292 | - if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 293 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 294 | - array('token' => $token))); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - if (!$this->validateShare($share)) { |
|
| 298 | - throw new NotFoundException(); |
|
| 299 | - } |
|
| 300 | - // We can't get the path of a file share |
|
| 301 | - try { |
|
| 302 | - if ($share->getNode() instanceof \OCP\Files\File && $path !== '') { |
|
| 303 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 304 | - throw new NotFoundException(); |
|
| 305 | - } |
|
| 306 | - } catch (\Exception $e) { |
|
| 307 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 308 | - throw $e; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $shareTmpl = []; |
|
| 312 | - $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 313 | - $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 314 | - $shareTmpl['filename'] = $share->getNode()->getName(); |
|
| 315 | - $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 316 | - $shareTmpl['mimetype'] = $share->getNode()->getMimetype(); |
|
| 317 | - $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype()); |
|
| 318 | - $shareTmpl['dirToken'] = $token; |
|
| 319 | - $shareTmpl['sharingToken'] = $token; |
|
| 320 | - $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 321 | - $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 322 | - $shareTmpl['dir'] = ''; |
|
| 323 | - $shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize(); |
|
| 324 | - $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize()); |
|
| 325 | - |
|
| 326 | - // Show file list |
|
| 327 | - $hideFileList = false; |
|
| 328 | - if ($share->getNode() instanceof \OCP\Files\Folder) { |
|
| 329 | - /** @var \OCP\Files\Folder $rootFolder */ |
|
| 330 | - $rootFolder = $share->getNode(); |
|
| 331 | - |
|
| 332 | - try { |
|
| 333 | - $folderNode = $rootFolder->get($path); |
|
| 334 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 335 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 336 | - throw new NotFoundException(); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - $shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath()); |
|
| 340 | - |
|
| 341 | - /* |
|
| 70 | + /** @var IConfig */ |
|
| 71 | + protected $config; |
|
| 72 | + /** @var IURLGenerator */ |
|
| 73 | + protected $urlGenerator; |
|
| 74 | + /** @var IUserManager */ |
|
| 75 | + protected $userManager; |
|
| 76 | + /** @var ILogger */ |
|
| 77 | + protected $logger; |
|
| 78 | + /** @var \OCP\Activity\IManager */ |
|
| 79 | + protected $activityManager; |
|
| 80 | + /** @var \OCP\Share\IManager */ |
|
| 81 | + protected $shareManager; |
|
| 82 | + /** @var ISession */ |
|
| 83 | + protected $session; |
|
| 84 | + /** @var IPreview */ |
|
| 85 | + protected $previewManager; |
|
| 86 | + /** @var IRootFolder */ |
|
| 87 | + protected $rootFolder; |
|
| 88 | + /** @var FederatedShareProvider */ |
|
| 89 | + protected $federatedShareProvider; |
|
| 90 | + /** @var EventDispatcherInterface */ |
|
| 91 | + protected $eventDispatcher; |
|
| 92 | + /** @var IL10N */ |
|
| 93 | + protected $l10n; |
|
| 94 | + /** @var Defaults */ |
|
| 95 | + protected $defaults; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @param string $appName |
|
| 99 | + * @param IRequest $request |
|
| 100 | + * @param IConfig $config |
|
| 101 | + * @param IURLGenerator $urlGenerator |
|
| 102 | + * @param IUserManager $userManager |
|
| 103 | + * @param ILogger $logger |
|
| 104 | + * @param \OCP\Activity\IManager $activityManager |
|
| 105 | + * @param \OCP\Share\IManager $shareManager |
|
| 106 | + * @param ISession $session |
|
| 107 | + * @param IPreview $previewManager |
|
| 108 | + * @param IRootFolder $rootFolder |
|
| 109 | + * @param FederatedShareProvider $federatedShareProvider |
|
| 110 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 111 | + * @param IL10N $l10n |
|
| 112 | + * @param Defaults $defaults |
|
| 113 | + */ |
|
| 114 | + public function __construct($appName, |
|
| 115 | + IRequest $request, |
|
| 116 | + IConfig $config, |
|
| 117 | + IURLGenerator $urlGenerator, |
|
| 118 | + IUserManager $userManager, |
|
| 119 | + ILogger $logger, |
|
| 120 | + \OCP\Activity\IManager $activityManager, |
|
| 121 | + \OCP\Share\IManager $shareManager, |
|
| 122 | + ISession $session, |
|
| 123 | + IPreview $previewManager, |
|
| 124 | + IRootFolder $rootFolder, |
|
| 125 | + FederatedShareProvider $federatedShareProvider, |
|
| 126 | + EventDispatcherInterface $eventDispatcher, |
|
| 127 | + IL10N $l10n, |
|
| 128 | + Defaults $defaults) { |
|
| 129 | + parent::__construct($appName, $request); |
|
| 130 | + |
|
| 131 | + $this->config = $config; |
|
| 132 | + $this->urlGenerator = $urlGenerator; |
|
| 133 | + $this->userManager = $userManager; |
|
| 134 | + $this->logger = $logger; |
|
| 135 | + $this->activityManager = $activityManager; |
|
| 136 | + $this->shareManager = $shareManager; |
|
| 137 | + $this->session = $session; |
|
| 138 | + $this->previewManager = $previewManager; |
|
| 139 | + $this->rootFolder = $rootFolder; |
|
| 140 | + $this->federatedShareProvider = $federatedShareProvider; |
|
| 141 | + $this->eventDispatcher = $eventDispatcher; |
|
| 142 | + $this->l10n = $l10n; |
|
| 143 | + $this->defaults = $defaults; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @PublicPage |
|
| 148 | + * @NoCSRFRequired |
|
| 149 | + * |
|
| 150 | + * @param string $token |
|
| 151 | + * @return TemplateResponse|RedirectResponse |
|
| 152 | + */ |
|
| 153 | + public function showAuthenticate($token) { |
|
| 154 | + $share = $this->shareManager->getShareByToken($token); |
|
| 155 | + |
|
| 156 | + if($this->linkShareAuth($share)) { |
|
| 157 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return new TemplateResponse($this->appName, 'authenticate', array(), 'guest'); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @PublicPage |
|
| 165 | + * @UseSession |
|
| 166 | + * @BruteForceProtection(action=publicLinkAuth) |
|
| 167 | + * |
|
| 168 | + * Authenticates against password-protected shares |
|
| 169 | + * @param string $token |
|
| 170 | + * @param string $password |
|
| 171 | + * @return RedirectResponse|TemplateResponse|NotFoundResponse |
|
| 172 | + */ |
|
| 173 | + public function authenticate($token, $password = '') { |
|
| 174 | + |
|
| 175 | + // Check whether share exists |
|
| 176 | + try { |
|
| 177 | + $share = $this->shareManager->getShareByToken($token); |
|
| 178 | + } catch (ShareNotFound $e) { |
|
| 179 | + return new NotFoundResponse(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $authenticate = $this->linkShareAuth($share, $password); |
|
| 183 | + |
|
| 184 | + if($authenticate === true) { |
|
| 185 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $response = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest'); |
|
| 189 | + $response->throttle(); |
|
| 190 | + return $response; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Authenticate a link item with the given password. |
|
| 195 | + * Or use the session if no password is provided. |
|
| 196 | + * |
|
| 197 | + * This is a modified version of Helper::authenticate |
|
| 198 | + * TODO: Try to merge back eventually with Helper::authenticate |
|
| 199 | + * |
|
| 200 | + * @param \OCP\Share\IShare $share |
|
| 201 | + * @param string|null $password |
|
| 202 | + * @return bool |
|
| 203 | + */ |
|
| 204 | + private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
|
| 205 | + if ($password !== null) { |
|
| 206 | + if ($this->shareManager->checkPassword($share, $password)) { |
|
| 207 | + $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 208 | + } else { |
|
| 209 | + $this->emitAccessShareHook($share, 403, 'Wrong password'); |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + } else { |
|
| 213 | + // not authenticated ? |
|
| 214 | + if ( ! $this->session->exists('public_link_authenticated') |
|
| 215 | + || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 216 | + return false; |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + return true; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * throws hooks when a share is attempted to be accessed |
|
| 224 | + * |
|
| 225 | + * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 226 | + * otherwise token |
|
| 227 | + * @param int $errorCode |
|
| 228 | + * @param string $errorMessage |
|
| 229 | + * @throws \OC\HintException |
|
| 230 | + * @throws \OC\ServerNotAvailableException |
|
| 231 | + */ |
|
| 232 | + protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 233 | + $itemType = $itemSource = $uidOwner = ''; |
|
| 234 | + $token = $share; |
|
| 235 | + $exception = null; |
|
| 236 | + if($share instanceof \OCP\Share\IShare) { |
|
| 237 | + try { |
|
| 238 | + $token = $share->getToken(); |
|
| 239 | + $uidOwner = $share->getSharedBy(); |
|
| 240 | + $itemType = $share->getNodeType(); |
|
| 241 | + $itemSource = $share->getNodeId(); |
|
| 242 | + } catch (\Exception $e) { |
|
| 243 | + // we log what we know and pass on the exception afterwards |
|
| 244 | + $exception = $e; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + \OC_Hook::emit(Share::class, 'share_link_access', [ |
|
| 248 | + 'itemType' => $itemType, |
|
| 249 | + 'itemSource' => $itemSource, |
|
| 250 | + 'uidOwner' => $uidOwner, |
|
| 251 | + 'token' => $token, |
|
| 252 | + 'errorCode' => $errorCode, |
|
| 253 | + 'errorMessage' => $errorMessage, |
|
| 254 | + ]); |
|
| 255 | + if(!is_null($exception)) { |
|
| 256 | + throw $exception; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Validate the permissions of the share |
|
| 262 | + * |
|
| 263 | + * @param Share\IShare $share |
|
| 264 | + * @return bool |
|
| 265 | + */ |
|
| 266 | + private function validateShare(\OCP\Share\IShare $share) { |
|
| 267 | + return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @PublicPage |
|
| 272 | + * @NoCSRFRequired |
|
| 273 | + * |
|
| 274 | + * @param string $token |
|
| 275 | + * @param string $path |
|
| 276 | + * @return TemplateResponse|RedirectResponse|NotFoundResponse |
|
| 277 | + * @throws NotFoundException |
|
| 278 | + * @throws \Exception |
|
| 279 | + */ |
|
| 280 | + public function showShare($token, $path = '') { |
|
| 281 | + \OC_User::setIncognitoMode(true); |
|
| 282 | + |
|
| 283 | + // Check whether share exists |
|
| 284 | + try { |
|
| 285 | + $share = $this->shareManager->getShareByToken($token); |
|
| 286 | + } catch (ShareNotFound $e) { |
|
| 287 | + $this->emitAccessShareHook($token, 404, 'Share not found'); |
|
| 288 | + return new NotFoundResponse(); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // Share is password protected - check whether the user is permitted to access the share |
|
| 292 | + if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 293 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 294 | + array('token' => $token))); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + if (!$this->validateShare($share)) { |
|
| 298 | + throw new NotFoundException(); |
|
| 299 | + } |
|
| 300 | + // We can't get the path of a file share |
|
| 301 | + try { |
|
| 302 | + if ($share->getNode() instanceof \OCP\Files\File && $path !== '') { |
|
| 303 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 304 | + throw new NotFoundException(); |
|
| 305 | + } |
|
| 306 | + } catch (\Exception $e) { |
|
| 307 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 308 | + throw $e; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $shareTmpl = []; |
|
| 312 | + $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 313 | + $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 314 | + $shareTmpl['filename'] = $share->getNode()->getName(); |
|
| 315 | + $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 316 | + $shareTmpl['mimetype'] = $share->getNode()->getMimetype(); |
|
| 317 | + $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype()); |
|
| 318 | + $shareTmpl['dirToken'] = $token; |
|
| 319 | + $shareTmpl['sharingToken'] = $token; |
|
| 320 | + $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 321 | + $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 322 | + $shareTmpl['dir'] = ''; |
|
| 323 | + $shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize(); |
|
| 324 | + $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize()); |
|
| 325 | + |
|
| 326 | + // Show file list |
|
| 327 | + $hideFileList = false; |
|
| 328 | + if ($share->getNode() instanceof \OCP\Files\Folder) { |
|
| 329 | + /** @var \OCP\Files\Folder $rootFolder */ |
|
| 330 | + $rootFolder = $share->getNode(); |
|
| 331 | + |
|
| 332 | + try { |
|
| 333 | + $folderNode = $rootFolder->get($path); |
|
| 334 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 335 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 336 | + throw new NotFoundException(); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + $shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath()); |
|
| 340 | + |
|
| 341 | + /* |
|
| 342 | 342 | * The OC_Util methods require a view. This just uses the node API |
| 343 | 343 | */ |
| 344 | - $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 345 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 346 | - $freeSpace = max($freeSpace, 0); |
|
| 347 | - } else { |
|
| 348 | - $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $hideFileList = !($share->getPermissions() & \OCP\Constants::PERMISSION_READ); |
|
| 352 | - $maxUploadFilesize = $freeSpace; |
|
| 353 | - |
|
| 354 | - $folder = new Template('files', 'list', ''); |
|
| 355 | - $folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath())); |
|
| 356 | - $folder->assign('dirToken', $token); |
|
| 357 | - $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 358 | - $folder->assign('isPublic', true); |
|
| 359 | - $folder->assign('hideFileList', $hideFileList); |
|
| 360 | - $folder->assign('publicUploadEnabled', 'no'); |
|
| 361 | - $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 362 | - $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 363 | - $folder->assign('freeSpace', $freeSpace); |
|
| 364 | - $folder->assign('usedSpacePercent', 0); |
|
| 365 | - $folder->assign('trash', false); |
|
| 366 | - $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - $shareTmpl['hideFileList'] = $hideFileList; |
|
| 370 | - $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 371 | - $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $token]); |
|
| 372 | - $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 373 | - $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 374 | - $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 375 | - $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 376 | - $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 377 | - $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 378 | - $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
|
| 379 | - $ogPreview = ''; |
|
| 380 | - if ($shareTmpl['previewSupported']) { |
|
| 381 | - $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 382 | - ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
|
| 383 | - $ogPreview = $shareTmpl['previewImage']; |
|
| 384 | - |
|
| 385 | - // We just have direct previews for image files |
|
| 386 | - if ($share->getNode()->getMimePart() === 'image') { |
|
| 387 | - $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 388 | - |
|
| 389 | - $ogPreview = $shareTmpl['previewURL']; |
|
| 390 | - |
|
| 391 | - //Whatapp is kind of picky about their size requirements |
|
| 392 | - if ($this->request->isUserAgent(['/^WhatsApp/'])) { |
|
| 393 | - $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [ |
|
| 394 | - 't' => $token, |
|
| 395 | - 'x' => 256, |
|
| 396 | - 'y' => 256, |
|
| 397 | - 'a' => true, |
|
| 398 | - ]); |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - } else { |
|
| 402 | - $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 403 | - $ogPreview = $shareTmpl['previewImage']; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - // Load files we need |
|
| 407 | - \OCP\Util::addScript('files', 'file-upload'); |
|
| 408 | - \OCP\Util::addStyle('files_sharing', 'publicView'); |
|
| 409 | - \OCP\Util::addScript('files_sharing', 'public'); |
|
| 410 | - \OCP\Util::addScript('files', 'fileactions'); |
|
| 411 | - \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 412 | - \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 413 | - \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 414 | - |
|
| 415 | - if (isset($shareTmpl['folder'])) { |
|
| 416 | - // JS required for folders |
|
| 417 | - \OCP\Util::addStyle('files', 'merged'); |
|
| 418 | - \OCP\Util::addScript('files', 'filesummary'); |
|
| 419 | - \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 420 | - \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 421 | - \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 422 | - \OCP\Util::addScript('files', 'files'); |
|
| 423 | - \OCP\Util::addScript('files', 'filelist'); |
|
| 424 | - \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - // OpenGraph Support: http://ogp.me/ |
|
| 428 | - \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]); |
|
| 429 | - \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 430 | - \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 431 | - \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 432 | - \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 433 | - \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]); |
|
| 434 | - |
|
| 435 | - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 436 | - |
|
| 437 | - $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 438 | - $csp->addAllowedFrameDomain('\'self\''); |
|
| 439 | - $response = new TemplateResponse($this->appName, 'public', $shareTmpl, 'base'); |
|
| 440 | - $response->setContentSecurityPolicy($csp); |
|
| 441 | - |
|
| 442 | - $this->emitAccessShareHook($share); |
|
| 443 | - |
|
| 444 | - return $response; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * @PublicPage |
|
| 449 | - * @NoCSRFRequired |
|
| 450 | - * |
|
| 451 | - * @param string $token |
|
| 452 | - * @param string $files |
|
| 453 | - * @param string $path |
|
| 454 | - * @param string $downloadStartSecret |
|
| 455 | - * @return void|\OCP\AppFramework\Http\Response |
|
| 456 | - * @throws NotFoundException |
|
| 457 | - */ |
|
| 458 | - public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 459 | - \OC_User::setIncognitoMode(true); |
|
| 460 | - |
|
| 461 | - $share = $this->shareManager->getShareByToken($token); |
|
| 462 | - |
|
| 463 | - if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 464 | - return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - // Share is password protected - check whether the user is permitted to access the share |
|
| 468 | - if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 469 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 470 | - ['token' => $token])); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - $files_list = null; |
|
| 474 | - if (!is_null($files)) { // download selected files |
|
| 475 | - $files_list = json_decode($files); |
|
| 476 | - // in case we get only a single file |
|
| 477 | - if ($files_list === null) { |
|
| 478 | - $files_list = [$files]; |
|
| 479 | - } |
|
| 480 | - // Just in case $files is a single int like '1234' |
|
| 481 | - if (!is_array($files_list)) { |
|
| 482 | - $files_list = [$files_list]; |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 487 | - $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 488 | - |
|
| 489 | - if (!$this->validateShare($share)) { |
|
| 490 | - throw new NotFoundException(); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - // Single file share |
|
| 494 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 495 | - // Single file download |
|
| 496 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 497 | - } |
|
| 498 | - // Directory share |
|
| 499 | - else { |
|
| 500 | - /** @var \OCP\Files\Folder $node */ |
|
| 501 | - $node = $share->getNode(); |
|
| 502 | - |
|
| 503 | - // Try to get the path |
|
| 504 | - if ($path !== '') { |
|
| 505 | - try { |
|
| 506 | - $node = $node->get($path); |
|
| 507 | - } catch (NotFoundException $e) { |
|
| 508 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 509 | - return new NotFoundResponse(); |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 514 | - |
|
| 515 | - if ($node instanceof \OCP\Files\File) { |
|
| 516 | - // Single file download |
|
| 517 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 518 | - } else if (!empty($files_list)) { |
|
| 519 | - $this->fileListDownloaded($share, $files_list, $node); |
|
| 520 | - } else { |
|
| 521 | - // The folder is downloaded |
|
| 522 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /* FIXME: We should do this all nicely in OCP */ |
|
| 527 | - OC_Util::tearDownFS(); |
|
| 528 | - OC_Util::setupFS($share->getShareOwner()); |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * this sets a cookie to be able to recognize the start of the download |
|
| 532 | - * the content must not be longer than 32 characters and must only contain |
|
| 533 | - * alphanumeric characters |
|
| 534 | - */ |
|
| 535 | - if (!empty($downloadStartSecret) |
|
| 536 | - && !isset($downloadStartSecret[32]) |
|
| 537 | - && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 538 | - |
|
| 539 | - // FIXME: set on the response once we use an actual app framework response |
|
| 540 | - setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - $this->emitAccessShareHook($share); |
|
| 544 | - |
|
| 545 | - $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Http range requests support |
|
| 549 | - */ |
|
| 550 | - if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 551 | - $server_params['range'] = $this->request->getHeader('Range'); |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - // download selected files |
|
| 555 | - if (!is_null($files) && $files !== '') { |
|
| 556 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 557 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 558 | - OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 559 | - exit(); |
|
| 560 | - } else { |
|
| 561 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 562 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 563 | - OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 564 | - exit(); |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * create activity for every downloaded file |
|
| 570 | - * |
|
| 571 | - * @param Share\IShare $share |
|
| 572 | - * @param array $files_list |
|
| 573 | - * @param \OCP\Files\Folder $node |
|
| 574 | - */ |
|
| 575 | - protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 576 | - foreach ($files_list as $file) { |
|
| 577 | - $subNode = $node->get($file); |
|
| 578 | - $this->singleFileDownloaded($share, $subNode); |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * create activity if a single file was downloaded from a link share |
|
| 585 | - * |
|
| 586 | - * @param Share\IShare $share |
|
| 587 | - */ |
|
| 588 | - protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 589 | - |
|
| 590 | - $fileId = $node->getId(); |
|
| 591 | - |
|
| 592 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 593 | - $userNodeList = $userFolder->getById($fileId); |
|
| 594 | - $userNode = $userNodeList[0]; |
|
| 595 | - $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 596 | - $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 597 | - $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 598 | - |
|
| 599 | - $parameters = [$userPath]; |
|
| 600 | - |
|
| 601 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 602 | - if ($node instanceof \OCP\Files\File) { |
|
| 603 | - $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 604 | - } else { |
|
| 605 | - $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 606 | - } |
|
| 607 | - $parameters[] = $share->getSharedWith(); |
|
| 608 | - } else { |
|
| 609 | - if ($node instanceof \OCP\Files\File) { |
|
| 610 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 611 | - } else { |
|
| 612 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 613 | - } |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 617 | - |
|
| 618 | - if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 619 | - $parameters[0] = $ownerPath; |
|
| 620 | - $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 621 | - } |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * publish activity |
|
| 626 | - * |
|
| 627 | - * @param string $subject |
|
| 628 | - * @param array $parameters |
|
| 629 | - * @param string $affectedUser |
|
| 630 | - * @param int $fileId |
|
| 631 | - * @param string $filePath |
|
| 632 | - */ |
|
| 633 | - protected function publishActivity($subject, |
|
| 634 | - array $parameters, |
|
| 635 | - $affectedUser, |
|
| 636 | - $fileId, |
|
| 637 | - $filePath) { |
|
| 638 | - |
|
| 639 | - $event = $this->activityManager->generateEvent(); |
|
| 640 | - $event->setApp('files_sharing') |
|
| 641 | - ->setType('public_links') |
|
| 642 | - ->setSubject($subject, $parameters) |
|
| 643 | - ->setAffectedUser($affectedUser) |
|
| 644 | - ->setObject('files', $fileId, $filePath); |
|
| 645 | - $this->activityManager->publish($event); |
|
| 646 | - } |
|
| 344 | + $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 345 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 346 | + $freeSpace = max($freeSpace, 0); |
|
| 347 | + } else { |
|
| 348 | + $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $hideFileList = !($share->getPermissions() & \OCP\Constants::PERMISSION_READ); |
|
| 352 | + $maxUploadFilesize = $freeSpace; |
|
| 353 | + |
|
| 354 | + $folder = new Template('files', 'list', ''); |
|
| 355 | + $folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath())); |
|
| 356 | + $folder->assign('dirToken', $token); |
|
| 357 | + $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 358 | + $folder->assign('isPublic', true); |
|
| 359 | + $folder->assign('hideFileList', $hideFileList); |
|
| 360 | + $folder->assign('publicUploadEnabled', 'no'); |
|
| 361 | + $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 362 | + $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 363 | + $folder->assign('freeSpace', $freeSpace); |
|
| 364 | + $folder->assign('usedSpacePercent', 0); |
|
| 365 | + $folder->assign('trash', false); |
|
| 366 | + $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + $shareTmpl['hideFileList'] = $hideFileList; |
|
| 370 | + $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 371 | + $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $token]); |
|
| 372 | + $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 373 | + $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 374 | + $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 375 | + $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 376 | + $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 377 | + $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 378 | + $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
|
| 379 | + $ogPreview = ''; |
|
| 380 | + if ($shareTmpl['previewSupported']) { |
|
| 381 | + $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 382 | + ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
|
| 383 | + $ogPreview = $shareTmpl['previewImage']; |
|
| 384 | + |
|
| 385 | + // We just have direct previews for image files |
|
| 386 | + if ($share->getNode()->getMimePart() === 'image') { |
|
| 387 | + $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 388 | + |
|
| 389 | + $ogPreview = $shareTmpl['previewURL']; |
|
| 390 | + |
|
| 391 | + //Whatapp is kind of picky about their size requirements |
|
| 392 | + if ($this->request->isUserAgent(['/^WhatsApp/'])) { |
|
| 393 | + $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [ |
|
| 394 | + 't' => $token, |
|
| 395 | + 'x' => 256, |
|
| 396 | + 'y' => 256, |
|
| 397 | + 'a' => true, |
|
| 398 | + ]); |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + } else { |
|
| 402 | + $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 403 | + $ogPreview = $shareTmpl['previewImage']; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + // Load files we need |
|
| 407 | + \OCP\Util::addScript('files', 'file-upload'); |
|
| 408 | + \OCP\Util::addStyle('files_sharing', 'publicView'); |
|
| 409 | + \OCP\Util::addScript('files_sharing', 'public'); |
|
| 410 | + \OCP\Util::addScript('files', 'fileactions'); |
|
| 411 | + \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 412 | + \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 413 | + \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 414 | + |
|
| 415 | + if (isset($shareTmpl['folder'])) { |
|
| 416 | + // JS required for folders |
|
| 417 | + \OCP\Util::addStyle('files', 'merged'); |
|
| 418 | + \OCP\Util::addScript('files', 'filesummary'); |
|
| 419 | + \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 420 | + \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 421 | + \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 422 | + \OCP\Util::addScript('files', 'files'); |
|
| 423 | + \OCP\Util::addScript('files', 'filelist'); |
|
| 424 | + \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + // OpenGraph Support: http://ogp.me/ |
|
| 428 | + \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]); |
|
| 429 | + \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 430 | + \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 431 | + \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 432 | + \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 433 | + \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]); |
|
| 434 | + |
|
| 435 | + $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 436 | + |
|
| 437 | + $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 438 | + $csp->addAllowedFrameDomain('\'self\''); |
|
| 439 | + $response = new TemplateResponse($this->appName, 'public', $shareTmpl, 'base'); |
|
| 440 | + $response->setContentSecurityPolicy($csp); |
|
| 441 | + |
|
| 442 | + $this->emitAccessShareHook($share); |
|
| 443 | + |
|
| 444 | + return $response; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * @PublicPage |
|
| 449 | + * @NoCSRFRequired |
|
| 450 | + * |
|
| 451 | + * @param string $token |
|
| 452 | + * @param string $files |
|
| 453 | + * @param string $path |
|
| 454 | + * @param string $downloadStartSecret |
|
| 455 | + * @return void|\OCP\AppFramework\Http\Response |
|
| 456 | + * @throws NotFoundException |
|
| 457 | + */ |
|
| 458 | + public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 459 | + \OC_User::setIncognitoMode(true); |
|
| 460 | + |
|
| 461 | + $share = $this->shareManager->getShareByToken($token); |
|
| 462 | + |
|
| 463 | + if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 464 | + return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + // Share is password protected - check whether the user is permitted to access the share |
|
| 468 | + if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 469 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 470 | + ['token' => $token])); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + $files_list = null; |
|
| 474 | + if (!is_null($files)) { // download selected files |
|
| 475 | + $files_list = json_decode($files); |
|
| 476 | + // in case we get only a single file |
|
| 477 | + if ($files_list === null) { |
|
| 478 | + $files_list = [$files]; |
|
| 479 | + } |
|
| 480 | + // Just in case $files is a single int like '1234' |
|
| 481 | + if (!is_array($files_list)) { |
|
| 482 | + $files_list = [$files_list]; |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 487 | + $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 488 | + |
|
| 489 | + if (!$this->validateShare($share)) { |
|
| 490 | + throw new NotFoundException(); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + // Single file share |
|
| 494 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 495 | + // Single file download |
|
| 496 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 497 | + } |
|
| 498 | + // Directory share |
|
| 499 | + else { |
|
| 500 | + /** @var \OCP\Files\Folder $node */ |
|
| 501 | + $node = $share->getNode(); |
|
| 502 | + |
|
| 503 | + // Try to get the path |
|
| 504 | + if ($path !== '') { |
|
| 505 | + try { |
|
| 506 | + $node = $node->get($path); |
|
| 507 | + } catch (NotFoundException $e) { |
|
| 508 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 509 | + return new NotFoundResponse(); |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 514 | + |
|
| 515 | + if ($node instanceof \OCP\Files\File) { |
|
| 516 | + // Single file download |
|
| 517 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 518 | + } else if (!empty($files_list)) { |
|
| 519 | + $this->fileListDownloaded($share, $files_list, $node); |
|
| 520 | + } else { |
|
| 521 | + // The folder is downloaded |
|
| 522 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /* FIXME: We should do this all nicely in OCP */ |
|
| 527 | + OC_Util::tearDownFS(); |
|
| 528 | + OC_Util::setupFS($share->getShareOwner()); |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * this sets a cookie to be able to recognize the start of the download |
|
| 532 | + * the content must not be longer than 32 characters and must only contain |
|
| 533 | + * alphanumeric characters |
|
| 534 | + */ |
|
| 535 | + if (!empty($downloadStartSecret) |
|
| 536 | + && !isset($downloadStartSecret[32]) |
|
| 537 | + && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 538 | + |
|
| 539 | + // FIXME: set on the response once we use an actual app framework response |
|
| 540 | + setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + $this->emitAccessShareHook($share); |
|
| 544 | + |
|
| 545 | + $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Http range requests support |
|
| 549 | + */ |
|
| 550 | + if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 551 | + $server_params['range'] = $this->request->getHeader('Range'); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + // download selected files |
|
| 555 | + if (!is_null($files) && $files !== '') { |
|
| 556 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 557 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 558 | + OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 559 | + exit(); |
|
| 560 | + } else { |
|
| 561 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 562 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 563 | + OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 564 | + exit(); |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * create activity for every downloaded file |
|
| 570 | + * |
|
| 571 | + * @param Share\IShare $share |
|
| 572 | + * @param array $files_list |
|
| 573 | + * @param \OCP\Files\Folder $node |
|
| 574 | + */ |
|
| 575 | + protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 576 | + foreach ($files_list as $file) { |
|
| 577 | + $subNode = $node->get($file); |
|
| 578 | + $this->singleFileDownloaded($share, $subNode); |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * create activity if a single file was downloaded from a link share |
|
| 585 | + * |
|
| 586 | + * @param Share\IShare $share |
|
| 587 | + */ |
|
| 588 | + protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 589 | + |
|
| 590 | + $fileId = $node->getId(); |
|
| 591 | + |
|
| 592 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 593 | + $userNodeList = $userFolder->getById($fileId); |
|
| 594 | + $userNode = $userNodeList[0]; |
|
| 595 | + $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 596 | + $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 597 | + $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 598 | + |
|
| 599 | + $parameters = [$userPath]; |
|
| 600 | + |
|
| 601 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 602 | + if ($node instanceof \OCP\Files\File) { |
|
| 603 | + $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 604 | + } else { |
|
| 605 | + $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 606 | + } |
|
| 607 | + $parameters[] = $share->getSharedWith(); |
|
| 608 | + } else { |
|
| 609 | + if ($node instanceof \OCP\Files\File) { |
|
| 610 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 611 | + } else { |
|
| 612 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 613 | + } |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 617 | + |
|
| 618 | + if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 619 | + $parameters[0] = $ownerPath; |
|
| 620 | + $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 621 | + } |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * publish activity |
|
| 626 | + * |
|
| 627 | + * @param string $subject |
|
| 628 | + * @param array $parameters |
|
| 629 | + * @param string $affectedUser |
|
| 630 | + * @param int $fileId |
|
| 631 | + * @param string $filePath |
|
| 632 | + */ |
|
| 633 | + protected function publishActivity($subject, |
|
| 634 | + array $parameters, |
|
| 635 | + $affectedUser, |
|
| 636 | + $fileId, |
|
| 637 | + $filePath) { |
|
| 638 | + |
|
| 639 | + $event = $this->activityManager->generateEvent(); |
|
| 640 | + $event->setApp('files_sharing') |
|
| 641 | + ->setType('public_links') |
|
| 642 | + ->setSubject($subject, $parameters) |
|
| 643 | + ->setAffectedUser($affectedUser) |
|
| 644 | + ->setObject('files', $fileId, $filePath); |
|
| 645 | + $this->activityManager->publish($event); |
|
| 646 | + } |
|
| 647 | 647 | |
| 648 | 648 | |
| 649 | 649 | } |
@@ -225,7 +225,7 @@ |
||
| 225 | 225 | /** |
| 226 | 226 | * creates a array with all user data |
| 227 | 227 | * |
| 228 | - * @param $userId |
|
| 228 | + * @param string $userId |
|
| 229 | 229 | * @return array |
| 230 | 230 | * @throws OCSException |
| 231 | 231 | */ |
@@ -335,7 +335,7 @@ |
||
| 335 | 335 | } |
| 336 | 336 | if($quota === 0) { |
| 337 | 337 | $quota = 'default'; |
| 338 | - }else if($quota === -1) { |
|
| 338 | + } else if($quota === -1) { |
|
| 339 | 339 | $quota = 'none'; |
| 340 | 340 | } else { |
| 341 | 341 | $quota = \OCP\Util::humanFileSize($quota); |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | // Admin? Or SubAdmin? |
| 124 | 124 | $uid = $user->getUID(); |
| 125 | 125 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 126 | - if($this->groupManager->isAdmin($uid)){ |
|
| 126 | + if ($this->groupManager->isAdmin($uid)) { |
|
| 127 | 127 | $users = $this->userManager->search($search, $limit, $offset); |
| 128 | 128 | } else if ($subAdminManager->isSubAdmin($user)) { |
| 129 | 129 | $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | $subAdminOfGroups[$key] = $group->getGID(); |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if($offset === null) { |
|
| 134 | + if ($offset === null) { |
|
| 135 | 135 | $offset = 0; |
| 136 | 136 | } |
| 137 | 137 | |
@@ -165,22 +165,22 @@ discard block |
||
| 165 | 165 | $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
| 166 | 166 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 167 | 167 | |
| 168 | - if($this->userManager->userExists($userid)) { |
|
| 168 | + if ($this->userManager->userExists($userid)) { |
|
| 169 | 169 | $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
| 170 | 170 | throw new OCSException('User already exists', 102); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - if(is_array($groups)) { |
|
| 173 | + if (is_array($groups)) { |
|
| 174 | 174 | foreach ($groups as $group) { |
| 175 | - if(!$this->groupManager->groupExists($group)) { |
|
| 175 | + if (!$this->groupManager->groupExists($group)) { |
|
| 176 | 176 | throw new OCSException('group '.$group.' does not exist', 104); |
| 177 | 177 | } |
| 178 | - if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 179 | - throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 178 | + if (!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 179 | + throw new OCSException('insufficient privileges for group '.$group, 105); |
|
| 180 | 180 | } |
| 181 | 181 | } |
| 182 | 182 | } else { |
| 183 | - if(!$isAdmin) { |
|
| 183 | + if (!$isAdmin) { |
|
| 184 | 184 | throw new OCSException('no group specified (required for subadmins)', 106); |
| 185 | 185 | } |
| 186 | 186 | } |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | public function getCurrentUser() { |
| 234 | 234 | $user = $this->userSession->getUser(); |
| 235 | 235 | if ($user) { |
| 236 | - $data = $this->getUserData($user->getUID()); |
|
| 236 | + $data = $this->getUserData($user->getUID()); |
|
| 237 | 237 | // rename "displayname" to "display-name" only for this call to keep |
| 238 | 238 | // the API stable. |
| 239 | 239 | $data['display-name'] = $data['displayname']; |
@@ -259,17 +259,17 @@ discard block |
||
| 259 | 259 | |
| 260 | 260 | // Check if the target user exists |
| 261 | 261 | $targetUserObject = $this->userManager->get($userId); |
| 262 | - if($targetUserObject === null) { |
|
| 262 | + if ($targetUserObject === null) { |
|
| 263 | 263 | throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | // Admin? Or SubAdmin? |
| 267 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 267 | + if ($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 268 | 268 | || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
| 269 | 269 | $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
| 270 | 270 | } else { |
| 271 | 271 | // Check they are looking up themselves |
| 272 | - if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 272 | + if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 273 | 273 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 274 | 274 | } |
| 275 | 275 | } |
@@ -314,12 +314,12 @@ discard block |
||
| 314 | 314 | $currentLoggedInUser = $this->userSession->getUser(); |
| 315 | 315 | |
| 316 | 316 | $targetUser = $this->userManager->get($userId); |
| 317 | - if($targetUser === null) { |
|
| 317 | + if ($targetUser === null) { |
|
| 318 | 318 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | $permittedFields = []; |
| 322 | - if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 322 | + if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 323 | 323 | // Editing self (display, email) |
| 324 | 324 | if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
| 325 | 325 | $permittedFields[] = 'display'; |
@@ -345,13 +345,13 @@ discard block |
||
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | // If admin they can edit their own quota |
| 348 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 348 | + if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 349 | 349 | $permittedFields[] = 'quota'; |
| 350 | 350 | } |
| 351 | 351 | } else { |
| 352 | 352 | // Check if admin / subadmin |
| 353 | 353 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 354 | - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 354 | + if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 355 | 355 | || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
| 356 | 356 | // They have permissions over the user |
| 357 | 357 | $permittedFields[] = 'display'; |
@@ -370,18 +370,18 @@ discard block |
||
| 370 | 370 | } |
| 371 | 371 | } |
| 372 | 372 | // Check if permitted to edit this field |
| 373 | - if(!in_array($key, $permittedFields)) { |
|
| 373 | + if (!in_array($key, $permittedFields)) { |
|
| 374 | 374 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 375 | 375 | } |
| 376 | 376 | // Process the edit |
| 377 | - switch($key) { |
|
| 377 | + switch ($key) { |
|
| 378 | 378 | case 'display': |
| 379 | 379 | case AccountManager::PROPERTY_DISPLAYNAME: |
| 380 | 380 | $targetUser->setDisplayName($value); |
| 381 | 381 | break; |
| 382 | 382 | case 'quota': |
| 383 | 383 | $quota = $value; |
| 384 | - if($quota !== 'none' && $quota !== 'default') { |
|
| 384 | + if ($quota !== 'none' && $quota !== 'default') { |
|
| 385 | 385 | if (is_numeric($quota)) { |
| 386 | 386 | $quota = (float) $quota; |
| 387 | 387 | } else { |
@@ -390,9 +390,9 @@ discard block |
||
| 390 | 390 | if ($quota === false) { |
| 391 | 391 | throw new OCSException('Invalid quota value '.$value, 103); |
| 392 | 392 | } |
| 393 | - if($quota === 0) { |
|
| 393 | + if ($quota === 0) { |
|
| 394 | 394 | $quota = 'default'; |
| 395 | - }else if($quota === -1) { |
|
| 395 | + } else if ($quota === -1) { |
|
| 396 | 396 | $quota = 'none'; |
| 397 | 397 | } else { |
| 398 | 398 | $quota = \OCP\Util::humanFileSize($quota); |
@@ -411,7 +411,7 @@ discard block |
||
| 411 | 411 | $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
| 412 | 412 | break; |
| 413 | 413 | case AccountManager::PROPERTY_EMAIL: |
| 414 | - if(filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 414 | + if (filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 415 | 415 | $targetUser->setEMailAddress($value); |
| 416 | 416 | } else { |
| 417 | 417 | throw new OCSException('', 102); |
@@ -447,18 +447,18 @@ discard block |
||
| 447 | 447 | |
| 448 | 448 | $targetUser = $this->userManager->get($userId); |
| 449 | 449 | |
| 450 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 450 | + if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 451 | 451 | throw new OCSException('', 101); |
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | // If not permitted |
| 455 | 455 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 456 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 456 | + if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 457 | 457 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | 460 | // Go ahead with the delete |
| 461 | - if($targetUser->delete()) { |
|
| 461 | + if ($targetUser->delete()) { |
|
| 462 | 462 | return new DataResponse(); |
| 463 | 463 | } else { |
| 464 | 464 | throw new OCSException('', 101); |
@@ -502,13 +502,13 @@ discard block |
||
| 502 | 502 | $currentLoggedInUser = $this->userSession->getUser(); |
| 503 | 503 | |
| 504 | 504 | $targetUser = $this->userManager->get($userId); |
| 505 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 505 | + if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 506 | 506 | throw new OCSException('', 101); |
| 507 | 507 | } |
| 508 | 508 | |
| 509 | 509 | // If not permitted |
| 510 | 510 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 511 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 511 | + if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 512 | 512 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
| 513 | 513 | } |
| 514 | 514 | |
@@ -529,11 +529,11 @@ discard block |
||
| 529 | 529 | $loggedInUser = $this->userSession->getUser(); |
| 530 | 530 | |
| 531 | 531 | $targetUser = $this->userManager->get($userId); |
| 532 | - if($targetUser === null) { |
|
| 532 | + if ($targetUser === null) { |
|
| 533 | 533 | throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
| 534 | 534 | } |
| 535 | 535 | |
| 536 | - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 536 | + if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 537 | 537 | // Self lookup or admin lookup |
| 538 | 538 | return new DataResponse([ |
| 539 | 539 | 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 543 | 543 | |
| 544 | 544 | // Looking up someone else |
| 545 | - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 545 | + if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 546 | 546 | // Return the group that the method caller is subadmin of for the user in question |
| 547 | 547 | /** @var IGroup[] $getSubAdminsGroups */ |
| 548 | 548 | $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
@@ -572,16 +572,16 @@ discard block |
||
| 572 | 572 | * @throws OCSException |
| 573 | 573 | */ |
| 574 | 574 | public function addToGroup($userId, $groupid = '') { |
| 575 | - if($groupid === '') { |
|
| 575 | + if ($groupid === '') { |
|
| 576 | 576 | throw new OCSException('', 101); |
| 577 | 577 | } |
| 578 | 578 | |
| 579 | 579 | $group = $this->groupManager->get($groupid); |
| 580 | 580 | $targetUser = $this->userManager->get($userId); |
| 581 | - if($group === null) { |
|
| 581 | + if ($group === null) { |
|
| 582 | 582 | throw new OCSException('', 102); |
| 583 | 583 | } |
| 584 | - if($targetUser === null) { |
|
| 584 | + if ($targetUser === null) { |
|
| 585 | 585 | throw new OCSException('', 103); |
| 586 | 586 | } |
| 587 | 587 | |
@@ -609,17 +609,17 @@ discard block |
||
| 609 | 609 | public function removeFromGroup($userId, $groupid) { |
| 610 | 610 | $loggedInUser = $this->userSession->getUser(); |
| 611 | 611 | |
| 612 | - if($groupid === null || trim($groupid) === '') { |
|
| 612 | + if ($groupid === null || trim($groupid) === '') { |
|
| 613 | 613 | throw new OCSException('', 101); |
| 614 | 614 | } |
| 615 | 615 | |
| 616 | 616 | $group = $this->groupManager->get($groupid); |
| 617 | - if($group === null) { |
|
| 617 | + if ($group === null) { |
|
| 618 | 618 | throw new OCSException('', 102); |
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | $targetUser = $this->userManager->get($userId); |
| 622 | - if($targetUser === null) { |
|
| 622 | + if ($targetUser === null) { |
|
| 623 | 623 | throw new OCSException('', 103); |
| 624 | 624 | } |
| 625 | 625 | |
@@ -643,7 +643,7 @@ discard block |
||
| 643 | 643 | } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
| 644 | 644 | /** @var IGroup[] $subAdminGroups */ |
| 645 | 645 | $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
| 646 | - $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 646 | + $subAdminGroups = array_map(function(IGroup $subAdminGroup) { |
|
| 647 | 647 | return $subAdminGroup->getGID(); |
| 648 | 648 | }, $subAdminGroups); |
| 649 | 649 | $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
@@ -675,15 +675,15 @@ discard block |
||
| 675 | 675 | $user = $this->userManager->get($userId); |
| 676 | 676 | |
| 677 | 677 | // Check if the user exists |
| 678 | - if($user === null) { |
|
| 678 | + if ($user === null) { |
|
| 679 | 679 | throw new OCSException('User does not exist', 101); |
| 680 | 680 | } |
| 681 | 681 | // Check if group exists |
| 682 | - if($group === null) { |
|
| 683 | - throw new OCSException('Group does not exist', 102); |
|
| 682 | + if ($group === null) { |
|
| 683 | + throw new OCSException('Group does not exist', 102); |
|
| 684 | 684 | } |
| 685 | 685 | // Check if trying to make subadmin of admin group |
| 686 | - if($group->getGID() === 'admin') { |
|
| 686 | + if ($group->getGID() === 'admin') { |
|
| 687 | 687 | throw new OCSException('Cannot create subadmins for admin group', 103); |
| 688 | 688 | } |
| 689 | 689 | |
@@ -694,7 +694,7 @@ discard block |
||
| 694 | 694 | return new DataResponse(); |
| 695 | 695 | } |
| 696 | 696 | // Go |
| 697 | - if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 697 | + if ($subAdminManager->createSubAdmin($user, $group)) { |
|
| 698 | 698 | return new DataResponse(); |
| 699 | 699 | } else { |
| 700 | 700 | throw new OCSException('Unknown error occurred', 103); |
@@ -717,20 +717,20 @@ discard block |
||
| 717 | 717 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 718 | 718 | |
| 719 | 719 | // Check if the user exists |
| 720 | - if($user === null) { |
|
| 720 | + if ($user === null) { |
|
| 721 | 721 | throw new OCSException('User does not exist', 101); |
| 722 | 722 | } |
| 723 | 723 | // Check if the group exists |
| 724 | - if($group === null) { |
|
| 724 | + if ($group === null) { |
|
| 725 | 725 | throw new OCSException('Group does not exist', 101); |
| 726 | 726 | } |
| 727 | 727 | // Check if they are a subadmin of this said group |
| 728 | - if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 728 | + if (!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 729 | 729 | throw new OCSException('User is not a subadmin of this group', 102); |
| 730 | 730 | } |
| 731 | 731 | |
| 732 | 732 | // Go |
| 733 | - if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 733 | + if ($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 734 | 734 | return new DataResponse(); |
| 735 | 735 | } else { |
| 736 | 736 | throw new OCSException('Unknown error occurred', 103); |
@@ -747,7 +747,7 @@ discard block |
||
| 747 | 747 | public function getUserSubAdminGroups($userId) { |
| 748 | 748 | $user = $this->userManager->get($userId); |
| 749 | 749 | // Check if the user exists |
| 750 | - if($user === null) { |
|
| 750 | + if ($user === null) { |
|
| 751 | 751 | throw new OCSException('User does not exist', 101); |
| 752 | 752 | } |
| 753 | 753 | |
@@ -757,7 +757,7 @@ discard block |
||
| 757 | 757 | $groups[$key] = $group->getGID(); |
| 758 | 758 | } |
| 759 | 759 | |
| 760 | - if(!$groups) { |
|
| 760 | + if (!$groups) { |
|
| 761 | 761 | throw new OCSException('Unknown error occurred', 102); |
| 762 | 762 | } else { |
| 763 | 763 | return new DataResponse($groups); |
@@ -801,13 +801,13 @@ discard block |
||
| 801 | 801 | $currentLoggedInUser = $this->userSession->getUser(); |
| 802 | 802 | |
| 803 | 803 | $targetUser = $this->userManager->get($userId); |
| 804 | - if($targetUser === null) { |
|
| 804 | + if ($targetUser === null) { |
|
| 805 | 805 | throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
| 806 | 806 | } |
| 807 | 807 | |
| 808 | 808 | // Check if admin / subadmin |
| 809 | 809 | $subAdminManager = $this->groupManager->getSubAdmin(); |
| 810 | - if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 810 | + if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 811 | 811 | && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
| 812 | 812 | // No rights |
| 813 | 813 | throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
@@ -829,7 +829,7 @@ discard block |
||
| 829 | 829 | $this->newUserMailHelper->setL10N($l10n); |
| 830 | 830 | $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
| 831 | 831 | $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
| 832 | - } catch(\Exception $e) { |
|
| 832 | + } catch (\Exception $e) { |
|
| 833 | 833 | $this->logger->logException($e, [ |
| 834 | 834 | 'message' => "Can't send new user mail to $email", |
| 835 | 835 | 'level' => \OCP\Util::ERROR, |
@@ -51,826 +51,826 @@ |
||
| 51 | 51 | |
| 52 | 52 | class UsersController extends OCSController { |
| 53 | 53 | |
| 54 | - /** @var IUserManager */ |
|
| 55 | - private $userManager; |
|
| 56 | - /** @var IConfig */ |
|
| 57 | - private $config; |
|
| 58 | - /** @var IAppManager */ |
|
| 59 | - private $appManager; |
|
| 60 | - /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
| 61 | - private $groupManager; |
|
| 62 | - /** @var IUserSession */ |
|
| 63 | - private $userSession; |
|
| 64 | - /** @var AccountManager */ |
|
| 65 | - private $accountManager; |
|
| 66 | - /** @var ILogger */ |
|
| 67 | - private $logger; |
|
| 68 | - /** @var IFactory */ |
|
| 69 | - private $l10nFactory; |
|
| 70 | - /** @var NewUserMailHelper */ |
|
| 71 | - private $newUserMailHelper; |
|
| 72 | - /** @var FederatedFileSharingFactory */ |
|
| 73 | - private $federatedFileSharingFactory; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $appName |
|
| 77 | - * @param IRequest $request |
|
| 78 | - * @param IUserManager $userManager |
|
| 79 | - * @param IConfig $config |
|
| 80 | - * @param IAppManager $appManager |
|
| 81 | - * @param IGroupManager $groupManager |
|
| 82 | - * @param IUserSession $userSession |
|
| 83 | - * @param AccountManager $accountManager |
|
| 84 | - * @param ILogger $logger |
|
| 85 | - * @param IFactory $l10nFactory |
|
| 86 | - * @param NewUserMailHelper $newUserMailHelper |
|
| 87 | - * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
| 88 | - */ |
|
| 89 | - public function __construct($appName, |
|
| 90 | - IRequest $request, |
|
| 91 | - IUserManager $userManager, |
|
| 92 | - IConfig $config, |
|
| 93 | - IAppManager $appManager, |
|
| 94 | - IGroupManager $groupManager, |
|
| 95 | - IUserSession $userSession, |
|
| 96 | - AccountManager $accountManager, |
|
| 97 | - ILogger $logger, |
|
| 98 | - IFactory $l10nFactory, |
|
| 99 | - NewUserMailHelper $newUserMailHelper, |
|
| 100 | - FederatedFileSharingFactory $federatedFileSharingFactory) { |
|
| 101 | - parent::__construct($appName, $request); |
|
| 102 | - |
|
| 103 | - $this->userManager = $userManager; |
|
| 104 | - $this->config = $config; |
|
| 105 | - $this->appManager = $appManager; |
|
| 106 | - $this->groupManager = $groupManager; |
|
| 107 | - $this->userSession = $userSession; |
|
| 108 | - $this->accountManager = $accountManager; |
|
| 109 | - $this->logger = $logger; |
|
| 110 | - $this->l10nFactory = $l10nFactory; |
|
| 111 | - $this->newUserMailHelper = $newUserMailHelper; |
|
| 112 | - $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @NoAdminRequired |
|
| 117 | - * |
|
| 118 | - * returns a list of users |
|
| 119 | - * |
|
| 120 | - * @param string $search |
|
| 121 | - * @param int $limit |
|
| 122 | - * @param int $offset |
|
| 123 | - * @return DataResponse |
|
| 124 | - */ |
|
| 125 | - public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 126 | - $user = $this->userSession->getUser(); |
|
| 127 | - $users = []; |
|
| 128 | - |
|
| 129 | - // Admin? Or SubAdmin? |
|
| 130 | - $uid = $user->getUID(); |
|
| 131 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 132 | - if($this->groupManager->isAdmin($uid)){ |
|
| 133 | - $users = $this->userManager->search($search, $limit, $offset); |
|
| 134 | - } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 135 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 136 | - foreach ($subAdminOfGroups as $key => $group) { |
|
| 137 | - $subAdminOfGroups[$key] = $group->getGID(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if($offset === null) { |
|
| 141 | - $offset = 0; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $users = []; |
|
| 145 | - foreach ($subAdminOfGroups as $group) { |
|
| 146 | - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search)); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $users = array_slice($users, $offset, $limit); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $users = array_keys($users); |
|
| 153 | - |
|
| 154 | - return new DataResponse([ |
|
| 155 | - 'users' => $users |
|
| 156 | - ]); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @PasswordConfirmationRequired |
|
| 161 | - * @NoAdminRequired |
|
| 162 | - * |
|
| 163 | - * @param string $userid |
|
| 164 | - * @param string $password |
|
| 165 | - * @param array $groups |
|
| 166 | - * @return DataResponse |
|
| 167 | - * @throws OCSException |
|
| 168 | - */ |
|
| 169 | - public function addUser($userid, $password, $groups = null) { |
|
| 170 | - $user = $this->userSession->getUser(); |
|
| 171 | - $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
| 172 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 173 | - |
|
| 174 | - if($this->userManager->userExists($userid)) { |
|
| 175 | - $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
| 176 | - throw new OCSException('User already exists', 102); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - if(is_array($groups)) { |
|
| 180 | - foreach ($groups as $group) { |
|
| 181 | - if(!$this->groupManager->groupExists($group)) { |
|
| 182 | - throw new OCSException('group '.$group.' does not exist', 104); |
|
| 183 | - } |
|
| 184 | - if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 185 | - throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } else { |
|
| 189 | - if(!$isAdmin) { |
|
| 190 | - throw new OCSException('no group specified (required for subadmins)', 106); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - try { |
|
| 195 | - $newUser = $this->userManager->createUser($userid, $password); |
|
| 196 | - $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']); |
|
| 197 | - |
|
| 198 | - if (is_array($groups)) { |
|
| 199 | - foreach ($groups as $group) { |
|
| 200 | - $this->groupManager->get($group)->addUser($newUser); |
|
| 201 | - $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - return new DataResponse(); |
|
| 205 | - } catch (\Exception $e) { |
|
| 206 | - $this->logger->logException($e, [ |
|
| 207 | - 'message' => 'Failed addUser attempt with exception.', |
|
| 208 | - 'level' => \OCP\Util::ERROR, |
|
| 209 | - 'app' => 'ocs_api', |
|
| 210 | - ]); |
|
| 211 | - throw new OCSException('Bad request', 101); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @NoAdminRequired |
|
| 217 | - * @NoSubAdminRequired |
|
| 218 | - * |
|
| 219 | - * gets user info |
|
| 220 | - * |
|
| 221 | - * @param string $userId |
|
| 222 | - * @return DataResponse |
|
| 223 | - * @throws OCSException |
|
| 224 | - */ |
|
| 225 | - public function getUser($userId) { |
|
| 226 | - $data = $this->getUserData($userId); |
|
| 227 | - return new DataResponse($data); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @NoAdminRequired |
|
| 232 | - * @NoSubAdminRequired |
|
| 233 | - * |
|
| 234 | - * gets user info from the currently logged in user |
|
| 235 | - * |
|
| 236 | - * @return DataResponse |
|
| 237 | - * @throws OCSException |
|
| 238 | - */ |
|
| 239 | - public function getCurrentUser() { |
|
| 240 | - $user = $this->userSession->getUser(); |
|
| 241 | - if ($user) { |
|
| 242 | - $data = $this->getUserData($user->getUID()); |
|
| 243 | - // rename "displayname" to "display-name" only for this call to keep |
|
| 244 | - // the API stable. |
|
| 245 | - $data['display-name'] = $data['displayname']; |
|
| 246 | - unset($data['displayname']); |
|
| 247 | - return new DataResponse($data); |
|
| 248 | - |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * creates a array with all user data |
|
| 256 | - * |
|
| 257 | - * @param $userId |
|
| 258 | - * @return array |
|
| 259 | - * @throws OCSException |
|
| 260 | - */ |
|
| 261 | - protected function getUserData($userId) { |
|
| 262 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 263 | - |
|
| 264 | - $data = []; |
|
| 265 | - |
|
| 266 | - // Check if the target user exists |
|
| 267 | - $targetUserObject = $this->userManager->get($userId); |
|
| 268 | - if($targetUserObject === null) { |
|
| 269 | - throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // Admin? Or SubAdmin? |
|
| 273 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 274 | - || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
| 275 | - $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
| 276 | - } else { |
|
| 277 | - // Check they are looking up themselves |
|
| 278 | - if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 279 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $userAccount = $this->accountManager->getUser($targetUserObject); |
|
| 284 | - $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
| 285 | - $gids = []; |
|
| 286 | - foreach ($groups as $group) { |
|
| 287 | - $gids[] = $group->getDisplayName(); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - // Find the data |
|
| 291 | - $data['id'] = $targetUserObject->getUID(); |
|
| 292 | - $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
| 293 | - $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
| 294 | - $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
| 295 | - $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
| 296 | - $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
| 297 | - $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 298 | - $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 299 | - $data['groups'] = $gids; |
|
| 300 | - $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
| 301 | - |
|
| 302 | - return $data; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * @NoAdminRequired |
|
| 307 | - * @NoSubAdminRequired |
|
| 308 | - */ |
|
| 309 | - public function getEditableFields() { |
|
| 310 | - $permittedFields = []; |
|
| 311 | - |
|
| 312 | - // Editing self (display, email) |
|
| 313 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 314 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 315 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 319 | - $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
| 320 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 321 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 322 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 323 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 324 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 325 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - return new DataResponse($permittedFields); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @NoAdminRequired |
|
| 334 | - * @NoSubAdminRequired |
|
| 335 | - * @PasswordConfirmationRequired |
|
| 336 | - * |
|
| 337 | - * edit users |
|
| 338 | - * |
|
| 339 | - * @param string $userId |
|
| 340 | - * @param string $key |
|
| 341 | - * @param string $value |
|
| 342 | - * @return DataResponse |
|
| 343 | - * @throws OCSException |
|
| 344 | - * @throws OCSForbiddenException |
|
| 345 | - */ |
|
| 346 | - public function editUser($userId, $key, $value) { |
|
| 347 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 348 | - |
|
| 349 | - $targetUser = $this->userManager->get($userId); |
|
| 350 | - if($targetUser === null) { |
|
| 351 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $permittedFields = []; |
|
| 355 | - if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 356 | - // Editing self (display, email) |
|
| 357 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 358 | - $permittedFields[] = 'display'; |
|
| 359 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 360 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - $permittedFields[] = 'password'; |
|
| 364 | - if ($this->config->getSystemValue('force_language', false) === false || |
|
| 365 | - $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 366 | - $permittedFields[] = 'language'; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 370 | - $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 371 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 372 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 373 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 374 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 375 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 376 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - // If admin they can edit their own quota |
|
| 381 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 382 | - $permittedFields[] = 'quota'; |
|
| 383 | - } |
|
| 384 | - } else { |
|
| 385 | - // Check if admin / subadmin |
|
| 386 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 387 | - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 388 | - || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 389 | - // They have permissions over the user |
|
| 390 | - $permittedFields[] = 'display'; |
|
| 391 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 392 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 393 | - $permittedFields[] = 'password'; |
|
| 394 | - $permittedFields[] = 'language'; |
|
| 395 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 396 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 397 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 398 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 399 | - $permittedFields[] = 'quota'; |
|
| 400 | - } else { |
|
| 401 | - // No rights |
|
| 402 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - // Check if permitted to edit this field |
|
| 406 | - if(!in_array($key, $permittedFields)) { |
|
| 407 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 408 | - } |
|
| 409 | - // Process the edit |
|
| 410 | - switch($key) { |
|
| 411 | - case 'display': |
|
| 412 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 413 | - $targetUser->setDisplayName($value); |
|
| 414 | - break; |
|
| 415 | - case 'quota': |
|
| 416 | - $quota = $value; |
|
| 417 | - if($quota !== 'none' && $quota !== 'default') { |
|
| 418 | - if (is_numeric($quota)) { |
|
| 419 | - $quota = (float) $quota; |
|
| 420 | - } else { |
|
| 421 | - $quota = \OCP\Util::computerFileSize($quota); |
|
| 422 | - } |
|
| 423 | - if ($quota === false) { |
|
| 424 | - throw new OCSException('Invalid quota value '.$value, 103); |
|
| 425 | - } |
|
| 426 | - if($quota === 0) { |
|
| 427 | - $quota = 'default'; |
|
| 428 | - }else if($quota === -1) { |
|
| 429 | - $quota = 'none'; |
|
| 430 | - } else { |
|
| 431 | - $quota = \OCP\Util::humanFileSize($quota); |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - $targetUser->setQuota($quota); |
|
| 435 | - break; |
|
| 436 | - case 'password': |
|
| 437 | - $targetUser->setPassword($value); |
|
| 438 | - break; |
|
| 439 | - case 'language': |
|
| 440 | - $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 441 | - if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
| 442 | - throw new OCSException('Invalid language', 102); |
|
| 443 | - } |
|
| 444 | - $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
| 445 | - break; |
|
| 446 | - case AccountManager::PROPERTY_EMAIL: |
|
| 447 | - if(filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 448 | - $targetUser->setEMailAddress($value); |
|
| 449 | - } else { |
|
| 450 | - throw new OCSException('', 102); |
|
| 451 | - } |
|
| 452 | - break; |
|
| 453 | - case AccountManager::PROPERTY_PHONE: |
|
| 454 | - case AccountManager::PROPERTY_ADDRESS: |
|
| 455 | - case AccountManager::PROPERTY_WEBSITE: |
|
| 456 | - case AccountManager::PROPERTY_TWITTER: |
|
| 457 | - $userAccount = $this->accountManager->getUser($targetUser); |
|
| 458 | - if ($userAccount[$key]['value'] !== $value) { |
|
| 459 | - $userAccount[$key]['value'] = $value; |
|
| 460 | - $this->accountManager->updateUser($targetUser, $userAccount); |
|
| 461 | - } |
|
| 462 | - break; |
|
| 463 | - default: |
|
| 464 | - throw new OCSException('', 103); |
|
| 465 | - } |
|
| 466 | - return new DataResponse(); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * @PasswordConfirmationRequired |
|
| 471 | - * @NoAdminRequired |
|
| 472 | - * |
|
| 473 | - * @param string $userId |
|
| 474 | - * @return DataResponse |
|
| 475 | - * @throws OCSException |
|
| 476 | - * @throws OCSForbiddenException |
|
| 477 | - */ |
|
| 478 | - public function deleteUser($userId) { |
|
| 479 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 480 | - |
|
| 481 | - $targetUser = $this->userManager->get($userId); |
|
| 482 | - |
|
| 483 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 484 | - throw new OCSException('', 101); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - // If not permitted |
|
| 488 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 489 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 490 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - // Go ahead with the delete |
|
| 494 | - if($targetUser->delete()) { |
|
| 495 | - return new DataResponse(); |
|
| 496 | - } else { |
|
| 497 | - throw new OCSException('', 101); |
|
| 498 | - } |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * @PasswordConfirmationRequired |
|
| 503 | - * @NoAdminRequired |
|
| 504 | - * |
|
| 505 | - * @param string $userId |
|
| 506 | - * @return DataResponse |
|
| 507 | - * @throws OCSException |
|
| 508 | - * @throws OCSForbiddenException |
|
| 509 | - */ |
|
| 510 | - public function disableUser($userId) { |
|
| 511 | - return $this->setEnabled($userId, false); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * @PasswordConfirmationRequired |
|
| 516 | - * @NoAdminRequired |
|
| 517 | - * |
|
| 518 | - * @param string $userId |
|
| 519 | - * @return DataResponse |
|
| 520 | - * @throws OCSException |
|
| 521 | - * @throws OCSForbiddenException |
|
| 522 | - */ |
|
| 523 | - public function enableUser($userId) { |
|
| 524 | - return $this->setEnabled($userId, true); |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * @param string $userId |
|
| 529 | - * @param bool $value |
|
| 530 | - * @return DataResponse |
|
| 531 | - * @throws OCSException |
|
| 532 | - * @throws OCSForbiddenException |
|
| 533 | - */ |
|
| 534 | - private function setEnabled($userId, $value) { |
|
| 535 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 536 | - |
|
| 537 | - $targetUser = $this->userManager->get($userId); |
|
| 538 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 539 | - throw new OCSException('', 101); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - // If not permitted |
|
| 543 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 544 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 545 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - // enable/disable the user now |
|
| 549 | - $targetUser->setEnabled($value); |
|
| 550 | - return new DataResponse(); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * @NoAdminRequired |
|
| 555 | - * @NoSubAdminRequired |
|
| 556 | - * |
|
| 557 | - * @param string $userId |
|
| 558 | - * @return DataResponse |
|
| 559 | - * @throws OCSException |
|
| 560 | - */ |
|
| 561 | - public function getUsersGroups($userId) { |
|
| 562 | - $loggedInUser = $this->userSession->getUser(); |
|
| 563 | - |
|
| 564 | - $targetUser = $this->userManager->get($userId); |
|
| 565 | - if($targetUser === null) { |
|
| 566 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 570 | - // Self lookup or admin lookup |
|
| 571 | - return new DataResponse([ |
|
| 572 | - 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
| 573 | - ]); |
|
| 574 | - } else { |
|
| 575 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 576 | - |
|
| 577 | - // Looking up someone else |
|
| 578 | - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 579 | - // Return the group that the method caller is subadmin of for the user in question |
|
| 580 | - /** @var IGroup[] $getSubAdminsGroups */ |
|
| 581 | - $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 582 | - foreach ($getSubAdminsGroups as $key => $group) { |
|
| 583 | - $getSubAdminsGroups[$key] = $group->getGID(); |
|
| 584 | - } |
|
| 585 | - $groups = array_intersect( |
|
| 586 | - $getSubAdminsGroups, |
|
| 587 | - $this->groupManager->getUserGroupIds($targetUser) |
|
| 588 | - ); |
|
| 589 | - return new DataResponse(['groups' => $groups]); |
|
| 590 | - } else { |
|
| 591 | - // Not permitted |
|
| 592 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * @PasswordConfirmationRequired |
|
| 600 | - * @NoAdminRequired |
|
| 601 | - * |
|
| 602 | - * @param string $userId |
|
| 603 | - * @param string $groupid |
|
| 604 | - * @return DataResponse |
|
| 605 | - * @throws OCSException |
|
| 606 | - */ |
|
| 607 | - public function addToGroup($userId, $groupid = '') { |
|
| 608 | - if($groupid === '') { |
|
| 609 | - throw new OCSException('', 101); |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - $group = $this->groupManager->get($groupid); |
|
| 613 | - $targetUser = $this->userManager->get($userId); |
|
| 614 | - if($group === null) { |
|
| 615 | - throw new OCSException('', 102); |
|
| 616 | - } |
|
| 617 | - if($targetUser === null) { |
|
| 618 | - throw new OCSException('', 103); |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - // If they're not an admin, check they are a subadmin of the group in question |
|
| 622 | - $loggedInUser = $this->userSession->getUser(); |
|
| 623 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 624 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 625 | - throw new OCSException('', 104); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - // Add user to group |
|
| 629 | - $group->addUser($targetUser); |
|
| 630 | - return new DataResponse(); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * @PasswordConfirmationRequired |
|
| 635 | - * @NoAdminRequired |
|
| 636 | - * |
|
| 637 | - * @param string $userId |
|
| 638 | - * @param string $groupid |
|
| 639 | - * @return DataResponse |
|
| 640 | - * @throws OCSException |
|
| 641 | - */ |
|
| 642 | - public function removeFromGroup($userId, $groupid) { |
|
| 643 | - $loggedInUser = $this->userSession->getUser(); |
|
| 644 | - |
|
| 645 | - if($groupid === null || trim($groupid) === '') { |
|
| 646 | - throw new OCSException('', 101); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - $group = $this->groupManager->get($groupid); |
|
| 650 | - if($group === null) { |
|
| 651 | - throw new OCSException('', 102); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $targetUser = $this->userManager->get($userId); |
|
| 655 | - if($targetUser === null) { |
|
| 656 | - throw new OCSException('', 103); |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - // If they're not an admin, check they are a subadmin of the group in question |
|
| 660 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 661 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 662 | - throw new OCSException('', 104); |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
| 666 | - if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
| 667 | - if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 668 | - if ($group->getGID() === 'admin') { |
|
| 669 | - throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
| 670 | - } |
|
| 671 | - } else { |
|
| 672 | - // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
| 673 | - throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 677 | - /** @var IGroup[] $subAdminGroups */ |
|
| 678 | - $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 679 | - $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 680 | - return $subAdminGroup->getGID(); |
|
| 681 | - }, $subAdminGroups); |
|
| 682 | - $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
| 683 | - $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
| 684 | - |
|
| 685 | - if (count($userSubAdminGroups) <= 1) { |
|
| 686 | - // Subadmin must not be able to remove a user from all their subadmin groups. |
|
| 687 | - throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
| 688 | - } |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - // Remove user from group |
|
| 692 | - $group->removeUser($targetUser); |
|
| 693 | - return new DataResponse(); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * Creates a subadmin |
|
| 698 | - * |
|
| 699 | - * @PasswordConfirmationRequired |
|
| 700 | - * |
|
| 701 | - * @param string $userId |
|
| 702 | - * @param string $groupid |
|
| 703 | - * @return DataResponse |
|
| 704 | - * @throws OCSException |
|
| 705 | - */ |
|
| 706 | - public function addSubAdmin($userId, $groupid) { |
|
| 707 | - $group = $this->groupManager->get($groupid); |
|
| 708 | - $user = $this->userManager->get($userId); |
|
| 709 | - |
|
| 710 | - // Check if the user exists |
|
| 711 | - if($user === null) { |
|
| 712 | - throw new OCSException('User does not exist', 101); |
|
| 713 | - } |
|
| 714 | - // Check if group exists |
|
| 715 | - if($group === null) { |
|
| 716 | - throw new OCSException('Group does not exist', 102); |
|
| 717 | - } |
|
| 718 | - // Check if trying to make subadmin of admin group |
|
| 719 | - if($group->getGID() === 'admin') { |
|
| 720 | - throw new OCSException('Cannot create subadmins for admin group', 103); |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 724 | - |
|
| 725 | - // We cannot be subadmin twice |
|
| 726 | - if ($subAdminManager->isSubAdminofGroup($user, $group)) { |
|
| 727 | - return new DataResponse(); |
|
| 728 | - } |
|
| 729 | - // Go |
|
| 730 | - if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 731 | - return new DataResponse(); |
|
| 732 | - } else { |
|
| 733 | - throw new OCSException('Unknown error occurred', 103); |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Removes a subadmin from a group |
|
| 739 | - * |
|
| 740 | - * @PasswordConfirmationRequired |
|
| 741 | - * |
|
| 742 | - * @param string $userId |
|
| 743 | - * @param string $groupid |
|
| 744 | - * @return DataResponse |
|
| 745 | - * @throws OCSException |
|
| 746 | - */ |
|
| 747 | - public function removeSubAdmin($userId, $groupid) { |
|
| 748 | - $group = $this->groupManager->get($groupid); |
|
| 749 | - $user = $this->userManager->get($userId); |
|
| 750 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 751 | - |
|
| 752 | - // Check if the user exists |
|
| 753 | - if($user === null) { |
|
| 754 | - throw new OCSException('User does not exist', 101); |
|
| 755 | - } |
|
| 756 | - // Check if the group exists |
|
| 757 | - if($group === null) { |
|
| 758 | - throw new OCSException('Group does not exist', 101); |
|
| 759 | - } |
|
| 760 | - // Check if they are a subadmin of this said group |
|
| 761 | - if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 762 | - throw new OCSException('User is not a subadmin of this group', 102); |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - // Go |
|
| 766 | - if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 767 | - return new DataResponse(); |
|
| 768 | - } else { |
|
| 769 | - throw new OCSException('Unknown error occurred', 103); |
|
| 770 | - } |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Get the groups a user is a subadmin of |
|
| 775 | - * |
|
| 776 | - * @param string $userId |
|
| 777 | - * @return DataResponse |
|
| 778 | - * @throws OCSException |
|
| 779 | - */ |
|
| 780 | - public function getUserSubAdminGroups($userId) { |
|
| 781 | - $user = $this->userManager->get($userId); |
|
| 782 | - // Check if the user exists |
|
| 783 | - if($user === null) { |
|
| 784 | - throw new OCSException('User does not exist', 101); |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - // Get the subadmin groups |
|
| 788 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
| 789 | - foreach ($groups as $key => $group) { |
|
| 790 | - $groups[$key] = $group->getGID(); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - if(!$groups) { |
|
| 794 | - throw new OCSException('Unknown error occurred', 102); |
|
| 795 | - } else { |
|
| 796 | - return new DataResponse($groups); |
|
| 797 | - } |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * @param string $userId |
|
| 802 | - * @return array |
|
| 803 | - * @throws \OCP\Files\NotFoundException |
|
| 804 | - */ |
|
| 805 | - protected function fillStorageInfo($userId) { |
|
| 806 | - try { |
|
| 807 | - \OC_Util::tearDownFS(); |
|
| 808 | - \OC_Util::setupFS($userId); |
|
| 809 | - $storage = OC_Helper::getStorageInfo('/'); |
|
| 810 | - $data = [ |
|
| 811 | - 'free' => $storage['free'], |
|
| 812 | - 'used' => $storage['used'], |
|
| 813 | - 'total' => $storage['total'], |
|
| 814 | - 'relative' => $storage['relative'], |
|
| 815 | - 'quota' => $storage['quota'], |
|
| 816 | - ]; |
|
| 817 | - } catch (NotFoundException $ex) { |
|
| 818 | - $data = []; |
|
| 819 | - } |
|
| 820 | - return $data; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * @NoAdminRequired |
|
| 825 | - * @PasswordConfirmationRequired |
|
| 826 | - * |
|
| 827 | - * resend welcome message |
|
| 828 | - * |
|
| 829 | - * @param string $userId |
|
| 830 | - * @return DataResponse |
|
| 831 | - * @throws OCSException |
|
| 832 | - */ |
|
| 833 | - public function resendWelcomeMessage($userId) { |
|
| 834 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 835 | - |
|
| 836 | - $targetUser = $this->userManager->get($userId); |
|
| 837 | - if($targetUser === null) { |
|
| 838 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - // Check if admin / subadmin |
|
| 842 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 843 | - if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 844 | - && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 845 | - // No rights |
|
| 846 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - $email = $targetUser->getEMailAddress(); |
|
| 850 | - if ($email === '' || $email === null) { |
|
| 851 | - throw new OCSException('Email address not available', 101); |
|
| 852 | - } |
|
| 853 | - $username = $targetUser->getUID(); |
|
| 854 | - $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
| 855 | - if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
| 856 | - $lang = 'en'; |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - $l10n = $this->l10nFactory->get('settings', $lang); |
|
| 860 | - |
|
| 861 | - try { |
|
| 862 | - $this->newUserMailHelper->setL10N($l10n); |
|
| 863 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
| 864 | - $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
| 865 | - } catch(\Exception $e) { |
|
| 866 | - $this->logger->logException($e, [ |
|
| 867 | - 'message' => "Can't send new user mail to $email", |
|
| 868 | - 'level' => \OCP\Util::ERROR, |
|
| 869 | - 'app' => 'settings', |
|
| 870 | - ]); |
|
| 871 | - throw new OCSException('Sending email failed', 102); |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - return new DataResponse(); |
|
| 875 | - } |
|
| 54 | + /** @var IUserManager */ |
|
| 55 | + private $userManager; |
|
| 56 | + /** @var IConfig */ |
|
| 57 | + private $config; |
|
| 58 | + /** @var IAppManager */ |
|
| 59 | + private $appManager; |
|
| 60 | + /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
| 61 | + private $groupManager; |
|
| 62 | + /** @var IUserSession */ |
|
| 63 | + private $userSession; |
|
| 64 | + /** @var AccountManager */ |
|
| 65 | + private $accountManager; |
|
| 66 | + /** @var ILogger */ |
|
| 67 | + private $logger; |
|
| 68 | + /** @var IFactory */ |
|
| 69 | + private $l10nFactory; |
|
| 70 | + /** @var NewUserMailHelper */ |
|
| 71 | + private $newUserMailHelper; |
|
| 72 | + /** @var FederatedFileSharingFactory */ |
|
| 73 | + private $federatedFileSharingFactory; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $appName |
|
| 77 | + * @param IRequest $request |
|
| 78 | + * @param IUserManager $userManager |
|
| 79 | + * @param IConfig $config |
|
| 80 | + * @param IAppManager $appManager |
|
| 81 | + * @param IGroupManager $groupManager |
|
| 82 | + * @param IUserSession $userSession |
|
| 83 | + * @param AccountManager $accountManager |
|
| 84 | + * @param ILogger $logger |
|
| 85 | + * @param IFactory $l10nFactory |
|
| 86 | + * @param NewUserMailHelper $newUserMailHelper |
|
| 87 | + * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
| 88 | + */ |
|
| 89 | + public function __construct($appName, |
|
| 90 | + IRequest $request, |
|
| 91 | + IUserManager $userManager, |
|
| 92 | + IConfig $config, |
|
| 93 | + IAppManager $appManager, |
|
| 94 | + IGroupManager $groupManager, |
|
| 95 | + IUserSession $userSession, |
|
| 96 | + AccountManager $accountManager, |
|
| 97 | + ILogger $logger, |
|
| 98 | + IFactory $l10nFactory, |
|
| 99 | + NewUserMailHelper $newUserMailHelper, |
|
| 100 | + FederatedFileSharingFactory $federatedFileSharingFactory) { |
|
| 101 | + parent::__construct($appName, $request); |
|
| 102 | + |
|
| 103 | + $this->userManager = $userManager; |
|
| 104 | + $this->config = $config; |
|
| 105 | + $this->appManager = $appManager; |
|
| 106 | + $this->groupManager = $groupManager; |
|
| 107 | + $this->userSession = $userSession; |
|
| 108 | + $this->accountManager = $accountManager; |
|
| 109 | + $this->logger = $logger; |
|
| 110 | + $this->l10nFactory = $l10nFactory; |
|
| 111 | + $this->newUserMailHelper = $newUserMailHelper; |
|
| 112 | + $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @NoAdminRequired |
|
| 117 | + * |
|
| 118 | + * returns a list of users |
|
| 119 | + * |
|
| 120 | + * @param string $search |
|
| 121 | + * @param int $limit |
|
| 122 | + * @param int $offset |
|
| 123 | + * @return DataResponse |
|
| 124 | + */ |
|
| 125 | + public function getUsers($search = '', $limit = null, $offset = null) { |
|
| 126 | + $user = $this->userSession->getUser(); |
|
| 127 | + $users = []; |
|
| 128 | + |
|
| 129 | + // Admin? Or SubAdmin? |
|
| 130 | + $uid = $user->getUID(); |
|
| 131 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 132 | + if($this->groupManager->isAdmin($uid)){ |
|
| 133 | + $users = $this->userManager->search($search, $limit, $offset); |
|
| 134 | + } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 135 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 136 | + foreach ($subAdminOfGroups as $key => $group) { |
|
| 137 | + $subAdminOfGroups[$key] = $group->getGID(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if($offset === null) { |
|
| 141 | + $offset = 0; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $users = []; |
|
| 145 | + foreach ($subAdminOfGroups as $group) { |
|
| 146 | + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search)); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $users = array_slice($users, $offset, $limit); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $users = array_keys($users); |
|
| 153 | + |
|
| 154 | + return new DataResponse([ |
|
| 155 | + 'users' => $users |
|
| 156 | + ]); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @PasswordConfirmationRequired |
|
| 161 | + * @NoAdminRequired |
|
| 162 | + * |
|
| 163 | + * @param string $userid |
|
| 164 | + * @param string $password |
|
| 165 | + * @param array $groups |
|
| 166 | + * @return DataResponse |
|
| 167 | + * @throws OCSException |
|
| 168 | + */ |
|
| 169 | + public function addUser($userid, $password, $groups = null) { |
|
| 170 | + $user = $this->userSession->getUser(); |
|
| 171 | + $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
| 172 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 173 | + |
|
| 174 | + if($this->userManager->userExists($userid)) { |
|
| 175 | + $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
| 176 | + throw new OCSException('User already exists', 102); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + if(is_array($groups)) { |
|
| 180 | + foreach ($groups as $group) { |
|
| 181 | + if(!$this->groupManager->groupExists($group)) { |
|
| 182 | + throw new OCSException('group '.$group.' does not exist', 104); |
|
| 183 | + } |
|
| 184 | + if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { |
|
| 185 | + throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } else { |
|
| 189 | + if(!$isAdmin) { |
|
| 190 | + throw new OCSException('no group specified (required for subadmins)', 106); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + try { |
|
| 195 | + $newUser = $this->userManager->createUser($userid, $password); |
|
| 196 | + $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']); |
|
| 197 | + |
|
| 198 | + if (is_array($groups)) { |
|
| 199 | + foreach ($groups as $group) { |
|
| 200 | + $this->groupManager->get($group)->addUser($newUser); |
|
| 201 | + $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + return new DataResponse(); |
|
| 205 | + } catch (\Exception $e) { |
|
| 206 | + $this->logger->logException($e, [ |
|
| 207 | + 'message' => 'Failed addUser attempt with exception.', |
|
| 208 | + 'level' => \OCP\Util::ERROR, |
|
| 209 | + 'app' => 'ocs_api', |
|
| 210 | + ]); |
|
| 211 | + throw new OCSException('Bad request', 101); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @NoAdminRequired |
|
| 217 | + * @NoSubAdminRequired |
|
| 218 | + * |
|
| 219 | + * gets user info |
|
| 220 | + * |
|
| 221 | + * @param string $userId |
|
| 222 | + * @return DataResponse |
|
| 223 | + * @throws OCSException |
|
| 224 | + */ |
|
| 225 | + public function getUser($userId) { |
|
| 226 | + $data = $this->getUserData($userId); |
|
| 227 | + return new DataResponse($data); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @NoAdminRequired |
|
| 232 | + * @NoSubAdminRequired |
|
| 233 | + * |
|
| 234 | + * gets user info from the currently logged in user |
|
| 235 | + * |
|
| 236 | + * @return DataResponse |
|
| 237 | + * @throws OCSException |
|
| 238 | + */ |
|
| 239 | + public function getCurrentUser() { |
|
| 240 | + $user = $this->userSession->getUser(); |
|
| 241 | + if ($user) { |
|
| 242 | + $data = $this->getUserData($user->getUID()); |
|
| 243 | + // rename "displayname" to "display-name" only for this call to keep |
|
| 244 | + // the API stable. |
|
| 245 | + $data['display-name'] = $data['displayname']; |
|
| 246 | + unset($data['displayname']); |
|
| 247 | + return new DataResponse($data); |
|
| 248 | + |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * creates a array with all user data |
|
| 256 | + * |
|
| 257 | + * @param $userId |
|
| 258 | + * @return array |
|
| 259 | + * @throws OCSException |
|
| 260 | + */ |
|
| 261 | + protected function getUserData($userId) { |
|
| 262 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 263 | + |
|
| 264 | + $data = []; |
|
| 265 | + |
|
| 266 | + // Check if the target user exists |
|
| 267 | + $targetUserObject = $this->userManager->get($userId); |
|
| 268 | + if($targetUserObject === null) { |
|
| 269 | + throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // Admin? Or SubAdmin? |
|
| 273 | + if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 274 | + || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
| 275 | + $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
| 276 | + } else { |
|
| 277 | + // Check they are looking up themselves |
|
| 278 | + if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 279 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $userAccount = $this->accountManager->getUser($targetUserObject); |
|
| 284 | + $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
| 285 | + $gids = []; |
|
| 286 | + foreach ($groups as $group) { |
|
| 287 | + $gids[] = $group->getDisplayName(); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + // Find the data |
|
| 291 | + $data['id'] = $targetUserObject->getUID(); |
|
| 292 | + $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
| 293 | + $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
| 294 | + $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
| 295 | + $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
| 296 | + $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
| 297 | + $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 298 | + $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 299 | + $data['groups'] = $gids; |
|
| 300 | + $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
| 301 | + |
|
| 302 | + return $data; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * @NoAdminRequired |
|
| 307 | + * @NoSubAdminRequired |
|
| 308 | + */ |
|
| 309 | + public function getEditableFields() { |
|
| 310 | + $permittedFields = []; |
|
| 311 | + |
|
| 312 | + // Editing self (display, email) |
|
| 313 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 314 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 315 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 319 | + $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
| 320 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 321 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 322 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 323 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 324 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 325 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + return new DataResponse($permittedFields); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @NoAdminRequired |
|
| 334 | + * @NoSubAdminRequired |
|
| 335 | + * @PasswordConfirmationRequired |
|
| 336 | + * |
|
| 337 | + * edit users |
|
| 338 | + * |
|
| 339 | + * @param string $userId |
|
| 340 | + * @param string $key |
|
| 341 | + * @param string $value |
|
| 342 | + * @return DataResponse |
|
| 343 | + * @throws OCSException |
|
| 344 | + * @throws OCSForbiddenException |
|
| 345 | + */ |
|
| 346 | + public function editUser($userId, $key, $value) { |
|
| 347 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 348 | + |
|
| 349 | + $targetUser = $this->userManager->get($userId); |
|
| 350 | + if($targetUser === null) { |
|
| 351 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $permittedFields = []; |
|
| 355 | + if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 356 | + // Editing self (display, email) |
|
| 357 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 358 | + $permittedFields[] = 'display'; |
|
| 359 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 360 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + $permittedFields[] = 'password'; |
|
| 364 | + if ($this->config->getSystemValue('force_language', false) === false || |
|
| 365 | + $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 366 | + $permittedFields[] = 'language'; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 370 | + $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 371 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 372 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 373 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 374 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 375 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 376 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + // If admin they can edit their own quota |
|
| 381 | + if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 382 | + $permittedFields[] = 'quota'; |
|
| 383 | + } |
|
| 384 | + } else { |
|
| 385 | + // Check if admin / subadmin |
|
| 386 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 387 | + if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 388 | + || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 389 | + // They have permissions over the user |
|
| 390 | + $permittedFields[] = 'display'; |
|
| 391 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 392 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 393 | + $permittedFields[] = 'password'; |
|
| 394 | + $permittedFields[] = 'language'; |
|
| 395 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 396 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 397 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 398 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 399 | + $permittedFields[] = 'quota'; |
|
| 400 | + } else { |
|
| 401 | + // No rights |
|
| 402 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + // Check if permitted to edit this field |
|
| 406 | + if(!in_array($key, $permittedFields)) { |
|
| 407 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 408 | + } |
|
| 409 | + // Process the edit |
|
| 410 | + switch($key) { |
|
| 411 | + case 'display': |
|
| 412 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 413 | + $targetUser->setDisplayName($value); |
|
| 414 | + break; |
|
| 415 | + case 'quota': |
|
| 416 | + $quota = $value; |
|
| 417 | + if($quota !== 'none' && $quota !== 'default') { |
|
| 418 | + if (is_numeric($quota)) { |
|
| 419 | + $quota = (float) $quota; |
|
| 420 | + } else { |
|
| 421 | + $quota = \OCP\Util::computerFileSize($quota); |
|
| 422 | + } |
|
| 423 | + if ($quota === false) { |
|
| 424 | + throw new OCSException('Invalid quota value '.$value, 103); |
|
| 425 | + } |
|
| 426 | + if($quota === 0) { |
|
| 427 | + $quota = 'default'; |
|
| 428 | + }else if($quota === -1) { |
|
| 429 | + $quota = 'none'; |
|
| 430 | + } else { |
|
| 431 | + $quota = \OCP\Util::humanFileSize($quota); |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + $targetUser->setQuota($quota); |
|
| 435 | + break; |
|
| 436 | + case 'password': |
|
| 437 | + $targetUser->setPassword($value); |
|
| 438 | + break; |
|
| 439 | + case 'language': |
|
| 440 | + $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 441 | + if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
| 442 | + throw new OCSException('Invalid language', 102); |
|
| 443 | + } |
|
| 444 | + $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
| 445 | + break; |
|
| 446 | + case AccountManager::PROPERTY_EMAIL: |
|
| 447 | + if(filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 448 | + $targetUser->setEMailAddress($value); |
|
| 449 | + } else { |
|
| 450 | + throw new OCSException('', 102); |
|
| 451 | + } |
|
| 452 | + break; |
|
| 453 | + case AccountManager::PROPERTY_PHONE: |
|
| 454 | + case AccountManager::PROPERTY_ADDRESS: |
|
| 455 | + case AccountManager::PROPERTY_WEBSITE: |
|
| 456 | + case AccountManager::PROPERTY_TWITTER: |
|
| 457 | + $userAccount = $this->accountManager->getUser($targetUser); |
|
| 458 | + if ($userAccount[$key]['value'] !== $value) { |
|
| 459 | + $userAccount[$key]['value'] = $value; |
|
| 460 | + $this->accountManager->updateUser($targetUser, $userAccount); |
|
| 461 | + } |
|
| 462 | + break; |
|
| 463 | + default: |
|
| 464 | + throw new OCSException('', 103); |
|
| 465 | + } |
|
| 466 | + return new DataResponse(); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * @PasswordConfirmationRequired |
|
| 471 | + * @NoAdminRequired |
|
| 472 | + * |
|
| 473 | + * @param string $userId |
|
| 474 | + * @return DataResponse |
|
| 475 | + * @throws OCSException |
|
| 476 | + * @throws OCSForbiddenException |
|
| 477 | + */ |
|
| 478 | + public function deleteUser($userId) { |
|
| 479 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 480 | + |
|
| 481 | + $targetUser = $this->userManager->get($userId); |
|
| 482 | + |
|
| 483 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 484 | + throw new OCSException('', 101); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + // If not permitted |
|
| 488 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 489 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 490 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + // Go ahead with the delete |
|
| 494 | + if($targetUser->delete()) { |
|
| 495 | + return new DataResponse(); |
|
| 496 | + } else { |
|
| 497 | + throw new OCSException('', 101); |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * @PasswordConfirmationRequired |
|
| 503 | + * @NoAdminRequired |
|
| 504 | + * |
|
| 505 | + * @param string $userId |
|
| 506 | + * @return DataResponse |
|
| 507 | + * @throws OCSException |
|
| 508 | + * @throws OCSForbiddenException |
|
| 509 | + */ |
|
| 510 | + public function disableUser($userId) { |
|
| 511 | + return $this->setEnabled($userId, false); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * @PasswordConfirmationRequired |
|
| 516 | + * @NoAdminRequired |
|
| 517 | + * |
|
| 518 | + * @param string $userId |
|
| 519 | + * @return DataResponse |
|
| 520 | + * @throws OCSException |
|
| 521 | + * @throws OCSForbiddenException |
|
| 522 | + */ |
|
| 523 | + public function enableUser($userId) { |
|
| 524 | + return $this->setEnabled($userId, true); |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * @param string $userId |
|
| 529 | + * @param bool $value |
|
| 530 | + * @return DataResponse |
|
| 531 | + * @throws OCSException |
|
| 532 | + * @throws OCSForbiddenException |
|
| 533 | + */ |
|
| 534 | + private function setEnabled($userId, $value) { |
|
| 535 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 536 | + |
|
| 537 | + $targetUser = $this->userManager->get($userId); |
|
| 538 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 539 | + throw new OCSException('', 101); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + // If not permitted |
|
| 543 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 544 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 545 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + // enable/disable the user now |
|
| 549 | + $targetUser->setEnabled($value); |
|
| 550 | + return new DataResponse(); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * @NoAdminRequired |
|
| 555 | + * @NoSubAdminRequired |
|
| 556 | + * |
|
| 557 | + * @param string $userId |
|
| 558 | + * @return DataResponse |
|
| 559 | + * @throws OCSException |
|
| 560 | + */ |
|
| 561 | + public function getUsersGroups($userId) { |
|
| 562 | + $loggedInUser = $this->userSession->getUser(); |
|
| 563 | + |
|
| 564 | + $targetUser = $this->userManager->get($userId); |
|
| 565 | + if($targetUser === null) { |
|
| 566 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 570 | + // Self lookup or admin lookup |
|
| 571 | + return new DataResponse([ |
|
| 572 | + 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
| 573 | + ]); |
|
| 574 | + } else { |
|
| 575 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 576 | + |
|
| 577 | + // Looking up someone else |
|
| 578 | + if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 579 | + // Return the group that the method caller is subadmin of for the user in question |
|
| 580 | + /** @var IGroup[] $getSubAdminsGroups */ |
|
| 581 | + $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 582 | + foreach ($getSubAdminsGroups as $key => $group) { |
|
| 583 | + $getSubAdminsGroups[$key] = $group->getGID(); |
|
| 584 | + } |
|
| 585 | + $groups = array_intersect( |
|
| 586 | + $getSubAdminsGroups, |
|
| 587 | + $this->groupManager->getUserGroupIds($targetUser) |
|
| 588 | + ); |
|
| 589 | + return new DataResponse(['groups' => $groups]); |
|
| 590 | + } else { |
|
| 591 | + // Not permitted |
|
| 592 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * @PasswordConfirmationRequired |
|
| 600 | + * @NoAdminRequired |
|
| 601 | + * |
|
| 602 | + * @param string $userId |
|
| 603 | + * @param string $groupid |
|
| 604 | + * @return DataResponse |
|
| 605 | + * @throws OCSException |
|
| 606 | + */ |
|
| 607 | + public function addToGroup($userId, $groupid = '') { |
|
| 608 | + if($groupid === '') { |
|
| 609 | + throw new OCSException('', 101); |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + $group = $this->groupManager->get($groupid); |
|
| 613 | + $targetUser = $this->userManager->get($userId); |
|
| 614 | + if($group === null) { |
|
| 615 | + throw new OCSException('', 102); |
|
| 616 | + } |
|
| 617 | + if($targetUser === null) { |
|
| 618 | + throw new OCSException('', 103); |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + // If they're not an admin, check they are a subadmin of the group in question |
|
| 622 | + $loggedInUser = $this->userSession->getUser(); |
|
| 623 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 624 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 625 | + throw new OCSException('', 104); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + // Add user to group |
|
| 629 | + $group->addUser($targetUser); |
|
| 630 | + return new DataResponse(); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * @PasswordConfirmationRequired |
|
| 635 | + * @NoAdminRequired |
|
| 636 | + * |
|
| 637 | + * @param string $userId |
|
| 638 | + * @param string $groupid |
|
| 639 | + * @return DataResponse |
|
| 640 | + * @throws OCSException |
|
| 641 | + */ |
|
| 642 | + public function removeFromGroup($userId, $groupid) { |
|
| 643 | + $loggedInUser = $this->userSession->getUser(); |
|
| 644 | + |
|
| 645 | + if($groupid === null || trim($groupid) === '') { |
|
| 646 | + throw new OCSException('', 101); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + $group = $this->groupManager->get($groupid); |
|
| 650 | + if($group === null) { |
|
| 651 | + throw new OCSException('', 102); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $targetUser = $this->userManager->get($userId); |
|
| 655 | + if($targetUser === null) { |
|
| 656 | + throw new OCSException('', 103); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + // If they're not an admin, check they are a subadmin of the group in question |
|
| 660 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 661 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 662 | + throw new OCSException('', 104); |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
| 666 | + if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
| 667 | + if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 668 | + if ($group->getGID() === 'admin') { |
|
| 669 | + throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
| 670 | + } |
|
| 671 | + } else { |
|
| 672 | + // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
| 673 | + throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 677 | + /** @var IGroup[] $subAdminGroups */ |
|
| 678 | + $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 679 | + $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 680 | + return $subAdminGroup->getGID(); |
|
| 681 | + }, $subAdminGroups); |
|
| 682 | + $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
| 683 | + $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
| 684 | + |
|
| 685 | + if (count($userSubAdminGroups) <= 1) { |
|
| 686 | + // Subadmin must not be able to remove a user from all their subadmin groups. |
|
| 687 | + throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
| 688 | + } |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + // Remove user from group |
|
| 692 | + $group->removeUser($targetUser); |
|
| 693 | + return new DataResponse(); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * Creates a subadmin |
|
| 698 | + * |
|
| 699 | + * @PasswordConfirmationRequired |
|
| 700 | + * |
|
| 701 | + * @param string $userId |
|
| 702 | + * @param string $groupid |
|
| 703 | + * @return DataResponse |
|
| 704 | + * @throws OCSException |
|
| 705 | + */ |
|
| 706 | + public function addSubAdmin($userId, $groupid) { |
|
| 707 | + $group = $this->groupManager->get($groupid); |
|
| 708 | + $user = $this->userManager->get($userId); |
|
| 709 | + |
|
| 710 | + // Check if the user exists |
|
| 711 | + if($user === null) { |
|
| 712 | + throw new OCSException('User does not exist', 101); |
|
| 713 | + } |
|
| 714 | + // Check if group exists |
|
| 715 | + if($group === null) { |
|
| 716 | + throw new OCSException('Group does not exist', 102); |
|
| 717 | + } |
|
| 718 | + // Check if trying to make subadmin of admin group |
|
| 719 | + if($group->getGID() === 'admin') { |
|
| 720 | + throw new OCSException('Cannot create subadmins for admin group', 103); |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 724 | + |
|
| 725 | + // We cannot be subadmin twice |
|
| 726 | + if ($subAdminManager->isSubAdminofGroup($user, $group)) { |
|
| 727 | + return new DataResponse(); |
|
| 728 | + } |
|
| 729 | + // Go |
|
| 730 | + if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 731 | + return new DataResponse(); |
|
| 732 | + } else { |
|
| 733 | + throw new OCSException('Unknown error occurred', 103); |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Removes a subadmin from a group |
|
| 739 | + * |
|
| 740 | + * @PasswordConfirmationRequired |
|
| 741 | + * |
|
| 742 | + * @param string $userId |
|
| 743 | + * @param string $groupid |
|
| 744 | + * @return DataResponse |
|
| 745 | + * @throws OCSException |
|
| 746 | + */ |
|
| 747 | + public function removeSubAdmin($userId, $groupid) { |
|
| 748 | + $group = $this->groupManager->get($groupid); |
|
| 749 | + $user = $this->userManager->get($userId); |
|
| 750 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 751 | + |
|
| 752 | + // Check if the user exists |
|
| 753 | + if($user === null) { |
|
| 754 | + throw new OCSException('User does not exist', 101); |
|
| 755 | + } |
|
| 756 | + // Check if the group exists |
|
| 757 | + if($group === null) { |
|
| 758 | + throw new OCSException('Group does not exist', 101); |
|
| 759 | + } |
|
| 760 | + // Check if they are a subadmin of this said group |
|
| 761 | + if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 762 | + throw new OCSException('User is not a subadmin of this group', 102); |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + // Go |
|
| 766 | + if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 767 | + return new DataResponse(); |
|
| 768 | + } else { |
|
| 769 | + throw new OCSException('Unknown error occurred', 103); |
|
| 770 | + } |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Get the groups a user is a subadmin of |
|
| 775 | + * |
|
| 776 | + * @param string $userId |
|
| 777 | + * @return DataResponse |
|
| 778 | + * @throws OCSException |
|
| 779 | + */ |
|
| 780 | + public function getUserSubAdminGroups($userId) { |
|
| 781 | + $user = $this->userManager->get($userId); |
|
| 782 | + // Check if the user exists |
|
| 783 | + if($user === null) { |
|
| 784 | + throw new OCSException('User does not exist', 101); |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + // Get the subadmin groups |
|
| 788 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
| 789 | + foreach ($groups as $key => $group) { |
|
| 790 | + $groups[$key] = $group->getGID(); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + if(!$groups) { |
|
| 794 | + throw new OCSException('Unknown error occurred', 102); |
|
| 795 | + } else { |
|
| 796 | + return new DataResponse($groups); |
|
| 797 | + } |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * @param string $userId |
|
| 802 | + * @return array |
|
| 803 | + * @throws \OCP\Files\NotFoundException |
|
| 804 | + */ |
|
| 805 | + protected function fillStorageInfo($userId) { |
|
| 806 | + try { |
|
| 807 | + \OC_Util::tearDownFS(); |
|
| 808 | + \OC_Util::setupFS($userId); |
|
| 809 | + $storage = OC_Helper::getStorageInfo('/'); |
|
| 810 | + $data = [ |
|
| 811 | + 'free' => $storage['free'], |
|
| 812 | + 'used' => $storage['used'], |
|
| 813 | + 'total' => $storage['total'], |
|
| 814 | + 'relative' => $storage['relative'], |
|
| 815 | + 'quota' => $storage['quota'], |
|
| 816 | + ]; |
|
| 817 | + } catch (NotFoundException $ex) { |
|
| 818 | + $data = []; |
|
| 819 | + } |
|
| 820 | + return $data; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * @NoAdminRequired |
|
| 825 | + * @PasswordConfirmationRequired |
|
| 826 | + * |
|
| 827 | + * resend welcome message |
|
| 828 | + * |
|
| 829 | + * @param string $userId |
|
| 830 | + * @return DataResponse |
|
| 831 | + * @throws OCSException |
|
| 832 | + */ |
|
| 833 | + public function resendWelcomeMessage($userId) { |
|
| 834 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 835 | + |
|
| 836 | + $targetUser = $this->userManager->get($userId); |
|
| 837 | + if($targetUser === null) { |
|
| 838 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + // Check if admin / subadmin |
|
| 842 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 843 | + if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 844 | + && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 845 | + // No rights |
|
| 846 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + $email = $targetUser->getEMailAddress(); |
|
| 850 | + if ($email === '' || $email === null) { |
|
| 851 | + throw new OCSException('Email address not available', 101); |
|
| 852 | + } |
|
| 853 | + $username = $targetUser->getUID(); |
|
| 854 | + $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
| 855 | + if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
| 856 | + $lang = 'en'; |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + $l10n = $this->l10nFactory->get('settings', $lang); |
|
| 860 | + |
|
| 861 | + try { |
|
| 862 | + $this->newUserMailHelper->setL10N($l10n); |
|
| 863 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
| 864 | + $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
| 865 | + } catch(\Exception $e) { |
|
| 866 | + $this->logger->logException($e, [ |
|
| 867 | + 'message' => "Can't send new user mail to $email", |
|
| 868 | + 'level' => \OCP\Util::ERROR, |
|
| 869 | + 'app' => 'settings', |
|
| 870 | + ]); |
|
| 871 | + throw new OCSException('Sending email failed', 102); |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + return new DataResponse(); |
|
| 875 | + } |
|
| 876 | 876 | } |
@@ -194,6 +194,9 @@ |
||
| 194 | 194 | return $this->getCache()->getStatus($this->getSourcePath($file)); |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | + /** |
|
| 198 | + * @param ICacheEntry[] $results |
|
| 199 | + */ |
|
| 197 | 200 | private function formatSearchResults($results) { |
| 198 | 201 | $results = array_filter($results, array($this, 'filterCacheEntry')); |
| 199 | 202 | $results = array_values($results); |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | if ($path === '') { |
| 59 | 59 | return $this->getRoot(); |
| 60 | 60 | } else { |
| 61 | - return $this->getRoot() . '/' . ltrim($path, '/'); |
|
| 61 | + return $this->getRoot().'/'.ltrim($path, '/'); |
|
| 62 | 62 | } |
| 63 | 63 | } |
| 64 | 64 | |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | $rootLength = strlen($this->getRoot()) + 1; |
| 74 | 74 | if ($path === $this->getRoot()) { |
| 75 | 75 | return ''; |
| 76 | - } else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') { |
|
| 76 | + } else if (substr($path, 0, $rootLength) === $this->getRoot().'/') { |
|
| 77 | 77 | return substr($path, $rootLength); |
| 78 | 78 | } else { |
| 79 | 79 | return null; |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | protected function filterCacheEntry($entry) { |
| 95 | 95 | $rootLength = strlen($this->getRoot()) + 1; |
| 96 | - return ($entry['path'] === $this->getRoot()) or (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/'); |
|
| 96 | + return ($entry['path'] === $this->getRoot()) or (substr($entry['path'], 0, $rootLength) === $this->getRoot().'/'); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | /** |
@@ -37,308 +37,308 @@ |
||
| 37 | 37 | * Jail to a subdirectory of the wrapped cache |
| 38 | 38 | */ |
| 39 | 39 | class CacheJail extends CacheWrapper { |
| 40 | - /** |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - protected $root; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param \OCP\Files\Cache\ICache $cache |
|
| 47 | - * @param string $root |
|
| 48 | - */ |
|
| 49 | - public function __construct($cache, $root) { |
|
| 50 | - parent::__construct($cache); |
|
| 51 | - $this->root = $root; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - protected function getRoot() { |
|
| 55 | - return $this->root; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - protected function getSourcePath($path) { |
|
| 59 | - if ($path === '') { |
|
| 60 | - return $this->getRoot(); |
|
| 61 | - } else { |
|
| 62 | - return $this->getRoot() . '/' . ltrim($path, '/'); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param string $path |
|
| 68 | - * @return null|string the jailed path or null if the path is outside the jail |
|
| 69 | - */ |
|
| 70 | - protected function getJailedPath($path) { |
|
| 71 | - if ($this->getRoot() === '') { |
|
| 72 | - return $path; |
|
| 73 | - } |
|
| 74 | - $rootLength = strlen($this->getRoot()) + 1; |
|
| 75 | - if ($path === $this->getRoot()) { |
|
| 76 | - return ''; |
|
| 77 | - } else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') { |
|
| 78 | - return substr($path, $rootLength); |
|
| 79 | - } else { |
|
| 80 | - return null; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param ICacheEntry|array $entry |
|
| 86 | - * @return array |
|
| 87 | - */ |
|
| 88 | - protected function formatCacheEntry($entry) { |
|
| 89 | - if (isset($entry['path'])) { |
|
| 90 | - $entry['path'] = $this->getJailedPath($entry['path']); |
|
| 91 | - } |
|
| 92 | - return $entry; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - protected function filterCacheEntry($entry) { |
|
| 96 | - $rootLength = strlen($this->getRoot()) + 1; |
|
| 97 | - return ($entry['path'] === $this->getRoot()) or (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/'); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * get the stored metadata of a file or folder |
|
| 102 | - * |
|
| 103 | - * @param string /int $file |
|
| 104 | - * @return ICacheEntry|false |
|
| 105 | - */ |
|
| 106 | - public function get($file) { |
|
| 107 | - if (is_string($file) or $file == '') { |
|
| 108 | - $file = $this->getSourcePath($file); |
|
| 109 | - } |
|
| 110 | - return parent::get($file); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * insert meta data for a new file or folder |
|
| 115 | - * |
|
| 116 | - * @param string $file |
|
| 117 | - * @param array $data |
|
| 118 | - * |
|
| 119 | - * @return int file id |
|
| 120 | - * @throws \RuntimeException |
|
| 121 | - */ |
|
| 122 | - public function insert($file, array $data) { |
|
| 123 | - return $this->getCache()->insert($this->getSourcePath($file), $data); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * update the metadata in the cache |
|
| 128 | - * |
|
| 129 | - * @param int $id |
|
| 130 | - * @param array $data |
|
| 131 | - */ |
|
| 132 | - public function update($id, array $data) { |
|
| 133 | - $this->getCache()->update($id, $data); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * get the file id for a file |
|
| 138 | - * |
|
| 139 | - * @param string $file |
|
| 140 | - * @return int |
|
| 141 | - */ |
|
| 142 | - public function getId($file) { |
|
| 143 | - return $this->getCache()->getId($this->getSourcePath($file)); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * get the id of the parent folder of a file |
|
| 148 | - * |
|
| 149 | - * @param string $file |
|
| 150 | - * @return int |
|
| 151 | - */ |
|
| 152 | - public function getParentId($file) { |
|
| 153 | - return $this->getCache()->getParentId($this->getSourcePath($file)); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * check if a file is available in the cache |
|
| 158 | - * |
|
| 159 | - * @param string $file |
|
| 160 | - * @return bool |
|
| 161 | - */ |
|
| 162 | - public function inCache($file) { |
|
| 163 | - return $this->getCache()->inCache($this->getSourcePath($file)); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * remove a file or folder from the cache |
|
| 168 | - * |
|
| 169 | - * @param string $file |
|
| 170 | - */ |
|
| 171 | - public function remove($file) { |
|
| 172 | - $this->getCache()->remove($this->getSourcePath($file)); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Move a file or folder in the cache |
|
| 177 | - * |
|
| 178 | - * @param string $source |
|
| 179 | - * @param string $target |
|
| 180 | - */ |
|
| 181 | - public function move($source, $target) { |
|
| 182 | - $this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target)); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Get the storage id and path needed for a move |
|
| 187 | - * |
|
| 188 | - * @param string $path |
|
| 189 | - * @return array [$storageId, $internalPath] |
|
| 190 | - */ |
|
| 191 | - protected function getMoveInfo($path) { |
|
| 192 | - return [$this->getNumericStorageId(), $this->getSourcePath($path)]; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * remove all entries for files that are stored on the storage from the cache |
|
| 197 | - */ |
|
| 198 | - public function clear() { |
|
| 199 | - $this->getCache()->remove($this->getRoot()); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @param string $file |
|
| 204 | - * |
|
| 205 | - * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
| 206 | - */ |
|
| 207 | - public function getStatus($file) { |
|
| 208 | - return $this->getCache()->getStatus($this->getSourcePath($file)); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - private function formatSearchResults($results) { |
|
| 212 | - $results = array_filter($results, array($this, 'filterCacheEntry')); |
|
| 213 | - $results = array_values($results); |
|
| 214 | - return array_map(array($this, 'formatCacheEntry'), $results); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * search for files matching $pattern |
|
| 219 | - * |
|
| 220 | - * @param string $pattern |
|
| 221 | - * @return array an array of file data |
|
| 222 | - */ |
|
| 223 | - public function search($pattern) { |
|
| 224 | - $results = $this->getCache()->search($pattern); |
|
| 225 | - return $this->formatSearchResults($results); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * search for files by mimetype |
|
| 230 | - * |
|
| 231 | - * @param string $mimetype |
|
| 232 | - * @return array |
|
| 233 | - */ |
|
| 234 | - public function searchByMime($mimetype) { |
|
| 235 | - $results = $this->getCache()->searchByMime($mimetype); |
|
| 236 | - return $this->formatSearchResults($results); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - public function searchQuery(ISearchQuery $query) { |
|
| 240 | - $simpleQuery = new SearchQuery($query->getSearchOperation(), 0, 0, $query->getOrder(), $query->getUser()); |
|
| 241 | - $results = $this->getCache()->searchQuery($simpleQuery); |
|
| 242 | - $results = $this->formatSearchResults($results); |
|
| 243 | - |
|
| 244 | - $limit = $query->getLimit() === 0 ? NULL : $query->getLimit(); |
|
| 245 | - $results = array_slice($results, $query->getOffset(), $limit); |
|
| 246 | - |
|
| 247 | - return $results; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * search for files by mimetype |
|
| 252 | - * |
|
| 253 | - * @param string|int $tag name or tag id |
|
| 254 | - * @param string $userId owner of the tags |
|
| 255 | - * @return array |
|
| 256 | - */ |
|
| 257 | - public function searchByTag($tag, $userId) { |
|
| 258 | - $results = $this->getCache()->searchByTag($tag, $userId); |
|
| 259 | - return $this->formatSearchResults($results); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * update the folder size and the size of all parent folders |
|
| 264 | - * |
|
| 265 | - * @param string|boolean $path |
|
| 266 | - * @param array $data (optional) meta data of the folder |
|
| 267 | - */ |
|
| 268 | - public function correctFolderSize($path, $data = null) { |
|
| 269 | - if ($this->getCache() instanceof Cache) { |
|
| 270 | - $this->getCache()->correctFolderSize($this->getSourcePath($path), $data); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * get the size of a folder and set it in the cache |
|
| 276 | - * |
|
| 277 | - * @param string $path |
|
| 278 | - * @param array $entry (optional) meta data of the folder |
|
| 279 | - * @return int |
|
| 280 | - */ |
|
| 281 | - public function calculateFolderSize($path, $entry = null) { |
|
| 282 | - if ($this->getCache() instanceof Cache) { |
|
| 283 | - return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry); |
|
| 284 | - } else { |
|
| 285 | - return 0; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * get all file ids on the files on the storage |
|
| 292 | - * |
|
| 293 | - * @return int[] |
|
| 294 | - */ |
|
| 295 | - public function getAll() { |
|
| 296 | - // not supported |
|
| 297 | - return array(); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * find a folder in the cache which has not been fully scanned |
|
| 302 | - * |
|
| 303 | - * If multiply incomplete folders are in the cache, the one with the highest id will be returned, |
|
| 304 | - * use the one with the highest id gives the best result with the background scanner, since that is most |
|
| 305 | - * likely the folder where we stopped scanning previously |
|
| 306 | - * |
|
| 307 | - * @return string|bool the path of the folder or false when no folder matched |
|
| 308 | - */ |
|
| 309 | - public function getIncomplete() { |
|
| 310 | - // not supported |
|
| 311 | - return false; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * get the path of a file on this storage by it's id |
|
| 316 | - * |
|
| 317 | - * @param int $id |
|
| 318 | - * @return string|null |
|
| 319 | - */ |
|
| 320 | - public function getPathById($id) { |
|
| 321 | - $path = $this->getCache()->getPathById($id); |
|
| 322 | - if ($path === null) { |
|
| 323 | - return null; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - return $this->getJailedPath($path); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Move a file or folder in the cache |
|
| 331 | - * |
|
| 332 | - * Note that this should make sure the entries are removed from the source cache |
|
| 333 | - * |
|
| 334 | - * @param \OCP\Files\Cache\ICache $sourceCache |
|
| 335 | - * @param string $sourcePath |
|
| 336 | - * @param string $targetPath |
|
| 337 | - */ |
|
| 338 | - public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) { |
|
| 339 | - if ($sourceCache === $this) { |
|
| 340 | - return $this->move($sourcePath, $targetPath); |
|
| 341 | - } |
|
| 342 | - return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath)); |
|
| 343 | - } |
|
| 40 | + /** |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + protected $root; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param \OCP\Files\Cache\ICache $cache |
|
| 47 | + * @param string $root |
|
| 48 | + */ |
|
| 49 | + public function __construct($cache, $root) { |
|
| 50 | + parent::__construct($cache); |
|
| 51 | + $this->root = $root; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + protected function getRoot() { |
|
| 55 | + return $this->root; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + protected function getSourcePath($path) { |
|
| 59 | + if ($path === '') { |
|
| 60 | + return $this->getRoot(); |
|
| 61 | + } else { |
|
| 62 | + return $this->getRoot() . '/' . ltrim($path, '/'); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param string $path |
|
| 68 | + * @return null|string the jailed path or null if the path is outside the jail |
|
| 69 | + */ |
|
| 70 | + protected function getJailedPath($path) { |
|
| 71 | + if ($this->getRoot() === '') { |
|
| 72 | + return $path; |
|
| 73 | + } |
|
| 74 | + $rootLength = strlen($this->getRoot()) + 1; |
|
| 75 | + if ($path === $this->getRoot()) { |
|
| 76 | + return ''; |
|
| 77 | + } else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') { |
|
| 78 | + return substr($path, $rootLength); |
|
| 79 | + } else { |
|
| 80 | + return null; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param ICacheEntry|array $entry |
|
| 86 | + * @return array |
|
| 87 | + */ |
|
| 88 | + protected function formatCacheEntry($entry) { |
|
| 89 | + if (isset($entry['path'])) { |
|
| 90 | + $entry['path'] = $this->getJailedPath($entry['path']); |
|
| 91 | + } |
|
| 92 | + return $entry; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + protected function filterCacheEntry($entry) { |
|
| 96 | + $rootLength = strlen($this->getRoot()) + 1; |
|
| 97 | + return ($entry['path'] === $this->getRoot()) or (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/'); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * get the stored metadata of a file or folder |
|
| 102 | + * |
|
| 103 | + * @param string /int $file |
|
| 104 | + * @return ICacheEntry|false |
|
| 105 | + */ |
|
| 106 | + public function get($file) { |
|
| 107 | + if (is_string($file) or $file == '') { |
|
| 108 | + $file = $this->getSourcePath($file); |
|
| 109 | + } |
|
| 110 | + return parent::get($file); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * insert meta data for a new file or folder |
|
| 115 | + * |
|
| 116 | + * @param string $file |
|
| 117 | + * @param array $data |
|
| 118 | + * |
|
| 119 | + * @return int file id |
|
| 120 | + * @throws \RuntimeException |
|
| 121 | + */ |
|
| 122 | + public function insert($file, array $data) { |
|
| 123 | + return $this->getCache()->insert($this->getSourcePath($file), $data); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * update the metadata in the cache |
|
| 128 | + * |
|
| 129 | + * @param int $id |
|
| 130 | + * @param array $data |
|
| 131 | + */ |
|
| 132 | + public function update($id, array $data) { |
|
| 133 | + $this->getCache()->update($id, $data); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * get the file id for a file |
|
| 138 | + * |
|
| 139 | + * @param string $file |
|
| 140 | + * @return int |
|
| 141 | + */ |
|
| 142 | + public function getId($file) { |
|
| 143 | + return $this->getCache()->getId($this->getSourcePath($file)); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * get the id of the parent folder of a file |
|
| 148 | + * |
|
| 149 | + * @param string $file |
|
| 150 | + * @return int |
|
| 151 | + */ |
|
| 152 | + public function getParentId($file) { |
|
| 153 | + return $this->getCache()->getParentId($this->getSourcePath($file)); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * check if a file is available in the cache |
|
| 158 | + * |
|
| 159 | + * @param string $file |
|
| 160 | + * @return bool |
|
| 161 | + */ |
|
| 162 | + public function inCache($file) { |
|
| 163 | + return $this->getCache()->inCache($this->getSourcePath($file)); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * remove a file or folder from the cache |
|
| 168 | + * |
|
| 169 | + * @param string $file |
|
| 170 | + */ |
|
| 171 | + public function remove($file) { |
|
| 172 | + $this->getCache()->remove($this->getSourcePath($file)); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Move a file or folder in the cache |
|
| 177 | + * |
|
| 178 | + * @param string $source |
|
| 179 | + * @param string $target |
|
| 180 | + */ |
|
| 181 | + public function move($source, $target) { |
|
| 182 | + $this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target)); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Get the storage id and path needed for a move |
|
| 187 | + * |
|
| 188 | + * @param string $path |
|
| 189 | + * @return array [$storageId, $internalPath] |
|
| 190 | + */ |
|
| 191 | + protected function getMoveInfo($path) { |
|
| 192 | + return [$this->getNumericStorageId(), $this->getSourcePath($path)]; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * remove all entries for files that are stored on the storage from the cache |
|
| 197 | + */ |
|
| 198 | + public function clear() { |
|
| 199 | + $this->getCache()->remove($this->getRoot()); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @param string $file |
|
| 204 | + * |
|
| 205 | + * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
| 206 | + */ |
|
| 207 | + public function getStatus($file) { |
|
| 208 | + return $this->getCache()->getStatus($this->getSourcePath($file)); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + private function formatSearchResults($results) { |
|
| 212 | + $results = array_filter($results, array($this, 'filterCacheEntry')); |
|
| 213 | + $results = array_values($results); |
|
| 214 | + return array_map(array($this, 'formatCacheEntry'), $results); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * search for files matching $pattern |
|
| 219 | + * |
|
| 220 | + * @param string $pattern |
|
| 221 | + * @return array an array of file data |
|
| 222 | + */ |
|
| 223 | + public function search($pattern) { |
|
| 224 | + $results = $this->getCache()->search($pattern); |
|
| 225 | + return $this->formatSearchResults($results); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * search for files by mimetype |
|
| 230 | + * |
|
| 231 | + * @param string $mimetype |
|
| 232 | + * @return array |
|
| 233 | + */ |
|
| 234 | + public function searchByMime($mimetype) { |
|
| 235 | + $results = $this->getCache()->searchByMime($mimetype); |
|
| 236 | + return $this->formatSearchResults($results); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + public function searchQuery(ISearchQuery $query) { |
|
| 240 | + $simpleQuery = new SearchQuery($query->getSearchOperation(), 0, 0, $query->getOrder(), $query->getUser()); |
|
| 241 | + $results = $this->getCache()->searchQuery($simpleQuery); |
|
| 242 | + $results = $this->formatSearchResults($results); |
|
| 243 | + |
|
| 244 | + $limit = $query->getLimit() === 0 ? NULL : $query->getLimit(); |
|
| 245 | + $results = array_slice($results, $query->getOffset(), $limit); |
|
| 246 | + |
|
| 247 | + return $results; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * search for files by mimetype |
|
| 252 | + * |
|
| 253 | + * @param string|int $tag name or tag id |
|
| 254 | + * @param string $userId owner of the tags |
|
| 255 | + * @return array |
|
| 256 | + */ |
|
| 257 | + public function searchByTag($tag, $userId) { |
|
| 258 | + $results = $this->getCache()->searchByTag($tag, $userId); |
|
| 259 | + return $this->formatSearchResults($results); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * update the folder size and the size of all parent folders |
|
| 264 | + * |
|
| 265 | + * @param string|boolean $path |
|
| 266 | + * @param array $data (optional) meta data of the folder |
|
| 267 | + */ |
|
| 268 | + public function correctFolderSize($path, $data = null) { |
|
| 269 | + if ($this->getCache() instanceof Cache) { |
|
| 270 | + $this->getCache()->correctFolderSize($this->getSourcePath($path), $data); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * get the size of a folder and set it in the cache |
|
| 276 | + * |
|
| 277 | + * @param string $path |
|
| 278 | + * @param array $entry (optional) meta data of the folder |
|
| 279 | + * @return int |
|
| 280 | + */ |
|
| 281 | + public function calculateFolderSize($path, $entry = null) { |
|
| 282 | + if ($this->getCache() instanceof Cache) { |
|
| 283 | + return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry); |
|
| 284 | + } else { |
|
| 285 | + return 0; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * get all file ids on the files on the storage |
|
| 292 | + * |
|
| 293 | + * @return int[] |
|
| 294 | + */ |
|
| 295 | + public function getAll() { |
|
| 296 | + // not supported |
|
| 297 | + return array(); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * find a folder in the cache which has not been fully scanned |
|
| 302 | + * |
|
| 303 | + * If multiply incomplete folders are in the cache, the one with the highest id will be returned, |
|
| 304 | + * use the one with the highest id gives the best result with the background scanner, since that is most |
|
| 305 | + * likely the folder where we stopped scanning previously |
|
| 306 | + * |
|
| 307 | + * @return string|bool the path of the folder or false when no folder matched |
|
| 308 | + */ |
|
| 309 | + public function getIncomplete() { |
|
| 310 | + // not supported |
|
| 311 | + return false; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * get the path of a file on this storage by it's id |
|
| 316 | + * |
|
| 317 | + * @param int $id |
|
| 318 | + * @return string|null |
|
| 319 | + */ |
|
| 320 | + public function getPathById($id) { |
|
| 321 | + $path = $this->getCache()->getPathById($id); |
|
| 322 | + if ($path === null) { |
|
| 323 | + return null; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + return $this->getJailedPath($path); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Move a file or folder in the cache |
|
| 331 | + * |
|
| 332 | + * Note that this should make sure the entries are removed from the source cache |
|
| 333 | + * |
|
| 334 | + * @param \OCP\Files\Cache\ICache $sourceCache |
|
| 335 | + * @param string $sourcePath |
|
| 336 | + * @param string $targetPath |
|
| 337 | + */ |
|
| 338 | + public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) { |
|
| 339 | + if ($sourceCache === $this) { |
|
| 340 | + return $this->move($sourcePath, $targetPath); |
|
| 341 | + } |
|
| 342 | + return $this->getCache()->moveFromCache($sourceCache, $sourcePath, $this->getSourcePath($targetPath)); |
|
| 343 | + } |
|
| 344 | 344 | } |