@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | * |
| 217 | 217 | * @param \Google_Service_Drive_DriveFile |
| 218 | 218 | * |
| 219 | - * @return true if the file is a Google Doc file, false otherwise |
|
| 219 | + * @return boolean if the file is a Google Doc file, false otherwise |
|
| 220 | 220 | */ |
| 221 | 221 | private function isGoogleDocFile($file) { |
| 222 | 222 | return $this->getGoogleDocExtension($file->getMimeType()) !== ''; |
@@ -505,6 +505,9 @@ discard block |
||
| 505 | 505 | } |
| 506 | 506 | } |
| 507 | 507 | |
| 508 | + /** |
|
| 509 | + * @param string $path |
|
| 510 | + */ |
|
| 508 | 511 | public function writeBack($tmpFile, $path) { |
| 509 | 512 | $parentFolder = $this->getDriveFile(dirname($path)); |
| 510 | 513 | if ($parentFolder) { |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | if (isset($this->driveFiles[$path])) { |
| 124 | 124 | $parentId = $this->driveFiles[$path]->getId(); |
| 125 | 125 | } else { |
| 126 | - $q = "title='" . str_replace("'","\\'", $name) . "' and '" . str_replace("'","\\'", $parentId) . "' in parents and trashed = false"; |
|
| 126 | + $q = "title='".str_replace("'", "\\'", $name)."' and '".str_replace("'", "\\'", $parentId)."' in parents and trashed = false"; |
|
| 127 | 127 | $result = $this->service->files->listFiles(array('q' => $q))->getItems(); |
| 128 | 128 | if (!empty($result)) { |
| 129 | 129 | // Google Drive allows files with the same name, Nextcloud doesn't |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | if ($result) { |
| 237 | 237 | $this->setDriveFile($path, $result); |
| 238 | 238 | } |
| 239 | - return (bool)$result; |
|
| 239 | + return (bool) $result; |
|
| 240 | 240 | } |
| 241 | 241 | } |
| 242 | 242 | return false; |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | } |
| 249 | 249 | if (trim($path, '/') === '') { |
| 250 | 250 | $dir = $this->opendir($path); |
| 251 | - if(is_resource($dir)) { |
|
| 251 | + if (is_resource($dir)) { |
|
| 252 | 252 | while (($file = readdir($dir)) !== false) { |
| 253 | 253 | if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
| 254 | 254 | if (!$this->unlink($path.'/'.$file)) { |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | if ($pageToken !== true) { |
| 277 | 277 | $params['pageToken'] = $pageToken; |
| 278 | 278 | } |
| 279 | - $params['q'] = "'" . str_replace("'","\\'", $folder->getId()) . "' in parents and trashed = false"; |
|
| 279 | + $params['q'] = "'".str_replace("'", "\\'", $folder->getId())."' in parents and trashed = false"; |
|
| 280 | 280 | $children = $this->service->files->listFiles($params); |
| 281 | 281 | foreach ($children->getItems() as $child) { |
| 282 | 282 | $name = $child->getTitle(); |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | public function file_exists($path) { |
| 372 | - return (bool)$this->getDriveFile($path); |
|
| 372 | + return (bool) $this->getDriveFile($path); |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | public function unlink($path) { |
@@ -379,7 +379,7 @@ discard block |
||
| 379 | 379 | if ($result) { |
| 380 | 380 | $this->setDriveFile($path, false); |
| 381 | 381 | } |
| 382 | - return (bool)$result; |
|
| 382 | + return (bool) $result; |
|
| 383 | 383 | } else { |
| 384 | 384 | return false; |
| 385 | 385 | } |
@@ -427,7 +427,7 @@ discard block |
||
| 427 | 427 | } |
| 428 | 428 | } |
| 429 | 429 | } |
| 430 | - return (bool)$result; |
|
| 430 | + return (bool) $result; |
|
| 431 | 431 | } else { |
| 432 | 432 | return false; |
| 433 | 433 | } |
@@ -462,10 +462,10 @@ discard block |
||
| 462 | 462 | $response = $client->get($downloadUrl, [ |
| 463 | 463 | 'headers' => $httpRequest->getRequestHeaders(), |
| 464 | 464 | 'stream' => true, |
| 465 | - 'verify' => realpath(__DIR__ . '/../../../3rdparty/google-api-php-client/src/Google/IO/cacerts.pem'), |
|
| 465 | + 'verify' => realpath(__DIR__.'/../../../3rdparty/google-api-php-client/src/Google/IO/cacerts.pem'), |
|
| 466 | 466 | ]); |
| 467 | 467 | } catch (RequestException $e) { |
| 468 | - if(!is_null($e->getResponse())) { |
|
| 468 | + if (!is_null($e->getResponse())) { |
|
| 469 | 469 | if ($e->getResponse()->getStatusCode() === 404) { |
| 470 | 470 | return false; |
| 471 | 471 | } else { |
@@ -499,7 +499,7 @@ discard block |
||
| 499 | 499 | file_put_contents($tmpFile, $source); |
| 500 | 500 | } |
| 501 | 501 | $handle = fopen($tmpFile, $mode); |
| 502 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 502 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 503 | 503 | $this->writeBack($tmpFile, $path); |
| 504 | 504 | }); |
| 505 | 505 | } |
@@ -641,7 +641,7 @@ discard block |
||
| 641 | 641 | if ($result) { |
| 642 | 642 | $this->setDriveFile($path, $result); |
| 643 | 643 | } |
| 644 | - return (bool)$result; |
|
| 644 | + return (bool) $result; |
|
| 645 | 645 | } |
| 646 | 646 | |
| 647 | 647 | public function test() { |
@@ -668,7 +668,7 @@ discard block |
||
| 668 | 668 | 'includeSubscribed' => true, |
| 669 | 669 | ); |
| 670 | 670 | if (isset($startChangeId)) { |
| 671 | - $startChangeId = (int)$startChangeId; |
|
| 671 | + $startChangeId = (int) $startChangeId; |
|
| 672 | 672 | $largestChangeId = $startChangeId; |
| 673 | 673 | $params['startChangeId'] = $startChangeId + 1; |
| 674 | 674 | } else { |
@@ -41,352 +41,352 @@ discard block |
||
| 41 | 41 | use Icewind\Streams\RetryWrapper; |
| 42 | 42 | |
| 43 | 43 | set_include_path(get_include_path().PATH_SEPARATOR. |
| 44 | - \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src'); |
|
| 44 | + \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src'); |
|
| 45 | 45 | require_once 'Google/autoload.php'; |
| 46 | 46 | |
| 47 | 47 | class Google extends \OC\Files\Storage\Common { |
| 48 | 48 | |
| 49 | - private $client; |
|
| 50 | - private $id; |
|
| 51 | - private $service; |
|
| 52 | - private $driveFiles; |
|
| 53 | - |
|
| 54 | - // Google Doc mimetypes |
|
| 55 | - const FOLDER = 'application/vnd.google-apps.folder'; |
|
| 56 | - const DOCUMENT = 'application/vnd.google-apps.document'; |
|
| 57 | - const SPREADSHEET = 'application/vnd.google-apps.spreadsheet'; |
|
| 58 | - const DRAWING = 'application/vnd.google-apps.drawing'; |
|
| 59 | - const PRESENTATION = 'application/vnd.google-apps.presentation'; |
|
| 60 | - const MAP = 'application/vnd.google-apps.map'; |
|
| 61 | - |
|
| 62 | - public function __construct($params) { |
|
| 63 | - if (isset($params['configured']) && $params['configured'] === 'true' |
|
| 64 | - && isset($params['client_id']) && isset($params['client_secret']) |
|
| 65 | - && isset($params['token']) |
|
| 66 | - ) { |
|
| 67 | - $this->client = new \Google_Client(); |
|
| 68 | - $this->client->setClientId($params['client_id']); |
|
| 69 | - $this->client->setClientSecret($params['client_secret']); |
|
| 70 | - $this->client->setScopes(array('https://www.googleapis.com/auth/drive')); |
|
| 71 | - $this->client->setAccessToken($params['token']); |
|
| 72 | - // if curl isn't available we're likely to run into |
|
| 73 | - // https://github.com/google/google-api-php-client/issues/59 |
|
| 74 | - // - disable gzip to avoid it. |
|
| 75 | - if (!function_exists('curl_version') || !function_exists('curl_exec')) { |
|
| 76 | - $this->client->setClassConfig("Google_Http_Request", "disable_gzip", true); |
|
| 77 | - } |
|
| 78 | - // note: API connection is lazy |
|
| 79 | - $this->service = new \Google_Service_Drive($this->client); |
|
| 80 | - $token = json_decode($params['token'], true); |
|
| 81 | - $this->id = 'google::'.substr($params['client_id'], 0, 30).$token['created']; |
|
| 82 | - } else { |
|
| 83 | - throw new \Exception('Creating Google storage failed'); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function getId() { |
|
| 88 | - return $this->id; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the Google_Service_Drive_DriveFile object for the specified path. |
|
| 93 | - * Returns false on failure. |
|
| 94 | - * @param string $path |
|
| 95 | - * @return \Google_Service_Drive_DriveFile|false |
|
| 96 | - */ |
|
| 97 | - private function getDriveFile($path) { |
|
| 98 | - // Remove leading and trailing slashes |
|
| 99 | - $path = trim($path, '/'); |
|
| 100 | - if ($path === '.') { |
|
| 101 | - $path = ''; |
|
| 102 | - } |
|
| 103 | - if (isset($this->driveFiles[$path])) { |
|
| 104 | - return $this->driveFiles[$path]; |
|
| 105 | - } else if ($path === '') { |
|
| 106 | - $root = $this->service->files->get('root'); |
|
| 107 | - $this->driveFiles[$path] = $root; |
|
| 108 | - return $root; |
|
| 109 | - } else { |
|
| 110 | - // Google Drive SDK does not have methods for retrieving files by path |
|
| 111 | - // Instead we must find the id of the parent folder of the file |
|
| 112 | - $parentId = $this->getDriveFile('')->getId(); |
|
| 113 | - $folderNames = explode('/', $path); |
|
| 114 | - $path = ''; |
|
| 115 | - // Loop through each folder of this path to get to the file |
|
| 116 | - foreach ($folderNames as $name) { |
|
| 117 | - // Reconstruct path from beginning |
|
| 118 | - if ($path === '') { |
|
| 119 | - $path .= $name; |
|
| 120 | - } else { |
|
| 121 | - $path .= '/'.$name; |
|
| 122 | - } |
|
| 123 | - if (isset($this->driveFiles[$path])) { |
|
| 124 | - $parentId = $this->driveFiles[$path]->getId(); |
|
| 125 | - } else { |
|
| 126 | - $q = "title='" . str_replace("'","\\'", $name) . "' and '" . str_replace("'","\\'", $parentId) . "' in parents and trashed = false"; |
|
| 127 | - $result = $this->service->files->listFiles(array('q' => $q))->getItems(); |
|
| 128 | - if (!empty($result)) { |
|
| 129 | - // Google Drive allows files with the same name, Nextcloud doesn't |
|
| 130 | - if (count($result) > 1) { |
|
| 131 | - $this->onDuplicateFileDetected($path); |
|
| 132 | - return false; |
|
| 133 | - } else { |
|
| 134 | - $file = current($result); |
|
| 135 | - $this->driveFiles[$path] = $file; |
|
| 136 | - $parentId = $file->getId(); |
|
| 137 | - } |
|
| 138 | - } else { |
|
| 139 | - // Google Docs have no extension in their title, so try without extension |
|
| 140 | - $pos = strrpos($path, '.'); |
|
| 141 | - if ($pos !== false) { |
|
| 142 | - $pathWithoutExt = substr($path, 0, $pos); |
|
| 143 | - $file = $this->getDriveFile($pathWithoutExt); |
|
| 144 | - if ($file && $this->isGoogleDocFile($file)) { |
|
| 145 | - // Switch cached Google_Service_Drive_DriveFile to the correct index |
|
| 146 | - unset($this->driveFiles[$pathWithoutExt]); |
|
| 147 | - $this->driveFiles[$path] = $file; |
|
| 148 | - $parentId = $file->getId(); |
|
| 149 | - } else { |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - } else { |
|
| 153 | - return false; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - return $this->driveFiles[$path]; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Set the Google_Service_Drive_DriveFile object in the cache |
|
| 164 | - * @param string $path |
|
| 165 | - * @param \Google_Service_Drive_DriveFile|false $file |
|
| 166 | - */ |
|
| 167 | - private function setDriveFile($path, $file) { |
|
| 168 | - $path = trim($path, '/'); |
|
| 169 | - $this->driveFiles[$path] = $file; |
|
| 170 | - if ($file === false) { |
|
| 171 | - // Remove all children |
|
| 172 | - $len = strlen($path); |
|
| 173 | - foreach ($this->driveFiles as $key => $file) { |
|
| 174 | - if (substr($key, 0, $len) === $path) { |
|
| 175 | - unset($this->driveFiles[$key]); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Write a log message to inform about duplicate file names |
|
| 183 | - * @param string $path |
|
| 184 | - */ |
|
| 185 | - private function onDuplicateFileDetected($path) { |
|
| 186 | - $about = $this->service->about->get(); |
|
| 187 | - $user = $about->getName(); |
|
| 188 | - \OCP\Util::writeLog('files_external', |
|
| 189 | - 'Ignoring duplicate file name: '.$path.' on Google Drive for Google user: '.$user, |
|
| 190 | - \OCP\Util::INFO |
|
| 191 | - ); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Generate file extension for a Google Doc, choosing Open Document formats for download |
|
| 196 | - * @param string $mimetype |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - private function getGoogleDocExtension($mimetype) { |
|
| 200 | - if ($mimetype === self::DOCUMENT) { |
|
| 201 | - return 'odt'; |
|
| 202 | - } else if ($mimetype === self::SPREADSHEET) { |
|
| 203 | - return 'ods'; |
|
| 204 | - } else if ($mimetype === self::DRAWING) { |
|
| 205 | - return 'jpg'; |
|
| 206 | - } else if ($mimetype === self::PRESENTATION) { |
|
| 207 | - // Download as .odp is not available |
|
| 208 | - return 'pdf'; |
|
| 209 | - } else { |
|
| 210 | - return ''; |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Returns whether the given drive file is a Google Doc file |
|
| 216 | - * |
|
| 217 | - * @param \Google_Service_Drive_DriveFile |
|
| 218 | - * |
|
| 219 | - * @return true if the file is a Google Doc file, false otherwise |
|
| 220 | - */ |
|
| 221 | - private function isGoogleDocFile($file) { |
|
| 222 | - return $this->getGoogleDocExtension($file->getMimeType()) !== ''; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - public function mkdir($path) { |
|
| 226 | - if (!$this->is_dir($path)) { |
|
| 227 | - $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 228 | - if ($parentFolder) { |
|
| 229 | - $folder = new \Google_Service_Drive_DriveFile(); |
|
| 230 | - $folder->setTitle(basename($path)); |
|
| 231 | - $folder->setMimeType(self::FOLDER); |
|
| 232 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 233 | - $parent->setId($parentFolder->getId()); |
|
| 234 | - $folder->setParents(array($parent)); |
|
| 235 | - $result = $this->service->files->insert($folder); |
|
| 236 | - if ($result) { |
|
| 237 | - $this->setDriveFile($path, $result); |
|
| 238 | - } |
|
| 239 | - return (bool)$result; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - public function rmdir($path) { |
|
| 246 | - if (!$this->isDeletable($path)) { |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - if (trim($path, '/') === '') { |
|
| 250 | - $dir = $this->opendir($path); |
|
| 251 | - if(is_resource($dir)) { |
|
| 252 | - while (($file = readdir($dir)) !== false) { |
|
| 253 | - if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 254 | - if (!$this->unlink($path.'/'.$file)) { |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - closedir($dir); |
|
| 260 | - } |
|
| 261 | - $this->driveFiles = array(); |
|
| 262 | - return true; |
|
| 263 | - } else { |
|
| 264 | - return $this->unlink($path); |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - public function opendir($path) { |
|
| 269 | - $folder = $this->getDriveFile($path); |
|
| 270 | - if ($folder) { |
|
| 271 | - $files = array(); |
|
| 272 | - $duplicates = array(); |
|
| 273 | - $pageToken = true; |
|
| 274 | - while ($pageToken) { |
|
| 275 | - $params = array(); |
|
| 276 | - if ($pageToken !== true) { |
|
| 277 | - $params['pageToken'] = $pageToken; |
|
| 278 | - } |
|
| 279 | - $params['q'] = "'" . str_replace("'","\\'", $folder->getId()) . "' in parents and trashed = false"; |
|
| 280 | - $children = $this->service->files->listFiles($params); |
|
| 281 | - foreach ($children->getItems() as $child) { |
|
| 282 | - $name = $child->getTitle(); |
|
| 283 | - // Check if this is a Google Doc i.e. no extension in name |
|
| 284 | - $extension = $child->getFileExtension(); |
|
| 285 | - if (empty($extension)) { |
|
| 286 | - if ($child->getMimeType() === self::MAP) { |
|
| 287 | - continue; // No method known to transfer map files, ignore it |
|
| 288 | - } else if ($child->getMimeType() !== self::FOLDER) { |
|
| 289 | - $name .= '.'.$this->getGoogleDocExtension($child->getMimeType()); |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - if ($path === '') { |
|
| 293 | - $filepath = $name; |
|
| 294 | - } else { |
|
| 295 | - $filepath = $path.'/'.$name; |
|
| 296 | - } |
|
| 297 | - // Google Drive allows files with the same name, Nextcloud doesn't |
|
| 298 | - // Prevent opendir() from returning any duplicate files |
|
| 299 | - $key = array_search($name, $files); |
|
| 300 | - if ($key !== false || isset($duplicates[$filepath])) { |
|
| 301 | - if (!isset($duplicates[$filepath])) { |
|
| 302 | - $duplicates[$filepath] = true; |
|
| 303 | - $this->setDriveFile($filepath, false); |
|
| 304 | - unset($files[$key]); |
|
| 305 | - $this->onDuplicateFileDetected($filepath); |
|
| 306 | - } |
|
| 307 | - } else { |
|
| 308 | - // Cache the Google_Service_Drive_DriveFile for future use |
|
| 309 | - $this->setDriveFile($filepath, $child); |
|
| 310 | - $files[] = $name; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - $pageToken = $children->getNextPageToken(); |
|
| 314 | - } |
|
| 315 | - return IteratorDirectory::wrap($files); |
|
| 316 | - } else { |
|
| 317 | - return false; |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - public function stat($path) { |
|
| 322 | - $file = $this->getDriveFile($path); |
|
| 323 | - if ($file) { |
|
| 324 | - $stat = array(); |
|
| 325 | - if ($this->filetype($path) === 'dir') { |
|
| 326 | - $stat['size'] = 0; |
|
| 327 | - } else { |
|
| 328 | - // Check if this is a Google Doc |
|
| 329 | - if ($this->isGoogleDocFile($file)) { |
|
| 330 | - // Return unknown file size |
|
| 331 | - $stat['size'] = \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
| 332 | - } else { |
|
| 333 | - $stat['size'] = $file->getFileSize(); |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - $stat['atime'] = strtotime($file->getLastViewedByMeDate()); |
|
| 337 | - $stat['mtime'] = strtotime($file->getModifiedDate()); |
|
| 338 | - $stat['ctime'] = strtotime($file->getCreatedDate()); |
|
| 339 | - return $stat; |
|
| 340 | - } else { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - public function filetype($path) { |
|
| 346 | - if ($path === '') { |
|
| 347 | - return 'dir'; |
|
| 348 | - } else { |
|
| 349 | - $file = $this->getDriveFile($path); |
|
| 350 | - if ($file) { |
|
| 351 | - if ($file->getMimeType() === self::FOLDER) { |
|
| 352 | - return 'dir'; |
|
| 353 | - } else { |
|
| 354 | - return 'file'; |
|
| 355 | - } |
|
| 356 | - } else { |
|
| 357 | - return false; |
|
| 358 | - } |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - public function isUpdatable($path) { |
|
| 363 | - $file = $this->getDriveFile($path); |
|
| 364 | - if ($file) { |
|
| 365 | - return $file->getEditable(); |
|
| 366 | - } else { |
|
| 367 | - return false; |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - public function file_exists($path) { |
|
| 372 | - return (bool)$this->getDriveFile($path); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - public function unlink($path) { |
|
| 376 | - $file = $this->getDriveFile($path); |
|
| 377 | - if ($file) { |
|
| 378 | - $result = $this->service->files->trash($file->getId()); |
|
| 379 | - if ($result) { |
|
| 380 | - $this->setDriveFile($path, false); |
|
| 381 | - } |
|
| 382 | - return (bool)$result; |
|
| 383 | - } else { |
|
| 384 | - return false; |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - public function rename($path1, $path2) { |
|
| 389 | - // Avoid duplicate files with the same name |
|
| 49 | + private $client; |
|
| 50 | + private $id; |
|
| 51 | + private $service; |
|
| 52 | + private $driveFiles; |
|
| 53 | + |
|
| 54 | + // Google Doc mimetypes |
|
| 55 | + const FOLDER = 'application/vnd.google-apps.folder'; |
|
| 56 | + const DOCUMENT = 'application/vnd.google-apps.document'; |
|
| 57 | + const SPREADSHEET = 'application/vnd.google-apps.spreadsheet'; |
|
| 58 | + const DRAWING = 'application/vnd.google-apps.drawing'; |
|
| 59 | + const PRESENTATION = 'application/vnd.google-apps.presentation'; |
|
| 60 | + const MAP = 'application/vnd.google-apps.map'; |
|
| 61 | + |
|
| 62 | + public function __construct($params) { |
|
| 63 | + if (isset($params['configured']) && $params['configured'] === 'true' |
|
| 64 | + && isset($params['client_id']) && isset($params['client_secret']) |
|
| 65 | + && isset($params['token']) |
|
| 66 | + ) { |
|
| 67 | + $this->client = new \Google_Client(); |
|
| 68 | + $this->client->setClientId($params['client_id']); |
|
| 69 | + $this->client->setClientSecret($params['client_secret']); |
|
| 70 | + $this->client->setScopes(array('https://www.googleapis.com/auth/drive')); |
|
| 71 | + $this->client->setAccessToken($params['token']); |
|
| 72 | + // if curl isn't available we're likely to run into |
|
| 73 | + // https://github.com/google/google-api-php-client/issues/59 |
|
| 74 | + // - disable gzip to avoid it. |
|
| 75 | + if (!function_exists('curl_version') || !function_exists('curl_exec')) { |
|
| 76 | + $this->client->setClassConfig("Google_Http_Request", "disable_gzip", true); |
|
| 77 | + } |
|
| 78 | + // note: API connection is lazy |
|
| 79 | + $this->service = new \Google_Service_Drive($this->client); |
|
| 80 | + $token = json_decode($params['token'], true); |
|
| 81 | + $this->id = 'google::'.substr($params['client_id'], 0, 30).$token['created']; |
|
| 82 | + } else { |
|
| 83 | + throw new \Exception('Creating Google storage failed'); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function getId() { |
|
| 88 | + return $this->id; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the Google_Service_Drive_DriveFile object for the specified path. |
|
| 93 | + * Returns false on failure. |
|
| 94 | + * @param string $path |
|
| 95 | + * @return \Google_Service_Drive_DriveFile|false |
|
| 96 | + */ |
|
| 97 | + private function getDriveFile($path) { |
|
| 98 | + // Remove leading and trailing slashes |
|
| 99 | + $path = trim($path, '/'); |
|
| 100 | + if ($path === '.') { |
|
| 101 | + $path = ''; |
|
| 102 | + } |
|
| 103 | + if (isset($this->driveFiles[$path])) { |
|
| 104 | + return $this->driveFiles[$path]; |
|
| 105 | + } else if ($path === '') { |
|
| 106 | + $root = $this->service->files->get('root'); |
|
| 107 | + $this->driveFiles[$path] = $root; |
|
| 108 | + return $root; |
|
| 109 | + } else { |
|
| 110 | + // Google Drive SDK does not have methods for retrieving files by path |
|
| 111 | + // Instead we must find the id of the parent folder of the file |
|
| 112 | + $parentId = $this->getDriveFile('')->getId(); |
|
| 113 | + $folderNames = explode('/', $path); |
|
| 114 | + $path = ''; |
|
| 115 | + // Loop through each folder of this path to get to the file |
|
| 116 | + foreach ($folderNames as $name) { |
|
| 117 | + // Reconstruct path from beginning |
|
| 118 | + if ($path === '') { |
|
| 119 | + $path .= $name; |
|
| 120 | + } else { |
|
| 121 | + $path .= '/'.$name; |
|
| 122 | + } |
|
| 123 | + if (isset($this->driveFiles[$path])) { |
|
| 124 | + $parentId = $this->driveFiles[$path]->getId(); |
|
| 125 | + } else { |
|
| 126 | + $q = "title='" . str_replace("'","\\'", $name) . "' and '" . str_replace("'","\\'", $parentId) . "' in parents and trashed = false"; |
|
| 127 | + $result = $this->service->files->listFiles(array('q' => $q))->getItems(); |
|
| 128 | + if (!empty($result)) { |
|
| 129 | + // Google Drive allows files with the same name, Nextcloud doesn't |
|
| 130 | + if (count($result) > 1) { |
|
| 131 | + $this->onDuplicateFileDetected($path); |
|
| 132 | + return false; |
|
| 133 | + } else { |
|
| 134 | + $file = current($result); |
|
| 135 | + $this->driveFiles[$path] = $file; |
|
| 136 | + $parentId = $file->getId(); |
|
| 137 | + } |
|
| 138 | + } else { |
|
| 139 | + // Google Docs have no extension in their title, so try without extension |
|
| 140 | + $pos = strrpos($path, '.'); |
|
| 141 | + if ($pos !== false) { |
|
| 142 | + $pathWithoutExt = substr($path, 0, $pos); |
|
| 143 | + $file = $this->getDriveFile($pathWithoutExt); |
|
| 144 | + if ($file && $this->isGoogleDocFile($file)) { |
|
| 145 | + // Switch cached Google_Service_Drive_DriveFile to the correct index |
|
| 146 | + unset($this->driveFiles[$pathWithoutExt]); |
|
| 147 | + $this->driveFiles[$path] = $file; |
|
| 148 | + $parentId = $file->getId(); |
|
| 149 | + } else { |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + } else { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + return $this->driveFiles[$path]; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Set the Google_Service_Drive_DriveFile object in the cache |
|
| 164 | + * @param string $path |
|
| 165 | + * @param \Google_Service_Drive_DriveFile|false $file |
|
| 166 | + */ |
|
| 167 | + private function setDriveFile($path, $file) { |
|
| 168 | + $path = trim($path, '/'); |
|
| 169 | + $this->driveFiles[$path] = $file; |
|
| 170 | + if ($file === false) { |
|
| 171 | + // Remove all children |
|
| 172 | + $len = strlen($path); |
|
| 173 | + foreach ($this->driveFiles as $key => $file) { |
|
| 174 | + if (substr($key, 0, $len) === $path) { |
|
| 175 | + unset($this->driveFiles[$key]); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Write a log message to inform about duplicate file names |
|
| 183 | + * @param string $path |
|
| 184 | + */ |
|
| 185 | + private function onDuplicateFileDetected($path) { |
|
| 186 | + $about = $this->service->about->get(); |
|
| 187 | + $user = $about->getName(); |
|
| 188 | + \OCP\Util::writeLog('files_external', |
|
| 189 | + 'Ignoring duplicate file name: '.$path.' on Google Drive for Google user: '.$user, |
|
| 190 | + \OCP\Util::INFO |
|
| 191 | + ); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Generate file extension for a Google Doc, choosing Open Document formats for download |
|
| 196 | + * @param string $mimetype |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + private function getGoogleDocExtension($mimetype) { |
|
| 200 | + if ($mimetype === self::DOCUMENT) { |
|
| 201 | + return 'odt'; |
|
| 202 | + } else if ($mimetype === self::SPREADSHEET) { |
|
| 203 | + return 'ods'; |
|
| 204 | + } else if ($mimetype === self::DRAWING) { |
|
| 205 | + return 'jpg'; |
|
| 206 | + } else if ($mimetype === self::PRESENTATION) { |
|
| 207 | + // Download as .odp is not available |
|
| 208 | + return 'pdf'; |
|
| 209 | + } else { |
|
| 210 | + return ''; |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Returns whether the given drive file is a Google Doc file |
|
| 216 | + * |
|
| 217 | + * @param \Google_Service_Drive_DriveFile |
|
| 218 | + * |
|
| 219 | + * @return true if the file is a Google Doc file, false otherwise |
|
| 220 | + */ |
|
| 221 | + private function isGoogleDocFile($file) { |
|
| 222 | + return $this->getGoogleDocExtension($file->getMimeType()) !== ''; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + public function mkdir($path) { |
|
| 226 | + if (!$this->is_dir($path)) { |
|
| 227 | + $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 228 | + if ($parentFolder) { |
|
| 229 | + $folder = new \Google_Service_Drive_DriveFile(); |
|
| 230 | + $folder->setTitle(basename($path)); |
|
| 231 | + $folder->setMimeType(self::FOLDER); |
|
| 232 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 233 | + $parent->setId($parentFolder->getId()); |
|
| 234 | + $folder->setParents(array($parent)); |
|
| 235 | + $result = $this->service->files->insert($folder); |
|
| 236 | + if ($result) { |
|
| 237 | + $this->setDriveFile($path, $result); |
|
| 238 | + } |
|
| 239 | + return (bool)$result; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + public function rmdir($path) { |
|
| 246 | + if (!$this->isDeletable($path)) { |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + if (trim($path, '/') === '') { |
|
| 250 | + $dir = $this->opendir($path); |
|
| 251 | + if(is_resource($dir)) { |
|
| 252 | + while (($file = readdir($dir)) !== false) { |
|
| 253 | + if (!\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 254 | + if (!$this->unlink($path.'/'.$file)) { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + closedir($dir); |
|
| 260 | + } |
|
| 261 | + $this->driveFiles = array(); |
|
| 262 | + return true; |
|
| 263 | + } else { |
|
| 264 | + return $this->unlink($path); |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + public function opendir($path) { |
|
| 269 | + $folder = $this->getDriveFile($path); |
|
| 270 | + if ($folder) { |
|
| 271 | + $files = array(); |
|
| 272 | + $duplicates = array(); |
|
| 273 | + $pageToken = true; |
|
| 274 | + while ($pageToken) { |
|
| 275 | + $params = array(); |
|
| 276 | + if ($pageToken !== true) { |
|
| 277 | + $params['pageToken'] = $pageToken; |
|
| 278 | + } |
|
| 279 | + $params['q'] = "'" . str_replace("'","\\'", $folder->getId()) . "' in parents and trashed = false"; |
|
| 280 | + $children = $this->service->files->listFiles($params); |
|
| 281 | + foreach ($children->getItems() as $child) { |
|
| 282 | + $name = $child->getTitle(); |
|
| 283 | + // Check if this is a Google Doc i.e. no extension in name |
|
| 284 | + $extension = $child->getFileExtension(); |
|
| 285 | + if (empty($extension)) { |
|
| 286 | + if ($child->getMimeType() === self::MAP) { |
|
| 287 | + continue; // No method known to transfer map files, ignore it |
|
| 288 | + } else if ($child->getMimeType() !== self::FOLDER) { |
|
| 289 | + $name .= '.'.$this->getGoogleDocExtension($child->getMimeType()); |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + if ($path === '') { |
|
| 293 | + $filepath = $name; |
|
| 294 | + } else { |
|
| 295 | + $filepath = $path.'/'.$name; |
|
| 296 | + } |
|
| 297 | + // Google Drive allows files with the same name, Nextcloud doesn't |
|
| 298 | + // Prevent opendir() from returning any duplicate files |
|
| 299 | + $key = array_search($name, $files); |
|
| 300 | + if ($key !== false || isset($duplicates[$filepath])) { |
|
| 301 | + if (!isset($duplicates[$filepath])) { |
|
| 302 | + $duplicates[$filepath] = true; |
|
| 303 | + $this->setDriveFile($filepath, false); |
|
| 304 | + unset($files[$key]); |
|
| 305 | + $this->onDuplicateFileDetected($filepath); |
|
| 306 | + } |
|
| 307 | + } else { |
|
| 308 | + // Cache the Google_Service_Drive_DriveFile for future use |
|
| 309 | + $this->setDriveFile($filepath, $child); |
|
| 310 | + $files[] = $name; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + $pageToken = $children->getNextPageToken(); |
|
| 314 | + } |
|
| 315 | + return IteratorDirectory::wrap($files); |
|
| 316 | + } else { |
|
| 317 | + return false; |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + public function stat($path) { |
|
| 322 | + $file = $this->getDriveFile($path); |
|
| 323 | + if ($file) { |
|
| 324 | + $stat = array(); |
|
| 325 | + if ($this->filetype($path) === 'dir') { |
|
| 326 | + $stat['size'] = 0; |
|
| 327 | + } else { |
|
| 328 | + // Check if this is a Google Doc |
|
| 329 | + if ($this->isGoogleDocFile($file)) { |
|
| 330 | + // Return unknown file size |
|
| 331 | + $stat['size'] = \OCP\Files\FileInfo::SPACE_UNKNOWN; |
|
| 332 | + } else { |
|
| 333 | + $stat['size'] = $file->getFileSize(); |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + $stat['atime'] = strtotime($file->getLastViewedByMeDate()); |
|
| 337 | + $stat['mtime'] = strtotime($file->getModifiedDate()); |
|
| 338 | + $stat['ctime'] = strtotime($file->getCreatedDate()); |
|
| 339 | + return $stat; |
|
| 340 | + } else { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + public function filetype($path) { |
|
| 346 | + if ($path === '') { |
|
| 347 | + return 'dir'; |
|
| 348 | + } else { |
|
| 349 | + $file = $this->getDriveFile($path); |
|
| 350 | + if ($file) { |
|
| 351 | + if ($file->getMimeType() === self::FOLDER) { |
|
| 352 | + return 'dir'; |
|
| 353 | + } else { |
|
| 354 | + return 'file'; |
|
| 355 | + } |
|
| 356 | + } else { |
|
| 357 | + return false; |
|
| 358 | + } |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + public function isUpdatable($path) { |
|
| 363 | + $file = $this->getDriveFile($path); |
|
| 364 | + if ($file) { |
|
| 365 | + return $file->getEditable(); |
|
| 366 | + } else { |
|
| 367 | + return false; |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + public function file_exists($path) { |
|
| 372 | + return (bool)$this->getDriveFile($path); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + public function unlink($path) { |
|
| 376 | + $file = $this->getDriveFile($path); |
|
| 377 | + if ($file) { |
|
| 378 | + $result = $this->service->files->trash($file->getId()); |
|
| 379 | + if ($result) { |
|
| 380 | + $this->setDriveFile($path, false); |
|
| 381 | + } |
|
| 382 | + return (bool)$result; |
|
| 383 | + } else { |
|
| 384 | + return false; |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + public function rename($path1, $path2) { |
|
| 389 | + // Avoid duplicate files with the same name |
|
| 390 | 390 | $testRegex = '/^.+\.ocTransferId\d+\.part$/'; |
| 391 | 391 | if (preg_match($testRegex, $path1)) { |
| 392 | 392 | if ($this->is_file($path2)) { |
@@ -399,339 +399,339 @@ discard block |
||
| 399 | 399 | } |
| 400 | 400 | } |
| 401 | 401 | |
| 402 | - $file = $this->getDriveFile($path1); |
|
| 403 | - if ($file) { |
|
| 404 | - $newFile = $this->getDriveFile($path2); |
|
| 405 | - if (dirname($path1) === dirname($path2)) { |
|
| 406 | - if ($newFile) { |
|
| 407 | - // rename to the name of the target file, could be an office file without extension |
|
| 408 | - $file->setTitle($newFile->getTitle()); |
|
| 409 | - } else { |
|
| 410 | - $file->setTitle(basename(($path2))); |
|
| 411 | - } |
|
| 412 | - } else { |
|
| 413 | - // Change file parent |
|
| 414 | - $parentFolder2 = $this->getDriveFile(dirname($path2)); |
|
| 415 | - if ($parentFolder2) { |
|
| 416 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 417 | - $parent->setId($parentFolder2->getId()); |
|
| 418 | - $file->setParents(array($parent)); |
|
| 419 | - } else { |
|
| 420 | - return false; |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - // We need to get the object for the existing file with the same |
|
| 424 | - // name (if there is one) before we do the patch. If oldfile |
|
| 425 | - // exists and is a directory we have to delete it before we |
|
| 426 | - // do the rename too. |
|
| 427 | - $oldfile = $this->getDriveFile($path2); |
|
| 428 | - if ($oldfile && $this->is_dir($path2)) { |
|
| 429 | - $this->rmdir($path2); |
|
| 430 | - $oldfile = false; |
|
| 431 | - } |
|
| 432 | - $result = $this->service->files->patch($file->getId(), $file); |
|
| 433 | - if ($result) { |
|
| 434 | - $this->setDriveFile($path1, false); |
|
| 435 | - $this->setDriveFile($path2, $result); |
|
| 436 | - if ($oldfile && $newFile) { |
|
| 437 | - // only delete if they have a different id (same id can happen for part files) |
|
| 438 | - if ($newFile->getId() !== $oldfile->getId()) { |
|
| 439 | - $this->service->files->delete($oldfile->getId()); |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - return (bool)$result; |
|
| 444 | - } else { |
|
| 445 | - return false; |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - public function fopen($path, $mode) { |
|
| 450 | - $pos = strrpos($path, '.'); |
|
| 451 | - if ($pos !== false) { |
|
| 452 | - $ext = substr($path, $pos); |
|
| 453 | - } else { |
|
| 454 | - $ext = ''; |
|
| 455 | - } |
|
| 456 | - switch ($mode) { |
|
| 457 | - case 'r': |
|
| 458 | - case 'rb': |
|
| 459 | - $file = $this->getDriveFile($path); |
|
| 460 | - if ($file) { |
|
| 461 | - $exportLinks = $file->getExportLinks(); |
|
| 462 | - $mimetype = $this->getMimeType($path); |
|
| 463 | - $downloadUrl = null; |
|
| 464 | - if ($exportLinks && isset($exportLinks[$mimetype])) { |
|
| 465 | - $downloadUrl = $exportLinks[$mimetype]; |
|
| 466 | - } else { |
|
| 467 | - $downloadUrl = $file->getDownloadUrl(); |
|
| 468 | - } |
|
| 469 | - if (isset($downloadUrl)) { |
|
| 470 | - $request = new \Google_Http_Request($downloadUrl, 'GET', null, null); |
|
| 471 | - $httpRequest = $this->client->getAuth()->sign($request); |
|
| 472 | - // the library's service doesn't support streaming, so we use Guzzle instead |
|
| 473 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 474 | - try { |
|
| 475 | - $response = $client->get($downloadUrl, [ |
|
| 476 | - 'headers' => $httpRequest->getRequestHeaders(), |
|
| 477 | - 'stream' => true, |
|
| 478 | - 'verify' => realpath(__DIR__ . '/../../../3rdparty/google-api-php-client/src/Google/IO/cacerts.pem'), |
|
| 479 | - ]); |
|
| 480 | - } catch (RequestException $e) { |
|
| 481 | - if(!is_null($e->getResponse())) { |
|
| 482 | - if ($e->getResponse()->getStatusCode() === 404) { |
|
| 483 | - return false; |
|
| 484 | - } else { |
|
| 485 | - throw $e; |
|
| 486 | - } |
|
| 487 | - } else { |
|
| 488 | - throw $e; |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - $handle = $response->getBody(); |
|
| 493 | - return RetryWrapper::wrap($handle); |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - return false; |
|
| 497 | - case 'w': |
|
| 498 | - case 'wb': |
|
| 499 | - case 'a': |
|
| 500 | - case 'ab': |
|
| 501 | - case 'r+': |
|
| 502 | - case 'w+': |
|
| 503 | - case 'wb+': |
|
| 504 | - case 'a+': |
|
| 505 | - case 'x': |
|
| 506 | - case 'x+': |
|
| 507 | - case 'c': |
|
| 508 | - case 'c+': |
|
| 509 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 510 | - if ($this->file_exists($path)) { |
|
| 511 | - $source = $this->fopen($path, 'rb'); |
|
| 512 | - file_put_contents($tmpFile, $source); |
|
| 513 | - } |
|
| 514 | - $handle = fopen($tmpFile, $mode); |
|
| 515 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 516 | - $this->writeBack($tmpFile, $path); |
|
| 517 | - }); |
|
| 518 | - } |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - public function writeBack($tmpFile, $path) { |
|
| 522 | - $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 523 | - if ($parentFolder) { |
|
| 524 | - $mimetype = \OC::$server->getMimeTypeDetector()->detect($tmpFile); |
|
| 525 | - $params = array( |
|
| 526 | - 'mimeType' => $mimetype, |
|
| 527 | - 'uploadType' => 'media' |
|
| 528 | - ); |
|
| 529 | - $result = false; |
|
| 530 | - |
|
| 531 | - $chunkSizeBytes = 10 * 1024 * 1024; |
|
| 532 | - |
|
| 533 | - $useChunking = false; |
|
| 534 | - $size = filesize($tmpFile); |
|
| 535 | - if ($size > $chunkSizeBytes) { |
|
| 536 | - $useChunking = true; |
|
| 537 | - } else { |
|
| 538 | - $params['data'] = file_get_contents($tmpFile); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - if ($this->file_exists($path)) { |
|
| 542 | - $file = $this->getDriveFile($path); |
|
| 543 | - $this->client->setDefer($useChunking); |
|
| 544 | - $request = $this->service->files->update($file->getId(), $file, $params); |
|
| 545 | - } else { |
|
| 546 | - $file = new \Google_Service_Drive_DriveFile(); |
|
| 547 | - $file->setTitle(basename($path)); |
|
| 548 | - $file->setMimeType($mimetype); |
|
| 549 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 550 | - $parent->setId($parentFolder->getId()); |
|
| 551 | - $file->setParents(array($parent)); |
|
| 552 | - $this->client->setDefer($useChunking); |
|
| 553 | - $request = $this->service->files->insert($file, $params); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - if ($useChunking) { |
|
| 557 | - // Create a media file upload to represent our upload process. |
|
| 558 | - $media = new \Google_Http_MediaFileUpload( |
|
| 559 | - $this->client, |
|
| 560 | - $request, |
|
| 561 | - 'text/plain', |
|
| 562 | - null, |
|
| 563 | - true, |
|
| 564 | - $chunkSizeBytes |
|
| 565 | - ); |
|
| 566 | - $media->setFileSize($size); |
|
| 567 | - |
|
| 568 | - // Upload the various chunks. $status will be false until the process is |
|
| 569 | - // complete. |
|
| 570 | - $status = false; |
|
| 571 | - $handle = fopen($tmpFile, 'rb'); |
|
| 572 | - while (!$status && !feof($handle)) { |
|
| 573 | - $chunk = fread($handle, $chunkSizeBytes); |
|
| 574 | - $status = $media->nextChunk($chunk); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - // The final value of $status will be the data from the API for the object |
|
| 578 | - // that has been uploaded. |
|
| 579 | - $result = false; |
|
| 580 | - if ($status !== false) { |
|
| 581 | - $result = $status; |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - fclose($handle); |
|
| 585 | - } else { |
|
| 586 | - $result = $request; |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - // Reset to the client to execute requests immediately in the future. |
|
| 590 | - $this->client->setDefer(false); |
|
| 591 | - |
|
| 592 | - if ($result) { |
|
| 593 | - $this->setDriveFile($path, $result); |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - public function getMimeType($path) { |
|
| 599 | - $file = $this->getDriveFile($path); |
|
| 600 | - if ($file) { |
|
| 601 | - $mimetype = $file->getMimeType(); |
|
| 602 | - // Convert Google Doc mimetypes, choosing Open Document formats for download |
|
| 603 | - if ($mimetype === self::FOLDER) { |
|
| 604 | - return 'httpd/unix-directory'; |
|
| 605 | - } else if ($mimetype === self::DOCUMENT) { |
|
| 606 | - return 'application/vnd.oasis.opendocument.text'; |
|
| 607 | - } else if ($mimetype === self::SPREADSHEET) { |
|
| 608 | - return 'application/x-vnd.oasis.opendocument.spreadsheet'; |
|
| 609 | - } else if ($mimetype === self::DRAWING) { |
|
| 610 | - return 'image/jpeg'; |
|
| 611 | - } else if ($mimetype === self::PRESENTATION) { |
|
| 612 | - // Download as .odp is not available |
|
| 613 | - return 'application/pdf'; |
|
| 614 | - } else { |
|
| 615 | - // use extension-based detection, could be an encrypted file |
|
| 616 | - return parent::getMimeType($path); |
|
| 617 | - } |
|
| 618 | - } else { |
|
| 619 | - return false; |
|
| 620 | - } |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - public function free_space($path) { |
|
| 624 | - $about = $this->service->about->get(); |
|
| 625 | - return $about->getQuotaBytesTotal() - $about->getQuotaBytesUsed(); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - public function touch($path, $mtime = null) { |
|
| 629 | - $file = $this->getDriveFile($path); |
|
| 630 | - $result = false; |
|
| 631 | - if ($file) { |
|
| 632 | - if (isset($mtime)) { |
|
| 633 | - // This is just RFC3339, but frustratingly, GDrive's API *requires* |
|
| 634 | - // the fractions portion be present, while no handy PHP constant |
|
| 635 | - // for RFC3339 or ISO8601 includes it. So we do it ourselves. |
|
| 636 | - $file->setModifiedDate(date('Y-m-d\TH:i:s.uP', $mtime)); |
|
| 637 | - $result = $this->service->files->patch($file->getId(), $file, array( |
|
| 638 | - 'setModifiedDate' => true, |
|
| 639 | - )); |
|
| 640 | - } else { |
|
| 641 | - $result = $this->service->files->touch($file->getId()); |
|
| 642 | - } |
|
| 643 | - } else { |
|
| 644 | - $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 645 | - if ($parentFolder) { |
|
| 646 | - $file = new \Google_Service_Drive_DriveFile(); |
|
| 647 | - $file->setTitle(basename($path)); |
|
| 648 | - $parent = new \Google_Service_Drive_ParentReference(); |
|
| 649 | - $parent->setId($parentFolder->getId()); |
|
| 650 | - $file->setParents(array($parent)); |
|
| 651 | - $result = $this->service->files->insert($file); |
|
| 652 | - } |
|
| 653 | - } |
|
| 654 | - if ($result) { |
|
| 655 | - $this->setDriveFile($path, $result); |
|
| 656 | - } |
|
| 657 | - return (bool)$result; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - public function test() { |
|
| 661 | - if ($this->free_space('')) { |
|
| 662 | - return true; |
|
| 663 | - } |
|
| 664 | - return false; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - public function hasUpdated($path, $time) { |
|
| 668 | - $appConfig = \OC::$server->getAppConfig(); |
|
| 669 | - if ($this->is_file($path)) { |
|
| 670 | - return parent::hasUpdated($path, $time); |
|
| 671 | - } else { |
|
| 672 | - // Google Drive doesn't change modified times of folders when files inside are updated |
|
| 673 | - // Instead we use the Changes API to see if folders have been updated, and it's a pain |
|
| 674 | - $folder = $this->getDriveFile($path); |
|
| 675 | - if ($folder) { |
|
| 676 | - $result = false; |
|
| 677 | - $folderId = $folder->getId(); |
|
| 678 | - $startChangeId = $appConfig->getValue('files_external', $this->getId().'cId'); |
|
| 679 | - $params = array( |
|
| 680 | - 'includeDeleted' => true, |
|
| 681 | - 'includeSubscribed' => true, |
|
| 682 | - ); |
|
| 683 | - if (isset($startChangeId)) { |
|
| 684 | - $startChangeId = (int)$startChangeId; |
|
| 685 | - $largestChangeId = $startChangeId; |
|
| 686 | - $params['startChangeId'] = $startChangeId + 1; |
|
| 687 | - } else { |
|
| 688 | - $largestChangeId = 0; |
|
| 689 | - } |
|
| 690 | - $pageToken = true; |
|
| 691 | - while ($pageToken) { |
|
| 692 | - if ($pageToken !== true) { |
|
| 693 | - $params['pageToken'] = $pageToken; |
|
| 694 | - } |
|
| 695 | - $changes = $this->service->changes->listChanges($params); |
|
| 696 | - if ($largestChangeId === 0 || $largestChangeId === $startChangeId) { |
|
| 697 | - $largestChangeId = $changes->getLargestChangeId(); |
|
| 698 | - } |
|
| 699 | - if (isset($startChangeId)) { |
|
| 700 | - // Check if a file in this folder has been updated |
|
| 701 | - // There is no way to filter by folder at the API level... |
|
| 702 | - foreach ($changes->getItems() as $change) { |
|
| 703 | - $file = $change->getFile(); |
|
| 704 | - if ($file) { |
|
| 705 | - foreach ($file->getParents() as $parent) { |
|
| 706 | - if ($parent->getId() === $folderId) { |
|
| 707 | - $result = true; |
|
| 708 | - // Check if there are changes in different folders |
|
| 709 | - } else if ($change->getId() <= $largestChangeId) { |
|
| 710 | - // Decrement id so this change is fetched when called again |
|
| 711 | - $largestChangeId = $change->getId(); |
|
| 712 | - $largestChangeId--; |
|
| 713 | - } |
|
| 714 | - } |
|
| 715 | - } |
|
| 716 | - } |
|
| 717 | - $pageToken = $changes->getNextPageToken(); |
|
| 718 | - } else { |
|
| 719 | - // Assuming the initial scan just occurred and changes are negligible |
|
| 720 | - break; |
|
| 721 | - } |
|
| 722 | - } |
|
| 723 | - $appConfig->setValue('files_external', $this->getId().'cId', $largestChangeId); |
|
| 724 | - return $result; |
|
| 725 | - } |
|
| 726 | - } |
|
| 727 | - return false; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * check if curl is installed |
|
| 732 | - */ |
|
| 733 | - public static function checkDependencies() { |
|
| 734 | - return true; |
|
| 735 | - } |
|
| 402 | + $file = $this->getDriveFile($path1); |
|
| 403 | + if ($file) { |
|
| 404 | + $newFile = $this->getDriveFile($path2); |
|
| 405 | + if (dirname($path1) === dirname($path2)) { |
|
| 406 | + if ($newFile) { |
|
| 407 | + // rename to the name of the target file, could be an office file without extension |
|
| 408 | + $file->setTitle($newFile->getTitle()); |
|
| 409 | + } else { |
|
| 410 | + $file->setTitle(basename(($path2))); |
|
| 411 | + } |
|
| 412 | + } else { |
|
| 413 | + // Change file parent |
|
| 414 | + $parentFolder2 = $this->getDriveFile(dirname($path2)); |
|
| 415 | + if ($parentFolder2) { |
|
| 416 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 417 | + $parent->setId($parentFolder2->getId()); |
|
| 418 | + $file->setParents(array($parent)); |
|
| 419 | + } else { |
|
| 420 | + return false; |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + // We need to get the object for the existing file with the same |
|
| 424 | + // name (if there is one) before we do the patch. If oldfile |
|
| 425 | + // exists and is a directory we have to delete it before we |
|
| 426 | + // do the rename too. |
|
| 427 | + $oldfile = $this->getDriveFile($path2); |
|
| 428 | + if ($oldfile && $this->is_dir($path2)) { |
|
| 429 | + $this->rmdir($path2); |
|
| 430 | + $oldfile = false; |
|
| 431 | + } |
|
| 432 | + $result = $this->service->files->patch($file->getId(), $file); |
|
| 433 | + if ($result) { |
|
| 434 | + $this->setDriveFile($path1, false); |
|
| 435 | + $this->setDriveFile($path2, $result); |
|
| 436 | + if ($oldfile && $newFile) { |
|
| 437 | + // only delete if they have a different id (same id can happen for part files) |
|
| 438 | + if ($newFile->getId() !== $oldfile->getId()) { |
|
| 439 | + $this->service->files->delete($oldfile->getId()); |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + return (bool)$result; |
|
| 444 | + } else { |
|
| 445 | + return false; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + public function fopen($path, $mode) { |
|
| 450 | + $pos = strrpos($path, '.'); |
|
| 451 | + if ($pos !== false) { |
|
| 452 | + $ext = substr($path, $pos); |
|
| 453 | + } else { |
|
| 454 | + $ext = ''; |
|
| 455 | + } |
|
| 456 | + switch ($mode) { |
|
| 457 | + case 'r': |
|
| 458 | + case 'rb': |
|
| 459 | + $file = $this->getDriveFile($path); |
|
| 460 | + if ($file) { |
|
| 461 | + $exportLinks = $file->getExportLinks(); |
|
| 462 | + $mimetype = $this->getMimeType($path); |
|
| 463 | + $downloadUrl = null; |
|
| 464 | + if ($exportLinks && isset($exportLinks[$mimetype])) { |
|
| 465 | + $downloadUrl = $exportLinks[$mimetype]; |
|
| 466 | + } else { |
|
| 467 | + $downloadUrl = $file->getDownloadUrl(); |
|
| 468 | + } |
|
| 469 | + if (isset($downloadUrl)) { |
|
| 470 | + $request = new \Google_Http_Request($downloadUrl, 'GET', null, null); |
|
| 471 | + $httpRequest = $this->client->getAuth()->sign($request); |
|
| 472 | + // the library's service doesn't support streaming, so we use Guzzle instead |
|
| 473 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 474 | + try { |
|
| 475 | + $response = $client->get($downloadUrl, [ |
|
| 476 | + 'headers' => $httpRequest->getRequestHeaders(), |
|
| 477 | + 'stream' => true, |
|
| 478 | + 'verify' => realpath(__DIR__ . '/../../../3rdparty/google-api-php-client/src/Google/IO/cacerts.pem'), |
|
| 479 | + ]); |
|
| 480 | + } catch (RequestException $e) { |
|
| 481 | + if(!is_null($e->getResponse())) { |
|
| 482 | + if ($e->getResponse()->getStatusCode() === 404) { |
|
| 483 | + return false; |
|
| 484 | + } else { |
|
| 485 | + throw $e; |
|
| 486 | + } |
|
| 487 | + } else { |
|
| 488 | + throw $e; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + $handle = $response->getBody(); |
|
| 493 | + return RetryWrapper::wrap($handle); |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + return false; |
|
| 497 | + case 'w': |
|
| 498 | + case 'wb': |
|
| 499 | + case 'a': |
|
| 500 | + case 'ab': |
|
| 501 | + case 'r+': |
|
| 502 | + case 'w+': |
|
| 503 | + case 'wb+': |
|
| 504 | + case 'a+': |
|
| 505 | + case 'x': |
|
| 506 | + case 'x+': |
|
| 507 | + case 'c': |
|
| 508 | + case 'c+': |
|
| 509 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 510 | + if ($this->file_exists($path)) { |
|
| 511 | + $source = $this->fopen($path, 'rb'); |
|
| 512 | + file_put_contents($tmpFile, $source); |
|
| 513 | + } |
|
| 514 | + $handle = fopen($tmpFile, $mode); |
|
| 515 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 516 | + $this->writeBack($tmpFile, $path); |
|
| 517 | + }); |
|
| 518 | + } |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + public function writeBack($tmpFile, $path) { |
|
| 522 | + $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 523 | + if ($parentFolder) { |
|
| 524 | + $mimetype = \OC::$server->getMimeTypeDetector()->detect($tmpFile); |
|
| 525 | + $params = array( |
|
| 526 | + 'mimeType' => $mimetype, |
|
| 527 | + 'uploadType' => 'media' |
|
| 528 | + ); |
|
| 529 | + $result = false; |
|
| 530 | + |
|
| 531 | + $chunkSizeBytes = 10 * 1024 * 1024; |
|
| 532 | + |
|
| 533 | + $useChunking = false; |
|
| 534 | + $size = filesize($tmpFile); |
|
| 535 | + if ($size > $chunkSizeBytes) { |
|
| 536 | + $useChunking = true; |
|
| 537 | + } else { |
|
| 538 | + $params['data'] = file_get_contents($tmpFile); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + if ($this->file_exists($path)) { |
|
| 542 | + $file = $this->getDriveFile($path); |
|
| 543 | + $this->client->setDefer($useChunking); |
|
| 544 | + $request = $this->service->files->update($file->getId(), $file, $params); |
|
| 545 | + } else { |
|
| 546 | + $file = new \Google_Service_Drive_DriveFile(); |
|
| 547 | + $file->setTitle(basename($path)); |
|
| 548 | + $file->setMimeType($mimetype); |
|
| 549 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 550 | + $parent->setId($parentFolder->getId()); |
|
| 551 | + $file->setParents(array($parent)); |
|
| 552 | + $this->client->setDefer($useChunking); |
|
| 553 | + $request = $this->service->files->insert($file, $params); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + if ($useChunking) { |
|
| 557 | + // Create a media file upload to represent our upload process. |
|
| 558 | + $media = new \Google_Http_MediaFileUpload( |
|
| 559 | + $this->client, |
|
| 560 | + $request, |
|
| 561 | + 'text/plain', |
|
| 562 | + null, |
|
| 563 | + true, |
|
| 564 | + $chunkSizeBytes |
|
| 565 | + ); |
|
| 566 | + $media->setFileSize($size); |
|
| 567 | + |
|
| 568 | + // Upload the various chunks. $status will be false until the process is |
|
| 569 | + // complete. |
|
| 570 | + $status = false; |
|
| 571 | + $handle = fopen($tmpFile, 'rb'); |
|
| 572 | + while (!$status && !feof($handle)) { |
|
| 573 | + $chunk = fread($handle, $chunkSizeBytes); |
|
| 574 | + $status = $media->nextChunk($chunk); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + // The final value of $status will be the data from the API for the object |
|
| 578 | + // that has been uploaded. |
|
| 579 | + $result = false; |
|
| 580 | + if ($status !== false) { |
|
| 581 | + $result = $status; |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + fclose($handle); |
|
| 585 | + } else { |
|
| 586 | + $result = $request; |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + // Reset to the client to execute requests immediately in the future. |
|
| 590 | + $this->client->setDefer(false); |
|
| 591 | + |
|
| 592 | + if ($result) { |
|
| 593 | + $this->setDriveFile($path, $result); |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + public function getMimeType($path) { |
|
| 599 | + $file = $this->getDriveFile($path); |
|
| 600 | + if ($file) { |
|
| 601 | + $mimetype = $file->getMimeType(); |
|
| 602 | + // Convert Google Doc mimetypes, choosing Open Document formats for download |
|
| 603 | + if ($mimetype === self::FOLDER) { |
|
| 604 | + return 'httpd/unix-directory'; |
|
| 605 | + } else if ($mimetype === self::DOCUMENT) { |
|
| 606 | + return 'application/vnd.oasis.opendocument.text'; |
|
| 607 | + } else if ($mimetype === self::SPREADSHEET) { |
|
| 608 | + return 'application/x-vnd.oasis.opendocument.spreadsheet'; |
|
| 609 | + } else if ($mimetype === self::DRAWING) { |
|
| 610 | + return 'image/jpeg'; |
|
| 611 | + } else if ($mimetype === self::PRESENTATION) { |
|
| 612 | + // Download as .odp is not available |
|
| 613 | + return 'application/pdf'; |
|
| 614 | + } else { |
|
| 615 | + // use extension-based detection, could be an encrypted file |
|
| 616 | + return parent::getMimeType($path); |
|
| 617 | + } |
|
| 618 | + } else { |
|
| 619 | + return false; |
|
| 620 | + } |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + public function free_space($path) { |
|
| 624 | + $about = $this->service->about->get(); |
|
| 625 | + return $about->getQuotaBytesTotal() - $about->getQuotaBytesUsed(); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + public function touch($path, $mtime = null) { |
|
| 629 | + $file = $this->getDriveFile($path); |
|
| 630 | + $result = false; |
|
| 631 | + if ($file) { |
|
| 632 | + if (isset($mtime)) { |
|
| 633 | + // This is just RFC3339, but frustratingly, GDrive's API *requires* |
|
| 634 | + // the fractions portion be present, while no handy PHP constant |
|
| 635 | + // for RFC3339 or ISO8601 includes it. So we do it ourselves. |
|
| 636 | + $file->setModifiedDate(date('Y-m-d\TH:i:s.uP', $mtime)); |
|
| 637 | + $result = $this->service->files->patch($file->getId(), $file, array( |
|
| 638 | + 'setModifiedDate' => true, |
|
| 639 | + )); |
|
| 640 | + } else { |
|
| 641 | + $result = $this->service->files->touch($file->getId()); |
|
| 642 | + } |
|
| 643 | + } else { |
|
| 644 | + $parentFolder = $this->getDriveFile(dirname($path)); |
|
| 645 | + if ($parentFolder) { |
|
| 646 | + $file = new \Google_Service_Drive_DriveFile(); |
|
| 647 | + $file->setTitle(basename($path)); |
|
| 648 | + $parent = new \Google_Service_Drive_ParentReference(); |
|
| 649 | + $parent->setId($parentFolder->getId()); |
|
| 650 | + $file->setParents(array($parent)); |
|
| 651 | + $result = $this->service->files->insert($file); |
|
| 652 | + } |
|
| 653 | + } |
|
| 654 | + if ($result) { |
|
| 655 | + $this->setDriveFile($path, $result); |
|
| 656 | + } |
|
| 657 | + return (bool)$result; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + public function test() { |
|
| 661 | + if ($this->free_space('')) { |
|
| 662 | + return true; |
|
| 663 | + } |
|
| 664 | + return false; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + public function hasUpdated($path, $time) { |
|
| 668 | + $appConfig = \OC::$server->getAppConfig(); |
|
| 669 | + if ($this->is_file($path)) { |
|
| 670 | + return parent::hasUpdated($path, $time); |
|
| 671 | + } else { |
|
| 672 | + // Google Drive doesn't change modified times of folders when files inside are updated |
|
| 673 | + // Instead we use the Changes API to see if folders have been updated, and it's a pain |
|
| 674 | + $folder = $this->getDriveFile($path); |
|
| 675 | + if ($folder) { |
|
| 676 | + $result = false; |
|
| 677 | + $folderId = $folder->getId(); |
|
| 678 | + $startChangeId = $appConfig->getValue('files_external', $this->getId().'cId'); |
|
| 679 | + $params = array( |
|
| 680 | + 'includeDeleted' => true, |
|
| 681 | + 'includeSubscribed' => true, |
|
| 682 | + ); |
|
| 683 | + if (isset($startChangeId)) { |
|
| 684 | + $startChangeId = (int)$startChangeId; |
|
| 685 | + $largestChangeId = $startChangeId; |
|
| 686 | + $params['startChangeId'] = $startChangeId + 1; |
|
| 687 | + } else { |
|
| 688 | + $largestChangeId = 0; |
|
| 689 | + } |
|
| 690 | + $pageToken = true; |
|
| 691 | + while ($pageToken) { |
|
| 692 | + if ($pageToken !== true) { |
|
| 693 | + $params['pageToken'] = $pageToken; |
|
| 694 | + } |
|
| 695 | + $changes = $this->service->changes->listChanges($params); |
|
| 696 | + if ($largestChangeId === 0 || $largestChangeId === $startChangeId) { |
|
| 697 | + $largestChangeId = $changes->getLargestChangeId(); |
|
| 698 | + } |
|
| 699 | + if (isset($startChangeId)) { |
|
| 700 | + // Check if a file in this folder has been updated |
|
| 701 | + // There is no way to filter by folder at the API level... |
|
| 702 | + foreach ($changes->getItems() as $change) { |
|
| 703 | + $file = $change->getFile(); |
|
| 704 | + if ($file) { |
|
| 705 | + foreach ($file->getParents() as $parent) { |
|
| 706 | + if ($parent->getId() === $folderId) { |
|
| 707 | + $result = true; |
|
| 708 | + // Check if there are changes in different folders |
|
| 709 | + } else if ($change->getId() <= $largestChangeId) { |
|
| 710 | + // Decrement id so this change is fetched when called again |
|
| 711 | + $largestChangeId = $change->getId(); |
|
| 712 | + $largestChangeId--; |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | + } |
|
| 716 | + } |
|
| 717 | + $pageToken = $changes->getNextPageToken(); |
|
| 718 | + } else { |
|
| 719 | + // Assuming the initial scan just occurred and changes are negligible |
|
| 720 | + break; |
|
| 721 | + } |
|
| 722 | + } |
|
| 723 | + $appConfig->setValue('files_external', $this->getId().'cId', $largestChangeId); |
|
| 724 | + return $result; |
|
| 725 | + } |
|
| 726 | + } |
|
| 727 | + return false; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * check if curl is installed |
|
| 732 | + */ |
|
| 733 | + public static function checkDependencies() { |
|
| 734 | + return true; |
|
| 735 | + } |
|
| 736 | 736 | |
| 737 | 737 | } |
@@ -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,390 +31,390 @@ |
||
| 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 | - } else { |
|
| 198 | - //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - $this->getCache()->remove($path); |
|
| 202 | - return true; |
|
| 203 | - } |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - public function stat($path) { |
|
| 208 | - $path = $this->normalizePath($path); |
|
| 209 | - $cacheEntry = $this->getCache()->get($path); |
|
| 210 | - if ($cacheEntry instanceof CacheEntry) { |
|
| 211 | - return $cacheEntry->getData(); |
|
| 212 | - } else { |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 219 | - * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 220 | - * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 221 | - * |
|
| 222 | - * @param int $fileId the fileid |
|
| 223 | - * @return null|string the unified resource name used to identify the object |
|
| 224 | - */ |
|
| 225 | - protected function getURN($fileId) { |
|
| 226 | - if (is_numeric($fileId)) { |
|
| 227 | - return $this->objectPrefix . $fileId; |
|
| 228 | - } |
|
| 229 | - return null; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - public function opendir($path) { |
|
| 233 | - $path = $this->normalizePath($path); |
|
| 234 | - |
|
| 235 | - try { |
|
| 236 | - $files = array(); |
|
| 237 | - $folderContents = $this->getCache()->getFolderContents($path); |
|
| 238 | - foreach ($folderContents as $file) { |
|
| 239 | - $files[] = $file['name']; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - return IteratorDirectory::wrap($files); |
|
| 243 | - } catch (\Exception $e) { |
|
| 244 | - $this->logger->logException($e); |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - public function filetype($path) { |
|
| 250 | - $path = $this->normalizePath($path); |
|
| 251 | - $stat = $this->stat($path); |
|
| 252 | - if ($stat) { |
|
| 253 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 254 | - return 'dir'; |
|
| 255 | - } |
|
| 256 | - return 'file'; |
|
| 257 | - } else { |
|
| 258 | - return false; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - public function fopen($path, $mode) { |
|
| 263 | - $path = $this->normalizePath($path); |
|
| 264 | - |
|
| 265 | - switch ($mode) { |
|
| 266 | - case 'r': |
|
| 267 | - case 'rb': |
|
| 268 | - $stat = $this->stat($path); |
|
| 269 | - if (is_array($stat)) { |
|
| 270 | - try { |
|
| 271 | - return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 272 | - } catch (\Exception $ex) { |
|
| 273 | - $this->logger->logException($ex, [ |
|
| 274 | - 'app' => 'objectstore', |
|
| 275 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 276 | - ]); |
|
| 277 | - return false; |
|
| 278 | - } |
|
| 279 | - } else { |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - case 'w': |
|
| 283 | - case 'wb': |
|
| 284 | - case 'a': |
|
| 285 | - case 'ab': |
|
| 286 | - case 'r+': |
|
| 287 | - case 'w+': |
|
| 288 | - case 'wb+': |
|
| 289 | - case 'a+': |
|
| 290 | - case 'x': |
|
| 291 | - case 'x+': |
|
| 292 | - case 'c': |
|
| 293 | - case 'c+': |
|
| 294 | - if (strrpos($path, '.') !== false) { |
|
| 295 | - $ext = substr($path, strrpos($path, '.')); |
|
| 296 | - } else { |
|
| 297 | - $ext = ''; |
|
| 298 | - } |
|
| 299 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 300 | - if ($this->file_exists($path)) { |
|
| 301 | - $source = $this->fopen($path, 'r'); |
|
| 302 | - file_put_contents($tmpFile, $source); |
|
| 303 | - } |
|
| 304 | - $handle = fopen($tmpFile, $mode); |
|
| 305 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 306 | - $this->writeBack($tmpFile, $path); |
|
| 307 | - }); |
|
| 308 | - } |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - public function file_exists($path) { |
|
| 313 | - $path = $this->normalizePath($path); |
|
| 314 | - return (bool)$this->stat($path); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - public function rename($source, $target) { |
|
| 318 | - $source = $this->normalizePath($source); |
|
| 319 | - $target = $this->normalizePath($target); |
|
| 320 | - $this->remove($target); |
|
| 321 | - $this->getCache()->move($source, $target); |
|
| 322 | - $this->touch(dirname($target)); |
|
| 323 | - return true; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - public function getMimeType($path) { |
|
| 327 | - $path = $this->normalizePath($path); |
|
| 328 | - $stat = $this->stat($path); |
|
| 329 | - if (is_array($stat)) { |
|
| 330 | - return $stat['mimetype']; |
|
| 331 | - } else { |
|
| 332 | - return false; |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - public function touch($path, $mtime = null) { |
|
| 337 | - if (is_null($mtime)) { |
|
| 338 | - $mtime = time(); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - $path = $this->normalizePath($path); |
|
| 342 | - $dirName = dirname($path); |
|
| 343 | - $parentExists = $this->is_dir($dirName); |
|
| 344 | - if (!$parentExists) { |
|
| 345 | - return false; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $stat = $this->stat($path); |
|
| 349 | - if (is_array($stat)) { |
|
| 350 | - // update existing mtime in db |
|
| 351 | - $stat['mtime'] = $mtime; |
|
| 352 | - $this->getCache()->update($stat['fileid'], $stat); |
|
| 353 | - } else { |
|
| 354 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 355 | - // create new file |
|
| 356 | - $stat = array( |
|
| 357 | - 'etag' => $this->getETag($path), |
|
| 358 | - 'mimetype' => $mimeType, |
|
| 359 | - 'size' => 0, |
|
| 360 | - 'mtime' => $mtime, |
|
| 361 | - 'storage_mtime' => $mtime, |
|
| 362 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 363 | - ); |
|
| 364 | - $fileId = $this->getCache()->put($path, $stat); |
|
| 365 | - try { |
|
| 366 | - //read an empty file from memory |
|
| 367 | - $this->objectStore->writeObject($this->getURN($fileId), fopen('php://memory', 'r')); |
|
| 368 | - } catch (\Exception $ex) { |
|
| 369 | - $this->getCache()->remove($path); |
|
| 370 | - $this->logger->logException($ex, [ |
|
| 371 | - 'app' => 'objectstore', |
|
| 372 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 373 | - ]); |
|
| 374 | - return false; |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - return true; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - public function writeBack($tmpFile, $path) { |
|
| 381 | - $stat = $this->stat($path); |
|
| 382 | - if (empty($stat)) { |
|
| 383 | - // create new file |
|
| 384 | - $stat = array( |
|
| 385 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 386 | - ); |
|
| 387 | - } |
|
| 388 | - // update stat with new data |
|
| 389 | - $mTime = time(); |
|
| 390 | - $stat['size'] = filesize($tmpFile); |
|
| 391 | - $stat['mtime'] = $mTime; |
|
| 392 | - $stat['storage_mtime'] = $mTime; |
|
| 393 | - $stat['mimetype'] = \OC::$server->getMimeTypeDetector()->detect($tmpFile); |
|
| 394 | - $stat['etag'] = $this->getETag($path); |
|
| 395 | - |
|
| 396 | - $fileId = $this->getCache()->put($path, $stat); |
|
| 397 | - try { |
|
| 398 | - //upload to object storage |
|
| 399 | - $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 400 | - } catch (\Exception $ex) { |
|
| 401 | - $this->getCache()->remove($path); |
|
| 402 | - $this->logger->logException($ex, [ |
|
| 403 | - 'app' => 'objectstore', |
|
| 404 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 405 | - ]); |
|
| 406 | - throw $ex; // make this bubble up |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * external changes are not supported, exclusive access to the object storage is assumed |
|
| 412 | - * |
|
| 413 | - * @param string $path |
|
| 414 | - * @param int $time |
|
| 415 | - * @return false |
|
| 416 | - */ |
|
| 417 | - public function hasUpdated($path, $time) { |
|
| 418 | - return false; |
|
| 419 | - } |
|
| 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 | + } else { |
|
| 198 | + //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + $this->getCache()->remove($path); |
|
| 202 | + return true; |
|
| 203 | + } |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + public function stat($path) { |
|
| 208 | + $path = $this->normalizePath($path); |
|
| 209 | + $cacheEntry = $this->getCache()->get($path); |
|
| 210 | + if ($cacheEntry instanceof CacheEntry) { |
|
| 211 | + return $cacheEntry->getData(); |
|
| 212 | + } else { |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 219 | + * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 220 | + * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 221 | + * |
|
| 222 | + * @param int $fileId the fileid |
|
| 223 | + * @return null|string the unified resource name used to identify the object |
|
| 224 | + */ |
|
| 225 | + protected function getURN($fileId) { |
|
| 226 | + if (is_numeric($fileId)) { |
|
| 227 | + return $this->objectPrefix . $fileId; |
|
| 228 | + } |
|
| 229 | + return null; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + public function opendir($path) { |
|
| 233 | + $path = $this->normalizePath($path); |
|
| 234 | + |
|
| 235 | + try { |
|
| 236 | + $files = array(); |
|
| 237 | + $folderContents = $this->getCache()->getFolderContents($path); |
|
| 238 | + foreach ($folderContents as $file) { |
|
| 239 | + $files[] = $file['name']; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + return IteratorDirectory::wrap($files); |
|
| 243 | + } catch (\Exception $e) { |
|
| 244 | + $this->logger->logException($e); |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + public function filetype($path) { |
|
| 250 | + $path = $this->normalizePath($path); |
|
| 251 | + $stat = $this->stat($path); |
|
| 252 | + if ($stat) { |
|
| 253 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 254 | + return 'dir'; |
|
| 255 | + } |
|
| 256 | + return 'file'; |
|
| 257 | + } else { |
|
| 258 | + return false; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + public function fopen($path, $mode) { |
|
| 263 | + $path = $this->normalizePath($path); |
|
| 264 | + |
|
| 265 | + switch ($mode) { |
|
| 266 | + case 'r': |
|
| 267 | + case 'rb': |
|
| 268 | + $stat = $this->stat($path); |
|
| 269 | + if (is_array($stat)) { |
|
| 270 | + try { |
|
| 271 | + return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 272 | + } catch (\Exception $ex) { |
|
| 273 | + $this->logger->logException($ex, [ |
|
| 274 | + 'app' => 'objectstore', |
|
| 275 | + 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 276 | + ]); |
|
| 277 | + return false; |
|
| 278 | + } |
|
| 279 | + } else { |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + case 'w': |
|
| 283 | + case 'wb': |
|
| 284 | + case 'a': |
|
| 285 | + case 'ab': |
|
| 286 | + case 'r+': |
|
| 287 | + case 'w+': |
|
| 288 | + case 'wb+': |
|
| 289 | + case 'a+': |
|
| 290 | + case 'x': |
|
| 291 | + case 'x+': |
|
| 292 | + case 'c': |
|
| 293 | + case 'c+': |
|
| 294 | + if (strrpos($path, '.') !== false) { |
|
| 295 | + $ext = substr($path, strrpos($path, '.')); |
|
| 296 | + } else { |
|
| 297 | + $ext = ''; |
|
| 298 | + } |
|
| 299 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 300 | + if ($this->file_exists($path)) { |
|
| 301 | + $source = $this->fopen($path, 'r'); |
|
| 302 | + file_put_contents($tmpFile, $source); |
|
| 303 | + } |
|
| 304 | + $handle = fopen($tmpFile, $mode); |
|
| 305 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 306 | + $this->writeBack($tmpFile, $path); |
|
| 307 | + }); |
|
| 308 | + } |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + public function file_exists($path) { |
|
| 313 | + $path = $this->normalizePath($path); |
|
| 314 | + return (bool)$this->stat($path); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + public function rename($source, $target) { |
|
| 318 | + $source = $this->normalizePath($source); |
|
| 319 | + $target = $this->normalizePath($target); |
|
| 320 | + $this->remove($target); |
|
| 321 | + $this->getCache()->move($source, $target); |
|
| 322 | + $this->touch(dirname($target)); |
|
| 323 | + return true; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + public function getMimeType($path) { |
|
| 327 | + $path = $this->normalizePath($path); |
|
| 328 | + $stat = $this->stat($path); |
|
| 329 | + if (is_array($stat)) { |
|
| 330 | + return $stat['mimetype']; |
|
| 331 | + } else { |
|
| 332 | + return false; |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + public function touch($path, $mtime = null) { |
|
| 337 | + if (is_null($mtime)) { |
|
| 338 | + $mtime = time(); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + $path = $this->normalizePath($path); |
|
| 342 | + $dirName = dirname($path); |
|
| 343 | + $parentExists = $this->is_dir($dirName); |
|
| 344 | + if (!$parentExists) { |
|
| 345 | + return false; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $stat = $this->stat($path); |
|
| 349 | + if (is_array($stat)) { |
|
| 350 | + // update existing mtime in db |
|
| 351 | + $stat['mtime'] = $mtime; |
|
| 352 | + $this->getCache()->update($stat['fileid'], $stat); |
|
| 353 | + } else { |
|
| 354 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 355 | + // create new file |
|
| 356 | + $stat = array( |
|
| 357 | + 'etag' => $this->getETag($path), |
|
| 358 | + 'mimetype' => $mimeType, |
|
| 359 | + 'size' => 0, |
|
| 360 | + 'mtime' => $mtime, |
|
| 361 | + 'storage_mtime' => $mtime, |
|
| 362 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 363 | + ); |
|
| 364 | + $fileId = $this->getCache()->put($path, $stat); |
|
| 365 | + try { |
|
| 366 | + //read an empty file from memory |
|
| 367 | + $this->objectStore->writeObject($this->getURN($fileId), fopen('php://memory', 'r')); |
|
| 368 | + } catch (\Exception $ex) { |
|
| 369 | + $this->getCache()->remove($path); |
|
| 370 | + $this->logger->logException($ex, [ |
|
| 371 | + 'app' => 'objectstore', |
|
| 372 | + 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 373 | + ]); |
|
| 374 | + return false; |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + return true; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + public function writeBack($tmpFile, $path) { |
|
| 381 | + $stat = $this->stat($path); |
|
| 382 | + if (empty($stat)) { |
|
| 383 | + // create new file |
|
| 384 | + $stat = array( |
|
| 385 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 386 | + ); |
|
| 387 | + } |
|
| 388 | + // update stat with new data |
|
| 389 | + $mTime = time(); |
|
| 390 | + $stat['size'] = filesize($tmpFile); |
|
| 391 | + $stat['mtime'] = $mTime; |
|
| 392 | + $stat['storage_mtime'] = $mTime; |
|
| 393 | + $stat['mimetype'] = \OC::$server->getMimeTypeDetector()->detect($tmpFile); |
|
| 394 | + $stat['etag'] = $this->getETag($path); |
|
| 395 | + |
|
| 396 | + $fileId = $this->getCache()->put($path, $stat); |
|
| 397 | + try { |
|
| 398 | + //upload to object storage |
|
| 399 | + $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 400 | + } catch (\Exception $ex) { |
|
| 401 | + $this->getCache()->remove($path); |
|
| 402 | + $this->logger->logException($ex, [ |
|
| 403 | + 'app' => 'objectstore', |
|
| 404 | + 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 405 | + ]); |
|
| 406 | + throw $ex; // make this bubble up |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * external changes are not supported, exclusive access to the object storage is assumed |
|
| 412 | + * |
|
| 413 | + * @param string $path |
|
| 414 | + * @param int $time |
|
| 415 | + * @return false |
|
| 416 | + */ |
|
| 417 | + public function hasUpdated($path, $time) { |
|
| 418 | + return false; |
|
| 419 | + } |
|
| 420 | 420 | } |
@@ -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 | } else { |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | protected function getURN($fileId) { |
| 226 | 226 | if (is_numeric($fileId)) { |
| 227 | - return $this->objectPrefix . $fileId; |
|
| 227 | + return $this->objectPrefix.$fileId; |
|
| 228 | 228 | } |
| 229 | 229 | return null; |
| 230 | 230 | } |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | } catch (\Exception $ex) { |
| 273 | 273 | $this->logger->logException($ex, [ |
| 274 | 274 | 'app' => 'objectstore', |
| 275 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 275 | + 'message' => 'Count not get object '.$this->getURN($stat['fileid']).' for file '.$path, |
|
| 276 | 276 | ]); |
| 277 | 277 | return false; |
| 278 | 278 | } |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | file_put_contents($tmpFile, $source); |
| 303 | 303 | } |
| 304 | 304 | $handle = fopen($tmpFile, $mode); |
| 305 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 305 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 306 | 306 | $this->writeBack($tmpFile, $path); |
| 307 | 307 | }); |
| 308 | 308 | } |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | |
| 312 | 312 | public function file_exists($path) { |
| 313 | 313 | $path = $this->normalizePath($path); |
| 314 | - return (bool)$this->stat($path); |
|
| 314 | + return (bool) $this->stat($path); |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | public function rename($source, $target) { |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | $this->getCache()->remove($path); |
| 370 | 370 | $this->logger->logException($ex, [ |
| 371 | 371 | 'app' => 'objectstore', |
| 372 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 372 | + 'message' => 'Could not create object '.$this->getURN($fileId).' for '.$path, |
|
| 373 | 373 | ]); |
| 374 | 374 | return false; |
| 375 | 375 | } |
@@ -401,7 +401,7 @@ discard block |
||
| 401 | 401 | $this->getCache()->remove($path); |
| 402 | 402 | $this->logger->logException($ex, [ |
| 403 | 403 | 'app' => 'objectstore', |
| 404 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 404 | + 'message' => 'Could not create object '.$this->getURN($fileId).' for '.$path, |
|
| 405 | 405 | ]); |
| 406 | 406 | throw $ex; // make this bubble up |
| 407 | 407 | } |
@@ -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); |
@@ -1063,7 +1063,7 @@ discard block |
||
| 1063 | 1063 | * @param string $app |
| 1064 | 1064 | * @param \OCP\IConfig $config |
| 1065 | 1065 | * @param \OCP\IL10N $l |
| 1066 | - * @return bool |
|
| 1066 | + * @return string |
|
| 1067 | 1067 | * |
| 1068 | 1068 | * @throws Exception if app is not compatible with this version of ownCloud |
| 1069 | 1069 | * @throws Exception if no app-name was specified |
@@ -1243,6 +1243,11 @@ discard block |
||
| 1243 | 1243 | } |
| 1244 | 1244 | } |
| 1245 | 1245 | |
| 1246 | + /** |
|
| 1247 | + * @param string $lang |
|
| 1248 | + * |
|
| 1249 | + * @return string |
|
| 1250 | + */ |
|
| 1246 | 1251 | protected static function findBestL10NOption($options, $lang) { |
| 1247 | 1252 | $fallback = $similarLangFallback = $englishFallback = false; |
| 1248 | 1253 | |
@@ -61,1184 +61,1184 @@ |
||
| 61 | 61 | * upgrading and removing apps. |
| 62 | 62 | */ |
| 63 | 63 | class OC_App { |
| 64 | - static private $appVersion = []; |
|
| 65 | - static private $adminForms = array(); |
|
| 66 | - static private $personalForms = array(); |
|
| 67 | - static private $appInfo = array(); |
|
| 68 | - static private $appTypes = array(); |
|
| 69 | - static private $loadedApps = array(); |
|
| 70 | - static private $altLogin = array(); |
|
| 71 | - static private $alreadyRegistered = []; |
|
| 72 | - const officialApp = 200; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * clean the appId |
|
| 76 | - * |
|
| 77 | - * @param string|boolean $app AppId that needs to be cleaned |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public static function cleanAppId($app) { |
|
| 81 | - return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Check if an app is loaded |
|
| 86 | - * |
|
| 87 | - * @param string $app |
|
| 88 | - * @return bool |
|
| 89 | - */ |
|
| 90 | - public static function isAppLoaded($app) { |
|
| 91 | - return in_array($app, self::$loadedApps, true); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * loads all apps |
|
| 96 | - * |
|
| 97 | - * @param string[] | string | null $types |
|
| 98 | - * @return bool |
|
| 99 | - * |
|
| 100 | - * This function walks through the ownCloud directory and loads all apps |
|
| 101 | - * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 102 | - * exists. |
|
| 103 | - * |
|
| 104 | - * if $types is set, only apps of those types will be loaded |
|
| 105 | - */ |
|
| 106 | - public static function loadApps($types = null) { |
|
| 107 | - if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - // Load the enabled apps here |
|
| 111 | - $apps = self::getEnabledApps(); |
|
| 112 | - |
|
| 113 | - // Add each apps' folder as allowed class path |
|
| 114 | - foreach($apps as $app) { |
|
| 115 | - $path = self::getAppPath($app); |
|
| 116 | - if($path !== false) { |
|
| 117 | - self::registerAutoloading($app, $path); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // prevent app.php from printing output |
|
| 122 | - ob_start(); |
|
| 123 | - foreach ($apps as $app) { |
|
| 124 | - if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 125 | - self::loadApp($app); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - ob_end_clean(); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * load a single app |
|
| 135 | - * |
|
| 136 | - * @param string $app |
|
| 137 | - */ |
|
| 138 | - public static function loadApp($app) { |
|
| 139 | - self::$loadedApps[] = $app; |
|
| 140 | - $appPath = self::getAppPath($app); |
|
| 141 | - if($appPath === false) { |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // in case someone calls loadApp() directly |
|
| 146 | - self::registerAutoloading($app, $appPath); |
|
| 147 | - |
|
| 148 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 149 | - \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 150 | - self::requireAppFile($app); |
|
| 151 | - if (self::isType($app, array('authentication'))) { |
|
| 152 | - // since authentication apps affect the "is app enabled for group" check, |
|
| 153 | - // the enabled apps cache needs to be cleared to make sure that the |
|
| 154 | - // next time getEnableApps() is called it will also include apps that were |
|
| 155 | - // enabled for groups |
|
| 156 | - self::$enabledAppsCache = array(); |
|
| 157 | - } |
|
| 158 | - \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $info = self::getAppInfo($app); |
|
| 162 | - if (!empty($info['activity']['filters'])) { |
|
| 163 | - foreach ($info['activity']['filters'] as $filter) { |
|
| 164 | - \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - if (!empty($info['activity']['settings'])) { |
|
| 168 | - foreach ($info['activity']['settings'] as $setting) { |
|
| 169 | - \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - if (!empty($info['activity']['providers'])) { |
|
| 173 | - foreach ($info['activity']['providers'] as $provider) { |
|
| 174 | - \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @internal |
|
| 181 | - * @param string $app |
|
| 182 | - * @param string $path |
|
| 183 | - */ |
|
| 184 | - public static function registerAutoloading($app, $path) { |
|
| 185 | - $key = $app . '-' . $path; |
|
| 186 | - if(isset(self::$alreadyRegistered[$key])) { |
|
| 187 | - return; |
|
| 188 | - } |
|
| 189 | - self::$alreadyRegistered[$key] = true; |
|
| 190 | - // Register on PSR-4 composer autoloader |
|
| 191 | - $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 192 | - \OC::$server->registerNamespace($app, $appNamespace); |
|
| 193 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 194 | - if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 195 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - // Register on legacy autoloader |
|
| 199 | - \OC::$loader->addValidRoot($path); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Load app.php from the given app |
|
| 204 | - * |
|
| 205 | - * @param string $app app name |
|
| 206 | - */ |
|
| 207 | - private static function requireAppFile($app) { |
|
| 208 | - try { |
|
| 209 | - // encapsulated here to avoid variable scope conflicts |
|
| 210 | - require_once $app . '/appinfo/app.php'; |
|
| 211 | - } catch (Error $ex) { |
|
| 212 | - \OC::$server->getLogger()->logException($ex); |
|
| 213 | - $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 214 | - if (!in_array($app, $blacklist)) { |
|
| 215 | - self::disable($app); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * check if an app is of a specific type |
|
| 222 | - * |
|
| 223 | - * @param string $app |
|
| 224 | - * @param string|array $types |
|
| 225 | - * @return bool |
|
| 226 | - */ |
|
| 227 | - public static function isType($app, $types) { |
|
| 228 | - if (is_string($types)) { |
|
| 229 | - $types = array($types); |
|
| 230 | - } |
|
| 231 | - $appTypes = self::getAppTypes($app); |
|
| 232 | - foreach ($types as $type) { |
|
| 233 | - if (array_search($type, $appTypes) !== false) { |
|
| 234 | - return true; |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * get the types of an app |
|
| 242 | - * |
|
| 243 | - * @param string $app |
|
| 244 | - * @return array |
|
| 245 | - */ |
|
| 246 | - private static function getAppTypes($app) { |
|
| 247 | - //load the cache |
|
| 248 | - if (count(self::$appTypes) == 0) { |
|
| 249 | - self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if (isset(self::$appTypes[$app])) { |
|
| 253 | - return explode(',', self::$appTypes[$app]); |
|
| 254 | - } else { |
|
| 255 | - return array(); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * read app types from info.xml and cache them in the database |
|
| 261 | - */ |
|
| 262 | - public static function setAppTypes($app) { |
|
| 263 | - $appData = self::getAppInfo($app); |
|
| 264 | - if(!is_array($appData)) { |
|
| 265 | - return; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - if (isset($appData['types'])) { |
|
| 269 | - $appTypes = implode(',', $appData['types']); |
|
| 270 | - } else { |
|
| 271 | - $appTypes = ''; |
|
| 272 | - $appData['types'] = []; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes); |
|
| 276 | - |
|
| 277 | - if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) { |
|
| 278 | - $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes'); |
|
| 279 | - if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 280 | - \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes'); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * get all enabled apps |
|
| 287 | - */ |
|
| 288 | - protected static $enabledAppsCache = array(); |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Returns apps enabled for the current user. |
|
| 292 | - * |
|
| 293 | - * @param bool $forceRefresh whether to refresh the cache |
|
| 294 | - * @param bool $all whether to return apps for all users, not only the |
|
| 295 | - * currently logged in one |
|
| 296 | - * @return string[] |
|
| 297 | - */ |
|
| 298 | - public static function getEnabledApps($forceRefresh = false, $all = false) { |
|
| 299 | - if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 300 | - return array(); |
|
| 301 | - } |
|
| 302 | - // in incognito mode or when logged out, $user will be false, |
|
| 303 | - // which is also the case during an upgrade |
|
| 304 | - $appManager = \OC::$server->getAppManager(); |
|
| 305 | - if ($all) { |
|
| 306 | - $user = null; |
|
| 307 | - } else { |
|
| 308 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - if (is_null($user)) { |
|
| 312 | - $apps = $appManager->getInstalledApps(); |
|
| 313 | - } else { |
|
| 314 | - $apps = $appManager->getEnabledAppsForUser($user); |
|
| 315 | - } |
|
| 316 | - $apps = array_filter($apps, function ($app) { |
|
| 317 | - return $app !== 'files';//we add this manually |
|
| 318 | - }); |
|
| 319 | - sort($apps); |
|
| 320 | - array_unshift($apps, 'files'); |
|
| 321 | - return $apps; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * checks whether or not an app is enabled |
|
| 326 | - * |
|
| 327 | - * @param string $app app |
|
| 328 | - * @return bool |
|
| 329 | - * |
|
| 330 | - * This function checks whether or not an app is enabled. |
|
| 331 | - */ |
|
| 332 | - public static function isEnabled($app) { |
|
| 333 | - return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * enables an app |
|
| 338 | - * |
|
| 339 | - * @param string $appId |
|
| 340 | - * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 341 | - * @throws \Exception |
|
| 342 | - * @return void |
|
| 343 | - * |
|
| 344 | - * This function set an app as enabled in appconfig. |
|
| 345 | - */ |
|
| 346 | - public function enable($appId, |
|
| 347 | - $groups = null) { |
|
| 348 | - self::$enabledAppsCache = []; // flush |
|
| 349 | - |
|
| 350 | - // Check if app is already downloaded |
|
| 351 | - $installer = new Installer( |
|
| 352 | - \OC::$server->getAppFetcher(), |
|
| 353 | - \OC::$server->getHTTPClientService(), |
|
| 354 | - \OC::$server->getTempManager(), |
|
| 355 | - \OC::$server->getLogger(), |
|
| 356 | - \OC::$server->getConfig() |
|
| 357 | - ); |
|
| 358 | - $isDownloaded = $installer->isDownloaded($appId); |
|
| 359 | - |
|
| 360 | - if(!$isDownloaded) { |
|
| 361 | - $installer->downloadApp($appId); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - $installer->installApp($appId); |
|
| 365 | - |
|
| 366 | - $appManager = \OC::$server->getAppManager(); |
|
| 367 | - if (!is_null($groups)) { |
|
| 368 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 369 | - $groupsList = []; |
|
| 370 | - foreach ($groups as $group) { |
|
| 371 | - $groupItem = $groupManager->get($group); |
|
| 372 | - if ($groupItem instanceof \OCP\IGroup) { |
|
| 373 | - $groupsList[] = $groupManager->get($group); |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - $appManager->enableAppForGroups($appId, $groupsList); |
|
| 377 | - } else { |
|
| 378 | - $appManager->enableApp($appId); |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * @param string $app |
|
| 384 | - * @return bool |
|
| 385 | - */ |
|
| 386 | - public static function removeApp($app) { |
|
| 387 | - if (\OC::$server->getAppManager()->isShipped($app)) { |
|
| 388 | - return false; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - $installer = new Installer( |
|
| 392 | - \OC::$server->getAppFetcher(), |
|
| 393 | - \OC::$server->getHTTPClientService(), |
|
| 394 | - \OC::$server->getTempManager(), |
|
| 395 | - \OC::$server->getLogger(), |
|
| 396 | - \OC::$server->getConfig() |
|
| 397 | - ); |
|
| 398 | - return $installer->removeApp($app); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * This function set an app as disabled in appconfig. |
|
| 403 | - * |
|
| 404 | - * @param string $app app |
|
| 405 | - * @throws Exception |
|
| 406 | - */ |
|
| 407 | - public static function disable($app) { |
|
| 408 | - // flush |
|
| 409 | - self::$enabledAppsCache = array(); |
|
| 410 | - |
|
| 411 | - // run uninstall steps |
|
| 412 | - $appData = OC_App::getAppInfo($app); |
|
| 413 | - if (!is_null($appData)) { |
|
| 414 | - OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - // emit disable hook - needed anymore ? |
|
| 418 | - \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 419 | - |
|
| 420 | - // finally disable it |
|
| 421 | - $appManager = \OC::$server->getAppManager(); |
|
| 422 | - $appManager->disableApp($app); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - // This is private as well. It simply works, so don't ask for more details |
|
| 426 | - private static function proceedNavigation($list) { |
|
| 427 | - usort($list, function($a, $b) { |
|
| 428 | - if (isset($a['order']) && isset($b['order'])) { |
|
| 429 | - return ($a['order'] < $b['order']) ? -1 : 1; |
|
| 430 | - } else if (isset($a['order']) || isset($b['order'])) { |
|
| 431 | - return isset($a['order']) ? -1 : 1; |
|
| 432 | - } else { |
|
| 433 | - return ($a['name'] < $b['name']) ? -1 : 1; |
|
| 434 | - } |
|
| 435 | - }); |
|
| 436 | - |
|
| 437 | - $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 438 | - foreach ($list as $index => &$navEntry) { |
|
| 439 | - if ($navEntry['id'] == $activeApp) { |
|
| 440 | - $navEntry['active'] = true; |
|
| 441 | - } else { |
|
| 442 | - $navEntry['active'] = false; |
|
| 443 | - } |
|
| 444 | - } |
|
| 445 | - unset($navEntry); |
|
| 446 | - |
|
| 447 | - return $list; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Get the path where to install apps |
|
| 452 | - * |
|
| 453 | - * @return string|false |
|
| 454 | - */ |
|
| 455 | - public static function getInstallPath() { |
|
| 456 | - if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 457 | - return false; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 461 | - if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 462 | - return $dir['path']; |
|
| 463 | - } |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 467 | - return null; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * search for an app in all app-directories |
|
| 473 | - * |
|
| 474 | - * @param string $appId |
|
| 475 | - * @return false|string |
|
| 476 | - */ |
|
| 477 | - public static function findAppInDirectories($appId) { |
|
| 478 | - $sanitizedAppId = self::cleanAppId($appId); |
|
| 479 | - if($sanitizedAppId !== $appId) { |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 482 | - static $app_dir = array(); |
|
| 483 | - |
|
| 484 | - if (isset($app_dir[$appId])) { |
|
| 485 | - return $app_dir[$appId]; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - $possibleApps = array(); |
|
| 489 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 490 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 491 | - $possibleApps[] = $dir; |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - if (empty($possibleApps)) { |
|
| 496 | - return false; |
|
| 497 | - } elseif (count($possibleApps) === 1) { |
|
| 498 | - $dir = array_shift($possibleApps); |
|
| 499 | - $app_dir[$appId] = $dir; |
|
| 500 | - return $dir; |
|
| 501 | - } else { |
|
| 502 | - $versionToLoad = array(); |
|
| 503 | - foreach ($possibleApps as $possibleApp) { |
|
| 504 | - $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 505 | - if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 506 | - $versionToLoad = array( |
|
| 507 | - 'dir' => $possibleApp, |
|
| 508 | - 'version' => $version, |
|
| 509 | - ); |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - $app_dir[$appId] = $versionToLoad['dir']; |
|
| 513 | - return $versionToLoad['dir']; |
|
| 514 | - //TODO - write test |
|
| 515 | - } |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Get the directory for the given app. |
|
| 520 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 521 | - * |
|
| 522 | - * @param string $appId |
|
| 523 | - * @return string|false |
|
| 524 | - */ |
|
| 525 | - public static function getAppPath($appId) { |
|
| 526 | - if ($appId === null || trim($appId) === '') { |
|
| 527 | - return false; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 531 | - return $dir['path'] . '/' . $appId; |
|
| 532 | - } |
|
| 533 | - return false; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * Get the path for the given app on the access |
|
| 538 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 539 | - * |
|
| 540 | - * @param string $appId |
|
| 541 | - * @return string|false |
|
| 542 | - */ |
|
| 543 | - public static function getAppWebPath($appId) { |
|
| 544 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 545 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 546 | - } |
|
| 547 | - return false; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * get the last version of the app from appinfo/info.xml |
|
| 552 | - * |
|
| 553 | - * @param string $appId |
|
| 554 | - * @param bool $useCache |
|
| 555 | - * @return string |
|
| 556 | - */ |
|
| 557 | - public static function getAppVersion($appId, $useCache = true) { |
|
| 558 | - if($useCache && isset(self::$appVersion[$appId])) { |
|
| 559 | - return self::$appVersion[$appId]; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - $file = self::getAppPath($appId); |
|
| 563 | - self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0'; |
|
| 564 | - return self::$appVersion[$appId]; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * get app's version based on it's path |
|
| 569 | - * |
|
| 570 | - * @param string $path |
|
| 571 | - * @return string |
|
| 572 | - */ |
|
| 573 | - public static function getAppVersionByPath($path) { |
|
| 574 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 575 | - $appData = self::getAppInfo($infoFile, true); |
|
| 576 | - return isset($appData['version']) ? $appData['version'] : ''; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Read all app metadata from the info.xml file |
|
| 582 | - * |
|
| 583 | - * @param string $appId id of the app or the path of the info.xml file |
|
| 584 | - * @param bool $path |
|
| 585 | - * @param string $lang |
|
| 586 | - * @return array|null |
|
| 587 | - * @note all data is read from info.xml, not just pre-defined fields |
|
| 588 | - */ |
|
| 589 | - public static function getAppInfo($appId, $path = false, $lang = null) { |
|
| 590 | - if ($path) { |
|
| 591 | - $file = $appId; |
|
| 592 | - } else { |
|
| 593 | - if ($lang === null && isset(self::$appInfo[$appId])) { |
|
| 594 | - return self::$appInfo[$appId]; |
|
| 595 | - } |
|
| 596 | - $appPath = self::getAppPath($appId); |
|
| 597 | - if($appPath === false) { |
|
| 598 | - return null; |
|
| 599 | - } |
|
| 600 | - $file = $appPath . '/appinfo/info.xml'; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo')); |
|
| 604 | - $data = $parser->parse($file); |
|
| 605 | - |
|
| 606 | - if (is_array($data)) { |
|
| 607 | - $data = OC_App::parseAppInfo($data, $lang); |
|
| 608 | - } |
|
| 609 | - if(isset($data['ocsid'])) { |
|
| 610 | - $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
|
| 611 | - if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 612 | - $data['ocsid'] = $storedId; |
|
| 613 | - } |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - if ($lang === null) { |
|
| 617 | - self::$appInfo[$appId] = $data; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - return $data; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - /** |
|
| 624 | - * Returns the navigation |
|
| 625 | - * |
|
| 626 | - * @return array |
|
| 627 | - * |
|
| 628 | - * This function returns an array containing all entries added. The |
|
| 629 | - * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 630 | - * given for each app the following keys exist: |
|
| 631 | - * - active: boolean, signals if the user is on this navigation entry |
|
| 632 | - */ |
|
| 633 | - public static function getNavigation() { |
|
| 634 | - $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 635 | - return self::proceedNavigation($entries); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Returns the Settings Navigation |
|
| 640 | - * |
|
| 641 | - * @return string[] |
|
| 642 | - * |
|
| 643 | - * This function returns an array containing all settings pages added. The |
|
| 644 | - * entries are sorted by the key 'order' ascending. |
|
| 645 | - */ |
|
| 646 | - public static function getSettingsNavigation() { |
|
| 647 | - $entries = OC::$server->getNavigationManager()->getAll('settings'); |
|
| 648 | - return self::proceedNavigation($entries); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * get the id of loaded app |
|
| 653 | - * |
|
| 654 | - * @return string |
|
| 655 | - */ |
|
| 656 | - public static function getCurrentApp() { |
|
| 657 | - $request = \OC::$server->getRequest(); |
|
| 658 | - $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 659 | - $topFolder = substr($script, 0, strpos($script, '/')); |
|
| 660 | - if (empty($topFolder)) { |
|
| 661 | - $path_info = $request->getPathInfo(); |
|
| 662 | - if ($path_info) { |
|
| 663 | - $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 664 | - } |
|
| 665 | - } |
|
| 666 | - if ($topFolder == 'apps') { |
|
| 667 | - $length = strlen($topFolder); |
|
| 668 | - return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1); |
|
| 669 | - } else { |
|
| 670 | - return $topFolder; |
|
| 671 | - } |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * @param string $type |
|
| 676 | - * @return array |
|
| 677 | - */ |
|
| 678 | - public static function getForms($type) { |
|
| 679 | - $forms = array(); |
|
| 680 | - switch ($type) { |
|
| 681 | - case 'admin': |
|
| 682 | - $source = self::$adminForms; |
|
| 683 | - break; |
|
| 684 | - case 'personal': |
|
| 685 | - $source = self::$personalForms; |
|
| 686 | - break; |
|
| 687 | - default: |
|
| 688 | - return array(); |
|
| 689 | - } |
|
| 690 | - foreach ($source as $form) { |
|
| 691 | - $forms[] = include $form; |
|
| 692 | - } |
|
| 693 | - return $forms; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * register an admin form to be shown |
|
| 698 | - * |
|
| 699 | - * @param string $app |
|
| 700 | - * @param string $page |
|
| 701 | - */ |
|
| 702 | - public static function registerAdmin($app, $page) { |
|
| 703 | - self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * register a personal form to be shown |
|
| 708 | - * @param string $app |
|
| 709 | - * @param string $page |
|
| 710 | - */ |
|
| 711 | - public static function registerPersonal($app, $page) { |
|
| 712 | - self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - /** |
|
| 716 | - * @param array $entry |
|
| 717 | - */ |
|
| 718 | - public static function registerLogIn(array $entry) { |
|
| 719 | - self::$altLogin[] = $entry; |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * @return array |
|
| 724 | - */ |
|
| 725 | - public static function getAlternativeLogIns() { |
|
| 726 | - return self::$altLogin; |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - /** |
|
| 730 | - * get a list of all apps in the apps folder |
|
| 731 | - * |
|
| 732 | - * @return array an array of app names (string IDs) |
|
| 733 | - * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 734 | - */ |
|
| 735 | - public static function getAllApps() { |
|
| 736 | - |
|
| 737 | - $apps = array(); |
|
| 738 | - |
|
| 739 | - foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 740 | - if (!is_readable($apps_dir['path'])) { |
|
| 741 | - \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 742 | - continue; |
|
| 743 | - } |
|
| 744 | - $dh = opendir($apps_dir['path']); |
|
| 745 | - |
|
| 746 | - if (is_resource($dh)) { |
|
| 747 | - while (($file = readdir($dh)) !== false) { |
|
| 748 | - |
|
| 749 | - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 750 | - |
|
| 751 | - $apps[] = $file; |
|
| 752 | - } |
|
| 753 | - } |
|
| 754 | - } |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - return $apps; |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - /** |
|
| 761 | - * List all apps, this is used in apps.php |
|
| 762 | - * |
|
| 763 | - * @return array |
|
| 764 | - */ |
|
| 765 | - public function listAllApps() { |
|
| 766 | - $installedApps = OC_App::getAllApps(); |
|
| 767 | - |
|
| 768 | - $appManager = \OC::$server->getAppManager(); |
|
| 769 | - //we don't want to show configuration for these |
|
| 770 | - $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 771 | - $appList = array(); |
|
| 772 | - $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 773 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 774 | - |
|
| 775 | - foreach ($installedApps as $app) { |
|
| 776 | - if (array_search($app, $blacklist) === false) { |
|
| 777 | - |
|
| 778 | - $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 779 | - if (!is_array($info)) { |
|
| 780 | - \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 781 | - continue; |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - if (!isset($info['name'])) { |
|
| 785 | - \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 786 | - continue; |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no'); |
|
| 790 | - $info['groups'] = null; |
|
| 791 | - if ($enabled === 'yes') { |
|
| 792 | - $active = true; |
|
| 793 | - } else if ($enabled === 'no') { |
|
| 794 | - $active = false; |
|
| 795 | - } else { |
|
| 796 | - $active = true; |
|
| 797 | - $info['groups'] = $enabled; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - $info['active'] = $active; |
|
| 801 | - |
|
| 802 | - if ($appManager->isShipped($app)) { |
|
| 803 | - $info['internal'] = true; |
|
| 804 | - $info['level'] = self::officialApp; |
|
| 805 | - $info['removable'] = false; |
|
| 806 | - } else { |
|
| 807 | - $info['internal'] = false; |
|
| 808 | - $info['removable'] = true; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - $appPath = self::getAppPath($app); |
|
| 812 | - if($appPath !== false) { |
|
| 813 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 814 | - if (file_exists($appIcon)) { |
|
| 815 | - $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 816 | - $info['previewAsIcon'] = true; |
|
| 817 | - } else { |
|
| 818 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 819 | - if (file_exists($appIcon)) { |
|
| 820 | - $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 821 | - $info['previewAsIcon'] = true; |
|
| 822 | - } |
|
| 823 | - } |
|
| 824 | - } |
|
| 825 | - // fix documentation |
|
| 826 | - if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 827 | - foreach ($info['documentation'] as $key => $url) { |
|
| 828 | - // If it is not an absolute URL we assume it is a key |
|
| 829 | - // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 830 | - if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 831 | - $url = $urlGenerator->linkToDocs($url); |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - $info['documentation'][$key] = $url; |
|
| 835 | - } |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - $info['version'] = OC_App::getAppVersion($app); |
|
| 839 | - $appList[] = $info; |
|
| 840 | - } |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - return $appList; |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Returns the internal app ID or false |
|
| 848 | - * @param string $ocsID |
|
| 849 | - * @return string|false |
|
| 850 | - */ |
|
| 851 | - public static function getInternalAppIdByOcs($ocsID) { |
|
| 852 | - if(is_numeric($ocsID)) { |
|
| 853 | - $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid'); |
|
| 854 | - if(array_search($ocsID, $idArray)) { |
|
| 855 | - return array_search($ocsID, $idArray); |
|
| 856 | - } |
|
| 857 | - } |
|
| 858 | - return false; |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - public static function shouldUpgrade($app) { |
|
| 862 | - $versions = self::getAppVersions(); |
|
| 863 | - $currentVersion = OC_App::getAppVersion($app); |
|
| 864 | - if ($currentVersion && isset($versions[$app])) { |
|
| 865 | - $installedVersion = $versions[$app]; |
|
| 866 | - if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 867 | - return true; |
|
| 868 | - } |
|
| 869 | - } |
|
| 870 | - return false; |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * Adjust the number of version parts of $version1 to match |
|
| 875 | - * the number of version parts of $version2. |
|
| 876 | - * |
|
| 877 | - * @param string $version1 version to adjust |
|
| 878 | - * @param string $version2 version to take the number of parts from |
|
| 879 | - * @return string shortened $version1 |
|
| 880 | - */ |
|
| 881 | - private static function adjustVersionParts($version1, $version2) { |
|
| 882 | - $version1 = explode('.', $version1); |
|
| 883 | - $version2 = explode('.', $version2); |
|
| 884 | - // reduce $version1 to match the number of parts in $version2 |
|
| 885 | - while (count($version1) > count($version2)) { |
|
| 886 | - array_pop($version1); |
|
| 887 | - } |
|
| 888 | - // if $version1 does not have enough parts, add some |
|
| 889 | - while (count($version1) < count($version2)) { |
|
| 890 | - $version1[] = '0'; |
|
| 891 | - } |
|
| 892 | - return implode('.', $version1); |
|
| 893 | - } |
|
| 894 | - |
|
| 895 | - /** |
|
| 896 | - * Check whether the current ownCloud version matches the given |
|
| 897 | - * application's version requirements. |
|
| 898 | - * |
|
| 899 | - * The comparison is made based on the number of parts that the |
|
| 900 | - * app info version has. For example for ownCloud 6.0.3 if the |
|
| 901 | - * app info version is expecting version 6.0, the comparison is |
|
| 902 | - * made on the first two parts of the ownCloud version. |
|
| 903 | - * This means that it's possible to specify "requiremin" => 6 |
|
| 904 | - * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 905 | - * |
|
| 906 | - * @param string $ocVersion ownCloud version to check against |
|
| 907 | - * @param array $appInfo app info (from xml) |
|
| 908 | - * |
|
| 909 | - * @return boolean true if compatible, otherwise false |
|
| 910 | - */ |
|
| 911 | - public static function isAppCompatible($ocVersion, $appInfo) { |
|
| 912 | - $requireMin = ''; |
|
| 913 | - $requireMax = ''; |
|
| 914 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 915 | - $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 916 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 917 | - $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 918 | - } else if (isset($appInfo['requiremin'])) { |
|
| 919 | - $requireMin = $appInfo['requiremin']; |
|
| 920 | - } else if (isset($appInfo['require'])) { |
|
| 921 | - $requireMin = $appInfo['require']; |
|
| 922 | - } |
|
| 923 | - |
|
| 924 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 925 | - $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 926 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 927 | - $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 928 | - } else if (isset($appInfo['requiremax'])) { |
|
| 929 | - $requireMax = $appInfo['requiremax']; |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - if (is_array($ocVersion)) { |
|
| 933 | - $ocVersion = implode('.', $ocVersion); |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - if (!empty($requireMin) |
|
| 937 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 938 | - ) { |
|
| 939 | - |
|
| 940 | - return false; |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - if (!empty($requireMax) |
|
| 944 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 945 | - ) { |
|
| 946 | - return false; |
|
| 947 | - } |
|
| 948 | - |
|
| 949 | - return true; |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - /** |
|
| 953 | - * get the installed version of all apps |
|
| 954 | - */ |
|
| 955 | - public static function getAppVersions() { |
|
| 956 | - static $versions; |
|
| 957 | - |
|
| 958 | - if(!$versions) { |
|
| 959 | - $appConfig = \OC::$server->getAppConfig(); |
|
| 960 | - $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 961 | - } |
|
| 962 | - return $versions; |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - /** |
|
| 966 | - * @param string $app |
|
| 967 | - * @param \OCP\IConfig $config |
|
| 968 | - * @param \OCP\IL10N $l |
|
| 969 | - * @return bool |
|
| 970 | - * |
|
| 971 | - * @throws Exception if app is not compatible with this version of ownCloud |
|
| 972 | - * @throws Exception if no app-name was specified |
|
| 973 | - */ |
|
| 974 | - public function installApp($app, |
|
| 975 | - \OCP\IConfig $config, |
|
| 976 | - \OCP\IL10N $l) { |
|
| 977 | - if ($app !== false) { |
|
| 978 | - // check if the app is compatible with this version of ownCloud |
|
| 979 | - $info = self::getAppInfo($app); |
|
| 980 | - if(!is_array($info)) { |
|
| 981 | - throw new \Exception( |
|
| 982 | - $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 983 | - [$info['name']] |
|
| 984 | - ) |
|
| 985 | - ); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - $version = \OCP\Util::getVersion(); |
|
| 989 | - if (!self::isAppCompatible($version, $info)) { |
|
| 990 | - throw new \Exception( |
|
| 991 | - $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 992 | - array($info['name']) |
|
| 993 | - ) |
|
| 994 | - ); |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - // check for required dependencies |
|
| 998 | - self::checkAppDependencies($config, $l, $info); |
|
| 999 | - |
|
| 1000 | - $config->setAppValue($app, 'enabled', 'yes'); |
|
| 1001 | - if (isset($appData['id'])) { |
|
| 1002 | - $config->setAppValue($app, 'ocsid', $appData['id']); |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1006 | - $appPath = self::getAppPath($app); |
|
| 1007 | - self::registerAutoloading($app, $appPath); |
|
| 1008 | - \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
|
| 1012 | - } else { |
|
| 1013 | - if(empty($appName) ) { |
|
| 1014 | - throw new \Exception($l->t("No app name specified")); |
|
| 1015 | - } else { |
|
| 1016 | - throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
|
| 1017 | - } |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - return $app; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /** |
|
| 1024 | - * update the database for the app and call the update script |
|
| 1025 | - * |
|
| 1026 | - * @param string $appId |
|
| 1027 | - * @return bool |
|
| 1028 | - */ |
|
| 1029 | - public static function updateApp($appId) { |
|
| 1030 | - $appPath = self::getAppPath($appId); |
|
| 1031 | - if($appPath === false) { |
|
| 1032 | - return false; |
|
| 1033 | - } |
|
| 1034 | - self::registerAutoloading($appId, $appPath); |
|
| 1035 | - |
|
| 1036 | - $appData = self::getAppInfo($appId); |
|
| 1037 | - self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 1038 | - |
|
| 1039 | - if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1040 | - OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1041 | - } else { |
|
| 1042 | - $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
|
| 1043 | - $ms->migrate(); |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 1047 | - self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 1048 | - unset(self::$appVersion[$appId]); |
|
| 1049 | - |
|
| 1050 | - // run upgrade code |
|
| 1051 | - if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1052 | - self::loadApp($appId); |
|
| 1053 | - include $appPath . '/appinfo/update.php'; |
|
| 1054 | - } |
|
| 1055 | - self::setupBackgroundJobs($appData['background-jobs']); |
|
| 1056 | - if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1057 | - \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - //set remote/public handlers |
|
| 1061 | - if (array_key_exists('ocsid', $appData)) { |
|
| 1062 | - \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 1063 | - } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1064 | - \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 1065 | - } |
|
| 1066 | - foreach ($appData['remote'] as $name => $path) { |
|
| 1067 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1068 | - } |
|
| 1069 | - foreach ($appData['public'] as $name => $path) { |
|
| 1070 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - self::setAppTypes($appId); |
|
| 1074 | - |
|
| 1075 | - $version = \OC_App::getAppVersion($appId); |
|
| 1076 | - \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version); |
|
| 1077 | - |
|
| 1078 | - \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 1079 | - ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 1080 | - )); |
|
| 1081 | - |
|
| 1082 | - return true; |
|
| 1083 | - } |
|
| 1084 | - |
|
| 1085 | - /** |
|
| 1086 | - * @param string $appId |
|
| 1087 | - * @param string[] $steps |
|
| 1088 | - * @throws \OC\NeedsUpdateException |
|
| 1089 | - */ |
|
| 1090 | - public static function executeRepairSteps($appId, array $steps) { |
|
| 1091 | - if (empty($steps)) { |
|
| 1092 | - return; |
|
| 1093 | - } |
|
| 1094 | - // load the app |
|
| 1095 | - self::loadApp($appId); |
|
| 1096 | - |
|
| 1097 | - $dispatcher = OC::$server->getEventDispatcher(); |
|
| 1098 | - |
|
| 1099 | - // load the steps |
|
| 1100 | - $r = new Repair([], $dispatcher); |
|
| 1101 | - foreach ($steps as $step) { |
|
| 1102 | - try { |
|
| 1103 | - $r->addStep($step); |
|
| 1104 | - } catch (Exception $ex) { |
|
| 1105 | - $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 1106 | - \OC::$server->getLogger()->logException($ex); |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - // run the steps |
|
| 1110 | - $r->run(); |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - public static function setupBackgroundJobs(array $jobs) { |
|
| 1114 | - $queue = \OC::$server->getJobList(); |
|
| 1115 | - foreach ($jobs as $job) { |
|
| 1116 | - $queue->add($job); |
|
| 1117 | - } |
|
| 1118 | - } |
|
| 1119 | - |
|
| 1120 | - /** |
|
| 1121 | - * @param string $appId |
|
| 1122 | - * @param string[] $steps |
|
| 1123 | - */ |
|
| 1124 | - private static function setupLiveMigrations($appId, array $steps) { |
|
| 1125 | - $queue = \OC::$server->getJobList(); |
|
| 1126 | - foreach ($steps as $step) { |
|
| 1127 | - $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1128 | - 'app' => $appId, |
|
| 1129 | - 'step' => $step]); |
|
| 1130 | - } |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - /** |
|
| 1134 | - * @param string $appId |
|
| 1135 | - * @return \OC\Files\View|false |
|
| 1136 | - */ |
|
| 1137 | - public static function getStorage($appId) { |
|
| 1138 | - if (OC_App::isEnabled($appId)) { //sanity check |
|
| 1139 | - if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1140 | - $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1141 | - if (!$view->file_exists($appId)) { |
|
| 1142 | - $view->mkdir($appId); |
|
| 1143 | - } |
|
| 1144 | - return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1145 | - } else { |
|
| 1146 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1147 | - return false; |
|
| 1148 | - } |
|
| 1149 | - } else { |
|
| 1150 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1151 | - return false; |
|
| 1152 | - } |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - protected static function findBestL10NOption($options, $lang) { |
|
| 1156 | - $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1157 | - |
|
| 1158 | - $lang = strtolower($lang); |
|
| 1159 | - $similarLang = $lang; |
|
| 1160 | - if (strpos($similarLang, '_')) { |
|
| 1161 | - // For "de_DE" we want to find "de" and the other way around |
|
| 1162 | - $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - foreach ($options as $option) { |
|
| 1166 | - if (is_array($option)) { |
|
| 1167 | - if ($fallback === false) { |
|
| 1168 | - $fallback = $option['@value']; |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - if (!isset($option['@attributes']['lang'])) { |
|
| 1172 | - continue; |
|
| 1173 | - } |
|
| 1174 | - |
|
| 1175 | - $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1176 | - if ($attributeLang === $lang) { |
|
| 1177 | - return $option['@value']; |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - if ($attributeLang === $similarLang) { |
|
| 1181 | - $similarLangFallback = $option['@value']; |
|
| 1182 | - } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1183 | - if ($similarLangFallback === false) { |
|
| 1184 | - $similarLangFallback = $option['@value']; |
|
| 1185 | - } |
|
| 1186 | - } |
|
| 1187 | - } else { |
|
| 1188 | - $englishFallback = $option; |
|
| 1189 | - } |
|
| 1190 | - } |
|
| 1191 | - |
|
| 1192 | - if ($similarLangFallback !== false) { |
|
| 1193 | - return $similarLangFallback; |
|
| 1194 | - } else if ($englishFallback !== false) { |
|
| 1195 | - return $englishFallback; |
|
| 1196 | - } |
|
| 1197 | - return (string) $fallback; |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - /** |
|
| 1201 | - * parses the app data array and enhanced the 'description' value |
|
| 1202 | - * |
|
| 1203 | - * @param array $data the app data |
|
| 1204 | - * @param string $lang |
|
| 1205 | - * @return array improved app data |
|
| 1206 | - */ |
|
| 1207 | - public static function parseAppInfo(array $data, $lang = null) { |
|
| 1208 | - |
|
| 1209 | - if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1210 | - $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1211 | - } |
|
| 1212 | - if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1213 | - $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1214 | - } |
|
| 1215 | - if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1216 | - $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1217 | - } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1218 | - $data['description'] = trim($data['description']); |
|
| 1219 | - } else { |
|
| 1220 | - $data['description'] = ''; |
|
| 1221 | - } |
|
| 1222 | - |
|
| 1223 | - return $data; |
|
| 1224 | - } |
|
| 1225 | - |
|
| 1226 | - /** |
|
| 1227 | - * @param \OCP\IConfig $config |
|
| 1228 | - * @param \OCP\IL10N $l |
|
| 1229 | - * @param array $info |
|
| 1230 | - * @throws \Exception |
|
| 1231 | - */ |
|
| 1232 | - public static function checkAppDependencies($config, $l, $info) { |
|
| 1233 | - $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1234 | - $missing = $dependencyAnalyzer->analyze($info); |
|
| 1235 | - if (!empty($missing)) { |
|
| 1236 | - $missingMsg = implode(PHP_EOL, $missing); |
|
| 1237 | - throw new \Exception( |
|
| 1238 | - $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1239 | - [$info['name'], $missingMsg] |
|
| 1240 | - ) |
|
| 1241 | - ); |
|
| 1242 | - } |
|
| 1243 | - } |
|
| 64 | + static private $appVersion = []; |
|
| 65 | + static private $adminForms = array(); |
|
| 66 | + static private $personalForms = array(); |
|
| 67 | + static private $appInfo = array(); |
|
| 68 | + static private $appTypes = array(); |
|
| 69 | + static private $loadedApps = array(); |
|
| 70 | + static private $altLogin = array(); |
|
| 71 | + static private $alreadyRegistered = []; |
|
| 72 | + const officialApp = 200; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * clean the appId |
|
| 76 | + * |
|
| 77 | + * @param string|boolean $app AppId that needs to be cleaned |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public static function cleanAppId($app) { |
|
| 81 | + return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Check if an app is loaded |
|
| 86 | + * |
|
| 87 | + * @param string $app |
|
| 88 | + * @return bool |
|
| 89 | + */ |
|
| 90 | + public static function isAppLoaded($app) { |
|
| 91 | + return in_array($app, self::$loadedApps, true); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * loads all apps |
|
| 96 | + * |
|
| 97 | + * @param string[] | string | null $types |
|
| 98 | + * @return bool |
|
| 99 | + * |
|
| 100 | + * This function walks through the ownCloud directory and loads all apps |
|
| 101 | + * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 102 | + * exists. |
|
| 103 | + * |
|
| 104 | + * if $types is set, only apps of those types will be loaded |
|
| 105 | + */ |
|
| 106 | + public static function loadApps($types = null) { |
|
| 107 | + if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + // Load the enabled apps here |
|
| 111 | + $apps = self::getEnabledApps(); |
|
| 112 | + |
|
| 113 | + // Add each apps' folder as allowed class path |
|
| 114 | + foreach($apps as $app) { |
|
| 115 | + $path = self::getAppPath($app); |
|
| 116 | + if($path !== false) { |
|
| 117 | + self::registerAutoloading($app, $path); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // prevent app.php from printing output |
|
| 122 | + ob_start(); |
|
| 123 | + foreach ($apps as $app) { |
|
| 124 | + if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 125 | + self::loadApp($app); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + ob_end_clean(); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * load a single app |
|
| 135 | + * |
|
| 136 | + * @param string $app |
|
| 137 | + */ |
|
| 138 | + public static function loadApp($app) { |
|
| 139 | + self::$loadedApps[] = $app; |
|
| 140 | + $appPath = self::getAppPath($app); |
|
| 141 | + if($appPath === false) { |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // in case someone calls loadApp() directly |
|
| 146 | + self::registerAutoloading($app, $appPath); |
|
| 147 | + |
|
| 148 | + if (is_file($appPath . '/appinfo/app.php')) { |
|
| 149 | + \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 150 | + self::requireAppFile($app); |
|
| 151 | + if (self::isType($app, array('authentication'))) { |
|
| 152 | + // since authentication apps affect the "is app enabled for group" check, |
|
| 153 | + // the enabled apps cache needs to be cleared to make sure that the |
|
| 154 | + // next time getEnableApps() is called it will also include apps that were |
|
| 155 | + // enabled for groups |
|
| 156 | + self::$enabledAppsCache = array(); |
|
| 157 | + } |
|
| 158 | + \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $info = self::getAppInfo($app); |
|
| 162 | + if (!empty($info['activity']['filters'])) { |
|
| 163 | + foreach ($info['activity']['filters'] as $filter) { |
|
| 164 | + \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + if (!empty($info['activity']['settings'])) { |
|
| 168 | + foreach ($info['activity']['settings'] as $setting) { |
|
| 169 | + \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + if (!empty($info['activity']['providers'])) { |
|
| 173 | + foreach ($info['activity']['providers'] as $provider) { |
|
| 174 | + \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @internal |
|
| 181 | + * @param string $app |
|
| 182 | + * @param string $path |
|
| 183 | + */ |
|
| 184 | + public static function registerAutoloading($app, $path) { |
|
| 185 | + $key = $app . '-' . $path; |
|
| 186 | + if(isset(self::$alreadyRegistered[$key])) { |
|
| 187 | + return; |
|
| 188 | + } |
|
| 189 | + self::$alreadyRegistered[$key] = true; |
|
| 190 | + // Register on PSR-4 composer autoloader |
|
| 191 | + $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 192 | + \OC::$server->registerNamespace($app, $appNamespace); |
|
| 193 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 194 | + if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 195 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + // Register on legacy autoloader |
|
| 199 | + \OC::$loader->addValidRoot($path); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Load app.php from the given app |
|
| 204 | + * |
|
| 205 | + * @param string $app app name |
|
| 206 | + */ |
|
| 207 | + private static function requireAppFile($app) { |
|
| 208 | + try { |
|
| 209 | + // encapsulated here to avoid variable scope conflicts |
|
| 210 | + require_once $app . '/appinfo/app.php'; |
|
| 211 | + } catch (Error $ex) { |
|
| 212 | + \OC::$server->getLogger()->logException($ex); |
|
| 213 | + $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 214 | + if (!in_array($app, $blacklist)) { |
|
| 215 | + self::disable($app); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * check if an app is of a specific type |
|
| 222 | + * |
|
| 223 | + * @param string $app |
|
| 224 | + * @param string|array $types |
|
| 225 | + * @return bool |
|
| 226 | + */ |
|
| 227 | + public static function isType($app, $types) { |
|
| 228 | + if (is_string($types)) { |
|
| 229 | + $types = array($types); |
|
| 230 | + } |
|
| 231 | + $appTypes = self::getAppTypes($app); |
|
| 232 | + foreach ($types as $type) { |
|
| 233 | + if (array_search($type, $appTypes) !== false) { |
|
| 234 | + return true; |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * get the types of an app |
|
| 242 | + * |
|
| 243 | + * @param string $app |
|
| 244 | + * @return array |
|
| 245 | + */ |
|
| 246 | + private static function getAppTypes($app) { |
|
| 247 | + //load the cache |
|
| 248 | + if (count(self::$appTypes) == 0) { |
|
| 249 | + self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if (isset(self::$appTypes[$app])) { |
|
| 253 | + return explode(',', self::$appTypes[$app]); |
|
| 254 | + } else { |
|
| 255 | + return array(); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * read app types from info.xml and cache them in the database |
|
| 261 | + */ |
|
| 262 | + public static function setAppTypes($app) { |
|
| 263 | + $appData = self::getAppInfo($app); |
|
| 264 | + if(!is_array($appData)) { |
|
| 265 | + return; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + if (isset($appData['types'])) { |
|
| 269 | + $appTypes = implode(',', $appData['types']); |
|
| 270 | + } else { |
|
| 271 | + $appTypes = ''; |
|
| 272 | + $appData['types'] = []; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes); |
|
| 276 | + |
|
| 277 | + if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) { |
|
| 278 | + $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes'); |
|
| 279 | + if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 280 | + \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes'); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * get all enabled apps |
|
| 287 | + */ |
|
| 288 | + protected static $enabledAppsCache = array(); |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Returns apps enabled for the current user. |
|
| 292 | + * |
|
| 293 | + * @param bool $forceRefresh whether to refresh the cache |
|
| 294 | + * @param bool $all whether to return apps for all users, not only the |
|
| 295 | + * currently logged in one |
|
| 296 | + * @return string[] |
|
| 297 | + */ |
|
| 298 | + public static function getEnabledApps($forceRefresh = false, $all = false) { |
|
| 299 | + if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 300 | + return array(); |
|
| 301 | + } |
|
| 302 | + // in incognito mode or when logged out, $user will be false, |
|
| 303 | + // which is also the case during an upgrade |
|
| 304 | + $appManager = \OC::$server->getAppManager(); |
|
| 305 | + if ($all) { |
|
| 306 | + $user = null; |
|
| 307 | + } else { |
|
| 308 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + if (is_null($user)) { |
|
| 312 | + $apps = $appManager->getInstalledApps(); |
|
| 313 | + } else { |
|
| 314 | + $apps = $appManager->getEnabledAppsForUser($user); |
|
| 315 | + } |
|
| 316 | + $apps = array_filter($apps, function ($app) { |
|
| 317 | + return $app !== 'files';//we add this manually |
|
| 318 | + }); |
|
| 319 | + sort($apps); |
|
| 320 | + array_unshift($apps, 'files'); |
|
| 321 | + return $apps; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * checks whether or not an app is enabled |
|
| 326 | + * |
|
| 327 | + * @param string $app app |
|
| 328 | + * @return bool |
|
| 329 | + * |
|
| 330 | + * This function checks whether or not an app is enabled. |
|
| 331 | + */ |
|
| 332 | + public static function isEnabled($app) { |
|
| 333 | + return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * enables an app |
|
| 338 | + * |
|
| 339 | + * @param string $appId |
|
| 340 | + * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 341 | + * @throws \Exception |
|
| 342 | + * @return void |
|
| 343 | + * |
|
| 344 | + * This function set an app as enabled in appconfig. |
|
| 345 | + */ |
|
| 346 | + public function enable($appId, |
|
| 347 | + $groups = null) { |
|
| 348 | + self::$enabledAppsCache = []; // flush |
|
| 349 | + |
|
| 350 | + // Check if app is already downloaded |
|
| 351 | + $installer = new Installer( |
|
| 352 | + \OC::$server->getAppFetcher(), |
|
| 353 | + \OC::$server->getHTTPClientService(), |
|
| 354 | + \OC::$server->getTempManager(), |
|
| 355 | + \OC::$server->getLogger(), |
|
| 356 | + \OC::$server->getConfig() |
|
| 357 | + ); |
|
| 358 | + $isDownloaded = $installer->isDownloaded($appId); |
|
| 359 | + |
|
| 360 | + if(!$isDownloaded) { |
|
| 361 | + $installer->downloadApp($appId); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + $installer->installApp($appId); |
|
| 365 | + |
|
| 366 | + $appManager = \OC::$server->getAppManager(); |
|
| 367 | + if (!is_null($groups)) { |
|
| 368 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 369 | + $groupsList = []; |
|
| 370 | + foreach ($groups as $group) { |
|
| 371 | + $groupItem = $groupManager->get($group); |
|
| 372 | + if ($groupItem instanceof \OCP\IGroup) { |
|
| 373 | + $groupsList[] = $groupManager->get($group); |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + $appManager->enableAppForGroups($appId, $groupsList); |
|
| 377 | + } else { |
|
| 378 | + $appManager->enableApp($appId); |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * @param string $app |
|
| 384 | + * @return bool |
|
| 385 | + */ |
|
| 386 | + public static function removeApp($app) { |
|
| 387 | + if (\OC::$server->getAppManager()->isShipped($app)) { |
|
| 388 | + return false; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + $installer = new Installer( |
|
| 392 | + \OC::$server->getAppFetcher(), |
|
| 393 | + \OC::$server->getHTTPClientService(), |
|
| 394 | + \OC::$server->getTempManager(), |
|
| 395 | + \OC::$server->getLogger(), |
|
| 396 | + \OC::$server->getConfig() |
|
| 397 | + ); |
|
| 398 | + return $installer->removeApp($app); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * This function set an app as disabled in appconfig. |
|
| 403 | + * |
|
| 404 | + * @param string $app app |
|
| 405 | + * @throws Exception |
|
| 406 | + */ |
|
| 407 | + public static function disable($app) { |
|
| 408 | + // flush |
|
| 409 | + self::$enabledAppsCache = array(); |
|
| 410 | + |
|
| 411 | + // run uninstall steps |
|
| 412 | + $appData = OC_App::getAppInfo($app); |
|
| 413 | + if (!is_null($appData)) { |
|
| 414 | + OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + // emit disable hook - needed anymore ? |
|
| 418 | + \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 419 | + |
|
| 420 | + // finally disable it |
|
| 421 | + $appManager = \OC::$server->getAppManager(); |
|
| 422 | + $appManager->disableApp($app); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + // This is private as well. It simply works, so don't ask for more details |
|
| 426 | + private static function proceedNavigation($list) { |
|
| 427 | + usort($list, function($a, $b) { |
|
| 428 | + if (isset($a['order']) && isset($b['order'])) { |
|
| 429 | + return ($a['order'] < $b['order']) ? -1 : 1; |
|
| 430 | + } else if (isset($a['order']) || isset($b['order'])) { |
|
| 431 | + return isset($a['order']) ? -1 : 1; |
|
| 432 | + } else { |
|
| 433 | + return ($a['name'] < $b['name']) ? -1 : 1; |
|
| 434 | + } |
|
| 435 | + }); |
|
| 436 | + |
|
| 437 | + $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 438 | + foreach ($list as $index => &$navEntry) { |
|
| 439 | + if ($navEntry['id'] == $activeApp) { |
|
| 440 | + $navEntry['active'] = true; |
|
| 441 | + } else { |
|
| 442 | + $navEntry['active'] = false; |
|
| 443 | + } |
|
| 444 | + } |
|
| 445 | + unset($navEntry); |
|
| 446 | + |
|
| 447 | + return $list; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Get the path where to install apps |
|
| 452 | + * |
|
| 453 | + * @return string|false |
|
| 454 | + */ |
|
| 455 | + public static function getInstallPath() { |
|
| 456 | + if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 457 | + return false; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 461 | + if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 462 | + return $dir['path']; |
|
| 463 | + } |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 467 | + return null; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * search for an app in all app-directories |
|
| 473 | + * |
|
| 474 | + * @param string $appId |
|
| 475 | + * @return false|string |
|
| 476 | + */ |
|
| 477 | + public static function findAppInDirectories($appId) { |
|
| 478 | + $sanitizedAppId = self::cleanAppId($appId); |
|
| 479 | + if($sanitizedAppId !== $appId) { |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | + static $app_dir = array(); |
|
| 483 | + |
|
| 484 | + if (isset($app_dir[$appId])) { |
|
| 485 | + return $app_dir[$appId]; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + $possibleApps = array(); |
|
| 489 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 490 | + if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 491 | + $possibleApps[] = $dir; |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + if (empty($possibleApps)) { |
|
| 496 | + return false; |
|
| 497 | + } elseif (count($possibleApps) === 1) { |
|
| 498 | + $dir = array_shift($possibleApps); |
|
| 499 | + $app_dir[$appId] = $dir; |
|
| 500 | + return $dir; |
|
| 501 | + } else { |
|
| 502 | + $versionToLoad = array(); |
|
| 503 | + foreach ($possibleApps as $possibleApp) { |
|
| 504 | + $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 505 | + if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 506 | + $versionToLoad = array( |
|
| 507 | + 'dir' => $possibleApp, |
|
| 508 | + 'version' => $version, |
|
| 509 | + ); |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + $app_dir[$appId] = $versionToLoad['dir']; |
|
| 513 | + return $versionToLoad['dir']; |
|
| 514 | + //TODO - write test |
|
| 515 | + } |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Get the directory for the given app. |
|
| 520 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 521 | + * |
|
| 522 | + * @param string $appId |
|
| 523 | + * @return string|false |
|
| 524 | + */ |
|
| 525 | + public static function getAppPath($appId) { |
|
| 526 | + if ($appId === null || trim($appId) === '') { |
|
| 527 | + return false; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 531 | + return $dir['path'] . '/' . $appId; |
|
| 532 | + } |
|
| 533 | + return false; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Get the path for the given app on the access |
|
| 538 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 539 | + * |
|
| 540 | + * @param string $appId |
|
| 541 | + * @return string|false |
|
| 542 | + */ |
|
| 543 | + public static function getAppWebPath($appId) { |
|
| 544 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 545 | + return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 546 | + } |
|
| 547 | + return false; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * get the last version of the app from appinfo/info.xml |
|
| 552 | + * |
|
| 553 | + * @param string $appId |
|
| 554 | + * @param bool $useCache |
|
| 555 | + * @return string |
|
| 556 | + */ |
|
| 557 | + public static function getAppVersion($appId, $useCache = true) { |
|
| 558 | + if($useCache && isset(self::$appVersion[$appId])) { |
|
| 559 | + return self::$appVersion[$appId]; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + $file = self::getAppPath($appId); |
|
| 563 | + self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0'; |
|
| 564 | + return self::$appVersion[$appId]; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * get app's version based on it's path |
|
| 569 | + * |
|
| 570 | + * @param string $path |
|
| 571 | + * @return string |
|
| 572 | + */ |
|
| 573 | + public static function getAppVersionByPath($path) { |
|
| 574 | + $infoFile = $path . '/appinfo/info.xml'; |
|
| 575 | + $appData = self::getAppInfo($infoFile, true); |
|
| 576 | + return isset($appData['version']) ? $appData['version'] : ''; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Read all app metadata from the info.xml file |
|
| 582 | + * |
|
| 583 | + * @param string $appId id of the app or the path of the info.xml file |
|
| 584 | + * @param bool $path |
|
| 585 | + * @param string $lang |
|
| 586 | + * @return array|null |
|
| 587 | + * @note all data is read from info.xml, not just pre-defined fields |
|
| 588 | + */ |
|
| 589 | + public static function getAppInfo($appId, $path = false, $lang = null) { |
|
| 590 | + if ($path) { |
|
| 591 | + $file = $appId; |
|
| 592 | + } else { |
|
| 593 | + if ($lang === null && isset(self::$appInfo[$appId])) { |
|
| 594 | + return self::$appInfo[$appId]; |
|
| 595 | + } |
|
| 596 | + $appPath = self::getAppPath($appId); |
|
| 597 | + if($appPath === false) { |
|
| 598 | + return null; |
|
| 599 | + } |
|
| 600 | + $file = $appPath . '/appinfo/info.xml'; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo')); |
|
| 604 | + $data = $parser->parse($file); |
|
| 605 | + |
|
| 606 | + if (is_array($data)) { |
|
| 607 | + $data = OC_App::parseAppInfo($data, $lang); |
|
| 608 | + } |
|
| 609 | + if(isset($data['ocsid'])) { |
|
| 610 | + $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
|
| 611 | + if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 612 | + $data['ocsid'] = $storedId; |
|
| 613 | + } |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + if ($lang === null) { |
|
| 617 | + self::$appInfo[$appId] = $data; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + return $data; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + /** |
|
| 624 | + * Returns the navigation |
|
| 625 | + * |
|
| 626 | + * @return array |
|
| 627 | + * |
|
| 628 | + * This function returns an array containing all entries added. The |
|
| 629 | + * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 630 | + * given for each app the following keys exist: |
|
| 631 | + * - active: boolean, signals if the user is on this navigation entry |
|
| 632 | + */ |
|
| 633 | + public static function getNavigation() { |
|
| 634 | + $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 635 | + return self::proceedNavigation($entries); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Returns the Settings Navigation |
|
| 640 | + * |
|
| 641 | + * @return string[] |
|
| 642 | + * |
|
| 643 | + * This function returns an array containing all settings pages added. The |
|
| 644 | + * entries are sorted by the key 'order' ascending. |
|
| 645 | + */ |
|
| 646 | + public static function getSettingsNavigation() { |
|
| 647 | + $entries = OC::$server->getNavigationManager()->getAll('settings'); |
|
| 648 | + return self::proceedNavigation($entries); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * get the id of loaded app |
|
| 653 | + * |
|
| 654 | + * @return string |
|
| 655 | + */ |
|
| 656 | + public static function getCurrentApp() { |
|
| 657 | + $request = \OC::$server->getRequest(); |
|
| 658 | + $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 659 | + $topFolder = substr($script, 0, strpos($script, '/')); |
|
| 660 | + if (empty($topFolder)) { |
|
| 661 | + $path_info = $request->getPathInfo(); |
|
| 662 | + if ($path_info) { |
|
| 663 | + $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 664 | + } |
|
| 665 | + } |
|
| 666 | + if ($topFolder == 'apps') { |
|
| 667 | + $length = strlen($topFolder); |
|
| 668 | + return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1); |
|
| 669 | + } else { |
|
| 670 | + return $topFolder; |
|
| 671 | + } |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * @param string $type |
|
| 676 | + * @return array |
|
| 677 | + */ |
|
| 678 | + public static function getForms($type) { |
|
| 679 | + $forms = array(); |
|
| 680 | + switch ($type) { |
|
| 681 | + case 'admin': |
|
| 682 | + $source = self::$adminForms; |
|
| 683 | + break; |
|
| 684 | + case 'personal': |
|
| 685 | + $source = self::$personalForms; |
|
| 686 | + break; |
|
| 687 | + default: |
|
| 688 | + return array(); |
|
| 689 | + } |
|
| 690 | + foreach ($source as $form) { |
|
| 691 | + $forms[] = include $form; |
|
| 692 | + } |
|
| 693 | + return $forms; |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * register an admin form to be shown |
|
| 698 | + * |
|
| 699 | + * @param string $app |
|
| 700 | + * @param string $page |
|
| 701 | + */ |
|
| 702 | + public static function registerAdmin($app, $page) { |
|
| 703 | + self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * register a personal form to be shown |
|
| 708 | + * @param string $app |
|
| 709 | + * @param string $page |
|
| 710 | + */ |
|
| 711 | + public static function registerPersonal($app, $page) { |
|
| 712 | + self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + /** |
|
| 716 | + * @param array $entry |
|
| 717 | + */ |
|
| 718 | + public static function registerLogIn(array $entry) { |
|
| 719 | + self::$altLogin[] = $entry; |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * @return array |
|
| 724 | + */ |
|
| 725 | + public static function getAlternativeLogIns() { |
|
| 726 | + return self::$altLogin; |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + /** |
|
| 730 | + * get a list of all apps in the apps folder |
|
| 731 | + * |
|
| 732 | + * @return array an array of app names (string IDs) |
|
| 733 | + * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 734 | + */ |
|
| 735 | + public static function getAllApps() { |
|
| 736 | + |
|
| 737 | + $apps = array(); |
|
| 738 | + |
|
| 739 | + foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 740 | + if (!is_readable($apps_dir['path'])) { |
|
| 741 | + \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 742 | + continue; |
|
| 743 | + } |
|
| 744 | + $dh = opendir($apps_dir['path']); |
|
| 745 | + |
|
| 746 | + if (is_resource($dh)) { |
|
| 747 | + while (($file = readdir($dh)) !== false) { |
|
| 748 | + |
|
| 749 | + if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 750 | + |
|
| 751 | + $apps[] = $file; |
|
| 752 | + } |
|
| 753 | + } |
|
| 754 | + } |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + return $apps; |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + /** |
|
| 761 | + * List all apps, this is used in apps.php |
|
| 762 | + * |
|
| 763 | + * @return array |
|
| 764 | + */ |
|
| 765 | + public function listAllApps() { |
|
| 766 | + $installedApps = OC_App::getAllApps(); |
|
| 767 | + |
|
| 768 | + $appManager = \OC::$server->getAppManager(); |
|
| 769 | + //we don't want to show configuration for these |
|
| 770 | + $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 771 | + $appList = array(); |
|
| 772 | + $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 773 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 774 | + |
|
| 775 | + foreach ($installedApps as $app) { |
|
| 776 | + if (array_search($app, $blacklist) === false) { |
|
| 777 | + |
|
| 778 | + $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 779 | + if (!is_array($info)) { |
|
| 780 | + \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 781 | + continue; |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + if (!isset($info['name'])) { |
|
| 785 | + \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 786 | + continue; |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no'); |
|
| 790 | + $info['groups'] = null; |
|
| 791 | + if ($enabled === 'yes') { |
|
| 792 | + $active = true; |
|
| 793 | + } else if ($enabled === 'no') { |
|
| 794 | + $active = false; |
|
| 795 | + } else { |
|
| 796 | + $active = true; |
|
| 797 | + $info['groups'] = $enabled; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + $info['active'] = $active; |
|
| 801 | + |
|
| 802 | + if ($appManager->isShipped($app)) { |
|
| 803 | + $info['internal'] = true; |
|
| 804 | + $info['level'] = self::officialApp; |
|
| 805 | + $info['removable'] = false; |
|
| 806 | + } else { |
|
| 807 | + $info['internal'] = false; |
|
| 808 | + $info['removable'] = true; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + $appPath = self::getAppPath($app); |
|
| 812 | + if($appPath !== false) { |
|
| 813 | + $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 814 | + if (file_exists($appIcon)) { |
|
| 815 | + $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 816 | + $info['previewAsIcon'] = true; |
|
| 817 | + } else { |
|
| 818 | + $appIcon = $appPath . '/img/app.svg'; |
|
| 819 | + if (file_exists($appIcon)) { |
|
| 820 | + $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 821 | + $info['previewAsIcon'] = true; |
|
| 822 | + } |
|
| 823 | + } |
|
| 824 | + } |
|
| 825 | + // fix documentation |
|
| 826 | + if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 827 | + foreach ($info['documentation'] as $key => $url) { |
|
| 828 | + // If it is not an absolute URL we assume it is a key |
|
| 829 | + // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 830 | + if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 831 | + $url = $urlGenerator->linkToDocs($url); |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + $info['documentation'][$key] = $url; |
|
| 835 | + } |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + $info['version'] = OC_App::getAppVersion($app); |
|
| 839 | + $appList[] = $info; |
|
| 840 | + } |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + return $appList; |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Returns the internal app ID or false |
|
| 848 | + * @param string $ocsID |
|
| 849 | + * @return string|false |
|
| 850 | + */ |
|
| 851 | + public static function getInternalAppIdByOcs($ocsID) { |
|
| 852 | + if(is_numeric($ocsID)) { |
|
| 853 | + $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid'); |
|
| 854 | + if(array_search($ocsID, $idArray)) { |
|
| 855 | + return array_search($ocsID, $idArray); |
|
| 856 | + } |
|
| 857 | + } |
|
| 858 | + return false; |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + public static function shouldUpgrade($app) { |
|
| 862 | + $versions = self::getAppVersions(); |
|
| 863 | + $currentVersion = OC_App::getAppVersion($app); |
|
| 864 | + if ($currentVersion && isset($versions[$app])) { |
|
| 865 | + $installedVersion = $versions[$app]; |
|
| 866 | + if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 867 | + return true; |
|
| 868 | + } |
|
| 869 | + } |
|
| 870 | + return false; |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * Adjust the number of version parts of $version1 to match |
|
| 875 | + * the number of version parts of $version2. |
|
| 876 | + * |
|
| 877 | + * @param string $version1 version to adjust |
|
| 878 | + * @param string $version2 version to take the number of parts from |
|
| 879 | + * @return string shortened $version1 |
|
| 880 | + */ |
|
| 881 | + private static function adjustVersionParts($version1, $version2) { |
|
| 882 | + $version1 = explode('.', $version1); |
|
| 883 | + $version2 = explode('.', $version2); |
|
| 884 | + // reduce $version1 to match the number of parts in $version2 |
|
| 885 | + while (count($version1) > count($version2)) { |
|
| 886 | + array_pop($version1); |
|
| 887 | + } |
|
| 888 | + // if $version1 does not have enough parts, add some |
|
| 889 | + while (count($version1) < count($version2)) { |
|
| 890 | + $version1[] = '0'; |
|
| 891 | + } |
|
| 892 | + return implode('.', $version1); |
|
| 893 | + } |
|
| 894 | + |
|
| 895 | + /** |
|
| 896 | + * Check whether the current ownCloud version matches the given |
|
| 897 | + * application's version requirements. |
|
| 898 | + * |
|
| 899 | + * The comparison is made based on the number of parts that the |
|
| 900 | + * app info version has. For example for ownCloud 6.0.3 if the |
|
| 901 | + * app info version is expecting version 6.0, the comparison is |
|
| 902 | + * made on the first two parts of the ownCloud version. |
|
| 903 | + * This means that it's possible to specify "requiremin" => 6 |
|
| 904 | + * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 905 | + * |
|
| 906 | + * @param string $ocVersion ownCloud version to check against |
|
| 907 | + * @param array $appInfo app info (from xml) |
|
| 908 | + * |
|
| 909 | + * @return boolean true if compatible, otherwise false |
|
| 910 | + */ |
|
| 911 | + public static function isAppCompatible($ocVersion, $appInfo) { |
|
| 912 | + $requireMin = ''; |
|
| 913 | + $requireMax = ''; |
|
| 914 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 915 | + $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 916 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 917 | + $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 918 | + } else if (isset($appInfo['requiremin'])) { |
|
| 919 | + $requireMin = $appInfo['requiremin']; |
|
| 920 | + } else if (isset($appInfo['require'])) { |
|
| 921 | + $requireMin = $appInfo['require']; |
|
| 922 | + } |
|
| 923 | + |
|
| 924 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 925 | + $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 926 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 927 | + $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 928 | + } else if (isset($appInfo['requiremax'])) { |
|
| 929 | + $requireMax = $appInfo['requiremax']; |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + if (is_array($ocVersion)) { |
|
| 933 | + $ocVersion = implode('.', $ocVersion); |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + if (!empty($requireMin) |
|
| 937 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 938 | + ) { |
|
| 939 | + |
|
| 940 | + return false; |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + if (!empty($requireMax) |
|
| 944 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 945 | + ) { |
|
| 946 | + return false; |
|
| 947 | + } |
|
| 948 | + |
|
| 949 | + return true; |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + /** |
|
| 953 | + * get the installed version of all apps |
|
| 954 | + */ |
|
| 955 | + public static function getAppVersions() { |
|
| 956 | + static $versions; |
|
| 957 | + |
|
| 958 | + if(!$versions) { |
|
| 959 | + $appConfig = \OC::$server->getAppConfig(); |
|
| 960 | + $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 961 | + } |
|
| 962 | + return $versions; |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + /** |
|
| 966 | + * @param string $app |
|
| 967 | + * @param \OCP\IConfig $config |
|
| 968 | + * @param \OCP\IL10N $l |
|
| 969 | + * @return bool |
|
| 970 | + * |
|
| 971 | + * @throws Exception if app is not compatible with this version of ownCloud |
|
| 972 | + * @throws Exception if no app-name was specified |
|
| 973 | + */ |
|
| 974 | + public function installApp($app, |
|
| 975 | + \OCP\IConfig $config, |
|
| 976 | + \OCP\IL10N $l) { |
|
| 977 | + if ($app !== false) { |
|
| 978 | + // check if the app is compatible with this version of ownCloud |
|
| 979 | + $info = self::getAppInfo($app); |
|
| 980 | + if(!is_array($info)) { |
|
| 981 | + throw new \Exception( |
|
| 982 | + $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 983 | + [$info['name']] |
|
| 984 | + ) |
|
| 985 | + ); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + $version = \OCP\Util::getVersion(); |
|
| 989 | + if (!self::isAppCompatible($version, $info)) { |
|
| 990 | + throw new \Exception( |
|
| 991 | + $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 992 | + array($info['name']) |
|
| 993 | + ) |
|
| 994 | + ); |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + // check for required dependencies |
|
| 998 | + self::checkAppDependencies($config, $l, $info); |
|
| 999 | + |
|
| 1000 | + $config->setAppValue($app, 'enabled', 'yes'); |
|
| 1001 | + if (isset($appData['id'])) { |
|
| 1002 | + $config->setAppValue($app, 'ocsid', $appData['id']); |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1006 | + $appPath = self::getAppPath($app); |
|
| 1007 | + self::registerAutoloading($app, $appPath); |
|
| 1008 | + \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
|
| 1012 | + } else { |
|
| 1013 | + if(empty($appName) ) { |
|
| 1014 | + throw new \Exception($l->t("No app name specified")); |
|
| 1015 | + } else { |
|
| 1016 | + throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
|
| 1017 | + } |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + return $app; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /** |
|
| 1024 | + * update the database for the app and call the update script |
|
| 1025 | + * |
|
| 1026 | + * @param string $appId |
|
| 1027 | + * @return bool |
|
| 1028 | + */ |
|
| 1029 | + public static function updateApp($appId) { |
|
| 1030 | + $appPath = self::getAppPath($appId); |
|
| 1031 | + if($appPath === false) { |
|
| 1032 | + return false; |
|
| 1033 | + } |
|
| 1034 | + self::registerAutoloading($appId, $appPath); |
|
| 1035 | + |
|
| 1036 | + $appData = self::getAppInfo($appId); |
|
| 1037 | + self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 1038 | + |
|
| 1039 | + if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1040 | + OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1041 | + } else { |
|
| 1042 | + $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
|
| 1043 | + $ms->migrate(); |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 1047 | + self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 1048 | + unset(self::$appVersion[$appId]); |
|
| 1049 | + |
|
| 1050 | + // run upgrade code |
|
| 1051 | + if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1052 | + self::loadApp($appId); |
|
| 1053 | + include $appPath . '/appinfo/update.php'; |
|
| 1054 | + } |
|
| 1055 | + self::setupBackgroundJobs($appData['background-jobs']); |
|
| 1056 | + if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1057 | + \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + //set remote/public handlers |
|
| 1061 | + if (array_key_exists('ocsid', $appData)) { |
|
| 1062 | + \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 1063 | + } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1064 | + \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 1065 | + } |
|
| 1066 | + foreach ($appData['remote'] as $name => $path) { |
|
| 1067 | + \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1068 | + } |
|
| 1069 | + foreach ($appData['public'] as $name => $path) { |
|
| 1070 | + \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + self::setAppTypes($appId); |
|
| 1074 | + |
|
| 1075 | + $version = \OC_App::getAppVersion($appId); |
|
| 1076 | + \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version); |
|
| 1077 | + |
|
| 1078 | + \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 1079 | + ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 1080 | + )); |
|
| 1081 | + |
|
| 1082 | + return true; |
|
| 1083 | + } |
|
| 1084 | + |
|
| 1085 | + /** |
|
| 1086 | + * @param string $appId |
|
| 1087 | + * @param string[] $steps |
|
| 1088 | + * @throws \OC\NeedsUpdateException |
|
| 1089 | + */ |
|
| 1090 | + public static function executeRepairSteps($appId, array $steps) { |
|
| 1091 | + if (empty($steps)) { |
|
| 1092 | + return; |
|
| 1093 | + } |
|
| 1094 | + // load the app |
|
| 1095 | + self::loadApp($appId); |
|
| 1096 | + |
|
| 1097 | + $dispatcher = OC::$server->getEventDispatcher(); |
|
| 1098 | + |
|
| 1099 | + // load the steps |
|
| 1100 | + $r = new Repair([], $dispatcher); |
|
| 1101 | + foreach ($steps as $step) { |
|
| 1102 | + try { |
|
| 1103 | + $r->addStep($step); |
|
| 1104 | + } catch (Exception $ex) { |
|
| 1105 | + $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 1106 | + \OC::$server->getLogger()->logException($ex); |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + // run the steps |
|
| 1110 | + $r->run(); |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + public static function setupBackgroundJobs(array $jobs) { |
|
| 1114 | + $queue = \OC::$server->getJobList(); |
|
| 1115 | + foreach ($jobs as $job) { |
|
| 1116 | + $queue->add($job); |
|
| 1117 | + } |
|
| 1118 | + } |
|
| 1119 | + |
|
| 1120 | + /** |
|
| 1121 | + * @param string $appId |
|
| 1122 | + * @param string[] $steps |
|
| 1123 | + */ |
|
| 1124 | + private static function setupLiveMigrations($appId, array $steps) { |
|
| 1125 | + $queue = \OC::$server->getJobList(); |
|
| 1126 | + foreach ($steps as $step) { |
|
| 1127 | + $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1128 | + 'app' => $appId, |
|
| 1129 | + 'step' => $step]); |
|
| 1130 | + } |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + /** |
|
| 1134 | + * @param string $appId |
|
| 1135 | + * @return \OC\Files\View|false |
|
| 1136 | + */ |
|
| 1137 | + public static function getStorage($appId) { |
|
| 1138 | + if (OC_App::isEnabled($appId)) { //sanity check |
|
| 1139 | + if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1140 | + $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1141 | + if (!$view->file_exists($appId)) { |
|
| 1142 | + $view->mkdir($appId); |
|
| 1143 | + } |
|
| 1144 | + return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1145 | + } else { |
|
| 1146 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1147 | + return false; |
|
| 1148 | + } |
|
| 1149 | + } else { |
|
| 1150 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1151 | + return false; |
|
| 1152 | + } |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + protected static function findBestL10NOption($options, $lang) { |
|
| 1156 | + $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1157 | + |
|
| 1158 | + $lang = strtolower($lang); |
|
| 1159 | + $similarLang = $lang; |
|
| 1160 | + if (strpos($similarLang, '_')) { |
|
| 1161 | + // For "de_DE" we want to find "de" and the other way around |
|
| 1162 | + $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + foreach ($options as $option) { |
|
| 1166 | + if (is_array($option)) { |
|
| 1167 | + if ($fallback === false) { |
|
| 1168 | + $fallback = $option['@value']; |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + if (!isset($option['@attributes']['lang'])) { |
|
| 1172 | + continue; |
|
| 1173 | + } |
|
| 1174 | + |
|
| 1175 | + $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1176 | + if ($attributeLang === $lang) { |
|
| 1177 | + return $option['@value']; |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + if ($attributeLang === $similarLang) { |
|
| 1181 | + $similarLangFallback = $option['@value']; |
|
| 1182 | + } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1183 | + if ($similarLangFallback === false) { |
|
| 1184 | + $similarLangFallback = $option['@value']; |
|
| 1185 | + } |
|
| 1186 | + } |
|
| 1187 | + } else { |
|
| 1188 | + $englishFallback = $option; |
|
| 1189 | + } |
|
| 1190 | + } |
|
| 1191 | + |
|
| 1192 | + if ($similarLangFallback !== false) { |
|
| 1193 | + return $similarLangFallback; |
|
| 1194 | + } else if ($englishFallback !== false) { |
|
| 1195 | + return $englishFallback; |
|
| 1196 | + } |
|
| 1197 | + return (string) $fallback; |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + /** |
|
| 1201 | + * parses the app data array and enhanced the 'description' value |
|
| 1202 | + * |
|
| 1203 | + * @param array $data the app data |
|
| 1204 | + * @param string $lang |
|
| 1205 | + * @return array improved app data |
|
| 1206 | + */ |
|
| 1207 | + public static function parseAppInfo(array $data, $lang = null) { |
|
| 1208 | + |
|
| 1209 | + if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1210 | + $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1211 | + } |
|
| 1212 | + if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1213 | + $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1214 | + } |
|
| 1215 | + if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1216 | + $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1217 | + } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1218 | + $data['description'] = trim($data['description']); |
|
| 1219 | + } else { |
|
| 1220 | + $data['description'] = ''; |
|
| 1221 | + } |
|
| 1222 | + |
|
| 1223 | + return $data; |
|
| 1224 | + } |
|
| 1225 | + |
|
| 1226 | + /** |
|
| 1227 | + * @param \OCP\IConfig $config |
|
| 1228 | + * @param \OCP\IL10N $l |
|
| 1229 | + * @param array $info |
|
| 1230 | + * @throws \Exception |
|
| 1231 | + */ |
|
| 1232 | + public static function checkAppDependencies($config, $l, $info) { |
|
| 1233 | + $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1234 | + $missing = $dependencyAnalyzer->analyze($info); |
|
| 1235 | + if (!empty($missing)) { |
|
| 1236 | + $missingMsg = implode(PHP_EOL, $missing); |
|
| 1237 | + throw new \Exception( |
|
| 1238 | + $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1239 | + [$info['name'], $missingMsg] |
|
| 1240 | + ) |
|
| 1241 | + ); |
|
| 1242 | + } |
|
| 1243 | + } |
|
| 1244 | 1244 | } |
@@ -111,9 +111,9 @@ discard block |
||
| 111 | 111 | $apps = self::getEnabledApps(); |
| 112 | 112 | |
| 113 | 113 | // Add each apps' folder as allowed class path |
| 114 | - foreach($apps as $app) { |
|
| 114 | + foreach ($apps as $app) { |
|
| 115 | 115 | $path = self::getAppPath($app); |
| 116 | - if($path !== false) { |
|
| 116 | + if ($path !== false) { |
|
| 117 | 117 | self::registerAutoloading($app, $path); |
| 118 | 118 | } |
| 119 | 119 | } |
@@ -138,15 +138,15 @@ discard block |
||
| 138 | 138 | public static function loadApp($app) { |
| 139 | 139 | self::$loadedApps[] = $app; |
| 140 | 140 | $appPath = self::getAppPath($app); |
| 141 | - if($appPath === false) { |
|
| 141 | + if ($appPath === false) { |
|
| 142 | 142 | return; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | // in case someone calls loadApp() directly |
| 146 | 146 | self::registerAutoloading($app, $appPath); |
| 147 | 147 | |
| 148 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 149 | - \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 148 | + if (is_file($appPath.'/appinfo/app.php')) { |
|
| 149 | + \OC::$server->getEventLogger()->start('load_app_'.$app, 'Load app: '.$app); |
|
| 150 | 150 | self::requireAppFile($app); |
| 151 | 151 | if (self::isType($app, array('authentication'))) { |
| 152 | 152 | // since authentication apps affect the "is app enabled for group" check, |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | // enabled for groups |
| 156 | 156 | self::$enabledAppsCache = array(); |
| 157 | 157 | } |
| 158 | - \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 158 | + \OC::$server->getEventLogger()->end('load_app_'.$app); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | $info = self::getAppInfo($app); |
@@ -182,17 +182,17 @@ discard block |
||
| 182 | 182 | * @param string $path |
| 183 | 183 | */ |
| 184 | 184 | public static function registerAutoloading($app, $path) { |
| 185 | - $key = $app . '-' . $path; |
|
| 186 | - if(isset(self::$alreadyRegistered[$key])) { |
|
| 185 | + $key = $app.'-'.$path; |
|
| 186 | + if (isset(self::$alreadyRegistered[$key])) { |
|
| 187 | 187 | return; |
| 188 | 188 | } |
| 189 | 189 | self::$alreadyRegistered[$key] = true; |
| 190 | 190 | // Register on PSR-4 composer autoloader |
| 191 | 191 | $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
| 192 | 192 | \OC::$server->registerNamespace($app, $appNamespace); |
| 193 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 193 | + \OC::$composerAutoloader->addPsr4($appNamespace.'\\', $path.'/lib/', true); |
|
| 194 | 194 | if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
| 195 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 195 | + \OC::$composerAutoloader->addPsr4($appNamespace.'\\Tests\\', $path.'/tests/', true); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | // Register on legacy autoloader |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | private static function requireAppFile($app) { |
| 208 | 208 | try { |
| 209 | 209 | // encapsulated here to avoid variable scope conflicts |
| 210 | - require_once $app . '/appinfo/app.php'; |
|
| 210 | + require_once $app.'/appinfo/app.php'; |
|
| 211 | 211 | } catch (Error $ex) { |
| 212 | 212 | \OC::$server->getLogger()->logException($ex); |
| 213 | 213 | $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | */ |
| 262 | 262 | public static function setAppTypes($app) { |
| 263 | 263 | $appData = self::getAppInfo($app); |
| 264 | - if(!is_array($appData)) { |
|
| 264 | + if (!is_array($appData)) { |
|
| 265 | 265 | return; |
| 266 | 266 | } |
| 267 | 267 | |
@@ -313,8 +313,8 @@ discard block |
||
| 313 | 313 | } else { |
| 314 | 314 | $apps = $appManager->getEnabledAppsForUser($user); |
| 315 | 315 | } |
| 316 | - $apps = array_filter($apps, function ($app) { |
|
| 317 | - return $app !== 'files';//we add this manually |
|
| 316 | + $apps = array_filter($apps, function($app) { |
|
| 317 | + return $app !== 'files'; //we add this manually |
|
| 318 | 318 | }); |
| 319 | 319 | sort($apps); |
| 320 | 320 | array_unshift($apps, 'files'); |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | ); |
| 358 | 358 | $isDownloaded = $installer->isDownloaded($appId); |
| 359 | 359 | |
| 360 | - if(!$isDownloaded) { |
|
| 360 | + if (!$isDownloaded) { |
|
| 361 | 361 | $installer->downloadApp($appId); |
| 362 | 362 | } |
| 363 | 363 | |
@@ -476,7 +476,7 @@ discard block |
||
| 476 | 476 | */ |
| 477 | 477 | public static function findAppInDirectories($appId) { |
| 478 | 478 | $sanitizedAppId = self::cleanAppId($appId); |
| 479 | - if($sanitizedAppId !== $appId) { |
|
| 479 | + if ($sanitizedAppId !== $appId) { |
|
| 480 | 480 | return false; |
| 481 | 481 | } |
| 482 | 482 | static $app_dir = array(); |
@@ -487,7 +487,7 @@ discard block |
||
| 487 | 487 | |
| 488 | 488 | $possibleApps = array(); |
| 489 | 489 | foreach (OC::$APPSROOTS as $dir) { |
| 490 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 490 | + if (file_exists($dir['path'].'/'.$appId)) { |
|
| 491 | 491 | $possibleApps[] = $dir; |
| 492 | 492 | } |
| 493 | 493 | } |
@@ -528,7 +528,7 @@ discard block |
||
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | if (($dir = self::findAppInDirectories($appId)) != false) { |
| 531 | - return $dir['path'] . '/' . $appId; |
|
| 531 | + return $dir['path'].'/'.$appId; |
|
| 532 | 532 | } |
| 533 | 533 | return false; |
| 534 | 534 | } |
@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | */ |
| 543 | 543 | public static function getAppWebPath($appId) { |
| 544 | 544 | if (($dir = self::findAppInDirectories($appId)) != false) { |
| 545 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 545 | + return OC::$WEBROOT.$dir['url'].'/'.$appId; |
|
| 546 | 546 | } |
| 547 | 547 | return false; |
| 548 | 548 | } |
@@ -555,7 +555,7 @@ discard block |
||
| 555 | 555 | * @return string |
| 556 | 556 | */ |
| 557 | 557 | public static function getAppVersion($appId, $useCache = true) { |
| 558 | - if($useCache && isset(self::$appVersion[$appId])) { |
|
| 558 | + if ($useCache && isset(self::$appVersion[$appId])) { |
|
| 559 | 559 | return self::$appVersion[$appId]; |
| 560 | 560 | } |
| 561 | 561 | |
@@ -571,7 +571,7 @@ discard block |
||
| 571 | 571 | * @return string |
| 572 | 572 | */ |
| 573 | 573 | public static function getAppVersionByPath($path) { |
| 574 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 574 | + $infoFile = $path.'/appinfo/info.xml'; |
|
| 575 | 575 | $appData = self::getAppInfo($infoFile, true); |
| 576 | 576 | return isset($appData['version']) ? $appData['version'] : ''; |
| 577 | 577 | } |
@@ -594,10 +594,10 @@ discard block |
||
| 594 | 594 | return self::$appInfo[$appId]; |
| 595 | 595 | } |
| 596 | 596 | $appPath = self::getAppPath($appId); |
| 597 | - if($appPath === false) { |
|
| 597 | + if ($appPath === false) { |
|
| 598 | 598 | return null; |
| 599 | 599 | } |
| 600 | - $file = $appPath . '/appinfo/info.xml'; |
|
| 600 | + $file = $appPath.'/appinfo/info.xml'; |
|
| 601 | 601 | } |
| 602 | 602 | |
| 603 | 603 | $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo')); |
@@ -606,9 +606,9 @@ discard block |
||
| 606 | 606 | if (is_array($data)) { |
| 607 | 607 | $data = OC_App::parseAppInfo($data, $lang); |
| 608 | 608 | } |
| 609 | - if(isset($data['ocsid'])) { |
|
| 609 | + if (isset($data['ocsid'])) { |
|
| 610 | 610 | $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
| 611 | - if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 611 | + if ($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 612 | 612 | $data['ocsid'] = $storedId; |
| 613 | 613 | } |
| 614 | 614 | } |
@@ -700,7 +700,7 @@ discard block |
||
| 700 | 700 | * @param string $page |
| 701 | 701 | */ |
| 702 | 702 | public static function registerAdmin($app, $page) { |
| 703 | - self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 703 | + self::$adminForms[] = $app.'/'.$page.'.php'; |
|
| 704 | 704 | } |
| 705 | 705 | |
| 706 | 706 | /** |
@@ -709,7 +709,7 @@ discard block |
||
| 709 | 709 | * @param string $page |
| 710 | 710 | */ |
| 711 | 711 | public static function registerPersonal($app, $page) { |
| 712 | - self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 712 | + self::$personalForms[] = $app.'/'.$page.'.php'; |
|
| 713 | 713 | } |
| 714 | 714 | |
| 715 | 715 | /** |
@@ -738,7 +738,7 @@ discard block |
||
| 738 | 738 | |
| 739 | 739 | foreach (OC::$APPSROOTS as $apps_dir) { |
| 740 | 740 | if (!is_readable($apps_dir['path'])) { |
| 741 | - \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 741 | + \OCP\Util::writeLog('core', 'unable to read app folder : '.$apps_dir['path'], \OCP\Util::WARN); |
|
| 742 | 742 | continue; |
| 743 | 743 | } |
| 744 | 744 | $dh = opendir($apps_dir['path']); |
@@ -746,7 +746,7 @@ discard block |
||
| 746 | 746 | if (is_resource($dh)) { |
| 747 | 747 | while (($file = readdir($dh)) !== false) { |
| 748 | 748 | |
| 749 | - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 749 | + if ($file[0] != '.' and is_dir($apps_dir['path'].'/'.$file) and is_file($apps_dir['path'].'/'.$file.'/appinfo/info.xml')) { |
|
| 750 | 750 | |
| 751 | 751 | $apps[] = $file; |
| 752 | 752 | } |
@@ -777,12 +777,12 @@ discard block |
||
| 777 | 777 | |
| 778 | 778 | $info = OC_App::getAppInfo($app, false, $langCode); |
| 779 | 779 | if (!is_array($info)) { |
| 780 | - \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 780 | + \OCP\Util::writeLog('core', 'Could not read app info file for app "'.$app.'"', \OCP\Util::ERROR); |
|
| 781 | 781 | continue; |
| 782 | 782 | } |
| 783 | 783 | |
| 784 | 784 | if (!isset($info['name'])) { |
| 785 | - \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 785 | + \OCP\Util::writeLog('core', 'App id "'.$app.'" has no name in appinfo', \OCP\Util::ERROR); |
|
| 786 | 786 | continue; |
| 787 | 787 | } |
| 788 | 788 | |
@@ -809,13 +809,13 @@ discard block |
||
| 809 | 809 | } |
| 810 | 810 | |
| 811 | 811 | $appPath = self::getAppPath($app); |
| 812 | - if($appPath !== false) { |
|
| 813 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 812 | + if ($appPath !== false) { |
|
| 813 | + $appIcon = $appPath.'/img/'.$app.'.svg'; |
|
| 814 | 814 | if (file_exists($appIcon)) { |
| 815 | - $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 815 | + $info['preview'] = $urlGenerator->imagePath($app, $app.'.svg'); |
|
| 816 | 816 | $info['previewAsIcon'] = true; |
| 817 | 817 | } else { |
| 818 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 818 | + $appIcon = $appPath.'/img/app.svg'; |
|
| 819 | 819 | if (file_exists($appIcon)) { |
| 820 | 820 | $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
| 821 | 821 | $info['previewAsIcon'] = true; |
@@ -849,9 +849,9 @@ discard block |
||
| 849 | 849 | * @return string|false |
| 850 | 850 | */ |
| 851 | 851 | public static function getInternalAppIdByOcs($ocsID) { |
| 852 | - if(is_numeric($ocsID)) { |
|
| 852 | + if (is_numeric($ocsID)) { |
|
| 853 | 853 | $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid'); |
| 854 | - if(array_search($ocsID, $idArray)) { |
|
| 854 | + if (array_search($ocsID, $idArray)) { |
|
| 855 | 855 | return array_search($ocsID, $idArray); |
| 856 | 856 | } |
| 857 | 857 | } |
@@ -955,7 +955,7 @@ discard block |
||
| 955 | 955 | public static function getAppVersions() { |
| 956 | 956 | static $versions; |
| 957 | 957 | |
| 958 | - if(!$versions) { |
|
| 958 | + if (!$versions) { |
|
| 959 | 959 | $appConfig = \OC::$server->getAppConfig(); |
| 960 | 960 | $versions = $appConfig->getValues(false, 'installed_version'); |
| 961 | 961 | } |
@@ -977,7 +977,7 @@ discard block |
||
| 977 | 977 | if ($app !== false) { |
| 978 | 978 | // check if the app is compatible with this version of ownCloud |
| 979 | 979 | $info = self::getAppInfo($app); |
| 980 | - if(!is_array($info)) { |
|
| 980 | + if (!is_array($info)) { |
|
| 981 | 981 | throw new \Exception( |
| 982 | 982 | $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
| 983 | 983 | [$info['name']] |
@@ -1002,7 +1002,7 @@ discard block |
||
| 1002 | 1002 | $config->setAppValue($app, 'ocsid', $appData['id']); |
| 1003 | 1003 | } |
| 1004 | 1004 | |
| 1005 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1005 | + if (isset($info['settings']) && is_array($info['settings'])) { |
|
| 1006 | 1006 | $appPath = self::getAppPath($app); |
| 1007 | 1007 | self::registerAutoloading($app, $appPath); |
| 1008 | 1008 | \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
@@ -1010,7 +1010,7 @@ discard block |
||
| 1010 | 1010 | |
| 1011 | 1011 | \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
| 1012 | 1012 | } else { |
| 1013 | - if(empty($appName) ) { |
|
| 1013 | + if (empty($appName)) { |
|
| 1014 | 1014 | throw new \Exception($l->t("No app name specified")); |
| 1015 | 1015 | } else { |
| 1016 | 1016 | throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
@@ -1028,7 +1028,7 @@ discard block |
||
| 1028 | 1028 | */ |
| 1029 | 1029 | public static function updateApp($appId) { |
| 1030 | 1030 | $appPath = self::getAppPath($appId); |
| 1031 | - if($appPath === false) { |
|
| 1031 | + if ($appPath === false) { |
|
| 1032 | 1032 | return false; |
| 1033 | 1033 | } |
| 1034 | 1034 | self::registerAutoloading($appId, $appPath); |
@@ -1036,8 +1036,8 @@ discard block |
||
| 1036 | 1036 | $appData = self::getAppInfo($appId); |
| 1037 | 1037 | self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
| 1038 | 1038 | |
| 1039 | - if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1040 | - OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1039 | + if (file_exists($appPath.'/appinfo/database.xml')) { |
|
| 1040 | + OC_DB::updateDbFromStructure($appPath.'/appinfo/database.xml'); |
|
| 1041 | 1041 | } else { |
| 1042 | 1042 | $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
| 1043 | 1043 | $ms->migrate(); |
@@ -1048,26 +1048,26 @@ discard block |
||
| 1048 | 1048 | unset(self::$appVersion[$appId]); |
| 1049 | 1049 | |
| 1050 | 1050 | // run upgrade code |
| 1051 | - if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1051 | + if (file_exists($appPath.'/appinfo/update.php')) { |
|
| 1052 | 1052 | self::loadApp($appId); |
| 1053 | - include $appPath . '/appinfo/update.php'; |
|
| 1053 | + include $appPath.'/appinfo/update.php'; |
|
| 1054 | 1054 | } |
| 1055 | 1055 | self::setupBackgroundJobs($appData['background-jobs']); |
| 1056 | - if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1056 | + if (isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1057 | 1057 | \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
| 1058 | 1058 | } |
| 1059 | 1059 | |
| 1060 | 1060 | //set remote/public handlers |
| 1061 | 1061 | if (array_key_exists('ocsid', $appData)) { |
| 1062 | 1062 | \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
| 1063 | - } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1063 | + } elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1064 | 1064 | \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
| 1065 | 1065 | } |
| 1066 | 1066 | foreach ($appData['remote'] as $name => $path) { |
| 1067 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1067 | + \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $appId.'/'.$path); |
|
| 1068 | 1068 | } |
| 1069 | 1069 | foreach ($appData['public'] as $name => $path) { |
| 1070 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1070 | + \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $appId.'/'.$path); |
|
| 1071 | 1071 | } |
| 1072 | 1072 | |
| 1073 | 1073 | self::setAppTypes($appId); |
@@ -1137,17 +1137,17 @@ discard block |
||
| 1137 | 1137 | public static function getStorage($appId) { |
| 1138 | 1138 | if (OC_App::isEnabled($appId)) { //sanity check |
| 1139 | 1139 | if (\OC::$server->getUserSession()->isLoggedIn()) { |
| 1140 | - $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1140 | + $view = new \OC\Files\View('/'.OC_User::getUser()); |
|
| 1141 | 1141 | if (!$view->file_exists($appId)) { |
| 1142 | 1142 | $view->mkdir($appId); |
| 1143 | 1143 | } |
| 1144 | - return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1144 | + return new \OC\Files\View('/'.OC_User::getUser().'/'.$appId); |
|
| 1145 | 1145 | } else { |
| 1146 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1146 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.', user not logged in', \OCP\Util::ERROR); |
|
| 1147 | 1147 | return false; |
| 1148 | 1148 | } |
| 1149 | 1149 | } else { |
| 1150 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1150 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.' not enabled', \OCP\Util::ERROR); |
|
| 1151 | 1151 | return false; |
| 1152 | 1152 | } |
| 1153 | 1153 | } |
@@ -1179,9 +1179,9 @@ discard block |
||
| 1179 | 1179 | |
| 1180 | 1180 | if ($attributeLang === $similarLang) { |
| 1181 | 1181 | $similarLangFallback = $option['@value']; |
| 1182 | - } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1182 | + } else if (strpos($attributeLang, $similarLang.'_') === 0) { |
|
| 1183 | 1183 | if ($similarLangFallback === false) { |
| 1184 | - $similarLangFallback = $option['@value']; |
|
| 1184 | + $similarLangFallback = $option['@value']; |
|
| 1185 | 1185 | } |
| 1186 | 1186 | } |
| 1187 | 1187 | } else { |
@@ -1216,7 +1216,7 @@ discard block |
||
| 1216 | 1216 | $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
| 1217 | 1217 | } else if (isset($data['description']) && is_string($data['description'])) { |
| 1218 | 1218 | $data['description'] = trim($data['description']); |
| 1219 | - } else { |
|
| 1219 | + } else { |
|
| 1220 | 1220 | $data['description'] = ''; |
| 1221 | 1221 | } |
| 1222 | 1222 | |
@@ -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); |
@@ -37,122 +37,122 @@ |
||
| 37 | 37 | use Icewind\Streams\RetryWrapper; |
| 38 | 38 | |
| 39 | 39 | class FTP extends StreamWrapper{ |
| 40 | - private $password; |
|
| 41 | - private $user; |
|
| 42 | - private $host; |
|
| 43 | - private $secure; |
|
| 44 | - private $root; |
|
| 40 | + private $password; |
|
| 41 | + private $user; |
|
| 42 | + private $host; |
|
| 43 | + private $secure; |
|
| 44 | + private $root; |
|
| 45 | 45 | |
| 46 | - private static $tempFiles=array(); |
|
| 46 | + private static $tempFiles=array(); |
|
| 47 | 47 | |
| 48 | - public function __construct($params) { |
|
| 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']; |
|
| 53 | - if (isset($params['secure'])) { |
|
| 54 | - $this->secure = $params['secure']; |
|
| 55 | - } else { |
|
| 56 | - $this->secure = false; |
|
| 57 | - } |
|
| 58 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | - if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | - $this->root='/'.$this->root; |
|
| 61 | - } |
|
| 62 | - if (substr($this->root, -1) !== '/') { |
|
| 63 | - $this->root .= '/'; |
|
| 64 | - } |
|
| 65 | - } else { |
|
| 66 | - throw new \Exception('Creating FTP storage failed'); |
|
| 67 | - } |
|
| 48 | + public function __construct($params) { |
|
| 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']; |
|
| 53 | + if (isset($params['secure'])) { |
|
| 54 | + $this->secure = $params['secure']; |
|
| 55 | + } else { |
|
| 56 | + $this->secure = false; |
|
| 57 | + } |
|
| 58 | + $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | + if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | + $this->root='/'.$this->root; |
|
| 61 | + } |
|
| 62 | + if (substr($this->root, -1) !== '/') { |
|
| 63 | + $this->root .= '/'; |
|
| 64 | + } |
|
| 65 | + } else { |
|
| 66 | + throw new \Exception('Creating FTP storage failed'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public function getId(){ |
|
| 72 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | - } |
|
| 71 | + public function getId(){ |
|
| 72 | + return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * construct the ftp url |
|
| 77 | - * @param string $path |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public function constructUrl($path) { |
|
| 81 | - $url='ftp'; |
|
| 82 | - if ($this->secure) { |
|
| 83 | - $url.='s'; |
|
| 84 | - } |
|
| 85 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | - return $url; |
|
| 87 | - } |
|
| 75 | + /** |
|
| 76 | + * construct the ftp url |
|
| 77 | + * @param string $path |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public function constructUrl($path) { |
|
| 81 | + $url='ftp'; |
|
| 82 | + if ($this->secure) { |
|
| 83 | + $url.='s'; |
|
| 84 | + } |
|
| 85 | + $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | + return $url; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Unlinks file or directory |
|
| 91 | - * @param string $path |
|
| 92 | - */ |
|
| 93 | - public function unlink($path) { |
|
| 94 | - if ($this->is_dir($path)) { |
|
| 95 | - return $this->rmdir($path); |
|
| 96 | - } |
|
| 97 | - else { |
|
| 98 | - $url = $this->constructUrl($path); |
|
| 99 | - $result = unlink($url); |
|
| 100 | - clearstatcache(true, $url); |
|
| 101 | - return $result; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - public function fopen($path,$mode) { |
|
| 105 | - switch($mode) { |
|
| 106 | - case 'r': |
|
| 107 | - case 'rb': |
|
| 108 | - case 'w': |
|
| 109 | - case 'wb': |
|
| 110 | - case 'a': |
|
| 111 | - case 'ab': |
|
| 112 | - //these are supported by the wrapper |
|
| 113 | - $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | - return RetryWrapper::wrap($handle); |
|
| 116 | - case 'r+': |
|
| 117 | - case 'w+': |
|
| 118 | - case 'wb+': |
|
| 119 | - case 'a+': |
|
| 120 | - case 'x': |
|
| 121 | - case 'x+': |
|
| 122 | - case 'c': |
|
| 123 | - case 'c+': |
|
| 124 | - //emulate these |
|
| 125 | - if (strrpos($path, '.')!==false) { |
|
| 126 | - $ext=substr($path, strrpos($path, '.')); |
|
| 127 | - } else { |
|
| 128 | - $ext=''; |
|
| 129 | - } |
|
| 130 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | - if ($this->file_exists($path)) { |
|
| 132 | - $this->getFile($path, $tmpFile); |
|
| 133 | - } |
|
| 134 | - $handle = fopen($tmpFile, $mode); |
|
| 135 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | - $this->writeBack($tmpFile, $path); |
|
| 137 | - }); |
|
| 138 | - } |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 89 | + /** |
|
| 90 | + * Unlinks file or directory |
|
| 91 | + * @param string $path |
|
| 92 | + */ |
|
| 93 | + public function unlink($path) { |
|
| 94 | + if ($this->is_dir($path)) { |
|
| 95 | + return $this->rmdir($path); |
|
| 96 | + } |
|
| 97 | + else { |
|
| 98 | + $url = $this->constructUrl($path); |
|
| 99 | + $result = unlink($url); |
|
| 100 | + clearstatcache(true, $url); |
|
| 101 | + return $result; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + public function fopen($path,$mode) { |
|
| 105 | + switch($mode) { |
|
| 106 | + case 'r': |
|
| 107 | + case 'rb': |
|
| 108 | + case 'w': |
|
| 109 | + case 'wb': |
|
| 110 | + case 'a': |
|
| 111 | + case 'ab': |
|
| 112 | + //these are supported by the wrapper |
|
| 113 | + $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | + return RetryWrapper::wrap($handle); |
|
| 116 | + case 'r+': |
|
| 117 | + case 'w+': |
|
| 118 | + case 'wb+': |
|
| 119 | + case 'a+': |
|
| 120 | + case 'x': |
|
| 121 | + case 'x+': |
|
| 122 | + case 'c': |
|
| 123 | + case 'c+': |
|
| 124 | + //emulate these |
|
| 125 | + if (strrpos($path, '.')!==false) { |
|
| 126 | + $ext=substr($path, strrpos($path, '.')); |
|
| 127 | + } else { |
|
| 128 | + $ext=''; |
|
| 129 | + } |
|
| 130 | + $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | + if ($this->file_exists($path)) { |
|
| 132 | + $this->getFile($path, $tmpFile); |
|
| 133 | + } |
|
| 134 | + $handle = fopen($tmpFile, $mode); |
|
| 135 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | + $this->writeBack($tmpFile, $path); |
|
| 137 | + }); |
|
| 138 | + } |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - public function writeBack($tmpFile, $path) { |
|
| 143 | - $this->uploadFile($tmpFile, $path); |
|
| 144 | - unlink($tmpFile); |
|
| 145 | - } |
|
| 142 | + public function writeBack($tmpFile, $path) { |
|
| 143 | + $this->uploadFile($tmpFile, $path); |
|
| 144 | + unlink($tmpFile); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * check if php-ftp is installed |
|
| 149 | - */ |
|
| 150 | - public static function checkDependencies() { |
|
| 151 | - if (function_exists('ftp_login')) { |
|
| 152 | - return(true); |
|
| 153 | - } else { |
|
| 154 | - return array('ftp'); |
|
| 155 | - } |
|
| 156 | - } |
|
| 147 | + /** |
|
| 148 | + * check if php-ftp is installed |
|
| 149 | + */ |
|
| 150 | + public static function checkDependencies() { |
|
| 151 | + if (function_exists('ftp_login')) { |
|
| 152 | + return(true); |
|
| 153 | + } else { |
|
| 154 | + return array('ftp'); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | } |
@@ -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 | } |
@@ -616,6 +616,9 @@ |
||
| 616 | 616 | return $this->container; |
| 617 | 617 | } |
| 618 | 618 | |
| 619 | + /** |
|
| 620 | + * @param string $path |
|
| 621 | + */ |
|
| 619 | 622 | public function writeBack($tmpFile, $path) { |
| 620 | 623 | $fileData = fopen($tmpFile, 'r'); |
| 621 | 624 | $this->getContainer()->uploadObject($path, $fileData); |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | throw new \Exception("API Key or password, Username, Bucket and Region have to be configured."); |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - $this->id = 'swift::' . $params['user'] . md5($params['bucket']); |
|
| 166 | + $this->id = 'swift::'.$params['user'].md5($params['bucket']); |
|
| 167 | 167 | |
| 168 | 168 | $bucketUrl = Url::factory($params['bucket']); |
| 169 | 169 | if ($bucketUrl->isAbsolute()) { |
@@ -236,16 +236,16 @@ discard block |
||
| 236 | 236 | continue; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - if ($this->is_dir($path . '/' . $file)) { |
|
| 240 | - $this->rmdir($path . '/' . $file); |
|
| 239 | + if ($this->is_dir($path.'/'.$file)) { |
|
| 240 | + $this->rmdir($path.'/'.$file); |
|
| 241 | 241 | } else { |
| 242 | - $this->unlink($path . '/' . $file); |
|
| 242 | + $this->unlink($path.'/'.$file); |
|
| 243 | 243 | } |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | try { |
| 247 | - $this->getContainer()->dataObject()->setName($path . '/')->delete(); |
|
| 248 | - $this->objectCache->remove($path . '/'); |
|
| 247 | + $this->getContainer()->dataObject()->setName($path.'/')->delete(); |
|
| 248 | + $this->objectCache->remove($path.'/'); |
|
| 249 | 249 | } catch (Exceptions\DeleteError $e) { |
| 250 | 250 | \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
| 251 | 251 | return false; |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | $stat = array(); |
| 329 | - $stat['size'] = (int)$object->getContentLength(); |
|
| 329 | + $stat['size'] = (int) $object->getContentLength(); |
|
| 330 | 330 | $stat['mtime'] = $mtime; |
| 331 | 331 | $stat['atime'] = time(); |
| 332 | 332 | return $stat; |
@@ -358,7 +358,7 @@ discard block |
||
| 358 | 358 | try { |
| 359 | 359 | $this->getContainer()->dataObject()->setName($path)->delete(); |
| 360 | 360 | $this->objectCache->remove($path); |
| 361 | - $this->objectCache->remove($path . '/'); |
|
| 361 | + $this->objectCache->remove($path.'/'); |
|
| 362 | 362 | } catch (ClientErrorResponseException $e) { |
| 363 | 363 | if ($e->getResponse()->getStatusCode() !== 404) { |
| 364 | 364 | \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
@@ -387,8 +387,8 @@ discard block |
||
| 387 | 387 | $streamInterface = $streamFactory->fromRequest($client->get($c->getUrl($path))); |
| 388 | 388 | $streamInterface->rewind(); |
| 389 | 389 | $stream = $streamInterface->getStream(); |
| 390 | - stream_context_set_option($stream, 'swift','content', $streamInterface); |
|
| 391 | - if(!strrpos($streamInterface |
|
| 390 | + stream_context_set_option($stream, 'swift', 'content', $streamInterface); |
|
| 391 | + if (!strrpos($streamInterface |
|
| 392 | 392 | ->getMetaData('wrapper_data')[0], '404 Not Found')) { |
| 393 | 393 | return $stream; |
| 394 | 394 | } |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | file_put_contents($tmpFile, $source); |
| 423 | 423 | } |
| 424 | 424 | $handle = fopen($tmpFile, $mode); |
| 425 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 425 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 426 | 426 | $this->writeBack($tmpFile, $path); |
| 427 | 427 | }); |
| 428 | 428 | } |
@@ -469,10 +469,10 @@ discard block |
||
| 469 | 469 | |
| 470 | 470 | try { |
| 471 | 471 | $source = $this->fetchObject($path1); |
| 472 | - $source->copy($this->bucket . '/' . $path2); |
|
| 472 | + $source->copy($this->bucket.'/'.$path2); |
|
| 473 | 473 | // invalidate target object to force repopulation on fetch |
| 474 | 474 | $this->objectCache->remove($path2); |
| 475 | - $this->objectCache->remove($path2 . '/'); |
|
| 475 | + $this->objectCache->remove($path2.'/'); |
|
| 476 | 476 | } catch (ClientErrorResponseException $e) { |
| 477 | 477 | \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
| 478 | 478 | return false; |
@@ -484,11 +484,11 @@ discard block |
||
| 484 | 484 | $this->unlink($path2); |
| 485 | 485 | |
| 486 | 486 | try { |
| 487 | - $source = $this->fetchObject($path1 . '/'); |
|
| 488 | - $source->copy($this->bucket . '/' . $path2 . '/'); |
|
| 487 | + $source = $this->fetchObject($path1.'/'); |
|
| 488 | + $source->copy($this->bucket.'/'.$path2.'/'); |
|
| 489 | 489 | // invalidate target object to force repopulation on fetch |
| 490 | 490 | $this->objectCache->remove($path2); |
| 491 | - $this->objectCache->remove($path2 . '/'); |
|
| 491 | + $this->objectCache->remove($path2.'/'); |
|
| 492 | 492 | } catch (ClientErrorResponseException $e) { |
| 493 | 493 | \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
| 494 | 494 | return false; |
@@ -500,8 +500,8 @@ discard block |
||
| 500 | 500 | continue; |
| 501 | 501 | } |
| 502 | 502 | |
| 503 | - $source = $path1 . '/' . $file; |
|
| 504 | - $target = $path2 . '/' . $file; |
|
| 503 | + $source = $path1.'/'.$file; |
|
| 504 | + $target = $path2.'/'.$file; |
|
| 505 | 505 | $this->copy($source, $target); |
| 506 | 506 | } |
| 507 | 507 | |
@@ -635,7 +635,7 @@ discard block |
||
| 635 | 635 | $path = ''; |
| 636 | 636 | } |
| 637 | 637 | $cachedContent = $this->getCache()->getFolderContents($path); |
| 638 | - $cachedNames = array_map(function ($content) { |
|
| 638 | + $cachedNames = array_map(function($content) { |
|
| 639 | 639 | return $content['name']; |
| 640 | 640 | }, $cachedContent); |
| 641 | 641 | sort($cachedNames); |
@@ -48,606 +48,606 @@ |
||
| 48 | 48 | |
| 49 | 49 | class Swift extends \OC\Files\Storage\Common { |
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var \OpenCloud\ObjectStore\Service |
|
| 53 | - */ |
|
| 54 | - private $connection; |
|
| 55 | - /** |
|
| 56 | - * @var \OpenCloud\ObjectStore\Resource\Container |
|
| 57 | - */ |
|
| 58 | - private $container; |
|
| 59 | - /** |
|
| 60 | - * @var \OpenCloud\OpenStack |
|
| 61 | - */ |
|
| 62 | - private $anchor; |
|
| 63 | - /** |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - private $bucket; |
|
| 67 | - /** |
|
| 68 | - * Connection parameters |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - private $params; |
|
| 73 | - |
|
| 74 | - /** @var string */ |
|
| 75 | - private $id; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - private static $tmpFiles = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Key value cache mapping path to data object. Maps path to |
|
| 84 | - * \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject for existing |
|
| 85 | - * paths and path to false for not existing paths. |
|
| 86 | - * @var \OCP\ICache |
|
| 87 | - */ |
|
| 88 | - private $objectCache; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $path |
|
| 92 | - */ |
|
| 93 | - private function normalizePath($path) { |
|
| 94 | - $path = trim($path, '/'); |
|
| 95 | - |
|
| 96 | - if (!$path) { |
|
| 97 | - $path = '.'; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $path = str_replace('#', '%23', $path); |
|
| 101 | - |
|
| 102 | - return $path; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - const SUBCONTAINER_FILE = '.subcontainers'; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * translate directory path to container name |
|
| 109 | - * |
|
| 110 | - * @param string $path |
|
| 111 | - * @return string |
|
| 112 | - */ |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Fetches an object from the API. |
|
| 116 | - * If the object is cached already or a |
|
| 117 | - * failed "doesn't exist" response was cached, |
|
| 118 | - * that one will be returned. |
|
| 119 | - * |
|
| 120 | - * @param string $path |
|
| 121 | - * @return \OpenCloud\ObjectStore\Resource\DataObject|bool object |
|
| 122 | - * or false if the object did not exist |
|
| 123 | - */ |
|
| 124 | - private function fetchObject($path) { |
|
| 125 | - if ($this->objectCache->hasKey($path)) { |
|
| 126 | - // might be "false" if object did not exist from last check |
|
| 127 | - return $this->objectCache->get($path); |
|
| 128 | - } |
|
| 129 | - try { |
|
| 130 | - $object = $this->getContainer()->getPartialObject($path); |
|
| 131 | - $this->objectCache->set($path, $object); |
|
| 132 | - return $object; |
|
| 133 | - } catch (ClientErrorResponseException $e) { |
|
| 134 | - // this exception happens when the object does not exist, which |
|
| 135 | - // is expected in most cases |
|
| 136 | - $this->objectCache->set($path, false); |
|
| 137 | - return false; |
|
| 138 | - } catch (ClientErrorResponseException $e) { |
|
| 139 | - // Expected response is "404 Not Found", so only log if it isn't |
|
| 140 | - if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 141 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 142 | - } |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Returns whether the given path exists. |
|
| 149 | - * |
|
| 150 | - * @param string $path |
|
| 151 | - * |
|
| 152 | - * @return bool true if the object exist, false otherwise |
|
| 153 | - */ |
|
| 154 | - private function doesObjectExist($path) { |
|
| 155 | - return $this->fetchObject($path) !== false; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function __construct($params) { |
|
| 159 | - if ((empty($params['key']) and empty($params['password'])) |
|
| 160 | - or empty($params['user']) or empty($params['bucket']) |
|
| 161 | - or empty($params['region']) |
|
| 162 | - ) { |
|
| 163 | - throw new \Exception("API Key or password, Username, Bucket and Region have to be configured."); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $this->id = 'swift::' . $params['user'] . md5($params['bucket']); |
|
| 167 | - |
|
| 168 | - $bucketUrl = Url::factory($params['bucket']); |
|
| 169 | - if ($bucketUrl->isAbsolute()) { |
|
| 170 | - $this->bucket = end(($bucketUrl->getPathSegments())); |
|
| 171 | - $params['endpoint_url'] = $bucketUrl->addPath('..')->normalizePath(); |
|
| 172 | - } else { |
|
| 173 | - $this->bucket = $params['bucket']; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - if (empty($params['url'])) { |
|
| 177 | - $params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/'; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - if (empty($params['service_name'])) { |
|
| 181 | - $params['service_name'] = 'cloudFiles'; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $this->params = $params; |
|
| 185 | - // FIXME: private class... |
|
| 186 | - $this->objectCache = new \OC\Cache\CappedMemoryCache(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - public function mkdir($path) { |
|
| 190 | - $path = $this->normalizePath($path); |
|
| 191 | - |
|
| 192 | - if ($this->is_dir($path)) { |
|
| 193 | - return false; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - if ($path !== '.') { |
|
| 197 | - $path .= '/'; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - try { |
|
| 201 | - $customHeaders = array('content-type' => 'httpd/unix-directory'); |
|
| 202 | - $metadataHeaders = DataObject::stockHeaders(array()); |
|
| 203 | - $allHeaders = $customHeaders + $metadataHeaders; |
|
| 204 | - $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 205 | - // invalidate so that the next access gets the real object |
|
| 206 | - // with all properties |
|
| 207 | - $this->objectCache->remove($path); |
|
| 208 | - } catch (Exceptions\CreateUpdateError $e) { |
|
| 209 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return true; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - public function file_exists($path) { |
|
| 217 | - $path = $this->normalizePath($path); |
|
| 218 | - |
|
| 219 | - if ($path !== '.' && $this->is_dir($path)) { |
|
| 220 | - $path .= '/'; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return $this->doesObjectExist($path); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - public function rmdir($path) { |
|
| 227 | - $path = $this->normalizePath($path); |
|
| 228 | - |
|
| 229 | - if (!$this->is_dir($path) || !$this->isDeletable($path)) { |
|
| 230 | - return false; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $dh = $this->opendir($path); |
|
| 234 | - while ($file = readdir($dh)) { |
|
| 235 | - if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 236 | - continue; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if ($this->is_dir($path . '/' . $file)) { |
|
| 240 | - $this->rmdir($path . '/' . $file); |
|
| 241 | - } else { |
|
| 242 | - $this->unlink($path . '/' . $file); |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - try { |
|
| 247 | - $this->getContainer()->dataObject()->setName($path . '/')->delete(); |
|
| 248 | - $this->objectCache->remove($path . '/'); |
|
| 249 | - } catch (Exceptions\DeleteError $e) { |
|
| 250 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return true; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - public function opendir($path) { |
|
| 258 | - $path = $this->normalizePath($path); |
|
| 259 | - |
|
| 260 | - if ($path === '.') { |
|
| 261 | - $path = ''; |
|
| 262 | - } else { |
|
| 263 | - $path .= '/'; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $path = str_replace('%23', '#', $path); // the prefix is sent as a query param, so revert the encoding of # |
|
| 267 | - |
|
| 268 | - try { |
|
| 269 | - $files = array(); |
|
| 270 | - /** @var OpenCloud\Common\Collection $objects */ |
|
| 271 | - $objects = $this->getContainer()->objectList(array( |
|
| 272 | - 'prefix' => $path, |
|
| 273 | - 'delimiter' => '/' |
|
| 274 | - )); |
|
| 275 | - |
|
| 276 | - /** @var OpenCloud\ObjectStore\Resource\DataObject $object */ |
|
| 277 | - foreach ($objects as $object) { |
|
| 278 | - $file = basename($object->getName()); |
|
| 279 | - if ($file !== basename($path)) { |
|
| 280 | - $files[] = $file; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return IteratorDirectory::wrap($files); |
|
| 285 | - } catch (\Exception $e) { |
|
| 286 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 287 | - return false; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - public function stat($path) { |
|
| 293 | - $path = $this->normalizePath($path); |
|
| 294 | - |
|
| 295 | - if ($path === '.') { |
|
| 296 | - $path = ''; |
|
| 297 | - } else if ($this->is_dir($path)) { |
|
| 298 | - $path .= '/'; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - try { |
|
| 302 | - /** @var DataObject $object */ |
|
| 303 | - $object = $this->fetchObject($path); |
|
| 304 | - if (!$object) { |
|
| 305 | - return false; |
|
| 306 | - } |
|
| 307 | - } catch (ClientErrorResponseException $e) { |
|
| 308 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $dateTime = \DateTime::createFromFormat(\DateTime::RFC1123, $object->getLastModified()); |
|
| 313 | - if ($dateTime !== false) { |
|
| 314 | - $mtime = $dateTime->getTimestamp(); |
|
| 315 | - } else { |
|
| 316 | - $mtime = null; |
|
| 317 | - } |
|
| 318 | - $objectMetadata = $object->getMetadata(); |
|
| 319 | - $metaTimestamp = $objectMetadata->getProperty('timestamp'); |
|
| 320 | - if (isset($metaTimestamp)) { |
|
| 321 | - $mtime = $metaTimestamp; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if (!empty($mtime)) { |
|
| 325 | - $mtime = floor($mtime); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - $stat = array(); |
|
| 329 | - $stat['size'] = (int)$object->getContentLength(); |
|
| 330 | - $stat['mtime'] = $mtime; |
|
| 331 | - $stat['atime'] = time(); |
|
| 332 | - return $stat; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - public function filetype($path) { |
|
| 336 | - $path = $this->normalizePath($path); |
|
| 337 | - |
|
| 338 | - if ($path !== '.' && $this->doesObjectExist($path)) { |
|
| 339 | - return 'file'; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - if ($path !== '.') { |
|
| 343 | - $path .= '/'; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - if ($this->doesObjectExist($path)) { |
|
| 347 | - return 'dir'; |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - public function unlink($path) { |
|
| 352 | - $path = $this->normalizePath($path); |
|
| 353 | - |
|
| 354 | - if ($this->is_dir($path)) { |
|
| 355 | - return $this->rmdir($path); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - try { |
|
| 359 | - $this->getContainer()->dataObject()->setName($path)->delete(); |
|
| 360 | - $this->objectCache->remove($path); |
|
| 361 | - $this->objectCache->remove($path . '/'); |
|
| 362 | - } catch (ClientErrorResponseException $e) { |
|
| 363 | - if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 364 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 365 | - } |
|
| 366 | - return false; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return true; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - public function fopen($path, $mode) { |
|
| 373 | - $path = $this->normalizePath($path); |
|
| 374 | - |
|
| 375 | - switch ($mode) { |
|
| 376 | - case 'a': |
|
| 377 | - case 'ab': |
|
| 378 | - case 'a+': |
|
| 379 | - return false; |
|
| 380 | - case 'r': |
|
| 381 | - case 'rb': |
|
| 382 | - try { |
|
| 383 | - $c = $this->getContainer(); |
|
| 384 | - $streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory(); |
|
| 385 | - /** @var \OpenCloud\Common\Http\Client $client */ |
|
| 386 | - $client = $c->getClient(); |
|
| 387 | - $streamInterface = $streamFactory->fromRequest($client->get($c->getUrl($path))); |
|
| 388 | - $streamInterface->rewind(); |
|
| 389 | - $stream = $streamInterface->getStream(); |
|
| 390 | - stream_context_set_option($stream, 'swift','content', $streamInterface); |
|
| 391 | - if(!strrpos($streamInterface |
|
| 392 | - ->getMetaData('wrapper_data')[0], '404 Not Found')) { |
|
| 393 | - return $stream; |
|
| 394 | - } |
|
| 395 | - return false; |
|
| 396 | - } catch (\Guzzle\Http\Exception\BadResponseException $e) { |
|
| 397 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 398 | - return false; |
|
| 399 | - } |
|
| 400 | - case 'w': |
|
| 401 | - case 'wb': |
|
| 402 | - case 'r+': |
|
| 403 | - case 'w+': |
|
| 404 | - case 'wb+': |
|
| 405 | - case 'x': |
|
| 406 | - case 'x+': |
|
| 407 | - case 'c': |
|
| 408 | - case 'c+': |
|
| 409 | - if (strrpos($path, '.') !== false) { |
|
| 410 | - $ext = substr($path, strrpos($path, '.')); |
|
| 411 | - } else { |
|
| 412 | - $ext = ''; |
|
| 413 | - } |
|
| 414 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 415 | - // Fetch existing file if required |
|
| 416 | - if ($mode[0] !== 'w' && $this->file_exists($path)) { |
|
| 417 | - if ($mode[0] === 'x') { |
|
| 418 | - // File cannot already exist |
|
| 419 | - return false; |
|
| 420 | - } |
|
| 421 | - $source = $this->fopen($path, 'r'); |
|
| 422 | - file_put_contents($tmpFile, $source); |
|
| 423 | - } |
|
| 424 | - $handle = fopen($tmpFile, $mode); |
|
| 425 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 426 | - $this->writeBack($tmpFile, $path); |
|
| 427 | - }); |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - public function touch($path, $mtime = null) { |
|
| 432 | - $path = $this->normalizePath($path); |
|
| 433 | - if (is_null($mtime)) { |
|
| 434 | - $mtime = time(); |
|
| 435 | - } |
|
| 436 | - $metadata = array('timestamp' => $mtime); |
|
| 437 | - if ($this->file_exists($path)) { |
|
| 438 | - if ($this->is_dir($path) && $path !== '.') { |
|
| 439 | - $path .= '/'; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $object = $this->fetchObject($path); |
|
| 443 | - if ($object->saveMetadata($metadata)) { |
|
| 444 | - // invalidate target object to force repopulation on fetch |
|
| 445 | - $this->objectCache->remove($path); |
|
| 446 | - } |
|
| 447 | - return true; |
|
| 448 | - } else { |
|
| 449 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 450 | - $customHeaders = array('content-type' => $mimeType); |
|
| 451 | - $metadataHeaders = DataObject::stockHeaders($metadata); |
|
| 452 | - $allHeaders = $customHeaders + $metadataHeaders; |
|
| 453 | - $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 454 | - // invalidate target object to force repopulation on fetch |
|
| 455 | - $this->objectCache->remove($path); |
|
| 456 | - return true; |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - public function copy($path1, $path2) { |
|
| 461 | - $path1 = $this->normalizePath($path1); |
|
| 462 | - $path2 = $this->normalizePath($path2); |
|
| 463 | - |
|
| 464 | - $fileType = $this->filetype($path1); |
|
| 465 | - if ($fileType === 'file') { |
|
| 466 | - |
|
| 467 | - // make way |
|
| 468 | - $this->unlink($path2); |
|
| 469 | - |
|
| 470 | - try { |
|
| 471 | - $source = $this->fetchObject($path1); |
|
| 472 | - $source->copy($this->bucket . '/' . $path2); |
|
| 473 | - // invalidate target object to force repopulation on fetch |
|
| 474 | - $this->objectCache->remove($path2); |
|
| 475 | - $this->objectCache->remove($path2 . '/'); |
|
| 476 | - } catch (ClientErrorResponseException $e) { |
|
| 477 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 478 | - return false; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - } else if ($fileType === 'dir') { |
|
| 482 | - |
|
| 483 | - // make way |
|
| 484 | - $this->unlink($path2); |
|
| 485 | - |
|
| 486 | - try { |
|
| 487 | - $source = $this->fetchObject($path1 . '/'); |
|
| 488 | - $source->copy($this->bucket . '/' . $path2 . '/'); |
|
| 489 | - // invalidate target object to force repopulation on fetch |
|
| 490 | - $this->objectCache->remove($path2); |
|
| 491 | - $this->objectCache->remove($path2 . '/'); |
|
| 492 | - } catch (ClientErrorResponseException $e) { |
|
| 493 | - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 494 | - return false; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $dh = $this->opendir($path1); |
|
| 498 | - while ($file = readdir($dh)) { |
|
| 499 | - if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 500 | - continue; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - $source = $path1 . '/' . $file; |
|
| 504 | - $target = $path2 . '/' . $file; |
|
| 505 | - $this->copy($source, $target); |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - } else { |
|
| 509 | - //file does not exist |
|
| 510 | - return false; |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - return true; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - public function rename($path1, $path2) { |
|
| 517 | - $path1 = $this->normalizePath($path1); |
|
| 518 | - $path2 = $this->normalizePath($path2); |
|
| 519 | - |
|
| 520 | - $fileType = $this->filetype($path1); |
|
| 521 | - |
|
| 522 | - if ($fileType === 'dir' || $fileType === 'file') { |
|
| 523 | - // copy |
|
| 524 | - if ($this->copy($path1, $path2) === false) { |
|
| 525 | - return false; |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - // cleanup |
|
| 529 | - if ($this->unlink($path1) === false) { |
|
| 530 | - $this->unlink($path2); |
|
| 531 | - return false; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - return true; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - return false; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - public function getId() { |
|
| 541 | - return $this->id; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - /** |
|
| 545 | - * Returns the connection |
|
| 546 | - * |
|
| 547 | - * @return OpenCloud\ObjectStore\Service connected client |
|
| 548 | - * @throws \Exception if connection could not be made |
|
| 549 | - */ |
|
| 550 | - public function getConnection() { |
|
| 551 | - if (!is_null($this->connection)) { |
|
| 552 | - return $this->connection; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - $settings = array( |
|
| 556 | - 'username' => $this->params['user'], |
|
| 557 | - ); |
|
| 558 | - |
|
| 559 | - if (!empty($this->params['password'])) { |
|
| 560 | - $settings['password'] = $this->params['password']; |
|
| 561 | - } else if (!empty($this->params['key'])) { |
|
| 562 | - $settings['apiKey'] = $this->params['key']; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - if (!empty($this->params['tenant'])) { |
|
| 566 | - $settings['tenantName'] = $this->params['tenant']; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - if (!empty($this->params['timeout'])) { |
|
| 570 | - $settings['timeout'] = $this->params['timeout']; |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - if (isset($settings['apiKey'])) { |
|
| 574 | - $this->anchor = new Rackspace($this->params['url'], $settings); |
|
| 575 | - } else { |
|
| 576 | - $this->anchor = new OpenStack($this->params['url'], $settings); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - $connection = $this->anchor->objectStoreService($this->params['service_name'], $this->params['region']); |
|
| 580 | - |
|
| 581 | - if (!empty($this->params['endpoint_url'])) { |
|
| 582 | - $endpoint = $connection->getEndpoint(); |
|
| 583 | - $endpoint->setPublicUrl($this->params['endpoint_url']); |
|
| 584 | - $endpoint->setPrivateUrl($this->params['endpoint_url']); |
|
| 585 | - $connection->setEndpoint($endpoint); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - $this->connection = $connection; |
|
| 589 | - |
|
| 590 | - return $this->connection; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Returns the initialized object store container. |
|
| 595 | - * |
|
| 596 | - * @return OpenCloud\ObjectStore\Resource\Container |
|
| 597 | - */ |
|
| 598 | - public function getContainer() { |
|
| 599 | - if (!is_null($this->container)) { |
|
| 600 | - return $this->container; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - try { |
|
| 604 | - $this->container = $this->getConnection()->getContainer($this->bucket); |
|
| 605 | - } catch (ClientErrorResponseException $e) { |
|
| 606 | - $this->container = $this->getConnection()->createContainer($this->bucket); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - if (!$this->file_exists('.')) { |
|
| 610 | - $this->mkdir('.'); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - return $this->container; |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - public function writeBack($tmpFile, $path) { |
|
| 617 | - $fileData = fopen($tmpFile, 'r'); |
|
| 618 | - $this->getContainer()->uploadObject($path, $fileData); |
|
| 619 | - // invalidate target object to force repopulation on fetch |
|
| 620 | - $this->objectCache->remove(self::$tmpFiles[$tmpFile]); |
|
| 621 | - unlink($tmpFile); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - public function hasUpdated($path, $time) { |
|
| 625 | - if ($this->is_file($path)) { |
|
| 626 | - return parent::hasUpdated($path, $time); |
|
| 627 | - } |
|
| 628 | - $path = $this->normalizePath($path); |
|
| 629 | - $dh = $this->opendir($path); |
|
| 630 | - $content = array(); |
|
| 631 | - while (($file = readdir($dh)) !== false) { |
|
| 632 | - $content[] = $file; |
|
| 633 | - } |
|
| 634 | - if ($path === '.') { |
|
| 635 | - $path = ''; |
|
| 636 | - } |
|
| 637 | - $cachedContent = $this->getCache()->getFolderContents($path); |
|
| 638 | - $cachedNames = array_map(function ($content) { |
|
| 639 | - return $content['name']; |
|
| 640 | - }, $cachedContent); |
|
| 641 | - sort($cachedNames); |
|
| 642 | - sort($content); |
|
| 643 | - return $cachedNames !== $content; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * check if curl is installed |
|
| 648 | - */ |
|
| 649 | - public static function checkDependencies() { |
|
| 650 | - return true; |
|
| 651 | - } |
|
| 51 | + /** |
|
| 52 | + * @var \OpenCloud\ObjectStore\Service |
|
| 53 | + */ |
|
| 54 | + private $connection; |
|
| 55 | + /** |
|
| 56 | + * @var \OpenCloud\ObjectStore\Resource\Container |
|
| 57 | + */ |
|
| 58 | + private $container; |
|
| 59 | + /** |
|
| 60 | + * @var \OpenCloud\OpenStack |
|
| 61 | + */ |
|
| 62 | + private $anchor; |
|
| 63 | + /** |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + private $bucket; |
|
| 67 | + /** |
|
| 68 | + * Connection parameters |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + private $params; |
|
| 73 | + |
|
| 74 | + /** @var string */ |
|
| 75 | + private $id; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + private static $tmpFiles = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Key value cache mapping path to data object. Maps path to |
|
| 84 | + * \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject for existing |
|
| 85 | + * paths and path to false for not existing paths. |
|
| 86 | + * @var \OCP\ICache |
|
| 87 | + */ |
|
| 88 | + private $objectCache; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $path |
|
| 92 | + */ |
|
| 93 | + private function normalizePath($path) { |
|
| 94 | + $path = trim($path, '/'); |
|
| 95 | + |
|
| 96 | + if (!$path) { |
|
| 97 | + $path = '.'; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $path = str_replace('#', '%23', $path); |
|
| 101 | + |
|
| 102 | + return $path; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + const SUBCONTAINER_FILE = '.subcontainers'; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * translate directory path to container name |
|
| 109 | + * |
|
| 110 | + * @param string $path |
|
| 111 | + * @return string |
|
| 112 | + */ |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Fetches an object from the API. |
|
| 116 | + * If the object is cached already or a |
|
| 117 | + * failed "doesn't exist" response was cached, |
|
| 118 | + * that one will be returned. |
|
| 119 | + * |
|
| 120 | + * @param string $path |
|
| 121 | + * @return \OpenCloud\ObjectStore\Resource\DataObject|bool object |
|
| 122 | + * or false if the object did not exist |
|
| 123 | + */ |
|
| 124 | + private function fetchObject($path) { |
|
| 125 | + if ($this->objectCache->hasKey($path)) { |
|
| 126 | + // might be "false" if object did not exist from last check |
|
| 127 | + return $this->objectCache->get($path); |
|
| 128 | + } |
|
| 129 | + try { |
|
| 130 | + $object = $this->getContainer()->getPartialObject($path); |
|
| 131 | + $this->objectCache->set($path, $object); |
|
| 132 | + return $object; |
|
| 133 | + } catch (ClientErrorResponseException $e) { |
|
| 134 | + // this exception happens when the object does not exist, which |
|
| 135 | + // is expected in most cases |
|
| 136 | + $this->objectCache->set($path, false); |
|
| 137 | + return false; |
|
| 138 | + } catch (ClientErrorResponseException $e) { |
|
| 139 | + // Expected response is "404 Not Found", so only log if it isn't |
|
| 140 | + if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 141 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 142 | + } |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Returns whether the given path exists. |
|
| 149 | + * |
|
| 150 | + * @param string $path |
|
| 151 | + * |
|
| 152 | + * @return bool true if the object exist, false otherwise |
|
| 153 | + */ |
|
| 154 | + private function doesObjectExist($path) { |
|
| 155 | + return $this->fetchObject($path) !== false; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function __construct($params) { |
|
| 159 | + if ((empty($params['key']) and empty($params['password'])) |
|
| 160 | + or empty($params['user']) or empty($params['bucket']) |
|
| 161 | + or empty($params['region']) |
|
| 162 | + ) { |
|
| 163 | + throw new \Exception("API Key or password, Username, Bucket and Region have to be configured."); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $this->id = 'swift::' . $params['user'] . md5($params['bucket']); |
|
| 167 | + |
|
| 168 | + $bucketUrl = Url::factory($params['bucket']); |
|
| 169 | + if ($bucketUrl->isAbsolute()) { |
|
| 170 | + $this->bucket = end(($bucketUrl->getPathSegments())); |
|
| 171 | + $params['endpoint_url'] = $bucketUrl->addPath('..')->normalizePath(); |
|
| 172 | + } else { |
|
| 173 | + $this->bucket = $params['bucket']; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + if (empty($params['url'])) { |
|
| 177 | + $params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/'; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + if (empty($params['service_name'])) { |
|
| 181 | + $params['service_name'] = 'cloudFiles'; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $this->params = $params; |
|
| 185 | + // FIXME: private class... |
|
| 186 | + $this->objectCache = new \OC\Cache\CappedMemoryCache(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + public function mkdir($path) { |
|
| 190 | + $path = $this->normalizePath($path); |
|
| 191 | + |
|
| 192 | + if ($this->is_dir($path)) { |
|
| 193 | + return false; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + if ($path !== '.') { |
|
| 197 | + $path .= '/'; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + try { |
|
| 201 | + $customHeaders = array('content-type' => 'httpd/unix-directory'); |
|
| 202 | + $metadataHeaders = DataObject::stockHeaders(array()); |
|
| 203 | + $allHeaders = $customHeaders + $metadataHeaders; |
|
| 204 | + $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 205 | + // invalidate so that the next access gets the real object |
|
| 206 | + // with all properties |
|
| 207 | + $this->objectCache->remove($path); |
|
| 208 | + } catch (Exceptions\CreateUpdateError $e) { |
|
| 209 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return true; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + public function file_exists($path) { |
|
| 217 | + $path = $this->normalizePath($path); |
|
| 218 | + |
|
| 219 | + if ($path !== '.' && $this->is_dir($path)) { |
|
| 220 | + $path .= '/'; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return $this->doesObjectExist($path); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + public function rmdir($path) { |
|
| 227 | + $path = $this->normalizePath($path); |
|
| 228 | + |
|
| 229 | + if (!$this->is_dir($path) || !$this->isDeletable($path)) { |
|
| 230 | + return false; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $dh = $this->opendir($path); |
|
| 234 | + while ($file = readdir($dh)) { |
|
| 235 | + if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 236 | + continue; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if ($this->is_dir($path . '/' . $file)) { |
|
| 240 | + $this->rmdir($path . '/' . $file); |
|
| 241 | + } else { |
|
| 242 | + $this->unlink($path . '/' . $file); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + try { |
|
| 247 | + $this->getContainer()->dataObject()->setName($path . '/')->delete(); |
|
| 248 | + $this->objectCache->remove($path . '/'); |
|
| 249 | + } catch (Exceptions\DeleteError $e) { |
|
| 250 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return true; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + public function opendir($path) { |
|
| 258 | + $path = $this->normalizePath($path); |
|
| 259 | + |
|
| 260 | + if ($path === '.') { |
|
| 261 | + $path = ''; |
|
| 262 | + } else { |
|
| 263 | + $path .= '/'; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $path = str_replace('%23', '#', $path); // the prefix is sent as a query param, so revert the encoding of # |
|
| 267 | + |
|
| 268 | + try { |
|
| 269 | + $files = array(); |
|
| 270 | + /** @var OpenCloud\Common\Collection $objects */ |
|
| 271 | + $objects = $this->getContainer()->objectList(array( |
|
| 272 | + 'prefix' => $path, |
|
| 273 | + 'delimiter' => '/' |
|
| 274 | + )); |
|
| 275 | + |
|
| 276 | + /** @var OpenCloud\ObjectStore\Resource\DataObject $object */ |
|
| 277 | + foreach ($objects as $object) { |
|
| 278 | + $file = basename($object->getName()); |
|
| 279 | + if ($file !== basename($path)) { |
|
| 280 | + $files[] = $file; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return IteratorDirectory::wrap($files); |
|
| 285 | + } catch (\Exception $e) { |
|
| 286 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 287 | + return false; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + public function stat($path) { |
|
| 293 | + $path = $this->normalizePath($path); |
|
| 294 | + |
|
| 295 | + if ($path === '.') { |
|
| 296 | + $path = ''; |
|
| 297 | + } else if ($this->is_dir($path)) { |
|
| 298 | + $path .= '/'; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + try { |
|
| 302 | + /** @var DataObject $object */ |
|
| 303 | + $object = $this->fetchObject($path); |
|
| 304 | + if (!$object) { |
|
| 305 | + return false; |
|
| 306 | + } |
|
| 307 | + } catch (ClientErrorResponseException $e) { |
|
| 308 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $dateTime = \DateTime::createFromFormat(\DateTime::RFC1123, $object->getLastModified()); |
|
| 313 | + if ($dateTime !== false) { |
|
| 314 | + $mtime = $dateTime->getTimestamp(); |
|
| 315 | + } else { |
|
| 316 | + $mtime = null; |
|
| 317 | + } |
|
| 318 | + $objectMetadata = $object->getMetadata(); |
|
| 319 | + $metaTimestamp = $objectMetadata->getProperty('timestamp'); |
|
| 320 | + if (isset($metaTimestamp)) { |
|
| 321 | + $mtime = $metaTimestamp; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if (!empty($mtime)) { |
|
| 325 | + $mtime = floor($mtime); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + $stat = array(); |
|
| 329 | + $stat['size'] = (int)$object->getContentLength(); |
|
| 330 | + $stat['mtime'] = $mtime; |
|
| 331 | + $stat['atime'] = time(); |
|
| 332 | + return $stat; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + public function filetype($path) { |
|
| 336 | + $path = $this->normalizePath($path); |
|
| 337 | + |
|
| 338 | + if ($path !== '.' && $this->doesObjectExist($path)) { |
|
| 339 | + return 'file'; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + if ($path !== '.') { |
|
| 343 | + $path .= '/'; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + if ($this->doesObjectExist($path)) { |
|
| 347 | + return 'dir'; |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + public function unlink($path) { |
|
| 352 | + $path = $this->normalizePath($path); |
|
| 353 | + |
|
| 354 | + if ($this->is_dir($path)) { |
|
| 355 | + return $this->rmdir($path); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + try { |
|
| 359 | + $this->getContainer()->dataObject()->setName($path)->delete(); |
|
| 360 | + $this->objectCache->remove($path); |
|
| 361 | + $this->objectCache->remove($path . '/'); |
|
| 362 | + } catch (ClientErrorResponseException $e) { |
|
| 363 | + if ($e->getResponse()->getStatusCode() !== 404) { |
|
| 364 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 365 | + } |
|
| 366 | + return false; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return true; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + public function fopen($path, $mode) { |
|
| 373 | + $path = $this->normalizePath($path); |
|
| 374 | + |
|
| 375 | + switch ($mode) { |
|
| 376 | + case 'a': |
|
| 377 | + case 'ab': |
|
| 378 | + case 'a+': |
|
| 379 | + return false; |
|
| 380 | + case 'r': |
|
| 381 | + case 'rb': |
|
| 382 | + try { |
|
| 383 | + $c = $this->getContainer(); |
|
| 384 | + $streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory(); |
|
| 385 | + /** @var \OpenCloud\Common\Http\Client $client */ |
|
| 386 | + $client = $c->getClient(); |
|
| 387 | + $streamInterface = $streamFactory->fromRequest($client->get($c->getUrl($path))); |
|
| 388 | + $streamInterface->rewind(); |
|
| 389 | + $stream = $streamInterface->getStream(); |
|
| 390 | + stream_context_set_option($stream, 'swift','content', $streamInterface); |
|
| 391 | + if(!strrpos($streamInterface |
|
| 392 | + ->getMetaData('wrapper_data')[0], '404 Not Found')) { |
|
| 393 | + return $stream; |
|
| 394 | + } |
|
| 395 | + return false; |
|
| 396 | + } catch (\Guzzle\Http\Exception\BadResponseException $e) { |
|
| 397 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 400 | + case 'w': |
|
| 401 | + case 'wb': |
|
| 402 | + case 'r+': |
|
| 403 | + case 'w+': |
|
| 404 | + case 'wb+': |
|
| 405 | + case 'x': |
|
| 406 | + case 'x+': |
|
| 407 | + case 'c': |
|
| 408 | + case 'c+': |
|
| 409 | + if (strrpos($path, '.') !== false) { |
|
| 410 | + $ext = substr($path, strrpos($path, '.')); |
|
| 411 | + } else { |
|
| 412 | + $ext = ''; |
|
| 413 | + } |
|
| 414 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 415 | + // Fetch existing file if required |
|
| 416 | + if ($mode[0] !== 'w' && $this->file_exists($path)) { |
|
| 417 | + if ($mode[0] === 'x') { |
|
| 418 | + // File cannot already exist |
|
| 419 | + return false; |
|
| 420 | + } |
|
| 421 | + $source = $this->fopen($path, 'r'); |
|
| 422 | + file_put_contents($tmpFile, $source); |
|
| 423 | + } |
|
| 424 | + $handle = fopen($tmpFile, $mode); |
|
| 425 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 426 | + $this->writeBack($tmpFile, $path); |
|
| 427 | + }); |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + public function touch($path, $mtime = null) { |
|
| 432 | + $path = $this->normalizePath($path); |
|
| 433 | + if (is_null($mtime)) { |
|
| 434 | + $mtime = time(); |
|
| 435 | + } |
|
| 436 | + $metadata = array('timestamp' => $mtime); |
|
| 437 | + if ($this->file_exists($path)) { |
|
| 438 | + if ($this->is_dir($path) && $path !== '.') { |
|
| 439 | + $path .= '/'; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $object = $this->fetchObject($path); |
|
| 443 | + if ($object->saveMetadata($metadata)) { |
|
| 444 | + // invalidate target object to force repopulation on fetch |
|
| 445 | + $this->objectCache->remove($path); |
|
| 446 | + } |
|
| 447 | + return true; |
|
| 448 | + } else { |
|
| 449 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 450 | + $customHeaders = array('content-type' => $mimeType); |
|
| 451 | + $metadataHeaders = DataObject::stockHeaders($metadata); |
|
| 452 | + $allHeaders = $customHeaders + $metadataHeaders; |
|
| 453 | + $this->getContainer()->uploadObject($path, '', $allHeaders); |
|
| 454 | + // invalidate target object to force repopulation on fetch |
|
| 455 | + $this->objectCache->remove($path); |
|
| 456 | + return true; |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + public function copy($path1, $path2) { |
|
| 461 | + $path1 = $this->normalizePath($path1); |
|
| 462 | + $path2 = $this->normalizePath($path2); |
|
| 463 | + |
|
| 464 | + $fileType = $this->filetype($path1); |
|
| 465 | + if ($fileType === 'file') { |
|
| 466 | + |
|
| 467 | + // make way |
|
| 468 | + $this->unlink($path2); |
|
| 469 | + |
|
| 470 | + try { |
|
| 471 | + $source = $this->fetchObject($path1); |
|
| 472 | + $source->copy($this->bucket . '/' . $path2); |
|
| 473 | + // invalidate target object to force repopulation on fetch |
|
| 474 | + $this->objectCache->remove($path2); |
|
| 475 | + $this->objectCache->remove($path2 . '/'); |
|
| 476 | + } catch (ClientErrorResponseException $e) { |
|
| 477 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 478 | + return false; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + } else if ($fileType === 'dir') { |
|
| 482 | + |
|
| 483 | + // make way |
|
| 484 | + $this->unlink($path2); |
|
| 485 | + |
|
| 486 | + try { |
|
| 487 | + $source = $this->fetchObject($path1 . '/'); |
|
| 488 | + $source->copy($this->bucket . '/' . $path2 . '/'); |
|
| 489 | + // invalidate target object to force repopulation on fetch |
|
| 490 | + $this->objectCache->remove($path2); |
|
| 491 | + $this->objectCache->remove($path2 . '/'); |
|
| 492 | + } catch (ClientErrorResponseException $e) { |
|
| 493 | + \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
|
| 494 | + return false; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $dh = $this->opendir($path1); |
|
| 498 | + while ($file = readdir($dh)) { |
|
| 499 | + if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 500 | + continue; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + $source = $path1 . '/' . $file; |
|
| 504 | + $target = $path2 . '/' . $file; |
|
| 505 | + $this->copy($source, $target); |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + } else { |
|
| 509 | + //file does not exist |
|
| 510 | + return false; |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + return true; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + public function rename($path1, $path2) { |
|
| 517 | + $path1 = $this->normalizePath($path1); |
|
| 518 | + $path2 = $this->normalizePath($path2); |
|
| 519 | + |
|
| 520 | + $fileType = $this->filetype($path1); |
|
| 521 | + |
|
| 522 | + if ($fileType === 'dir' || $fileType === 'file') { |
|
| 523 | + // copy |
|
| 524 | + if ($this->copy($path1, $path2) === false) { |
|
| 525 | + return false; |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + // cleanup |
|
| 529 | + if ($this->unlink($path1) === false) { |
|
| 530 | + $this->unlink($path2); |
|
| 531 | + return false; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + return true; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + return false; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + public function getId() { |
|
| 541 | + return $this->id; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + /** |
|
| 545 | + * Returns the connection |
|
| 546 | + * |
|
| 547 | + * @return OpenCloud\ObjectStore\Service connected client |
|
| 548 | + * @throws \Exception if connection could not be made |
|
| 549 | + */ |
|
| 550 | + public function getConnection() { |
|
| 551 | + if (!is_null($this->connection)) { |
|
| 552 | + return $this->connection; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + $settings = array( |
|
| 556 | + 'username' => $this->params['user'], |
|
| 557 | + ); |
|
| 558 | + |
|
| 559 | + if (!empty($this->params['password'])) { |
|
| 560 | + $settings['password'] = $this->params['password']; |
|
| 561 | + } else if (!empty($this->params['key'])) { |
|
| 562 | + $settings['apiKey'] = $this->params['key']; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + if (!empty($this->params['tenant'])) { |
|
| 566 | + $settings['tenantName'] = $this->params['tenant']; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + if (!empty($this->params['timeout'])) { |
|
| 570 | + $settings['timeout'] = $this->params['timeout']; |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + if (isset($settings['apiKey'])) { |
|
| 574 | + $this->anchor = new Rackspace($this->params['url'], $settings); |
|
| 575 | + } else { |
|
| 576 | + $this->anchor = new OpenStack($this->params['url'], $settings); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + $connection = $this->anchor->objectStoreService($this->params['service_name'], $this->params['region']); |
|
| 580 | + |
|
| 581 | + if (!empty($this->params['endpoint_url'])) { |
|
| 582 | + $endpoint = $connection->getEndpoint(); |
|
| 583 | + $endpoint->setPublicUrl($this->params['endpoint_url']); |
|
| 584 | + $endpoint->setPrivateUrl($this->params['endpoint_url']); |
|
| 585 | + $connection->setEndpoint($endpoint); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + $this->connection = $connection; |
|
| 589 | + |
|
| 590 | + return $this->connection; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Returns the initialized object store container. |
|
| 595 | + * |
|
| 596 | + * @return OpenCloud\ObjectStore\Resource\Container |
|
| 597 | + */ |
|
| 598 | + public function getContainer() { |
|
| 599 | + if (!is_null($this->container)) { |
|
| 600 | + return $this->container; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + try { |
|
| 604 | + $this->container = $this->getConnection()->getContainer($this->bucket); |
|
| 605 | + } catch (ClientErrorResponseException $e) { |
|
| 606 | + $this->container = $this->getConnection()->createContainer($this->bucket); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + if (!$this->file_exists('.')) { |
|
| 610 | + $this->mkdir('.'); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + return $this->container; |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + public function writeBack($tmpFile, $path) { |
|
| 617 | + $fileData = fopen($tmpFile, 'r'); |
|
| 618 | + $this->getContainer()->uploadObject($path, $fileData); |
|
| 619 | + // invalidate target object to force repopulation on fetch |
|
| 620 | + $this->objectCache->remove(self::$tmpFiles[$tmpFile]); |
|
| 621 | + unlink($tmpFile); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + public function hasUpdated($path, $time) { |
|
| 625 | + if ($this->is_file($path)) { |
|
| 626 | + return parent::hasUpdated($path, $time); |
|
| 627 | + } |
|
| 628 | + $path = $this->normalizePath($path); |
|
| 629 | + $dh = $this->opendir($path); |
|
| 630 | + $content = array(); |
|
| 631 | + while (($file = readdir($dh)) !== false) { |
|
| 632 | + $content[] = $file; |
|
| 633 | + } |
|
| 634 | + if ($path === '.') { |
|
| 635 | + $path = ''; |
|
| 636 | + } |
|
| 637 | + $cachedContent = $this->getCache()->getFolderContents($path); |
|
| 638 | + $cachedNames = array_map(function ($content) { |
|
| 639 | + return $content['name']; |
|
| 640 | + }, $cachedContent); |
|
| 641 | + sort($cachedNames); |
|
| 642 | + sort($content); |
|
| 643 | + return $cachedNames !== $content; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * check if curl is installed |
|
| 648 | + */ |
|
| 649 | + public static function checkDependencies() { |
|
| 650 | + return true; |
|
| 651 | + } |
|
| 652 | 652 | |
| 653 | 653 | } |