@@ -159,7 +159,7 @@ |
||
| 159 | 159 | * Checks whether the given path is a part file |
| 160 | 160 | * |
| 161 | 161 | * @param string $path Path that may identify a .part file |
| 162 | - * @return string File path without .part extension |
|
| 162 | + * @return boolean File path without .part extension |
|
| 163 | 163 | * @note this is needed for reusing keys |
| 164 | 164 | */ |
| 165 | 165 | private function isPartFile($path) { |
@@ -32,180 +32,180 @@ |
||
| 32 | 32 | |
| 33 | 33 | class Quota extends Wrapper { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var int $quota |
|
| 37 | - */ |
|
| 38 | - protected $quota; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var string $sizeRoot |
|
| 42 | - */ |
|
| 43 | - protected $sizeRoot; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param array $parameters |
|
| 47 | - */ |
|
| 48 | - public function __construct($parameters) { |
|
| 49 | - parent::__construct($parameters); |
|
| 50 | - $this->quota = $parameters['quota']; |
|
| 51 | - $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return int quota value |
|
| 56 | - */ |
|
| 57 | - public function getQuota() { |
|
| 58 | - return $this->quota; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param string $path |
|
| 63 | - * @param \OC\Files\Storage\Storage $storage |
|
| 64 | - */ |
|
| 65 | - protected function getSize($path, $storage = null) { |
|
| 66 | - if (is_null($storage)) { |
|
| 67 | - $cache = $this->getCache(); |
|
| 68 | - } else { |
|
| 69 | - $cache = $storage->getCache(); |
|
| 70 | - } |
|
| 71 | - $data = $cache->get($path); |
|
| 72 | - if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
| 73 | - return $data['size']; |
|
| 74 | - } else { |
|
| 75 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get free space as limited by the quota |
|
| 81 | - * |
|
| 82 | - * @param string $path |
|
| 83 | - * @return int |
|
| 84 | - */ |
|
| 85 | - public function free_space($path) { |
|
| 86 | - if ($this->quota < 0 || strpos($path, 'cache') === 0) { |
|
| 87 | - return $this->storage->free_space($path); |
|
| 88 | - } else { |
|
| 89 | - $used = $this->getSize($this->sizeRoot); |
|
| 90 | - if ($used < 0) { |
|
| 91 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 92 | - } else { |
|
| 93 | - $free = $this->storage->free_space($path); |
|
| 94 | - $quotaFree = max($this->quota - $used, 0); |
|
| 95 | - // if free space is known |
|
| 96 | - if ($free >= 0) { |
|
| 97 | - $free = min($free, $quotaFree); |
|
| 98 | - } else { |
|
| 99 | - $free = $quotaFree; |
|
| 100 | - } |
|
| 101 | - return $free; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
| 108 | - * |
|
| 109 | - * @param string $path |
|
| 110 | - * @param string $data |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - public function file_put_contents($path, $data) { |
|
| 114 | - $free = $this->free_space($path); |
|
| 115 | - if ($free < 0 or strlen($data) < $free) { |
|
| 116 | - return $this->storage->file_put_contents($path, $data); |
|
| 117 | - } else { |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * see http://php.net/manual/en/function.copy.php |
|
| 124 | - * |
|
| 125 | - * @param string $source |
|
| 126 | - * @param string $target |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function copy($source, $target) { |
|
| 130 | - $free = $this->free_space($target); |
|
| 131 | - if ($free < 0 or $this->getSize($source) < $free) { |
|
| 132 | - return $this->storage->copy($source, $target); |
|
| 133 | - } else { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * see http://php.net/manual/en/function.fopen.php |
|
| 140 | - * |
|
| 141 | - * @param string $path |
|
| 142 | - * @param string $mode |
|
| 143 | - * @return resource |
|
| 144 | - */ |
|
| 145 | - public function fopen($path, $mode) { |
|
| 146 | - $source = $this->storage->fopen($path, $mode); |
|
| 147 | - |
|
| 148 | - // don't apply quota for part files |
|
| 149 | - if (!$this->isPartFile($path)) { |
|
| 150 | - $free = $this->free_space($path); |
|
| 151 | - if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
| 152 | - // only apply quota for files, not metadata, trash or others |
|
| 153 | - if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
| 154 | - return \OC\Files\Stream\Quota::wrap($source, $free); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - return $source; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Checks whether the given path is a part file |
|
| 163 | - * |
|
| 164 | - * @param string $path Path that may identify a .part file |
|
| 165 | - * @return string File path without .part extension |
|
| 166 | - * @note this is needed for reusing keys |
|
| 167 | - */ |
|
| 168 | - private function isPartFile($path) { |
|
| 169 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 170 | - |
|
| 171 | - return ($extension === 'part'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @param IStorage $sourceStorage |
|
| 176 | - * @param string $sourceInternalPath |
|
| 177 | - * @param string $targetInternalPath |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 181 | - $free = $this->free_space($targetInternalPath); |
|
| 182 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 183 | - return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 184 | - } else { |
|
| 185 | - return false; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @param IStorage $sourceStorage |
|
| 191 | - * @param string $sourceInternalPath |
|
| 192 | - * @param string $targetInternalPath |
|
| 193 | - * @return bool |
|
| 194 | - */ |
|
| 195 | - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 196 | - $free = $this->free_space($targetInternalPath); |
|
| 197 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 198 | - return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 199 | - } else { |
|
| 200 | - return false; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - public function mkdir($path) { |
|
| 205 | - if ($this->quota === 0.0) { |
|
| 206 | - return false; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return parent::mkdir($path); |
|
| 210 | - } |
|
| 35 | + /** |
|
| 36 | + * @var int $quota |
|
| 37 | + */ |
|
| 38 | + protected $quota; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var string $sizeRoot |
|
| 42 | + */ |
|
| 43 | + protected $sizeRoot; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param array $parameters |
|
| 47 | + */ |
|
| 48 | + public function __construct($parameters) { |
|
| 49 | + parent::__construct($parameters); |
|
| 50 | + $this->quota = $parameters['quota']; |
|
| 51 | + $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return int quota value |
|
| 56 | + */ |
|
| 57 | + public function getQuota() { |
|
| 58 | + return $this->quota; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param string $path |
|
| 63 | + * @param \OC\Files\Storage\Storage $storage |
|
| 64 | + */ |
|
| 65 | + protected function getSize($path, $storage = null) { |
|
| 66 | + if (is_null($storage)) { |
|
| 67 | + $cache = $this->getCache(); |
|
| 68 | + } else { |
|
| 69 | + $cache = $storage->getCache(); |
|
| 70 | + } |
|
| 71 | + $data = $cache->get($path); |
|
| 72 | + if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
| 73 | + return $data['size']; |
|
| 74 | + } else { |
|
| 75 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get free space as limited by the quota |
|
| 81 | + * |
|
| 82 | + * @param string $path |
|
| 83 | + * @return int |
|
| 84 | + */ |
|
| 85 | + public function free_space($path) { |
|
| 86 | + if ($this->quota < 0 || strpos($path, 'cache') === 0) { |
|
| 87 | + return $this->storage->free_space($path); |
|
| 88 | + } else { |
|
| 89 | + $used = $this->getSize($this->sizeRoot); |
|
| 90 | + if ($used < 0) { |
|
| 91 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 92 | + } else { |
|
| 93 | + $free = $this->storage->free_space($path); |
|
| 94 | + $quotaFree = max($this->quota - $used, 0); |
|
| 95 | + // if free space is known |
|
| 96 | + if ($free >= 0) { |
|
| 97 | + $free = min($free, $quotaFree); |
|
| 98 | + } else { |
|
| 99 | + $free = $quotaFree; |
|
| 100 | + } |
|
| 101 | + return $free; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
| 108 | + * |
|
| 109 | + * @param string $path |
|
| 110 | + * @param string $data |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + public function file_put_contents($path, $data) { |
|
| 114 | + $free = $this->free_space($path); |
|
| 115 | + if ($free < 0 or strlen($data) < $free) { |
|
| 116 | + return $this->storage->file_put_contents($path, $data); |
|
| 117 | + } else { |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * see http://php.net/manual/en/function.copy.php |
|
| 124 | + * |
|
| 125 | + * @param string $source |
|
| 126 | + * @param string $target |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function copy($source, $target) { |
|
| 130 | + $free = $this->free_space($target); |
|
| 131 | + if ($free < 0 or $this->getSize($source) < $free) { |
|
| 132 | + return $this->storage->copy($source, $target); |
|
| 133 | + } else { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * see http://php.net/manual/en/function.fopen.php |
|
| 140 | + * |
|
| 141 | + * @param string $path |
|
| 142 | + * @param string $mode |
|
| 143 | + * @return resource |
|
| 144 | + */ |
|
| 145 | + public function fopen($path, $mode) { |
|
| 146 | + $source = $this->storage->fopen($path, $mode); |
|
| 147 | + |
|
| 148 | + // don't apply quota for part files |
|
| 149 | + if (!$this->isPartFile($path)) { |
|
| 150 | + $free = $this->free_space($path); |
|
| 151 | + if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
| 152 | + // only apply quota for files, not metadata, trash or others |
|
| 153 | + if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
| 154 | + return \OC\Files\Stream\Quota::wrap($source, $free); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + return $source; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Checks whether the given path is a part file |
|
| 163 | + * |
|
| 164 | + * @param string $path Path that may identify a .part file |
|
| 165 | + * @return string File path without .part extension |
|
| 166 | + * @note this is needed for reusing keys |
|
| 167 | + */ |
|
| 168 | + private function isPartFile($path) { |
|
| 169 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 170 | + |
|
| 171 | + return ($extension === 'part'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @param IStorage $sourceStorage |
|
| 176 | + * @param string $sourceInternalPath |
|
| 177 | + * @param string $targetInternalPath |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 181 | + $free = $this->free_space($targetInternalPath); |
|
| 182 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 183 | + return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 184 | + } else { |
|
| 185 | + return false; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @param IStorage $sourceStorage |
|
| 191 | + * @param string $sourceInternalPath |
|
| 192 | + * @param string $targetInternalPath |
|
| 193 | + * @return bool |
|
| 194 | + */ |
|
| 195 | + public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 196 | + $free = $this->free_space($targetInternalPath); |
|
| 197 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 198 | + return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 199 | + } else { |
|
| 200 | + return false; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + public function mkdir($path) { |
|
| 205 | + if ($this->quota === 0.0) { |
|
| 206 | + return false; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return parent::mkdir($path); |
|
| 210 | + } |
|
| 211 | 211 | } |
@@ -88,7 +88,7 @@ |
||
| 88 | 88 | * send a message to the client |
| 89 | 89 | * |
| 90 | 90 | * @param string $type |
| 91 | - * @param mixed $data |
|
| 91 | + * @param string $data |
|
| 92 | 92 | * |
| 93 | 93 | * @throws \BadMethodCallException |
| 94 | 94 | * if only one parameter is given, a typeless message will be send with that parameter as data |
@@ -33,99 +33,99 @@ |
||
| 33 | 33 | * use server side events with caution, to many open requests can hang the server |
| 34 | 34 | */ |
| 35 | 35 | class OC_EventSource implements \OCP\IEventSource { |
| 36 | - /** |
|
| 37 | - * @var bool |
|
| 38 | - */ |
|
| 39 | - private $fallback; |
|
| 36 | + /** |
|
| 37 | + * @var bool |
|
| 38 | + */ |
|
| 39 | + private $fallback; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 44 | - private $fallBackId = 0; |
|
| 41 | + /** |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | + private $fallBackId = 0; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @var bool |
|
| 48 | - */ |
|
| 49 | - private $started = false; |
|
| 46 | + /** |
|
| 47 | + * @var bool |
|
| 48 | + */ |
|
| 49 | + private $started = false; |
|
| 50 | 50 | |
| 51 | - protected function init() { |
|
| 52 | - if ($this->started) { |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - $this->started = true; |
|
| 51 | + protected function init() { |
|
| 52 | + if ($this->started) { |
|
| 53 | + return; |
|
| 54 | + } |
|
| 55 | + $this->started = true; |
|
| 56 | 56 | |
| 57 | - // prevent php output buffering, caching and nginx buffering |
|
| 58 | - OC_Util::obEnd(); |
|
| 59 | - header('Cache-Control: no-cache'); |
|
| 60 | - header('X-Accel-Buffering: no'); |
|
| 61 | - $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
| 62 | - if ($this->fallback) { |
|
| 63 | - $this->fallBackId = (int)$_GET['fallback_id']; |
|
| 64 | - /** |
|
| 65 | - * FIXME: The default content-security-policy of ownCloud forbids inline |
|
| 66 | - * JavaScript for security reasons. IE starting on Windows 10 will |
|
| 67 | - * however also obey the CSP which will break the event source fallback. |
|
| 68 | - * |
|
| 69 | - * As a workaround thus we set a custom policy which allows the execution |
|
| 70 | - * of inline JavaScript. |
|
| 71 | - * |
|
| 72 | - * @link https://github.com/owncloud/core/issues/14286 |
|
| 73 | - */ |
|
| 74 | - header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
| 75 | - header("Content-Type: text/html"); |
|
| 76 | - echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
| 77 | - } else { |
|
| 78 | - header("Content-Type: text/event-stream"); |
|
| 79 | - } |
|
| 80 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 81 | - header('Location: '.\OC::$WEBROOT); |
|
| 82 | - exit(); |
|
| 83 | - } |
|
| 84 | - if (!\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 85 | - $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
| 86 | - $this->close(); |
|
| 87 | - exit(); |
|
| 88 | - } |
|
| 89 | - flush(); |
|
| 90 | - } |
|
| 57 | + // prevent php output buffering, caching and nginx buffering |
|
| 58 | + OC_Util::obEnd(); |
|
| 59 | + header('Cache-Control: no-cache'); |
|
| 60 | + header('X-Accel-Buffering: no'); |
|
| 61 | + $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
| 62 | + if ($this->fallback) { |
|
| 63 | + $this->fallBackId = (int)$_GET['fallback_id']; |
|
| 64 | + /** |
|
| 65 | + * FIXME: The default content-security-policy of ownCloud forbids inline |
|
| 66 | + * JavaScript for security reasons. IE starting on Windows 10 will |
|
| 67 | + * however also obey the CSP which will break the event source fallback. |
|
| 68 | + * |
|
| 69 | + * As a workaround thus we set a custom policy which allows the execution |
|
| 70 | + * of inline JavaScript. |
|
| 71 | + * |
|
| 72 | + * @link https://github.com/owncloud/core/issues/14286 |
|
| 73 | + */ |
|
| 74 | + header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
| 75 | + header("Content-Type: text/html"); |
|
| 76 | + echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
| 77 | + } else { |
|
| 78 | + header("Content-Type: text/event-stream"); |
|
| 79 | + } |
|
| 80 | + if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 81 | + header('Location: '.\OC::$WEBROOT); |
|
| 82 | + exit(); |
|
| 83 | + } |
|
| 84 | + if (!\OC::$server->getRequest()->passesCSRFCheck()) { |
|
| 85 | + $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
| 86 | + $this->close(); |
|
| 87 | + exit(); |
|
| 88 | + } |
|
| 89 | + flush(); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * send a message to the client |
|
| 94 | - * |
|
| 95 | - * @param string $type |
|
| 96 | - * @param mixed $data |
|
| 97 | - * |
|
| 98 | - * @throws \BadMethodCallException |
|
| 99 | - * if only one parameter is given, a typeless message will be send with that parameter as data |
|
| 100 | - * @suppress PhanDeprecatedFunction |
|
| 101 | - */ |
|
| 102 | - public function send($type, $data = null) { |
|
| 103 | - if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
| 104 | - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
| 105 | - } |
|
| 106 | - $this->init(); |
|
| 107 | - if (is_null($data)) { |
|
| 108 | - $data = $type; |
|
| 109 | - $type = null; |
|
| 110 | - } |
|
| 111 | - if ($this->fallback) { |
|
| 112 | - $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
| 113 | - . $this->fallBackId . ',"' . $type . '",' . OC_JSON::encode($data) . ')</script>' . PHP_EOL; |
|
| 114 | - echo $response; |
|
| 115 | - } else { |
|
| 116 | - if ($type) { |
|
| 117 | - echo 'event: ' . $type . PHP_EOL; |
|
| 118 | - } |
|
| 119 | - echo 'data: ' . OC_JSON::encode($data) . PHP_EOL; |
|
| 120 | - } |
|
| 121 | - echo PHP_EOL; |
|
| 122 | - flush(); |
|
| 123 | - } |
|
| 92 | + /** |
|
| 93 | + * send a message to the client |
|
| 94 | + * |
|
| 95 | + * @param string $type |
|
| 96 | + * @param mixed $data |
|
| 97 | + * |
|
| 98 | + * @throws \BadMethodCallException |
|
| 99 | + * if only one parameter is given, a typeless message will be send with that parameter as data |
|
| 100 | + * @suppress PhanDeprecatedFunction |
|
| 101 | + */ |
|
| 102 | + public function send($type, $data = null) { |
|
| 103 | + if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
| 104 | + throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
| 105 | + } |
|
| 106 | + $this->init(); |
|
| 107 | + if (is_null($data)) { |
|
| 108 | + $data = $type; |
|
| 109 | + $type = null; |
|
| 110 | + } |
|
| 111 | + if ($this->fallback) { |
|
| 112 | + $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
| 113 | + . $this->fallBackId . ',"' . $type . '",' . OC_JSON::encode($data) . ')</script>' . PHP_EOL; |
|
| 114 | + echo $response; |
|
| 115 | + } else { |
|
| 116 | + if ($type) { |
|
| 117 | + echo 'event: ' . $type . PHP_EOL; |
|
| 118 | + } |
|
| 119 | + echo 'data: ' . OC_JSON::encode($data) . PHP_EOL; |
|
| 120 | + } |
|
| 121 | + echo PHP_EOL; |
|
| 122 | + flush(); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * close the connection of the event source |
|
| 127 | - */ |
|
| 128 | - public function close() { |
|
| 129 | - $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
| 130 | - } |
|
| 125 | + /** |
|
| 126 | + * close the connection of the event source |
|
| 127 | + */ |
|
| 128 | + public function close() { |
|
| 129 | + $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
| 130 | + } |
|
| 131 | 131 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | header('X-Accel-Buffering: no'); |
| 61 | 61 | $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
| 62 | 62 | if ($this->fallback) { |
| 63 | - $this->fallBackId = (int)$_GET['fallback_id']; |
|
| 63 | + $this->fallBackId = (int) $_GET['fallback_id']; |
|
| 64 | 64 | /** |
| 65 | 65 | * FIXME: The default content-security-policy of ownCloud forbids inline |
| 66 | 66 | * JavaScript for security reasons. IE starting on Windows 10 will |
@@ -73,11 +73,11 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
| 75 | 75 | header("Content-Type: text/html"); |
| 76 | - echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
| 76 | + echo str_repeat('<span></span>'.PHP_EOL, 10); //dummy data to keep IE happy |
|
| 77 | 77 | } else { |
| 78 | 78 | header("Content-Type: text/event-stream"); |
| 79 | 79 | } |
| 80 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 80 | + if (!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
| 81 | 81 | header('Location: '.\OC::$WEBROOT); |
| 82 | 82 | exit(); |
| 83 | 83 | } |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public function send($type, $data = null) { |
| 103 | 103 | if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
| 104 | - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
| 104 | + throw new BadMethodCallException('Type needs to be alphanumeric ('.$type.')'); |
|
| 105 | 105 | } |
| 106 | 106 | $this->init(); |
| 107 | 107 | if (is_null($data)) { |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | if ($this->fallback) { |
| 112 | 112 | $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
| 113 | - . $this->fallBackId . ',"' . $type . '",' . OC_JSON::encode($data) . ')</script>' . PHP_EOL; |
|
| 113 | + . $this->fallBackId.',"'.$type.'",'.OC_JSON::encode($data).')</script>'.PHP_EOL; |
|
| 114 | 114 | echo $response; |
| 115 | 115 | } else { |
| 116 | 116 | if ($type) { |
| 117 | - echo 'event: ' . $type . PHP_EOL; |
|
| 117 | + echo 'event: '.$type.PHP_EOL; |
|
| 118 | 118 | } |
| 119 | - echo 'data: ' . OC_JSON::encode($data) . PHP_EOL; |
|
| 119 | + echo 'data: '.OC_JSON::encode($data).PHP_EOL; |
|
| 120 | 120 | } |
| 121 | 121 | echo PHP_EOL; |
| 122 | 122 | flush(); |
@@ -155,7 +155,7 @@ |
||
| 155 | 155 | * Set a value in the cache if it's not already stored |
| 156 | 156 | * |
| 157 | 157 | * @param string $key |
| 158 | - * @param mixed $value |
|
| 158 | + * @param integer $value |
|
| 159 | 159 | * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
| 160 | 160 | * @return bool |
| 161 | 161 | * @throws \Exception |
@@ -33,193 +33,193 @@ |
||
| 33 | 33 | use OCP\IMemcache; |
| 34 | 34 | |
| 35 | 35 | class Memcached extends Cache implements IMemcache { |
| 36 | - use CASTrait; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var \Memcached $cache |
|
| 40 | - */ |
|
| 41 | - private static $cache = null; |
|
| 42 | - |
|
| 43 | - use CADTrait; |
|
| 44 | - |
|
| 45 | - public function __construct($prefix = '') { |
|
| 46 | - parent::__construct($prefix); |
|
| 47 | - if (is_null(self::$cache)) { |
|
| 48 | - self::$cache = new \Memcached(); |
|
| 49 | - |
|
| 50 | - $defaultOptions = [ |
|
| 51 | - \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
| 52 | - \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
| 53 | - \Memcached::OPT_SEND_TIMEOUT => 50, |
|
| 54 | - \Memcached::OPT_RECV_TIMEOUT => 50, |
|
| 55 | - \Memcached::OPT_POLL_TIMEOUT => 50, |
|
| 56 | - |
|
| 57 | - // Enable compression |
|
| 58 | - \Memcached::OPT_COMPRESSION => true, |
|
| 59 | - |
|
| 60 | - // Turn on consistent hashing |
|
| 61 | - \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
| 62 | - |
|
| 63 | - // Enable Binary Protocol |
|
| 64 | - //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
| 65 | - ]; |
|
| 66 | - // by default enable igbinary serializer if available |
|
| 67 | - if (\Memcached::HAVE_IGBINARY) { |
|
| 68 | - $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
| 69 | - \Memcached::SERIALIZER_IGBINARY; |
|
| 70 | - } |
|
| 71 | - $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
| 72 | - if (is_array($options)) { |
|
| 73 | - $options = $options + $defaultOptions; |
|
| 74 | - self::$cache->setOptions($options); |
|
| 75 | - } else { |
|
| 76 | - throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
| 80 | - if (!$servers) { |
|
| 81 | - $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
| 82 | - if ($server) { |
|
| 83 | - $servers = [$server]; |
|
| 84 | - } else { |
|
| 85 | - $servers = [['localhost', 11211]]; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - self::$cache->addServers($servers); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
| 94 | - */ |
|
| 95 | - protected function getNameSpace() { |
|
| 96 | - return $this->prefix; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - public function get($key) { |
|
| 100 | - $result = self::$cache->get($this->getNameSpace() . $key); |
|
| 101 | - if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
| 102 | - return null; |
|
| 103 | - } else { |
|
| 104 | - return $result; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function set($key, $value, $ttl = 0) { |
|
| 109 | - if ($ttl > 0) { |
|
| 110 | - $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
| 111 | - } else { |
|
| 112 | - $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
| 113 | - } |
|
| 114 | - if ($result !== true) { |
|
| 115 | - $this->verifyReturnCode(); |
|
| 116 | - } |
|
| 117 | - return $result; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function hasKey($key) { |
|
| 121 | - self::$cache->get($this->getNameSpace() . $key); |
|
| 122 | - return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function remove($key) { |
|
| 126 | - $result= self::$cache->delete($this->getNameSpace() . $key); |
|
| 127 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
| 128 | - $this->verifyReturnCode(); |
|
| 129 | - } |
|
| 130 | - return $result; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function clear($prefix = '') { |
|
| 134 | - $prefix = $this->getNameSpace() . $prefix; |
|
| 135 | - $allKeys = self::$cache->getAllKeys(); |
|
| 136 | - if ($allKeys === false) { |
|
| 137 | - // newer Memcached doesn't like getAllKeys(), flush everything |
|
| 138 | - self::$cache->flush(); |
|
| 139 | - return true; |
|
| 140 | - } |
|
| 141 | - $keys = array(); |
|
| 142 | - $prefixLength = strlen($prefix); |
|
| 143 | - foreach ($allKeys as $key) { |
|
| 144 | - if (substr($key, 0, $prefixLength) === $prefix) { |
|
| 145 | - $keys[] = $key; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - if (method_exists(self::$cache, 'deleteMulti')) { |
|
| 149 | - self::$cache->deleteMulti($keys); |
|
| 150 | - } else { |
|
| 151 | - foreach ($keys as $key) { |
|
| 152 | - self::$cache->delete($key); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - return true; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Set a value in the cache if it's not already stored |
|
| 160 | - * |
|
| 161 | - * @param string $key |
|
| 162 | - * @param mixed $value |
|
| 163 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
| 164 | - * @return bool |
|
| 165 | - * @throws \Exception |
|
| 166 | - */ |
|
| 167 | - public function add($key, $value, $ttl = 0) { |
|
| 168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
| 169 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
| 170 | - $this->verifyReturnCode(); |
|
| 171 | - } |
|
| 172 | - return $result; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Increase a stored number |
|
| 177 | - * |
|
| 178 | - * @param string $key |
|
| 179 | - * @param int $step |
|
| 180 | - * @return int | bool |
|
| 181 | - */ |
|
| 182 | - public function inc($key, $step = 1) { |
|
| 183 | - $this->add($key, 0); |
|
| 184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
| 185 | - |
|
| 186 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $result; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Decrease a stored number |
|
| 195 | - * |
|
| 196 | - * @param string $key |
|
| 197 | - * @param int $step |
|
| 198 | - * @return int | bool |
|
| 199 | - */ |
|
| 200 | - public function dec($key, $step = 1) { |
|
| 201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
| 202 | - |
|
| 203 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $result; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - static public function isAvailable() { |
|
| 211 | - return extension_loaded('memcached'); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * @throws \Exception |
|
| 216 | - */ |
|
| 217 | - private function verifyReturnCode() { |
|
| 218 | - $code = self::$cache->getResultCode(); |
|
| 219 | - if ($code === \Memcached::RES_SUCCESS) { |
|
| 220 | - return; |
|
| 221 | - } |
|
| 222 | - $message = self::$cache->getResultMessage(); |
|
| 223 | - throw new \Exception("Error $code interacting with memcached : $message"); |
|
| 224 | - } |
|
| 36 | + use CASTrait; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var \Memcached $cache |
|
| 40 | + */ |
|
| 41 | + private static $cache = null; |
|
| 42 | + |
|
| 43 | + use CADTrait; |
|
| 44 | + |
|
| 45 | + public function __construct($prefix = '') { |
|
| 46 | + parent::__construct($prefix); |
|
| 47 | + if (is_null(self::$cache)) { |
|
| 48 | + self::$cache = new \Memcached(); |
|
| 49 | + |
|
| 50 | + $defaultOptions = [ |
|
| 51 | + \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
| 52 | + \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
| 53 | + \Memcached::OPT_SEND_TIMEOUT => 50, |
|
| 54 | + \Memcached::OPT_RECV_TIMEOUT => 50, |
|
| 55 | + \Memcached::OPT_POLL_TIMEOUT => 50, |
|
| 56 | + |
|
| 57 | + // Enable compression |
|
| 58 | + \Memcached::OPT_COMPRESSION => true, |
|
| 59 | + |
|
| 60 | + // Turn on consistent hashing |
|
| 61 | + \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
| 62 | + |
|
| 63 | + // Enable Binary Protocol |
|
| 64 | + //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
| 65 | + ]; |
|
| 66 | + // by default enable igbinary serializer if available |
|
| 67 | + if (\Memcached::HAVE_IGBINARY) { |
|
| 68 | + $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
| 69 | + \Memcached::SERIALIZER_IGBINARY; |
|
| 70 | + } |
|
| 71 | + $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
| 72 | + if (is_array($options)) { |
|
| 73 | + $options = $options + $defaultOptions; |
|
| 74 | + self::$cache->setOptions($options); |
|
| 75 | + } else { |
|
| 76 | + throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
| 80 | + if (!$servers) { |
|
| 81 | + $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
| 82 | + if ($server) { |
|
| 83 | + $servers = [$server]; |
|
| 84 | + } else { |
|
| 85 | + $servers = [['localhost', 11211]]; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + self::$cache->addServers($servers); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
| 94 | + */ |
|
| 95 | + protected function getNameSpace() { |
|
| 96 | + return $this->prefix; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + public function get($key) { |
|
| 100 | + $result = self::$cache->get($this->getNameSpace() . $key); |
|
| 101 | + if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
| 102 | + return null; |
|
| 103 | + } else { |
|
| 104 | + return $result; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function set($key, $value, $ttl = 0) { |
|
| 109 | + if ($ttl > 0) { |
|
| 110 | + $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
| 111 | + } else { |
|
| 112 | + $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
| 113 | + } |
|
| 114 | + if ($result !== true) { |
|
| 115 | + $this->verifyReturnCode(); |
|
| 116 | + } |
|
| 117 | + return $result; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function hasKey($key) { |
|
| 121 | + self::$cache->get($this->getNameSpace() . $key); |
|
| 122 | + return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function remove($key) { |
|
| 126 | + $result= self::$cache->delete($this->getNameSpace() . $key); |
|
| 127 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
| 128 | + $this->verifyReturnCode(); |
|
| 129 | + } |
|
| 130 | + return $result; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function clear($prefix = '') { |
|
| 134 | + $prefix = $this->getNameSpace() . $prefix; |
|
| 135 | + $allKeys = self::$cache->getAllKeys(); |
|
| 136 | + if ($allKeys === false) { |
|
| 137 | + // newer Memcached doesn't like getAllKeys(), flush everything |
|
| 138 | + self::$cache->flush(); |
|
| 139 | + return true; |
|
| 140 | + } |
|
| 141 | + $keys = array(); |
|
| 142 | + $prefixLength = strlen($prefix); |
|
| 143 | + foreach ($allKeys as $key) { |
|
| 144 | + if (substr($key, 0, $prefixLength) === $prefix) { |
|
| 145 | + $keys[] = $key; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + if (method_exists(self::$cache, 'deleteMulti')) { |
|
| 149 | + self::$cache->deleteMulti($keys); |
|
| 150 | + } else { |
|
| 151 | + foreach ($keys as $key) { |
|
| 152 | + self::$cache->delete($key); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + return true; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Set a value in the cache if it's not already stored |
|
| 160 | + * |
|
| 161 | + * @param string $key |
|
| 162 | + * @param mixed $value |
|
| 163 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
| 164 | + * @return bool |
|
| 165 | + * @throws \Exception |
|
| 166 | + */ |
|
| 167 | + public function add($key, $value, $ttl = 0) { |
|
| 168 | + $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
| 169 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
| 170 | + $this->verifyReturnCode(); |
|
| 171 | + } |
|
| 172 | + return $result; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Increase a stored number |
|
| 177 | + * |
|
| 178 | + * @param string $key |
|
| 179 | + * @param int $step |
|
| 180 | + * @return int | bool |
|
| 181 | + */ |
|
| 182 | + public function inc($key, $step = 1) { |
|
| 183 | + $this->add($key, 0); |
|
| 184 | + $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
| 185 | + |
|
| 186 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $result; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Decrease a stored number |
|
| 195 | + * |
|
| 196 | + * @param string $key |
|
| 197 | + * @param int $step |
|
| 198 | + * @return int | bool |
|
| 199 | + */ |
|
| 200 | + public function dec($key, $step = 1) { |
|
| 201 | + $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
| 202 | + |
|
| 203 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $result; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + static public function isAvailable() { |
|
| 211 | + return extension_loaded('memcached'); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * @throws \Exception |
|
| 216 | + */ |
|
| 217 | + private function verifyReturnCode() { |
|
| 218 | + $code = self::$cache->getResultCode(); |
|
| 219 | + if ($code === \Memcached::RES_SUCCESS) { |
|
| 220 | + return; |
|
| 221 | + } |
|
| 222 | + $message = self::$cache->getResultMessage(); |
|
| 223 | + throw new \Exception("Error $code interacting with memcached : $message"); |
|
| 224 | + } |
|
| 225 | 225 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | public function get($key) { |
| 100 | - $result = self::$cache->get($this->getNameSpace() . $key); |
|
| 100 | + $result = self::$cache->get($this->getNameSpace().$key); |
|
| 101 | 101 | if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
| 102 | 102 | return null; |
| 103 | 103 | } else { |
@@ -107,9 +107,9 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | public function set($key, $value, $ttl = 0) { |
| 109 | 109 | if ($ttl > 0) { |
| 110 | - $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
| 110 | + $result = self::$cache->set($this->getNameSpace().$key, $value, $ttl); |
|
| 111 | 111 | } else { |
| 112 | - $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
| 112 | + $result = self::$cache->set($this->getNameSpace().$key, $value); |
|
| 113 | 113 | } |
| 114 | 114 | if ($result !== true) { |
| 115 | 115 | $this->verifyReturnCode(); |
@@ -118,12 +118,12 @@ discard block |
||
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | public function hasKey($key) { |
| 121 | - self::$cache->get($this->getNameSpace() . $key); |
|
| 121 | + self::$cache->get($this->getNameSpace().$key); |
|
| 122 | 122 | return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | public function remove($key) { |
| 126 | - $result= self::$cache->delete($this->getNameSpace() . $key); |
|
| 126 | + $result = self::$cache->delete($this->getNameSpace().$key); |
|
| 127 | 127 | if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
| 128 | 128 | $this->verifyReturnCode(); |
| 129 | 129 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | public function clear($prefix = '') { |
| 134 | - $prefix = $this->getNameSpace() . $prefix; |
|
| 134 | + $prefix = $this->getNameSpace().$prefix; |
|
| 135 | 135 | $allKeys = self::$cache->getAllKeys(); |
| 136 | 136 | if ($allKeys === false) { |
| 137 | 137 | // newer Memcached doesn't like getAllKeys(), flush everything |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | * @throws \Exception |
| 166 | 166 | */ |
| 167 | 167 | public function add($key, $value, $ttl = 0) { |
| 168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
| 168 | + $result = self::$cache->add($this->getPrefix().$key, $value, $ttl); |
|
| 169 | 169 | if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
| 170 | 170 | $this->verifyReturnCode(); |
| 171 | 171 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | public function inc($key, $step = 1) { |
| 183 | 183 | $this->add($key, 0); |
| 184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
| 184 | + $result = self::$cache->increment($this->getPrefix().$key, $step); |
|
| 185 | 185 | |
| 186 | 186 | if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
| 187 | 187 | return false; |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | * @return int | bool |
| 199 | 199 | */ |
| 200 | 200 | public function dec($key, $step = 1) { |
| 201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
| 201 | + $result = self::$cache->decrement($this->getPrefix().$key, $step); |
|
| 202 | 202 | |
| 203 | 203 | if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
| 204 | 204 | return false; |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * Returns expensive repair steps to be run on the |
| 140 | 140 | * command line with a special option. |
| 141 | 141 | * |
| 142 | - * @return IRepairStep[] |
|
| 142 | + * @return OldGroupMembershipShares[] |
|
| 143 | 143 | */ |
| 144 | 144 | public static function getExpensiveRepairSteps() { |
| 145 | 145 | return [ |
@@ -216,7 +216,6 @@ discard block |
||
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
| 219 | - * @param int $max |
|
| 220 | 219 | */ |
| 221 | 220 | public function finishProgress() { |
| 222 | 221 | // for now just emit as we did in the past |
@@ -58,7 +58,7 @@ |
||
| 58 | 58 | use Symfony\Component\EventDispatcher\EventDispatcher; |
| 59 | 59 | use Symfony\Component\EventDispatcher\GenericEvent; |
| 60 | 60 | |
| 61 | -class Repair implements IOutput{ |
|
| 61 | +class Repair implements IOutput { |
|
| 62 | 62 | /* @var IRepairStep[] */ |
| 63 | 63 | private $repairSteps; |
| 64 | 64 | /** @var EventDispatcher */ |
@@ -56,168 +56,168 @@ |
||
| 56 | 56 | use Symfony\Component\EventDispatcher\GenericEvent; |
| 57 | 57 | |
| 58 | 58 | class Repair implements IOutput{ |
| 59 | - /* @var IRepairStep[] */ |
|
| 60 | - private $repairSteps; |
|
| 61 | - /** @var EventDispatcher */ |
|
| 62 | - private $dispatcher; |
|
| 63 | - /** @var string */ |
|
| 64 | - private $currentStep; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Creates a new repair step runner |
|
| 68 | - * |
|
| 69 | - * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
| 70 | - * @param EventDispatcher $dispatcher |
|
| 71 | - */ |
|
| 72 | - public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
| 73 | - $this->repairSteps = $repairSteps; |
|
| 74 | - $this->dispatcher = $dispatcher; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Run a series of repair steps for common problems |
|
| 79 | - */ |
|
| 80 | - public function run() { |
|
| 81 | - if (count($this->repairSteps) === 0) { |
|
| 82 | - $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
| 83 | - return; |
|
| 84 | - } |
|
| 85 | - // run each repair step |
|
| 86 | - foreach ($this->repairSteps as $step) { |
|
| 87 | - $this->currentStep = $step->getName(); |
|
| 88 | - $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
| 89 | - $step->run($this); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Add repair step |
|
| 95 | - * |
|
| 96 | - * @param IRepairStep|string $repairStep repair step |
|
| 97 | - * @throws \Exception |
|
| 98 | - */ |
|
| 99 | - public function addStep($repairStep) { |
|
| 100 | - if (is_string($repairStep)) { |
|
| 101 | - try { |
|
| 102 | - $s = \OC::$server->query($repairStep); |
|
| 103 | - } catch (QueryException $e) { |
|
| 104 | - if (class_exists($repairStep)) { |
|
| 105 | - $s = new $repairStep(); |
|
| 106 | - } else { |
|
| 107 | - throw new \Exception("Repair step '$repairStep' is unknown"); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - if ($s instanceof IRepairStep) { |
|
| 112 | - $this->repairSteps[] = $s; |
|
| 113 | - } else { |
|
| 114 | - throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
| 115 | - } |
|
| 116 | - } else { |
|
| 117 | - $this->repairSteps[] = $repairStep; |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Returns the default repair steps to be run on the |
|
| 123 | - * command line or after an upgrade. |
|
| 124 | - * |
|
| 125 | - * @return IRepairStep[] |
|
| 126 | - */ |
|
| 127 | - public static function getRepairSteps() { |
|
| 128 | - return [ |
|
| 129 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
| 130 | - new RepairMimeTypes(\OC::$server->getConfig()), |
|
| 131 | - new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
| 132 | - new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
| 133 | - new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
| 134 | - new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
| 135 | - new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
| 136 | - new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), |
|
| 137 | - new AddLogRotateJob(\OC::$server->getJobList()), |
|
| 138 | - new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)), |
|
| 139 | - new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()), |
|
| 140 | - ]; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Returns expensive repair steps to be run on the |
|
| 145 | - * command line with a special option. |
|
| 146 | - * |
|
| 147 | - * @return IRepairStep[] |
|
| 148 | - */ |
|
| 149 | - public static function getExpensiveRepairSteps() { |
|
| 150 | - return [ |
|
| 151 | - new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), |
|
| 152 | - ]; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Returns the repair steps to be run before an |
|
| 157 | - * upgrade. |
|
| 158 | - * |
|
| 159 | - * @return IRepairStep[] |
|
| 160 | - */ |
|
| 161 | - public static function getBeforeUpgradeRepairSteps() { |
|
| 162 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 163 | - $config = \OC::$server->getConfig(); |
|
| 164 | - $steps = [ |
|
| 165 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
| 166 | - new SqliteAutoincrement($connection), |
|
| 167 | - new SaveAccountsTableData($connection, $config), |
|
| 168 | - new DropAccountTermsTable($connection), |
|
| 169 | - ]; |
|
| 170 | - |
|
| 171 | - return $steps; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @param string $scope |
|
| 176 | - * @param string $method |
|
| 177 | - * @param array $arguments |
|
| 178 | - */ |
|
| 179 | - public function emit($scope, $method, array $arguments = []) { |
|
| 180 | - if (!is_null($this->dispatcher)) { |
|
| 181 | - $this->dispatcher->dispatch("$scope::$method", |
|
| 182 | - new GenericEvent("$scope::$method", $arguments)); |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - public function info($string) { |
|
| 187 | - // for now just emit as we did in the past |
|
| 188 | - $this->emit('\OC\Repair', 'info', array($string)); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @param string $message |
|
| 193 | - */ |
|
| 194 | - public function warning($message) { |
|
| 195 | - // for now just emit as we did in the past |
|
| 196 | - $this->emit('\OC\Repair', 'warning', [$message]); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @param int $max |
|
| 201 | - */ |
|
| 202 | - public function startProgress($max = 0) { |
|
| 203 | - // for now just emit as we did in the past |
|
| 204 | - $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @param int $step |
|
| 209 | - * @param string $description |
|
| 210 | - */ |
|
| 211 | - public function advance($step = 1, $description = '') { |
|
| 212 | - // for now just emit as we did in the past |
|
| 213 | - $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @param int $max |
|
| 218 | - */ |
|
| 219 | - public function finishProgress() { |
|
| 220 | - // for now just emit as we did in the past |
|
| 221 | - $this->emit('\OC\Repair', 'finishProgress', []); |
|
| 222 | - } |
|
| 59 | + /* @var IRepairStep[] */ |
|
| 60 | + private $repairSteps; |
|
| 61 | + /** @var EventDispatcher */ |
|
| 62 | + private $dispatcher; |
|
| 63 | + /** @var string */ |
|
| 64 | + private $currentStep; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Creates a new repair step runner |
|
| 68 | + * |
|
| 69 | + * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
| 70 | + * @param EventDispatcher $dispatcher |
|
| 71 | + */ |
|
| 72 | + public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
| 73 | + $this->repairSteps = $repairSteps; |
|
| 74 | + $this->dispatcher = $dispatcher; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Run a series of repair steps for common problems |
|
| 79 | + */ |
|
| 80 | + public function run() { |
|
| 81 | + if (count($this->repairSteps) === 0) { |
|
| 82 | + $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
| 83 | + return; |
|
| 84 | + } |
|
| 85 | + // run each repair step |
|
| 86 | + foreach ($this->repairSteps as $step) { |
|
| 87 | + $this->currentStep = $step->getName(); |
|
| 88 | + $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
| 89 | + $step->run($this); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Add repair step |
|
| 95 | + * |
|
| 96 | + * @param IRepairStep|string $repairStep repair step |
|
| 97 | + * @throws \Exception |
|
| 98 | + */ |
|
| 99 | + public function addStep($repairStep) { |
|
| 100 | + if (is_string($repairStep)) { |
|
| 101 | + try { |
|
| 102 | + $s = \OC::$server->query($repairStep); |
|
| 103 | + } catch (QueryException $e) { |
|
| 104 | + if (class_exists($repairStep)) { |
|
| 105 | + $s = new $repairStep(); |
|
| 106 | + } else { |
|
| 107 | + throw new \Exception("Repair step '$repairStep' is unknown"); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + if ($s instanceof IRepairStep) { |
|
| 112 | + $this->repairSteps[] = $s; |
|
| 113 | + } else { |
|
| 114 | + throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
| 115 | + } |
|
| 116 | + } else { |
|
| 117 | + $this->repairSteps[] = $repairStep; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Returns the default repair steps to be run on the |
|
| 123 | + * command line or after an upgrade. |
|
| 124 | + * |
|
| 125 | + * @return IRepairStep[] |
|
| 126 | + */ |
|
| 127 | + public static function getRepairSteps() { |
|
| 128 | + return [ |
|
| 129 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
| 130 | + new RepairMimeTypes(\OC::$server->getConfig()), |
|
| 131 | + new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
| 132 | + new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
| 133 | + new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
| 134 | + new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
| 135 | + new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
| 136 | + new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), |
|
| 137 | + new AddLogRotateJob(\OC::$server->getJobList()), |
|
| 138 | + new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)), |
|
| 139 | + new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()), |
|
| 140 | + ]; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Returns expensive repair steps to be run on the |
|
| 145 | + * command line with a special option. |
|
| 146 | + * |
|
| 147 | + * @return IRepairStep[] |
|
| 148 | + */ |
|
| 149 | + public static function getExpensiveRepairSteps() { |
|
| 150 | + return [ |
|
| 151 | + new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), |
|
| 152 | + ]; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Returns the repair steps to be run before an |
|
| 157 | + * upgrade. |
|
| 158 | + * |
|
| 159 | + * @return IRepairStep[] |
|
| 160 | + */ |
|
| 161 | + public static function getBeforeUpgradeRepairSteps() { |
|
| 162 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 163 | + $config = \OC::$server->getConfig(); |
|
| 164 | + $steps = [ |
|
| 165 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
| 166 | + new SqliteAutoincrement($connection), |
|
| 167 | + new SaveAccountsTableData($connection, $config), |
|
| 168 | + new DropAccountTermsTable($connection), |
|
| 169 | + ]; |
|
| 170 | + |
|
| 171 | + return $steps; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @param string $scope |
|
| 176 | + * @param string $method |
|
| 177 | + * @param array $arguments |
|
| 178 | + */ |
|
| 179 | + public function emit($scope, $method, array $arguments = []) { |
|
| 180 | + if (!is_null($this->dispatcher)) { |
|
| 181 | + $this->dispatcher->dispatch("$scope::$method", |
|
| 182 | + new GenericEvent("$scope::$method", $arguments)); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + public function info($string) { |
|
| 187 | + // for now just emit as we did in the past |
|
| 188 | + $this->emit('\OC\Repair', 'info', array($string)); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @param string $message |
|
| 193 | + */ |
|
| 194 | + public function warning($message) { |
|
| 195 | + // for now just emit as we did in the past |
|
| 196 | + $this->emit('\OC\Repair', 'warning', [$message]); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @param int $max |
|
| 201 | + */ |
|
| 202 | + public function startProgress($max = 0) { |
|
| 203 | + // for now just emit as we did in the past |
|
| 204 | + $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @param int $step |
|
| 209 | + * @param string $description |
|
| 210 | + */ |
|
| 211 | + public function advance($step = 1, $description = '') { |
|
| 212 | + // for now just emit as we did in the past |
|
| 213 | + $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @param int $max |
|
| 218 | + */ |
|
| 219 | + public function finishProgress() { |
|
| 220 | + // for now just emit as we did in the past |
|
| 221 | + $this->emit('\OC\Repair', 'finishProgress', []); |
|
| 222 | + } |
|
| 223 | 223 | } |
@@ -176,7 +176,7 @@ |
||
| 176 | 176 | |
| 177 | 177 | /** |
| 178 | 178 | * @param mixed $offset |
| 179 | - * @return mixed |
|
| 179 | + * @return string|null |
|
| 180 | 180 | */ |
| 181 | 181 | public function offsetGet($offset) { |
| 182 | 182 | return $this->get($offset); |
@@ -37,176 +37,176 @@ |
||
| 37 | 37 | * @package OC\Session |
| 38 | 38 | */ |
| 39 | 39 | class CryptoSessionData implements \ArrayAccess, ISession { |
| 40 | - /** @var ISession */ |
|
| 41 | - protected $session; |
|
| 42 | - /** @var \OCP\Security\ICrypto */ |
|
| 43 | - protected $crypto; |
|
| 44 | - /** @var string */ |
|
| 45 | - protected $passphrase; |
|
| 46 | - /** @var array */ |
|
| 47 | - protected $sessionValues; |
|
| 48 | - /** @var bool */ |
|
| 49 | - protected $isModified = false; |
|
| 50 | - CONST encryptedSessionName = 'encrypted_session_data'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param ISession $session |
|
| 54 | - * @param ICrypto $crypto |
|
| 55 | - * @param string $passphrase |
|
| 56 | - */ |
|
| 57 | - public function __construct(ISession $session, |
|
| 58 | - ICrypto $crypto, |
|
| 59 | - string $passphrase) { |
|
| 60 | - $this->crypto = $crypto; |
|
| 61 | - $this->session = $session; |
|
| 62 | - $this->passphrase = $passphrase; |
|
| 63 | - $this->initializeSession(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Close session if class gets destructed |
|
| 68 | - */ |
|
| 69 | - public function __destruct() { |
|
| 70 | - try { |
|
| 71 | - $this->close(); |
|
| 72 | - } catch (SessionNotAvailableException $e){ |
|
| 73 | - // This exception can occur if session is already closed |
|
| 74 | - // So it is safe to ignore it and let the garbage collector to proceed |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - protected function initializeSession() { |
|
| 79 | - $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: ''; |
|
| 80 | - try { |
|
| 81 | - $this->sessionValues = json_decode( |
|
| 82 | - $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
| 83 | - true |
|
| 84 | - ); |
|
| 85 | - } catch (\Exception $e) { |
|
| 86 | - $this->sessionValues = []; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Set a value in the session |
|
| 92 | - * |
|
| 93 | - * @param string $key |
|
| 94 | - * @param mixed $value |
|
| 95 | - */ |
|
| 96 | - public function set(string $key, $value) { |
|
| 97 | - $this->sessionValues[$key] = $value; |
|
| 98 | - $this->isModified = true; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get a value from the session |
|
| 103 | - * |
|
| 104 | - * @param string $key |
|
| 105 | - * @return string|null Either the value or null |
|
| 106 | - */ |
|
| 107 | - public function get(string $key) { |
|
| 108 | - if(isset($this->sessionValues[$key])) { |
|
| 109 | - return $this->sessionValues[$key]; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return null; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Check if a named key exists in the session |
|
| 117 | - * |
|
| 118 | - * @param string $key |
|
| 119 | - * @return bool |
|
| 120 | - */ |
|
| 121 | - public function exists(string $key): bool { |
|
| 122 | - return isset($this->sessionValues[$key]); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Remove a $key/$value pair from the session |
|
| 127 | - * |
|
| 128 | - * @param string $key |
|
| 129 | - */ |
|
| 130 | - public function remove(string $key) { |
|
| 131 | - $this->isModified = true; |
|
| 132 | - unset($this->sessionValues[$key]); |
|
| 133 | - $this->session->remove(self::encryptedSessionName); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Reset and recreate the session |
|
| 138 | - */ |
|
| 139 | - public function clear() { |
|
| 140 | - $requesttoken = $this->get('requesttoken'); |
|
| 141 | - $this->sessionValues = []; |
|
| 142 | - if ($requesttoken !== null) { |
|
| 143 | - $this->set('requesttoken', $requesttoken); |
|
| 144 | - } |
|
| 145 | - $this->isModified = true; |
|
| 146 | - $this->session->clear(); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Wrapper around session_regenerate_id |
|
| 151 | - * |
|
| 152 | - * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
| 153 | - * @return void |
|
| 154 | - */ |
|
| 155 | - public function regenerateId(bool $deleteOldSession = true) { |
|
| 156 | - $this->session->regenerateId($deleteOldSession); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Wrapper around session_id |
|
| 161 | - * |
|
| 162 | - * @return string |
|
| 163 | - * @throws SessionNotAvailableException |
|
| 164 | - * @since 9.1.0 |
|
| 165 | - */ |
|
| 166 | - public function getId(): string { |
|
| 167 | - return $this->session->getId(); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Close the session and release the lock, also writes all changed data in batch |
|
| 172 | - */ |
|
| 173 | - public function close() { |
|
| 174 | - if($this->isModified) { |
|
| 175 | - $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
| 176 | - $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
| 177 | - $this->isModified = false; |
|
| 178 | - } |
|
| 179 | - $this->session->close(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param mixed $offset |
|
| 184 | - * @return bool |
|
| 185 | - */ |
|
| 186 | - public function offsetExists($offset): bool { |
|
| 187 | - return $this->exists($offset); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @param mixed $offset |
|
| 192 | - * @return mixed |
|
| 193 | - */ |
|
| 194 | - public function offsetGet($offset) { |
|
| 195 | - return $this->get($offset); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @param mixed $offset |
|
| 200 | - * @param mixed $value |
|
| 201 | - */ |
|
| 202 | - public function offsetSet($offset, $value) { |
|
| 203 | - $this->set($offset, $value); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param mixed $offset |
|
| 208 | - */ |
|
| 209 | - public function offsetUnset($offset) { |
|
| 210 | - $this->remove($offset); |
|
| 211 | - } |
|
| 40 | + /** @var ISession */ |
|
| 41 | + protected $session; |
|
| 42 | + /** @var \OCP\Security\ICrypto */ |
|
| 43 | + protected $crypto; |
|
| 44 | + /** @var string */ |
|
| 45 | + protected $passphrase; |
|
| 46 | + /** @var array */ |
|
| 47 | + protected $sessionValues; |
|
| 48 | + /** @var bool */ |
|
| 49 | + protected $isModified = false; |
|
| 50 | + CONST encryptedSessionName = 'encrypted_session_data'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param ISession $session |
|
| 54 | + * @param ICrypto $crypto |
|
| 55 | + * @param string $passphrase |
|
| 56 | + */ |
|
| 57 | + public function __construct(ISession $session, |
|
| 58 | + ICrypto $crypto, |
|
| 59 | + string $passphrase) { |
|
| 60 | + $this->crypto = $crypto; |
|
| 61 | + $this->session = $session; |
|
| 62 | + $this->passphrase = $passphrase; |
|
| 63 | + $this->initializeSession(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Close session if class gets destructed |
|
| 68 | + */ |
|
| 69 | + public function __destruct() { |
|
| 70 | + try { |
|
| 71 | + $this->close(); |
|
| 72 | + } catch (SessionNotAvailableException $e){ |
|
| 73 | + // This exception can occur if session is already closed |
|
| 74 | + // So it is safe to ignore it and let the garbage collector to proceed |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + protected function initializeSession() { |
|
| 79 | + $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: ''; |
|
| 80 | + try { |
|
| 81 | + $this->sessionValues = json_decode( |
|
| 82 | + $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
| 83 | + true |
|
| 84 | + ); |
|
| 85 | + } catch (\Exception $e) { |
|
| 86 | + $this->sessionValues = []; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Set a value in the session |
|
| 92 | + * |
|
| 93 | + * @param string $key |
|
| 94 | + * @param mixed $value |
|
| 95 | + */ |
|
| 96 | + public function set(string $key, $value) { |
|
| 97 | + $this->sessionValues[$key] = $value; |
|
| 98 | + $this->isModified = true; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get a value from the session |
|
| 103 | + * |
|
| 104 | + * @param string $key |
|
| 105 | + * @return string|null Either the value or null |
|
| 106 | + */ |
|
| 107 | + public function get(string $key) { |
|
| 108 | + if(isset($this->sessionValues[$key])) { |
|
| 109 | + return $this->sessionValues[$key]; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return null; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Check if a named key exists in the session |
|
| 117 | + * |
|
| 118 | + * @param string $key |
|
| 119 | + * @return bool |
|
| 120 | + */ |
|
| 121 | + public function exists(string $key): bool { |
|
| 122 | + return isset($this->sessionValues[$key]); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Remove a $key/$value pair from the session |
|
| 127 | + * |
|
| 128 | + * @param string $key |
|
| 129 | + */ |
|
| 130 | + public function remove(string $key) { |
|
| 131 | + $this->isModified = true; |
|
| 132 | + unset($this->sessionValues[$key]); |
|
| 133 | + $this->session->remove(self::encryptedSessionName); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Reset and recreate the session |
|
| 138 | + */ |
|
| 139 | + public function clear() { |
|
| 140 | + $requesttoken = $this->get('requesttoken'); |
|
| 141 | + $this->sessionValues = []; |
|
| 142 | + if ($requesttoken !== null) { |
|
| 143 | + $this->set('requesttoken', $requesttoken); |
|
| 144 | + } |
|
| 145 | + $this->isModified = true; |
|
| 146 | + $this->session->clear(); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Wrapper around session_regenerate_id |
|
| 151 | + * |
|
| 152 | + * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
| 153 | + * @return void |
|
| 154 | + */ |
|
| 155 | + public function regenerateId(bool $deleteOldSession = true) { |
|
| 156 | + $this->session->regenerateId($deleteOldSession); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Wrapper around session_id |
|
| 161 | + * |
|
| 162 | + * @return string |
|
| 163 | + * @throws SessionNotAvailableException |
|
| 164 | + * @since 9.1.0 |
|
| 165 | + */ |
|
| 166 | + public function getId(): string { |
|
| 167 | + return $this->session->getId(); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Close the session and release the lock, also writes all changed data in batch |
|
| 172 | + */ |
|
| 173 | + public function close() { |
|
| 174 | + if($this->isModified) { |
|
| 175 | + $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
| 176 | + $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
| 177 | + $this->isModified = false; |
|
| 178 | + } |
|
| 179 | + $this->session->close(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param mixed $offset |
|
| 184 | + * @return bool |
|
| 185 | + */ |
|
| 186 | + public function offsetExists($offset): bool { |
|
| 187 | + return $this->exists($offset); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @param mixed $offset |
|
| 192 | + * @return mixed |
|
| 193 | + */ |
|
| 194 | + public function offsetGet($offset) { |
|
| 195 | + return $this->get($offset); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @param mixed $offset |
|
| 200 | + * @param mixed $value |
|
| 201 | + */ |
|
| 202 | + public function offsetSet($offset, $value) { |
|
| 203 | + $this->set($offset, $value); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param mixed $offset |
|
| 208 | + */ |
|
| 209 | + public function offsetUnset($offset) { |
|
| 210 | + $this->remove($offset); |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | public function __destruct() { |
| 70 | 70 | try { |
| 71 | 71 | $this->close(); |
| 72 | - } catch (SessionNotAvailableException $e){ |
|
| 72 | + } catch (SessionNotAvailableException $e) { |
|
| 73 | 73 | // This exception can occur if session is already closed |
| 74 | 74 | // So it is safe to ignore it and let the garbage collector to proceed |
| 75 | 75 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * @return string|null Either the value or null |
| 106 | 106 | */ |
| 107 | 107 | public function get(string $key) { |
| 108 | - if(isset($this->sessionValues[$key])) { |
|
| 108 | + if (isset($this->sessionValues[$key])) { |
|
| 109 | 109 | return $this->sessionValues[$key]; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | * Close the session and release the lock, also writes all changed data in batch |
| 172 | 172 | */ |
| 173 | 173 | public function close() { |
| 174 | - if($this->isModified) { |
|
| 174 | + if ($this->isModified) { |
|
| 175 | 175 | $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
| 176 | 176 | $this->session->set(self::encryptedSessionName, $encryptedValue); |
| 177 | 177 | $this->isModified = false; |
@@ -742,11 +742,19 @@ discard block |
||
| 742 | 742 | } |
| 743 | 743 | |
| 744 | 744 | // case-insensitive array_search |
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * @param string $needle |
|
| 748 | + */ |
|
| 745 | 749 | protected function array_searchi($needle, $haystack, $mem='getName') { |
| 746 | 750 | if(!is_array($haystack)) { |
| 747 | 751 | return false; |
| 748 | 752 | } |
| 749 | 753 | return array_search(strtolower($needle), array_map( |
| 754 | + |
|
| 755 | + /** |
|
| 756 | + * @param string $tag |
|
| 757 | + */ |
|
| 750 | 758 | function($tag) use($mem) { |
| 751 | 759 | return strtolower(call_user_func(array($tag, $mem))); |
| 752 | 760 | }, $haystack) |
@@ -771,7 +779,7 @@ discard block |
||
| 771 | 779 | * Get a tag by its name. |
| 772 | 780 | * |
| 773 | 781 | * @param string $name The tag name. |
| 774 | - * @return integer|bool The tag object's offset within the $this->tags |
|
| 782 | + * @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags |
|
| 775 | 783 | * array or false if it doesn't exist. |
| 776 | 784 | */ |
| 777 | 785 | private function getTagByName($name) { |
@@ -782,7 +790,7 @@ discard block |
||
| 782 | 790 | * Get a tag by its ID. |
| 783 | 791 | * |
| 784 | 792 | * @param string $id The tag ID to look for. |
| 785 | - * @return integer|bool The tag object's offset within the $this->tags |
|
| 793 | + * @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags |
|
| 786 | 794 | * array or false if it doesn't exist. |
| 787 | 795 | */ |
| 788 | 796 | private function getTagById($id) { |
@@ -51,807 +51,807 @@ |
||
| 51 | 51 | |
| 52 | 52 | class Tags implements \OCP\ITags { |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Tags |
|
| 56 | - * |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - private $tags = array(); |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Used for storing objectid/categoryname pairs while rescanning. |
|
| 63 | - * |
|
| 64 | - * @var array |
|
| 65 | - */ |
|
| 66 | - private static $relations = array(); |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Type |
|
| 70 | - * |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - private $type; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * User |
|
| 77 | - * |
|
| 78 | - * @var string |
|
| 79 | - */ |
|
| 80 | - private $user; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Are we including tags for shared items? |
|
| 84 | - * |
|
| 85 | - * @var bool |
|
| 86 | - */ |
|
| 87 | - private $includeShared = false; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The current user, plus any owners of the items shared with the current |
|
| 91 | - * user, if $this->includeShared === true. |
|
| 92 | - * |
|
| 93 | - * @var array |
|
| 94 | - */ |
|
| 95 | - private $owners = array(); |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * The Mapper we're using to communicate our Tag objects to the database. |
|
| 99 | - * |
|
| 100 | - * @var TagMapper |
|
| 101 | - */ |
|
| 102 | - private $mapper; |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * The sharing backend for objects of $this->type. Required if |
|
| 106 | - * $this->includeShared === true to determine ownership of items. |
|
| 107 | - * |
|
| 108 | - * @var \OCP\Share_Backend |
|
| 109 | - */ |
|
| 110 | - private $backend; |
|
| 111 | - |
|
| 112 | - const TAG_TABLE = '*PREFIX*vcategory'; |
|
| 113 | - const RELATION_TABLE = '*PREFIX*vcategory_to_object'; |
|
| 114 | - |
|
| 115 | - const TAG_FAVORITE = '_$!<Favorite>!$_'; |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Constructor. |
|
| 119 | - * |
|
| 120 | - * @param TagMapper $mapper Instance of the TagMapper abstraction layer. |
|
| 121 | - * @param string $user The user whose data the object will operate on. |
|
| 122 | - * @param string $type The type of items for which tags will be loaded. |
|
| 123 | - * @param array $defaultTags Tags that should be created at construction. |
|
| 124 | - * @param boolean $includeShared Whether to include tags for items shared with this user by others. |
|
| 125 | - */ |
|
| 126 | - public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) { |
|
| 127 | - $this->mapper = $mapper; |
|
| 128 | - $this->user = $user; |
|
| 129 | - $this->type = $type; |
|
| 130 | - $this->includeShared = $includeShared; |
|
| 131 | - $this->owners = array($this->user); |
|
| 132 | - if ($this->includeShared) { |
|
| 133 | - $this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true)); |
|
| 134 | - $this->backend = \OC\Share\Share::getBackend($this->type); |
|
| 135 | - } |
|
| 136 | - $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
| 137 | - |
|
| 138 | - if(count($defaultTags) > 0 && count($this->tags) === 0) { |
|
| 139 | - $this->addMultiple($defaultTags, true); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Check if any tags are saved for this type and user. |
|
| 145 | - * |
|
| 146 | - * @return boolean |
|
| 147 | - */ |
|
| 148 | - public function isEmpty() { |
|
| 149 | - return count($this->tags) === 0; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Returns an array mapping a given tag's properties to its values: |
|
| 154 | - * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
| 155 | - * |
|
| 156 | - * @param string $id The ID of the tag that is going to be mapped |
|
| 157 | - * @return array|false |
|
| 158 | - */ |
|
| 159 | - public function getTag($id) { |
|
| 160 | - $key = $this->getTagById($id); |
|
| 161 | - if ($key !== false) { |
|
| 162 | - return $this->tagMap($this->tags[$key]); |
|
| 163 | - } |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get the tags for a specific user. |
|
| 169 | - * |
|
| 170 | - * This returns an array with maps containing each tag's properties: |
|
| 171 | - * [ |
|
| 172 | - * ['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'], |
|
| 173 | - * ['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'], |
|
| 174 | - * ] |
|
| 175 | - * |
|
| 176 | - * @return array |
|
| 177 | - */ |
|
| 178 | - public function getTags() { |
|
| 179 | - if(!count($this->tags)) { |
|
| 180 | - return array(); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - usort($this->tags, function($a, $b) { |
|
| 184 | - return strnatcasecmp($a->getName(), $b->getName()); |
|
| 185 | - }); |
|
| 186 | - $tagMap = array(); |
|
| 187 | - |
|
| 188 | - foreach($this->tags as $tag) { |
|
| 189 | - if($tag->getName() !== self::TAG_FAVORITE) { |
|
| 190 | - $tagMap[] = $this->tagMap($tag); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - return $tagMap; |
|
| 194 | - |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Return only the tags owned by the given user, omitting any tags shared |
|
| 199 | - * by other users. |
|
| 200 | - * |
|
| 201 | - * @param string $user The user whose tags are to be checked. |
|
| 202 | - * @return array An array of Tag objects. |
|
| 203 | - */ |
|
| 204 | - public function getTagsForUser($user) { |
|
| 205 | - return array_filter($this->tags, |
|
| 206 | - function($tag) use($user) { |
|
| 207 | - return $tag->getOwner() === $user; |
|
| 208 | - } |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Get the list of tags for the given ids. |
|
| 214 | - * |
|
| 215 | - * @param array $objIds array of object ids |
|
| 216 | - * @return array|boolean of tags id as key to array of tag names |
|
| 217 | - * or false if an error occurred |
|
| 218 | - */ |
|
| 219 | - public function getTagsForObjects(array $objIds) { |
|
| 220 | - $entries = array(); |
|
| 221 | - |
|
| 222 | - try { |
|
| 223 | - $conn = \OC::$server->getDatabaseConnection(); |
|
| 224 | - $chunks = array_chunk($objIds, 900, false); |
|
| 225 | - foreach ($chunks as $chunk) { |
|
| 226 | - $result = $conn->executeQuery( |
|
| 227 | - 'SELECT `category`, `categoryid`, `objid` ' . |
|
| 228 | - 'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' . |
|
| 229 | - 'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)', |
|
| 230 | - array($this->user, $this->type, $chunk), |
|
| 231 | - array(null, null, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 232 | - ); |
|
| 233 | - while ($row = $result->fetch()) { |
|
| 234 | - $objId = (int)$row['objid']; |
|
| 235 | - if (!isset($entries[$objId])) { |
|
| 236 | - $entries[$objId] = array(); |
|
| 237 | - } |
|
| 238 | - $entries[$objId][] = $row['category']; |
|
| 239 | - } |
|
| 240 | - if ($result === null) { |
|
| 241 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - } catch(\Exception $e) { |
|
| 246 | - \OC::$server->getLogger()->logException($e, [ |
|
| 247 | - 'message' => __METHOD__, |
|
| 248 | - 'level' => ILogger::ERROR, |
|
| 249 | - 'app' => 'core', |
|
| 250 | - ]); |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $entries; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Get the a list if items tagged with $tag. |
|
| 259 | - * |
|
| 260 | - * Throws an exception if the tag could not be found. |
|
| 261 | - * |
|
| 262 | - * @param string $tag Tag id or name. |
|
| 263 | - * @return array|false An array of object ids or false on error. |
|
| 264 | - * @throws \Exception |
|
| 265 | - */ |
|
| 266 | - public function getIdsForTag($tag) { |
|
| 267 | - $result = null; |
|
| 268 | - $tagId = false; |
|
| 269 | - if(is_numeric($tag)) { |
|
| 270 | - $tagId = $tag; |
|
| 271 | - } elseif(is_string($tag)) { |
|
| 272 | - $tag = trim($tag); |
|
| 273 | - if($tag === '') { |
|
| 274 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG); |
|
| 275 | - return false; |
|
| 276 | - } |
|
| 277 | - $tagId = $this->getTagId($tag); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - if($tagId === false) { |
|
| 281 | - $l10n = \OC::$server->getL10N('core'); |
|
| 282 | - throw new \Exception( |
|
| 283 | - $l10n->t('Could not find category "%s"', [$tag]) |
|
| 284 | - ); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - $ids = array(); |
|
| 288 | - $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE |
|
| 289 | - . '` WHERE `categoryid` = ?'; |
|
| 290 | - |
|
| 291 | - try { |
|
| 292 | - $stmt = \OC_DB::prepare($sql); |
|
| 293 | - $result = $stmt->execute(array($tagId)); |
|
| 294 | - if ($result === null) { |
|
| 295 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 296 | - return false; |
|
| 297 | - } |
|
| 298 | - } catch(\Exception $e) { |
|
| 299 | - \OC::$server->getLogger()->logException($e, [ |
|
| 300 | - 'message' => __METHOD__, |
|
| 301 | - 'level' => ILogger::ERROR, |
|
| 302 | - 'app' => 'core', |
|
| 303 | - ]); |
|
| 304 | - return false; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - if(!is_null($result)) { |
|
| 308 | - while( $row = $result->fetchRow()) { |
|
| 309 | - $id = (int)$row['objid']; |
|
| 310 | - |
|
| 311 | - if ($this->includeShared) { |
|
| 312 | - // We have to check if we are really allowed to access the |
|
| 313 | - // items that are tagged with $tag. To that end, we ask the |
|
| 314 | - // corresponding sharing backend if the item identified by $id |
|
| 315 | - // is owned by any of $this->owners. |
|
| 316 | - foreach ($this->owners as $owner) { |
|
| 317 | - if ($this->backend->isValidSource($id, $owner)) { |
|
| 318 | - $ids[] = $id; |
|
| 319 | - break; |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - } else { |
|
| 323 | - $ids[] = $id; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return $ids; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Checks whether a tag is saved for the given user, |
|
| 333 | - * disregarding the ones shared with him or her. |
|
| 334 | - * |
|
| 335 | - * @param string $name The tag name to check for. |
|
| 336 | - * @param string $user The user whose tags are to be checked. |
|
| 337 | - * @return bool |
|
| 338 | - */ |
|
| 339 | - public function userHasTag($name, $user) { |
|
| 340 | - $key = $this->array_searchi($name, $this->getTagsForUser($user)); |
|
| 341 | - return ($key !== false) ? $this->tags[$key]->getId() : false; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Checks whether a tag is saved for or shared with the current user. |
|
| 346 | - * |
|
| 347 | - * @param string $name The tag name to check for. |
|
| 348 | - * @return bool |
|
| 349 | - */ |
|
| 350 | - public function hasTag($name) { |
|
| 351 | - return $this->getTagId($name) !== false; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Add a new tag. |
|
| 356 | - * |
|
| 357 | - * @param string $name A string with a name of the tag |
|
| 358 | - * @return false|int the id of the added tag or false on error. |
|
| 359 | - */ |
|
| 360 | - public function add($name) { |
|
| 361 | - $name = trim($name); |
|
| 362 | - |
|
| 363 | - if($name === '') { |
|
| 364 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG); |
|
| 365 | - return false; |
|
| 366 | - } |
|
| 367 | - if($this->userHasTag($name, $this->user)) { |
|
| 368 | - \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', ILogger::DEBUG); |
|
| 369 | - return false; |
|
| 370 | - } |
|
| 371 | - try { |
|
| 372 | - $tag = new Tag($this->user, $this->type, $name); |
|
| 373 | - $tag = $this->mapper->insert($tag); |
|
| 374 | - $this->tags[] = $tag; |
|
| 375 | - } catch(\Exception $e) { |
|
| 376 | - \OC::$server->getLogger()->logException($e, [ |
|
| 377 | - 'message' => __METHOD__, |
|
| 378 | - 'level' => ILogger::ERROR, |
|
| 379 | - 'app' => 'core', |
|
| 380 | - ]); |
|
| 381 | - return false; |
|
| 382 | - } |
|
| 383 | - \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), ILogger::DEBUG); |
|
| 384 | - return $tag->getId(); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Rename tag. |
|
| 389 | - * |
|
| 390 | - * @param string|integer $from The name or ID of the existing tag |
|
| 391 | - * @param string $to The new name of the tag. |
|
| 392 | - * @return bool |
|
| 393 | - */ |
|
| 394 | - public function rename($from, $to) { |
|
| 395 | - $from = trim($from); |
|
| 396 | - $to = trim($to); |
|
| 397 | - |
|
| 398 | - if($to === '' || $from === '') { |
|
| 399 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG); |
|
| 400 | - return false; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - if (is_numeric($from)) { |
|
| 404 | - $key = $this->getTagById($from); |
|
| 405 | - } else { |
|
| 406 | - $key = $this->getTagByName($from); |
|
| 407 | - } |
|
| 408 | - if($key === false) { |
|
| 409 | - \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', ILogger::DEBUG); |
|
| 410 | - return false; |
|
| 411 | - } |
|
| 412 | - $tag = $this->tags[$key]; |
|
| 413 | - |
|
| 414 | - if($this->userHasTag($to, $tag->getOwner())) { |
|
| 415 | - \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', ILogger::DEBUG); |
|
| 416 | - return false; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - try { |
|
| 420 | - $tag->setName($to); |
|
| 421 | - $this->tags[$key] = $this->mapper->update($tag); |
|
| 422 | - } catch(\Exception $e) { |
|
| 423 | - \OC::$server->getLogger()->logException($e, [ |
|
| 424 | - 'message' => __METHOD__, |
|
| 425 | - 'level' => ILogger::ERROR, |
|
| 426 | - 'app' => 'core', |
|
| 427 | - ]); |
|
| 428 | - return false; |
|
| 429 | - } |
|
| 430 | - return true; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Add a list of new tags. |
|
| 435 | - * |
|
| 436 | - * @param string[] $names A string with a name or an array of strings containing |
|
| 437 | - * the name(s) of the tag(s) to add. |
|
| 438 | - * @param bool $sync When true, save the tags |
|
| 439 | - * @param int|null $id int Optional object id to add to this|these tag(s) |
|
| 440 | - * @return bool Returns false on error. |
|
| 441 | - */ |
|
| 442 | - public function addMultiple($names, $sync=false, $id = null) { |
|
| 443 | - if(!is_array($names)) { |
|
| 444 | - $names = array($names); |
|
| 445 | - } |
|
| 446 | - $names = array_map('trim', $names); |
|
| 447 | - array_filter($names); |
|
| 448 | - |
|
| 449 | - $newones = array(); |
|
| 450 | - foreach($names as $name) { |
|
| 451 | - if(!$this->hasTag($name) && $name !== '') { |
|
| 452 | - $newones[] = new Tag($this->user, $this->type, $name); |
|
| 453 | - } |
|
| 454 | - if(!is_null($id) ) { |
|
| 455 | - // Insert $objectid, $categoryid pairs if not exist. |
|
| 456 | - self::$relations[] = array('objid' => $id, 'tag' => $name); |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - $this->tags = array_merge($this->tags, $newones); |
|
| 460 | - if($sync === true) { |
|
| 461 | - $this->save(); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - return true; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Save the list of tags and their object relations |
|
| 469 | - */ |
|
| 470 | - protected function save() { |
|
| 471 | - if(is_array($this->tags)) { |
|
| 472 | - foreach($this->tags as $tag) { |
|
| 473 | - try { |
|
| 474 | - if (!$this->mapper->tagExists($tag)) { |
|
| 475 | - $this->mapper->insert($tag); |
|
| 476 | - } |
|
| 477 | - } catch(\Exception $e) { |
|
| 478 | - \OC::$server->getLogger()->logException($e, [ |
|
| 479 | - 'message' => __METHOD__, |
|
| 480 | - 'level' => ILogger::ERROR, |
|
| 481 | - 'app' => 'core', |
|
| 482 | - ]); |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - // reload tags to get the proper ids. |
|
| 487 | - $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
| 488 | - \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
| 489 | - ILogger::DEBUG); |
|
| 490 | - // Loop through temporarily cached objectid/tagname pairs |
|
| 491 | - // and save relations. |
|
| 492 | - $tags = $this->tags; |
|
| 493 | - // For some reason this is needed or array_search(i) will return 0..? |
|
| 494 | - ksort($tags); |
|
| 495 | - $dbConnection = \OC::$server->getDatabaseConnection(); |
|
| 496 | - foreach(self::$relations as $relation) { |
|
| 497 | - $tagId = $this->getTagId($relation['tag']); |
|
| 498 | - \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, ILogger::DEBUG); |
|
| 499 | - if($tagId) { |
|
| 500 | - try { |
|
| 501 | - $dbConnection->insertIfNotExist(self::RELATION_TABLE, |
|
| 502 | - array( |
|
| 503 | - 'objid' => $relation['objid'], |
|
| 504 | - 'categoryid' => $tagId, |
|
| 505 | - 'type' => $this->type, |
|
| 506 | - )); |
|
| 507 | - } catch(\Exception $e) { |
|
| 508 | - \OC::$server->getLogger()->logException($e, [ |
|
| 509 | - 'message' => __METHOD__, |
|
| 510 | - 'level' => ILogger::ERROR, |
|
| 511 | - 'app' => 'core', |
|
| 512 | - ]); |
|
| 513 | - } |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - self::$relations = array(); // reset |
|
| 517 | - } else { |
|
| 518 | - \OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! ' |
|
| 519 | - . print_r($this->tags, true), ILogger::ERROR); |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Delete tags and tag/object relations for a user. |
|
| 525 | - * |
|
| 526 | - * For hooking up on post_deleteUser |
|
| 527 | - * |
|
| 528 | - * @param array $arguments |
|
| 529 | - */ |
|
| 530 | - public static function post_deleteUser($arguments) { |
|
| 531 | - // Find all objectid/tagId pairs. |
|
| 532 | - $result = null; |
|
| 533 | - try { |
|
| 534 | - $stmt = \OC_DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` ' |
|
| 535 | - . 'WHERE `uid` = ?'); |
|
| 536 | - $result = $stmt->execute(array($arguments['uid'])); |
|
| 537 | - if ($result === null) { |
|
| 538 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 539 | - } |
|
| 540 | - } catch(\Exception $e) { |
|
| 541 | - \OC::$server->getLogger()->logException($e, [ |
|
| 542 | - 'message' => __METHOD__, |
|
| 543 | - 'level' => ILogger::ERROR, |
|
| 544 | - 'app' => 'core', |
|
| 545 | - ]); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - if(!is_null($result)) { |
|
| 549 | - try { |
|
| 550 | - $stmt = \OC_DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 551 | - . 'WHERE `categoryid` = ?'); |
|
| 552 | - while( $row = $result->fetchRow()) { |
|
| 553 | - try { |
|
| 554 | - $stmt->execute(array($row['id'])); |
|
| 555 | - } catch(\Exception $e) { |
|
| 556 | - \OC::$server->getLogger()->logException($e, [ |
|
| 557 | - 'message' => __METHOD__, |
|
| 558 | - 'level' => ILogger::ERROR, |
|
| 559 | - 'app' => 'core', |
|
| 560 | - ]); |
|
| 561 | - } |
|
| 562 | - } |
|
| 563 | - } catch(\Exception $e) { |
|
| 564 | - \OC::$server->getLogger()->logException($e, [ |
|
| 565 | - 'message' => __METHOD__, |
|
| 566 | - 'level' => ILogger::ERROR, |
|
| 567 | - 'app' => 'core', |
|
| 568 | - ]); |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - try { |
|
| 572 | - $stmt = \OC_DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` ' |
|
| 573 | - . 'WHERE `uid` = ?'); |
|
| 574 | - $result = $stmt->execute(array($arguments['uid'])); |
|
| 575 | - if ($result === null) { |
|
| 576 | - \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 577 | - } |
|
| 578 | - } catch(\Exception $e) { |
|
| 579 | - \OC::$server->getLogger()->logException($e, [ |
|
| 580 | - 'message' => __METHOD__, |
|
| 581 | - 'level' => ILogger::ERROR, |
|
| 582 | - 'app' => 'core', |
|
| 583 | - ]); |
|
| 584 | - } |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Delete tag/object relations from the db |
|
| 589 | - * |
|
| 590 | - * @param array $ids The ids of the objects |
|
| 591 | - * @return boolean Returns false on error. |
|
| 592 | - */ |
|
| 593 | - public function purgeObjects(array $ids) { |
|
| 594 | - if(count($ids) === 0) { |
|
| 595 | - // job done ;) |
|
| 596 | - return true; |
|
| 597 | - } |
|
| 598 | - $updates = $ids; |
|
| 599 | - try { |
|
| 600 | - $query = 'DELETE FROM `' . self::RELATION_TABLE . '` '; |
|
| 601 | - $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) '; |
|
| 602 | - $query .= 'AND `type`= ?'; |
|
| 603 | - $updates[] = $this->type; |
|
| 604 | - $stmt = \OC_DB::prepare($query); |
|
| 605 | - $result = $stmt->execute($updates); |
|
| 606 | - if ($result === null) { |
|
| 607 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 608 | - return false; |
|
| 609 | - } |
|
| 610 | - } catch(\Exception $e) { |
|
| 611 | - \OC::$server->getLogger()->logException($e, [ |
|
| 612 | - 'message' => __METHOD__, |
|
| 613 | - 'level' => ILogger::ERROR, |
|
| 614 | - 'app' => 'core', |
|
| 615 | - ]); |
|
| 616 | - return false; |
|
| 617 | - } |
|
| 618 | - return true; |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Get favorites for an object type |
|
| 623 | - * |
|
| 624 | - * @return array|false An array of object ids. |
|
| 625 | - */ |
|
| 626 | - public function getFavorites() { |
|
| 627 | - try { |
|
| 628 | - return $this->getIdsForTag(self::TAG_FAVORITE); |
|
| 629 | - } catch(\Exception $e) { |
|
| 630 | - \OC::$server->getLogger()->logException($e, [ |
|
| 631 | - 'message' => __METHOD__, |
|
| 632 | - 'level' => ILogger::ERROR, |
|
| 633 | - 'app' => 'core', |
|
| 634 | - ]); |
|
| 635 | - return array(); |
|
| 636 | - } |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Add an object to favorites |
|
| 641 | - * |
|
| 642 | - * @param int $objid The id of the object |
|
| 643 | - * @return boolean |
|
| 644 | - */ |
|
| 645 | - public function addToFavorites($objid) { |
|
| 646 | - if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
| 647 | - $this->add(self::TAG_FAVORITE); |
|
| 648 | - } |
|
| 649 | - return $this->tagAs($objid, self::TAG_FAVORITE); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Remove an object from favorites |
|
| 654 | - * |
|
| 655 | - * @param int $objid The id of the object |
|
| 656 | - * @return boolean |
|
| 657 | - */ |
|
| 658 | - public function removeFromFavorites($objid) { |
|
| 659 | - return $this->unTag($objid, self::TAG_FAVORITE); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Creates a tag/object relation. |
|
| 664 | - * |
|
| 665 | - * @param int $objid The id of the object |
|
| 666 | - * @param string $tag The id or name of the tag |
|
| 667 | - * @return boolean Returns false on error. |
|
| 668 | - */ |
|
| 669 | - public function tagAs($objid, $tag) { |
|
| 670 | - if(is_string($tag) && !is_numeric($tag)) { |
|
| 671 | - $tag = trim($tag); |
|
| 672 | - if($tag === '') { |
|
| 673 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG); |
|
| 674 | - return false; |
|
| 675 | - } |
|
| 676 | - if(!$this->hasTag($tag)) { |
|
| 677 | - $this->add($tag); |
|
| 678 | - } |
|
| 679 | - $tagId = $this->getTagId($tag); |
|
| 680 | - } else { |
|
| 681 | - $tagId = $tag; |
|
| 682 | - } |
|
| 683 | - try { |
|
| 684 | - \OC::$server->getDatabaseConnection()->insertIfNotExist(self::RELATION_TABLE, |
|
| 685 | - array( |
|
| 686 | - 'objid' => $objid, |
|
| 687 | - 'categoryid' => $tagId, |
|
| 688 | - 'type' => $this->type, |
|
| 689 | - )); |
|
| 690 | - } catch(\Exception $e) { |
|
| 691 | - \OC::$server->getLogger()->logException($e, [ |
|
| 692 | - 'message' => __METHOD__, |
|
| 693 | - 'level' => ILogger::ERROR, |
|
| 694 | - 'app' => 'core', |
|
| 695 | - ]); |
|
| 696 | - return false; |
|
| 697 | - } |
|
| 698 | - return true; |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Delete single tag/object relation from the db |
|
| 703 | - * |
|
| 704 | - * @param int $objid The id of the object |
|
| 705 | - * @param string $tag The id or name of the tag |
|
| 706 | - * @return boolean |
|
| 707 | - */ |
|
| 708 | - public function unTag($objid, $tag) { |
|
| 709 | - if(is_string($tag) && !is_numeric($tag)) { |
|
| 710 | - $tag = trim($tag); |
|
| 711 | - if($tag === '') { |
|
| 712 | - \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', ILogger::DEBUG); |
|
| 713 | - return false; |
|
| 714 | - } |
|
| 715 | - $tagId = $this->getTagId($tag); |
|
| 716 | - } else { |
|
| 717 | - $tagId = $tag; |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - try { |
|
| 721 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 722 | - . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?'; |
|
| 723 | - $stmt = \OC_DB::prepare($sql); |
|
| 724 | - $stmt->execute(array($objid, $tagId, $this->type)); |
|
| 725 | - } catch(\Exception $e) { |
|
| 726 | - \OC::$server->getLogger()->logException($e, [ |
|
| 727 | - 'message' => __METHOD__, |
|
| 728 | - 'level' => ILogger::ERROR, |
|
| 729 | - 'app' => 'core', |
|
| 730 | - ]); |
|
| 731 | - return false; |
|
| 732 | - } |
|
| 733 | - return true; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Delete tags from the database. |
|
| 738 | - * |
|
| 739 | - * @param string[]|integer[] $names An array of tags (names or IDs) to delete |
|
| 740 | - * @return bool Returns false on error |
|
| 741 | - */ |
|
| 742 | - public function delete($names) { |
|
| 743 | - if(!is_array($names)) { |
|
| 744 | - $names = array($names); |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - $names = array_map('trim', $names); |
|
| 748 | - array_filter($names); |
|
| 749 | - |
|
| 750 | - \OCP\Util::writeLog('core', __METHOD__ . ', before: ' |
|
| 751 | - . print_r($this->tags, true), ILogger::DEBUG); |
|
| 752 | - foreach($names as $name) { |
|
| 753 | - $id = null; |
|
| 754 | - |
|
| 755 | - if (is_numeric($name)) { |
|
| 756 | - $key = $this->getTagById($name); |
|
| 757 | - } else { |
|
| 758 | - $key = $this->getTagByName($name); |
|
| 759 | - } |
|
| 760 | - if ($key !== false) { |
|
| 761 | - $tag = $this->tags[$key]; |
|
| 762 | - $id = $tag->getId(); |
|
| 763 | - unset($this->tags[$key]); |
|
| 764 | - $this->mapper->delete($tag); |
|
| 765 | - } else { |
|
| 766 | - \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name |
|
| 767 | - . ': not found.', ILogger::ERROR); |
|
| 768 | - } |
|
| 769 | - if(!is_null($id) && $id !== false) { |
|
| 770 | - try { |
|
| 771 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 772 | - . 'WHERE `categoryid` = ?'; |
|
| 773 | - $stmt = \OC_DB::prepare($sql); |
|
| 774 | - $result = $stmt->execute(array($id)); |
|
| 775 | - if ($result === null) { |
|
| 776 | - \OCP\Util::writeLog('core', |
|
| 777 | - __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), |
|
| 778 | - ILogger::ERROR); |
|
| 779 | - return false; |
|
| 780 | - } |
|
| 781 | - } catch(\Exception $e) { |
|
| 782 | - \OC::$server->getLogger()->logException($e, [ |
|
| 783 | - 'message' => __METHOD__, |
|
| 784 | - 'level' => ILogger::ERROR, |
|
| 785 | - 'app' => 'core', |
|
| 786 | - ]); |
|
| 787 | - return false; |
|
| 788 | - } |
|
| 789 | - } |
|
| 790 | - } |
|
| 791 | - return true; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - // case-insensitive array_search |
|
| 795 | - protected function array_searchi($needle, $haystack, $mem='getName') { |
|
| 796 | - if(!is_array($haystack)) { |
|
| 797 | - return false; |
|
| 798 | - } |
|
| 799 | - return array_search(strtolower($needle), array_map( |
|
| 800 | - function($tag) use($mem) { |
|
| 801 | - return strtolower(call_user_func(array($tag, $mem))); |
|
| 802 | - }, $haystack) |
|
| 803 | - ); |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - /** |
|
| 807 | - * Get a tag's ID. |
|
| 808 | - * |
|
| 809 | - * @param string $name The tag name to look for. |
|
| 810 | - * @return string|bool The tag's id or false if no matching tag is found. |
|
| 811 | - */ |
|
| 812 | - private function getTagId($name) { |
|
| 813 | - $key = $this->array_searchi($name, $this->tags); |
|
| 814 | - if ($key !== false) { |
|
| 815 | - return $this->tags[$key]->getId(); |
|
| 816 | - } |
|
| 817 | - return false; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * Get a tag by its name. |
|
| 822 | - * |
|
| 823 | - * @param string $name The tag name. |
|
| 824 | - * @return integer|bool The tag object's offset within the $this->tags |
|
| 825 | - * array or false if it doesn't exist. |
|
| 826 | - */ |
|
| 827 | - private function getTagByName($name) { |
|
| 828 | - return $this->array_searchi($name, $this->tags, 'getName'); |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - /** |
|
| 832 | - * Get a tag by its ID. |
|
| 833 | - * |
|
| 834 | - * @param string $id The tag ID to look for. |
|
| 835 | - * @return integer|bool The tag object's offset within the $this->tags |
|
| 836 | - * array or false if it doesn't exist. |
|
| 837 | - */ |
|
| 838 | - private function getTagById($id) { |
|
| 839 | - return $this->array_searchi($id, $this->tags, 'getId'); |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - /** |
|
| 843 | - * Returns an array mapping a given tag's properties to its values: |
|
| 844 | - * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
| 845 | - * |
|
| 846 | - * @param Tag $tag The tag that is going to be mapped |
|
| 847 | - * @return array |
|
| 848 | - */ |
|
| 849 | - private function tagMap(Tag $tag) { |
|
| 850 | - return array( |
|
| 851 | - 'id' => $tag->getId(), |
|
| 852 | - 'name' => $tag->getName(), |
|
| 853 | - 'owner' => $tag->getOwner(), |
|
| 854 | - 'type' => $tag->getType() |
|
| 855 | - ); |
|
| 856 | - } |
|
| 54 | + /** |
|
| 55 | + * Tags |
|
| 56 | + * |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + private $tags = array(); |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Used for storing objectid/categoryname pairs while rescanning. |
|
| 63 | + * |
|
| 64 | + * @var array |
|
| 65 | + */ |
|
| 66 | + private static $relations = array(); |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Type |
|
| 70 | + * |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + private $type; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * User |
|
| 77 | + * |
|
| 78 | + * @var string |
|
| 79 | + */ |
|
| 80 | + private $user; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Are we including tags for shared items? |
|
| 84 | + * |
|
| 85 | + * @var bool |
|
| 86 | + */ |
|
| 87 | + private $includeShared = false; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The current user, plus any owners of the items shared with the current |
|
| 91 | + * user, if $this->includeShared === true. |
|
| 92 | + * |
|
| 93 | + * @var array |
|
| 94 | + */ |
|
| 95 | + private $owners = array(); |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * The Mapper we're using to communicate our Tag objects to the database. |
|
| 99 | + * |
|
| 100 | + * @var TagMapper |
|
| 101 | + */ |
|
| 102 | + private $mapper; |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * The sharing backend for objects of $this->type. Required if |
|
| 106 | + * $this->includeShared === true to determine ownership of items. |
|
| 107 | + * |
|
| 108 | + * @var \OCP\Share_Backend |
|
| 109 | + */ |
|
| 110 | + private $backend; |
|
| 111 | + |
|
| 112 | + const TAG_TABLE = '*PREFIX*vcategory'; |
|
| 113 | + const RELATION_TABLE = '*PREFIX*vcategory_to_object'; |
|
| 114 | + |
|
| 115 | + const TAG_FAVORITE = '_$!<Favorite>!$_'; |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Constructor. |
|
| 119 | + * |
|
| 120 | + * @param TagMapper $mapper Instance of the TagMapper abstraction layer. |
|
| 121 | + * @param string $user The user whose data the object will operate on. |
|
| 122 | + * @param string $type The type of items for which tags will be loaded. |
|
| 123 | + * @param array $defaultTags Tags that should be created at construction. |
|
| 124 | + * @param boolean $includeShared Whether to include tags for items shared with this user by others. |
|
| 125 | + */ |
|
| 126 | + public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) { |
|
| 127 | + $this->mapper = $mapper; |
|
| 128 | + $this->user = $user; |
|
| 129 | + $this->type = $type; |
|
| 130 | + $this->includeShared = $includeShared; |
|
| 131 | + $this->owners = array($this->user); |
|
| 132 | + if ($this->includeShared) { |
|
| 133 | + $this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true)); |
|
| 134 | + $this->backend = \OC\Share\Share::getBackend($this->type); |
|
| 135 | + } |
|
| 136 | + $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
| 137 | + |
|
| 138 | + if(count($defaultTags) > 0 && count($this->tags) === 0) { |
|
| 139 | + $this->addMultiple($defaultTags, true); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Check if any tags are saved for this type and user. |
|
| 145 | + * |
|
| 146 | + * @return boolean |
|
| 147 | + */ |
|
| 148 | + public function isEmpty() { |
|
| 149 | + return count($this->tags) === 0; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Returns an array mapping a given tag's properties to its values: |
|
| 154 | + * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
| 155 | + * |
|
| 156 | + * @param string $id The ID of the tag that is going to be mapped |
|
| 157 | + * @return array|false |
|
| 158 | + */ |
|
| 159 | + public function getTag($id) { |
|
| 160 | + $key = $this->getTagById($id); |
|
| 161 | + if ($key !== false) { |
|
| 162 | + return $this->tagMap($this->tags[$key]); |
|
| 163 | + } |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get the tags for a specific user. |
|
| 169 | + * |
|
| 170 | + * This returns an array with maps containing each tag's properties: |
|
| 171 | + * [ |
|
| 172 | + * ['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'], |
|
| 173 | + * ['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'], |
|
| 174 | + * ] |
|
| 175 | + * |
|
| 176 | + * @return array |
|
| 177 | + */ |
|
| 178 | + public function getTags() { |
|
| 179 | + if(!count($this->tags)) { |
|
| 180 | + return array(); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + usort($this->tags, function($a, $b) { |
|
| 184 | + return strnatcasecmp($a->getName(), $b->getName()); |
|
| 185 | + }); |
|
| 186 | + $tagMap = array(); |
|
| 187 | + |
|
| 188 | + foreach($this->tags as $tag) { |
|
| 189 | + if($tag->getName() !== self::TAG_FAVORITE) { |
|
| 190 | + $tagMap[] = $this->tagMap($tag); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + return $tagMap; |
|
| 194 | + |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Return only the tags owned by the given user, omitting any tags shared |
|
| 199 | + * by other users. |
|
| 200 | + * |
|
| 201 | + * @param string $user The user whose tags are to be checked. |
|
| 202 | + * @return array An array of Tag objects. |
|
| 203 | + */ |
|
| 204 | + public function getTagsForUser($user) { |
|
| 205 | + return array_filter($this->tags, |
|
| 206 | + function($tag) use($user) { |
|
| 207 | + return $tag->getOwner() === $user; |
|
| 208 | + } |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Get the list of tags for the given ids. |
|
| 214 | + * |
|
| 215 | + * @param array $objIds array of object ids |
|
| 216 | + * @return array|boolean of tags id as key to array of tag names |
|
| 217 | + * or false if an error occurred |
|
| 218 | + */ |
|
| 219 | + public function getTagsForObjects(array $objIds) { |
|
| 220 | + $entries = array(); |
|
| 221 | + |
|
| 222 | + try { |
|
| 223 | + $conn = \OC::$server->getDatabaseConnection(); |
|
| 224 | + $chunks = array_chunk($objIds, 900, false); |
|
| 225 | + foreach ($chunks as $chunk) { |
|
| 226 | + $result = $conn->executeQuery( |
|
| 227 | + 'SELECT `category`, `categoryid`, `objid` ' . |
|
| 228 | + 'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' . |
|
| 229 | + 'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)', |
|
| 230 | + array($this->user, $this->type, $chunk), |
|
| 231 | + array(null, null, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 232 | + ); |
|
| 233 | + while ($row = $result->fetch()) { |
|
| 234 | + $objId = (int)$row['objid']; |
|
| 235 | + if (!isset($entries[$objId])) { |
|
| 236 | + $entries[$objId] = array(); |
|
| 237 | + } |
|
| 238 | + $entries[$objId][] = $row['category']; |
|
| 239 | + } |
|
| 240 | + if ($result === null) { |
|
| 241 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + } catch(\Exception $e) { |
|
| 246 | + \OC::$server->getLogger()->logException($e, [ |
|
| 247 | + 'message' => __METHOD__, |
|
| 248 | + 'level' => ILogger::ERROR, |
|
| 249 | + 'app' => 'core', |
|
| 250 | + ]); |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $entries; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Get the a list if items tagged with $tag. |
|
| 259 | + * |
|
| 260 | + * Throws an exception if the tag could not be found. |
|
| 261 | + * |
|
| 262 | + * @param string $tag Tag id or name. |
|
| 263 | + * @return array|false An array of object ids or false on error. |
|
| 264 | + * @throws \Exception |
|
| 265 | + */ |
|
| 266 | + public function getIdsForTag($tag) { |
|
| 267 | + $result = null; |
|
| 268 | + $tagId = false; |
|
| 269 | + if(is_numeric($tag)) { |
|
| 270 | + $tagId = $tag; |
|
| 271 | + } elseif(is_string($tag)) { |
|
| 272 | + $tag = trim($tag); |
|
| 273 | + if($tag === '') { |
|
| 274 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG); |
|
| 275 | + return false; |
|
| 276 | + } |
|
| 277 | + $tagId = $this->getTagId($tag); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + if($tagId === false) { |
|
| 281 | + $l10n = \OC::$server->getL10N('core'); |
|
| 282 | + throw new \Exception( |
|
| 283 | + $l10n->t('Could not find category "%s"', [$tag]) |
|
| 284 | + ); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + $ids = array(); |
|
| 288 | + $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE |
|
| 289 | + . '` WHERE `categoryid` = ?'; |
|
| 290 | + |
|
| 291 | + try { |
|
| 292 | + $stmt = \OC_DB::prepare($sql); |
|
| 293 | + $result = $stmt->execute(array($tagId)); |
|
| 294 | + if ($result === null) { |
|
| 295 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 296 | + return false; |
|
| 297 | + } |
|
| 298 | + } catch(\Exception $e) { |
|
| 299 | + \OC::$server->getLogger()->logException($e, [ |
|
| 300 | + 'message' => __METHOD__, |
|
| 301 | + 'level' => ILogger::ERROR, |
|
| 302 | + 'app' => 'core', |
|
| 303 | + ]); |
|
| 304 | + return false; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + if(!is_null($result)) { |
|
| 308 | + while( $row = $result->fetchRow()) { |
|
| 309 | + $id = (int)$row['objid']; |
|
| 310 | + |
|
| 311 | + if ($this->includeShared) { |
|
| 312 | + // We have to check if we are really allowed to access the |
|
| 313 | + // items that are tagged with $tag. To that end, we ask the |
|
| 314 | + // corresponding sharing backend if the item identified by $id |
|
| 315 | + // is owned by any of $this->owners. |
|
| 316 | + foreach ($this->owners as $owner) { |
|
| 317 | + if ($this->backend->isValidSource($id, $owner)) { |
|
| 318 | + $ids[] = $id; |
|
| 319 | + break; |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + } else { |
|
| 323 | + $ids[] = $id; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return $ids; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Checks whether a tag is saved for the given user, |
|
| 333 | + * disregarding the ones shared with him or her. |
|
| 334 | + * |
|
| 335 | + * @param string $name The tag name to check for. |
|
| 336 | + * @param string $user The user whose tags are to be checked. |
|
| 337 | + * @return bool |
|
| 338 | + */ |
|
| 339 | + public function userHasTag($name, $user) { |
|
| 340 | + $key = $this->array_searchi($name, $this->getTagsForUser($user)); |
|
| 341 | + return ($key !== false) ? $this->tags[$key]->getId() : false; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Checks whether a tag is saved for or shared with the current user. |
|
| 346 | + * |
|
| 347 | + * @param string $name The tag name to check for. |
|
| 348 | + * @return bool |
|
| 349 | + */ |
|
| 350 | + public function hasTag($name) { |
|
| 351 | + return $this->getTagId($name) !== false; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Add a new tag. |
|
| 356 | + * |
|
| 357 | + * @param string $name A string with a name of the tag |
|
| 358 | + * @return false|int the id of the added tag or false on error. |
|
| 359 | + */ |
|
| 360 | + public function add($name) { |
|
| 361 | + $name = trim($name); |
|
| 362 | + |
|
| 363 | + if($name === '') { |
|
| 364 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG); |
|
| 365 | + return false; |
|
| 366 | + } |
|
| 367 | + if($this->userHasTag($name, $this->user)) { |
|
| 368 | + \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', ILogger::DEBUG); |
|
| 369 | + return false; |
|
| 370 | + } |
|
| 371 | + try { |
|
| 372 | + $tag = new Tag($this->user, $this->type, $name); |
|
| 373 | + $tag = $this->mapper->insert($tag); |
|
| 374 | + $this->tags[] = $tag; |
|
| 375 | + } catch(\Exception $e) { |
|
| 376 | + \OC::$server->getLogger()->logException($e, [ |
|
| 377 | + 'message' => __METHOD__, |
|
| 378 | + 'level' => ILogger::ERROR, |
|
| 379 | + 'app' => 'core', |
|
| 380 | + ]); |
|
| 381 | + return false; |
|
| 382 | + } |
|
| 383 | + \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), ILogger::DEBUG); |
|
| 384 | + return $tag->getId(); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Rename tag. |
|
| 389 | + * |
|
| 390 | + * @param string|integer $from The name or ID of the existing tag |
|
| 391 | + * @param string $to The new name of the tag. |
|
| 392 | + * @return bool |
|
| 393 | + */ |
|
| 394 | + public function rename($from, $to) { |
|
| 395 | + $from = trim($from); |
|
| 396 | + $to = trim($to); |
|
| 397 | + |
|
| 398 | + if($to === '' || $from === '') { |
|
| 399 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG); |
|
| 400 | + return false; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + if (is_numeric($from)) { |
|
| 404 | + $key = $this->getTagById($from); |
|
| 405 | + } else { |
|
| 406 | + $key = $this->getTagByName($from); |
|
| 407 | + } |
|
| 408 | + if($key === false) { |
|
| 409 | + \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', ILogger::DEBUG); |
|
| 410 | + return false; |
|
| 411 | + } |
|
| 412 | + $tag = $this->tags[$key]; |
|
| 413 | + |
|
| 414 | + if($this->userHasTag($to, $tag->getOwner())) { |
|
| 415 | + \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', ILogger::DEBUG); |
|
| 416 | + return false; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + try { |
|
| 420 | + $tag->setName($to); |
|
| 421 | + $this->tags[$key] = $this->mapper->update($tag); |
|
| 422 | + } catch(\Exception $e) { |
|
| 423 | + \OC::$server->getLogger()->logException($e, [ |
|
| 424 | + 'message' => __METHOD__, |
|
| 425 | + 'level' => ILogger::ERROR, |
|
| 426 | + 'app' => 'core', |
|
| 427 | + ]); |
|
| 428 | + return false; |
|
| 429 | + } |
|
| 430 | + return true; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Add a list of new tags. |
|
| 435 | + * |
|
| 436 | + * @param string[] $names A string with a name or an array of strings containing |
|
| 437 | + * the name(s) of the tag(s) to add. |
|
| 438 | + * @param bool $sync When true, save the tags |
|
| 439 | + * @param int|null $id int Optional object id to add to this|these tag(s) |
|
| 440 | + * @return bool Returns false on error. |
|
| 441 | + */ |
|
| 442 | + public function addMultiple($names, $sync=false, $id = null) { |
|
| 443 | + if(!is_array($names)) { |
|
| 444 | + $names = array($names); |
|
| 445 | + } |
|
| 446 | + $names = array_map('trim', $names); |
|
| 447 | + array_filter($names); |
|
| 448 | + |
|
| 449 | + $newones = array(); |
|
| 450 | + foreach($names as $name) { |
|
| 451 | + if(!$this->hasTag($name) && $name !== '') { |
|
| 452 | + $newones[] = new Tag($this->user, $this->type, $name); |
|
| 453 | + } |
|
| 454 | + if(!is_null($id) ) { |
|
| 455 | + // Insert $objectid, $categoryid pairs if not exist. |
|
| 456 | + self::$relations[] = array('objid' => $id, 'tag' => $name); |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + $this->tags = array_merge($this->tags, $newones); |
|
| 460 | + if($sync === true) { |
|
| 461 | + $this->save(); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + return true; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Save the list of tags and their object relations |
|
| 469 | + */ |
|
| 470 | + protected function save() { |
|
| 471 | + if(is_array($this->tags)) { |
|
| 472 | + foreach($this->tags as $tag) { |
|
| 473 | + try { |
|
| 474 | + if (!$this->mapper->tagExists($tag)) { |
|
| 475 | + $this->mapper->insert($tag); |
|
| 476 | + } |
|
| 477 | + } catch(\Exception $e) { |
|
| 478 | + \OC::$server->getLogger()->logException($e, [ |
|
| 479 | + 'message' => __METHOD__, |
|
| 480 | + 'level' => ILogger::ERROR, |
|
| 481 | + 'app' => 'core', |
|
| 482 | + ]); |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + // reload tags to get the proper ids. |
|
| 487 | + $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
| 488 | + \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
| 489 | + ILogger::DEBUG); |
|
| 490 | + // Loop through temporarily cached objectid/tagname pairs |
|
| 491 | + // and save relations. |
|
| 492 | + $tags = $this->tags; |
|
| 493 | + // For some reason this is needed or array_search(i) will return 0..? |
|
| 494 | + ksort($tags); |
|
| 495 | + $dbConnection = \OC::$server->getDatabaseConnection(); |
|
| 496 | + foreach(self::$relations as $relation) { |
|
| 497 | + $tagId = $this->getTagId($relation['tag']); |
|
| 498 | + \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, ILogger::DEBUG); |
|
| 499 | + if($tagId) { |
|
| 500 | + try { |
|
| 501 | + $dbConnection->insertIfNotExist(self::RELATION_TABLE, |
|
| 502 | + array( |
|
| 503 | + 'objid' => $relation['objid'], |
|
| 504 | + 'categoryid' => $tagId, |
|
| 505 | + 'type' => $this->type, |
|
| 506 | + )); |
|
| 507 | + } catch(\Exception $e) { |
|
| 508 | + \OC::$server->getLogger()->logException($e, [ |
|
| 509 | + 'message' => __METHOD__, |
|
| 510 | + 'level' => ILogger::ERROR, |
|
| 511 | + 'app' => 'core', |
|
| 512 | + ]); |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + self::$relations = array(); // reset |
|
| 517 | + } else { |
|
| 518 | + \OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! ' |
|
| 519 | + . print_r($this->tags, true), ILogger::ERROR); |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Delete tags and tag/object relations for a user. |
|
| 525 | + * |
|
| 526 | + * For hooking up on post_deleteUser |
|
| 527 | + * |
|
| 528 | + * @param array $arguments |
|
| 529 | + */ |
|
| 530 | + public static function post_deleteUser($arguments) { |
|
| 531 | + // Find all objectid/tagId pairs. |
|
| 532 | + $result = null; |
|
| 533 | + try { |
|
| 534 | + $stmt = \OC_DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` ' |
|
| 535 | + . 'WHERE `uid` = ?'); |
|
| 536 | + $result = $stmt->execute(array($arguments['uid'])); |
|
| 537 | + if ($result === null) { |
|
| 538 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 539 | + } |
|
| 540 | + } catch(\Exception $e) { |
|
| 541 | + \OC::$server->getLogger()->logException($e, [ |
|
| 542 | + 'message' => __METHOD__, |
|
| 543 | + 'level' => ILogger::ERROR, |
|
| 544 | + 'app' => 'core', |
|
| 545 | + ]); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + if(!is_null($result)) { |
|
| 549 | + try { |
|
| 550 | + $stmt = \OC_DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 551 | + . 'WHERE `categoryid` = ?'); |
|
| 552 | + while( $row = $result->fetchRow()) { |
|
| 553 | + try { |
|
| 554 | + $stmt->execute(array($row['id'])); |
|
| 555 | + } catch(\Exception $e) { |
|
| 556 | + \OC::$server->getLogger()->logException($e, [ |
|
| 557 | + 'message' => __METHOD__, |
|
| 558 | + 'level' => ILogger::ERROR, |
|
| 559 | + 'app' => 'core', |
|
| 560 | + ]); |
|
| 561 | + } |
|
| 562 | + } |
|
| 563 | + } catch(\Exception $e) { |
|
| 564 | + \OC::$server->getLogger()->logException($e, [ |
|
| 565 | + 'message' => __METHOD__, |
|
| 566 | + 'level' => ILogger::ERROR, |
|
| 567 | + 'app' => 'core', |
|
| 568 | + ]); |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + try { |
|
| 572 | + $stmt = \OC_DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` ' |
|
| 573 | + . 'WHERE `uid` = ?'); |
|
| 574 | + $result = $stmt->execute(array($arguments['uid'])); |
|
| 575 | + if ($result === null) { |
|
| 576 | + \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 577 | + } |
|
| 578 | + } catch(\Exception $e) { |
|
| 579 | + \OC::$server->getLogger()->logException($e, [ |
|
| 580 | + 'message' => __METHOD__, |
|
| 581 | + 'level' => ILogger::ERROR, |
|
| 582 | + 'app' => 'core', |
|
| 583 | + ]); |
|
| 584 | + } |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Delete tag/object relations from the db |
|
| 589 | + * |
|
| 590 | + * @param array $ids The ids of the objects |
|
| 591 | + * @return boolean Returns false on error. |
|
| 592 | + */ |
|
| 593 | + public function purgeObjects(array $ids) { |
|
| 594 | + if(count($ids) === 0) { |
|
| 595 | + // job done ;) |
|
| 596 | + return true; |
|
| 597 | + } |
|
| 598 | + $updates = $ids; |
|
| 599 | + try { |
|
| 600 | + $query = 'DELETE FROM `' . self::RELATION_TABLE . '` '; |
|
| 601 | + $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) '; |
|
| 602 | + $query .= 'AND `type`= ?'; |
|
| 603 | + $updates[] = $this->type; |
|
| 604 | + $stmt = \OC_DB::prepare($query); |
|
| 605 | + $result = $stmt->execute($updates); |
|
| 606 | + if ($result === null) { |
|
| 607 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 608 | + return false; |
|
| 609 | + } |
|
| 610 | + } catch(\Exception $e) { |
|
| 611 | + \OC::$server->getLogger()->logException($e, [ |
|
| 612 | + 'message' => __METHOD__, |
|
| 613 | + 'level' => ILogger::ERROR, |
|
| 614 | + 'app' => 'core', |
|
| 615 | + ]); |
|
| 616 | + return false; |
|
| 617 | + } |
|
| 618 | + return true; |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + /** |
|
| 622 | + * Get favorites for an object type |
|
| 623 | + * |
|
| 624 | + * @return array|false An array of object ids. |
|
| 625 | + */ |
|
| 626 | + public function getFavorites() { |
|
| 627 | + try { |
|
| 628 | + return $this->getIdsForTag(self::TAG_FAVORITE); |
|
| 629 | + } catch(\Exception $e) { |
|
| 630 | + \OC::$server->getLogger()->logException($e, [ |
|
| 631 | + 'message' => __METHOD__, |
|
| 632 | + 'level' => ILogger::ERROR, |
|
| 633 | + 'app' => 'core', |
|
| 634 | + ]); |
|
| 635 | + return array(); |
|
| 636 | + } |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Add an object to favorites |
|
| 641 | + * |
|
| 642 | + * @param int $objid The id of the object |
|
| 643 | + * @return boolean |
|
| 644 | + */ |
|
| 645 | + public function addToFavorites($objid) { |
|
| 646 | + if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
| 647 | + $this->add(self::TAG_FAVORITE); |
|
| 648 | + } |
|
| 649 | + return $this->tagAs($objid, self::TAG_FAVORITE); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Remove an object from favorites |
|
| 654 | + * |
|
| 655 | + * @param int $objid The id of the object |
|
| 656 | + * @return boolean |
|
| 657 | + */ |
|
| 658 | + public function removeFromFavorites($objid) { |
|
| 659 | + return $this->unTag($objid, self::TAG_FAVORITE); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Creates a tag/object relation. |
|
| 664 | + * |
|
| 665 | + * @param int $objid The id of the object |
|
| 666 | + * @param string $tag The id or name of the tag |
|
| 667 | + * @return boolean Returns false on error. |
|
| 668 | + */ |
|
| 669 | + public function tagAs($objid, $tag) { |
|
| 670 | + if(is_string($tag) && !is_numeric($tag)) { |
|
| 671 | + $tag = trim($tag); |
|
| 672 | + if($tag === '') { |
|
| 673 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG); |
|
| 674 | + return false; |
|
| 675 | + } |
|
| 676 | + if(!$this->hasTag($tag)) { |
|
| 677 | + $this->add($tag); |
|
| 678 | + } |
|
| 679 | + $tagId = $this->getTagId($tag); |
|
| 680 | + } else { |
|
| 681 | + $tagId = $tag; |
|
| 682 | + } |
|
| 683 | + try { |
|
| 684 | + \OC::$server->getDatabaseConnection()->insertIfNotExist(self::RELATION_TABLE, |
|
| 685 | + array( |
|
| 686 | + 'objid' => $objid, |
|
| 687 | + 'categoryid' => $tagId, |
|
| 688 | + 'type' => $this->type, |
|
| 689 | + )); |
|
| 690 | + } catch(\Exception $e) { |
|
| 691 | + \OC::$server->getLogger()->logException($e, [ |
|
| 692 | + 'message' => __METHOD__, |
|
| 693 | + 'level' => ILogger::ERROR, |
|
| 694 | + 'app' => 'core', |
|
| 695 | + ]); |
|
| 696 | + return false; |
|
| 697 | + } |
|
| 698 | + return true; |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Delete single tag/object relation from the db |
|
| 703 | + * |
|
| 704 | + * @param int $objid The id of the object |
|
| 705 | + * @param string $tag The id or name of the tag |
|
| 706 | + * @return boolean |
|
| 707 | + */ |
|
| 708 | + public function unTag($objid, $tag) { |
|
| 709 | + if(is_string($tag) && !is_numeric($tag)) { |
|
| 710 | + $tag = trim($tag); |
|
| 711 | + if($tag === '') { |
|
| 712 | + \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', ILogger::DEBUG); |
|
| 713 | + return false; |
|
| 714 | + } |
|
| 715 | + $tagId = $this->getTagId($tag); |
|
| 716 | + } else { |
|
| 717 | + $tagId = $tag; |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + try { |
|
| 721 | + $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 722 | + . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?'; |
|
| 723 | + $stmt = \OC_DB::prepare($sql); |
|
| 724 | + $stmt->execute(array($objid, $tagId, $this->type)); |
|
| 725 | + } catch(\Exception $e) { |
|
| 726 | + \OC::$server->getLogger()->logException($e, [ |
|
| 727 | + 'message' => __METHOD__, |
|
| 728 | + 'level' => ILogger::ERROR, |
|
| 729 | + 'app' => 'core', |
|
| 730 | + ]); |
|
| 731 | + return false; |
|
| 732 | + } |
|
| 733 | + return true; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Delete tags from the database. |
|
| 738 | + * |
|
| 739 | + * @param string[]|integer[] $names An array of tags (names or IDs) to delete |
|
| 740 | + * @return bool Returns false on error |
|
| 741 | + */ |
|
| 742 | + public function delete($names) { |
|
| 743 | + if(!is_array($names)) { |
|
| 744 | + $names = array($names); |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + $names = array_map('trim', $names); |
|
| 748 | + array_filter($names); |
|
| 749 | + |
|
| 750 | + \OCP\Util::writeLog('core', __METHOD__ . ', before: ' |
|
| 751 | + . print_r($this->tags, true), ILogger::DEBUG); |
|
| 752 | + foreach($names as $name) { |
|
| 753 | + $id = null; |
|
| 754 | + |
|
| 755 | + if (is_numeric($name)) { |
|
| 756 | + $key = $this->getTagById($name); |
|
| 757 | + } else { |
|
| 758 | + $key = $this->getTagByName($name); |
|
| 759 | + } |
|
| 760 | + if ($key !== false) { |
|
| 761 | + $tag = $this->tags[$key]; |
|
| 762 | + $id = $tag->getId(); |
|
| 763 | + unset($this->tags[$key]); |
|
| 764 | + $this->mapper->delete($tag); |
|
| 765 | + } else { |
|
| 766 | + \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name |
|
| 767 | + . ': not found.', ILogger::ERROR); |
|
| 768 | + } |
|
| 769 | + if(!is_null($id) && $id !== false) { |
|
| 770 | + try { |
|
| 771 | + $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 772 | + . 'WHERE `categoryid` = ?'; |
|
| 773 | + $stmt = \OC_DB::prepare($sql); |
|
| 774 | + $result = $stmt->execute(array($id)); |
|
| 775 | + if ($result === null) { |
|
| 776 | + \OCP\Util::writeLog('core', |
|
| 777 | + __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), |
|
| 778 | + ILogger::ERROR); |
|
| 779 | + return false; |
|
| 780 | + } |
|
| 781 | + } catch(\Exception $e) { |
|
| 782 | + \OC::$server->getLogger()->logException($e, [ |
|
| 783 | + 'message' => __METHOD__, |
|
| 784 | + 'level' => ILogger::ERROR, |
|
| 785 | + 'app' => 'core', |
|
| 786 | + ]); |
|
| 787 | + return false; |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | + } |
|
| 791 | + return true; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + // case-insensitive array_search |
|
| 795 | + protected function array_searchi($needle, $haystack, $mem='getName') { |
|
| 796 | + if(!is_array($haystack)) { |
|
| 797 | + return false; |
|
| 798 | + } |
|
| 799 | + return array_search(strtolower($needle), array_map( |
|
| 800 | + function($tag) use($mem) { |
|
| 801 | + return strtolower(call_user_func(array($tag, $mem))); |
|
| 802 | + }, $haystack) |
|
| 803 | + ); |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + /** |
|
| 807 | + * Get a tag's ID. |
|
| 808 | + * |
|
| 809 | + * @param string $name The tag name to look for. |
|
| 810 | + * @return string|bool The tag's id or false if no matching tag is found. |
|
| 811 | + */ |
|
| 812 | + private function getTagId($name) { |
|
| 813 | + $key = $this->array_searchi($name, $this->tags); |
|
| 814 | + if ($key !== false) { |
|
| 815 | + return $this->tags[$key]->getId(); |
|
| 816 | + } |
|
| 817 | + return false; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * Get a tag by its name. |
|
| 822 | + * |
|
| 823 | + * @param string $name The tag name. |
|
| 824 | + * @return integer|bool The tag object's offset within the $this->tags |
|
| 825 | + * array or false if it doesn't exist. |
|
| 826 | + */ |
|
| 827 | + private function getTagByName($name) { |
|
| 828 | + return $this->array_searchi($name, $this->tags, 'getName'); |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + /** |
|
| 832 | + * Get a tag by its ID. |
|
| 833 | + * |
|
| 834 | + * @param string $id The tag ID to look for. |
|
| 835 | + * @return integer|bool The tag object's offset within the $this->tags |
|
| 836 | + * array or false if it doesn't exist. |
|
| 837 | + */ |
|
| 838 | + private function getTagById($id) { |
|
| 839 | + return $this->array_searchi($id, $this->tags, 'getId'); |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + /** |
|
| 843 | + * Returns an array mapping a given tag's properties to its values: |
|
| 844 | + * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
| 845 | + * |
|
| 846 | + * @param Tag $tag The tag that is going to be mapped |
|
| 847 | + * @return array |
|
| 848 | + */ |
|
| 849 | + private function tagMap(Tag $tag) { |
|
| 850 | + return array( |
|
| 851 | + 'id' => $tag->getId(), |
|
| 852 | + 'name' => $tag->getName(), |
|
| 853 | + 'owner' => $tag->getOwner(), |
|
| 854 | + 'type' => $tag->getType() |
|
| 855 | + ); |
|
| 856 | + } |
|
| 857 | 857 | } |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | } |
| 136 | 136 | $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
| 137 | 137 | |
| 138 | - if(count($defaultTags) > 0 && count($this->tags) === 0) { |
|
| 138 | + if (count($defaultTags) > 0 && count($this->tags) === 0) { |
|
| 139 | 139 | $this->addMultiple($defaultTags, true); |
| 140 | 140 | } |
| 141 | 141 | } |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | * @return array |
| 177 | 177 | */ |
| 178 | 178 | public function getTags() { |
| 179 | - if(!count($this->tags)) { |
|
| 179 | + if (!count($this->tags)) { |
|
| 180 | 180 | return array(); |
| 181 | 181 | } |
| 182 | 182 | |
@@ -185,8 +185,8 @@ discard block |
||
| 185 | 185 | }); |
| 186 | 186 | $tagMap = array(); |
| 187 | 187 | |
| 188 | - foreach($this->tags as $tag) { |
|
| 189 | - if($tag->getName() !== self::TAG_FAVORITE) { |
|
| 188 | + foreach ($this->tags as $tag) { |
|
| 189 | + if ($tag->getName() !== self::TAG_FAVORITE) { |
|
| 190 | 190 | $tagMap[] = $this->tagMap($tag); |
| 191 | 191 | } |
| 192 | 192 | } |
@@ -224,25 +224,25 @@ discard block |
||
| 224 | 224 | $chunks = array_chunk($objIds, 900, false); |
| 225 | 225 | foreach ($chunks as $chunk) { |
| 226 | 226 | $result = $conn->executeQuery( |
| 227 | - 'SELECT `category`, `categoryid`, `objid` ' . |
|
| 228 | - 'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' . |
|
| 227 | + 'SELECT `category`, `categoryid`, `objid` '. |
|
| 228 | + 'FROM `'.self::RELATION_TABLE.'` r, `'.self::TAG_TABLE.'` '. |
|
| 229 | 229 | 'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)', |
| 230 | 230 | array($this->user, $this->type, $chunk), |
| 231 | 231 | array(null, null, IQueryBuilder::PARAM_INT_ARRAY) |
| 232 | 232 | ); |
| 233 | 233 | while ($row = $result->fetch()) { |
| 234 | - $objId = (int)$row['objid']; |
|
| 234 | + $objId = (int) $row['objid']; |
|
| 235 | 235 | if (!isset($entries[$objId])) { |
| 236 | 236 | $entries[$objId] = array(); |
| 237 | 237 | } |
| 238 | 238 | $entries[$objId][] = $row['category']; |
| 239 | 239 | } |
| 240 | 240 | if ($result === null) { |
| 241 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 241 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 242 | 242 | return false; |
| 243 | 243 | } |
| 244 | 244 | } |
| 245 | - } catch(\Exception $e) { |
|
| 245 | + } catch (\Exception $e) { |
|
| 246 | 246 | \OC::$server->getLogger()->logException($e, [ |
| 247 | 247 | 'message' => __METHOD__, |
| 248 | 248 | 'level' => ILogger::ERROR, |
@@ -266,18 +266,18 @@ discard block |
||
| 266 | 266 | public function getIdsForTag($tag) { |
| 267 | 267 | $result = null; |
| 268 | 268 | $tagId = false; |
| 269 | - if(is_numeric($tag)) { |
|
| 269 | + if (is_numeric($tag)) { |
|
| 270 | 270 | $tagId = $tag; |
| 271 | - } elseif(is_string($tag)) { |
|
| 271 | + } elseif (is_string($tag)) { |
|
| 272 | 272 | $tag = trim($tag); |
| 273 | - if($tag === '') { |
|
| 273 | + if ($tag === '') { |
|
| 274 | 274 | \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG); |
| 275 | 275 | return false; |
| 276 | 276 | } |
| 277 | 277 | $tagId = $this->getTagId($tag); |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - if($tagId === false) { |
|
| 280 | + if ($tagId === false) { |
|
| 281 | 281 | $l10n = \OC::$server->getL10N('core'); |
| 282 | 282 | throw new \Exception( |
| 283 | 283 | $l10n->t('Could not find category "%s"', [$tag]) |
@@ -285,17 +285,17 @@ discard block |
||
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | $ids = array(); |
| 288 | - $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE |
|
| 288 | + $sql = 'SELECT `objid` FROM `'.self::RELATION_TABLE |
|
| 289 | 289 | . '` WHERE `categoryid` = ?'; |
| 290 | 290 | |
| 291 | 291 | try { |
| 292 | 292 | $stmt = \OC_DB::prepare($sql); |
| 293 | 293 | $result = $stmt->execute(array($tagId)); |
| 294 | 294 | if ($result === null) { |
| 295 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 295 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 296 | 296 | return false; |
| 297 | 297 | } |
| 298 | - } catch(\Exception $e) { |
|
| 298 | + } catch (\Exception $e) { |
|
| 299 | 299 | \OC::$server->getLogger()->logException($e, [ |
| 300 | 300 | 'message' => __METHOD__, |
| 301 | 301 | 'level' => ILogger::ERROR, |
@@ -304,9 +304,9 @@ discard block |
||
| 304 | 304 | return false; |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | - if(!is_null($result)) { |
|
| 308 | - while( $row = $result->fetchRow()) { |
|
| 309 | - $id = (int)$row['objid']; |
|
| 307 | + if (!is_null($result)) { |
|
| 308 | + while ($row = $result->fetchRow()) { |
|
| 309 | + $id = (int) $row['objid']; |
|
| 310 | 310 | |
| 311 | 311 | if ($this->includeShared) { |
| 312 | 312 | // We have to check if we are really allowed to access the |
@@ -360,19 +360,19 @@ discard block |
||
| 360 | 360 | public function add($name) { |
| 361 | 361 | $name = trim($name); |
| 362 | 362 | |
| 363 | - if($name === '') { |
|
| 363 | + if ($name === '') { |
|
| 364 | 364 | \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG); |
| 365 | 365 | return false; |
| 366 | 366 | } |
| 367 | - if($this->userHasTag($name, $this->user)) { |
|
| 368 | - \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', ILogger::DEBUG); |
|
| 367 | + if ($this->userHasTag($name, $this->user)) { |
|
| 368 | + \OCP\Util::writeLog('core', __METHOD__.', name: '.$name.' exists already', ILogger::DEBUG); |
|
| 369 | 369 | return false; |
| 370 | 370 | } |
| 371 | 371 | try { |
| 372 | 372 | $tag = new Tag($this->user, $this->type, $name); |
| 373 | 373 | $tag = $this->mapper->insert($tag); |
| 374 | 374 | $this->tags[] = $tag; |
| 375 | - } catch(\Exception $e) { |
|
| 375 | + } catch (\Exception $e) { |
|
| 376 | 376 | \OC::$server->getLogger()->logException($e, [ |
| 377 | 377 | 'message' => __METHOD__, |
| 378 | 378 | 'level' => ILogger::ERROR, |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | ]); |
| 381 | 381 | return false; |
| 382 | 382 | } |
| 383 | - \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), ILogger::DEBUG); |
|
| 383 | + \OCP\Util::writeLog('core', __METHOD__.', id: '.$tag->getId(), ILogger::DEBUG); |
|
| 384 | 384 | return $tag->getId(); |
| 385 | 385 | } |
| 386 | 386 | |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | $from = trim($from); |
| 396 | 396 | $to = trim($to); |
| 397 | 397 | |
| 398 | - if($to === '' || $from === '') { |
|
| 398 | + if ($to === '' || $from === '') { |
|
| 399 | 399 | \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG); |
| 400 | 400 | return false; |
| 401 | 401 | } |
@@ -405,21 +405,21 @@ discard block |
||
| 405 | 405 | } else { |
| 406 | 406 | $key = $this->getTagByName($from); |
| 407 | 407 | } |
| 408 | - if($key === false) { |
|
| 409 | - \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', ILogger::DEBUG); |
|
| 408 | + if ($key === false) { |
|
| 409 | + \OCP\Util::writeLog('core', __METHOD__.', tag: '.$from.' does not exist', ILogger::DEBUG); |
|
| 410 | 410 | return false; |
| 411 | 411 | } |
| 412 | 412 | $tag = $this->tags[$key]; |
| 413 | 413 | |
| 414 | - if($this->userHasTag($to, $tag->getOwner())) { |
|
| 415 | - \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', ILogger::DEBUG); |
|
| 414 | + if ($this->userHasTag($to, $tag->getOwner())) { |
|
| 415 | + \OCP\Util::writeLog('core', __METHOD__.', A tag named '.$to.' already exists for user '.$tag->getOwner().'.', ILogger::DEBUG); |
|
| 416 | 416 | return false; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | try { |
| 420 | 420 | $tag->setName($to); |
| 421 | 421 | $this->tags[$key] = $this->mapper->update($tag); |
| 422 | - } catch(\Exception $e) { |
|
| 422 | + } catch (\Exception $e) { |
|
| 423 | 423 | \OC::$server->getLogger()->logException($e, [ |
| 424 | 424 | 'message' => __METHOD__, |
| 425 | 425 | 'level' => ILogger::ERROR, |
@@ -439,25 +439,25 @@ discard block |
||
| 439 | 439 | * @param int|null $id int Optional object id to add to this|these tag(s) |
| 440 | 440 | * @return bool Returns false on error. |
| 441 | 441 | */ |
| 442 | - public function addMultiple($names, $sync=false, $id = null) { |
|
| 443 | - if(!is_array($names)) { |
|
| 442 | + public function addMultiple($names, $sync = false, $id = null) { |
|
| 443 | + if (!is_array($names)) { |
|
| 444 | 444 | $names = array($names); |
| 445 | 445 | } |
| 446 | 446 | $names = array_map('trim', $names); |
| 447 | 447 | array_filter($names); |
| 448 | 448 | |
| 449 | 449 | $newones = array(); |
| 450 | - foreach($names as $name) { |
|
| 451 | - if(!$this->hasTag($name) && $name !== '') { |
|
| 450 | + foreach ($names as $name) { |
|
| 451 | + if (!$this->hasTag($name) && $name !== '') { |
|
| 452 | 452 | $newones[] = new Tag($this->user, $this->type, $name); |
| 453 | 453 | } |
| 454 | - if(!is_null($id) ) { |
|
| 454 | + if (!is_null($id)) { |
|
| 455 | 455 | // Insert $objectid, $categoryid pairs if not exist. |
| 456 | 456 | self::$relations[] = array('objid' => $id, 'tag' => $name); |
| 457 | 457 | } |
| 458 | 458 | } |
| 459 | 459 | $this->tags = array_merge($this->tags, $newones); |
| 460 | - if($sync === true) { |
|
| 460 | + if ($sync === true) { |
|
| 461 | 461 | $this->save(); |
| 462 | 462 | } |
| 463 | 463 | |
@@ -468,13 +468,13 @@ discard block |
||
| 468 | 468 | * Save the list of tags and their object relations |
| 469 | 469 | */ |
| 470 | 470 | protected function save() { |
| 471 | - if(is_array($this->tags)) { |
|
| 472 | - foreach($this->tags as $tag) { |
|
| 471 | + if (is_array($this->tags)) { |
|
| 472 | + foreach ($this->tags as $tag) { |
|
| 473 | 473 | try { |
| 474 | 474 | if (!$this->mapper->tagExists($tag)) { |
| 475 | 475 | $this->mapper->insert($tag); |
| 476 | 476 | } |
| 477 | - } catch(\Exception $e) { |
|
| 477 | + } catch (\Exception $e) { |
|
| 478 | 478 | \OC::$server->getLogger()->logException($e, [ |
| 479 | 479 | 'message' => __METHOD__, |
| 480 | 480 | 'level' => ILogger::ERROR, |
@@ -485,7 +485,7 @@ discard block |
||
| 485 | 485 | |
| 486 | 486 | // reload tags to get the proper ids. |
| 487 | 487 | $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
| 488 | - \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
| 488 | + \OCP\Util::writeLog('core', __METHOD__.', tags: '.print_r($this->tags, true), |
|
| 489 | 489 | ILogger::DEBUG); |
| 490 | 490 | // Loop through temporarily cached objectid/tagname pairs |
| 491 | 491 | // and save relations. |
@@ -493,10 +493,10 @@ discard block |
||
| 493 | 493 | // For some reason this is needed or array_search(i) will return 0..? |
| 494 | 494 | ksort($tags); |
| 495 | 495 | $dbConnection = \OC::$server->getDatabaseConnection(); |
| 496 | - foreach(self::$relations as $relation) { |
|
| 496 | + foreach (self::$relations as $relation) { |
|
| 497 | 497 | $tagId = $this->getTagId($relation['tag']); |
| 498 | - \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, ILogger::DEBUG); |
|
| 499 | - if($tagId) { |
|
| 498 | + \OCP\Util::writeLog('core', __METHOD__.'catid, '.$relation['tag'].' '.$tagId, ILogger::DEBUG); |
|
| 499 | + if ($tagId) { |
|
| 500 | 500 | try { |
| 501 | 501 | $dbConnection->insertIfNotExist(self::RELATION_TABLE, |
| 502 | 502 | array( |
@@ -504,7 +504,7 @@ discard block |
||
| 504 | 504 | 'categoryid' => $tagId, |
| 505 | 505 | 'type' => $this->type, |
| 506 | 506 | )); |
| 507 | - } catch(\Exception $e) { |
|
| 507 | + } catch (\Exception $e) { |
|
| 508 | 508 | \OC::$server->getLogger()->logException($e, [ |
| 509 | 509 | 'message' => __METHOD__, |
| 510 | 510 | 'level' => ILogger::ERROR, |
@@ -531,13 +531,13 @@ discard block |
||
| 531 | 531 | // Find all objectid/tagId pairs. |
| 532 | 532 | $result = null; |
| 533 | 533 | try { |
| 534 | - $stmt = \OC_DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` ' |
|
| 534 | + $stmt = \OC_DB::prepare('SELECT `id` FROM `'.self::TAG_TABLE.'` ' |
|
| 535 | 535 | . 'WHERE `uid` = ?'); |
| 536 | 536 | $result = $stmt->execute(array($arguments['uid'])); |
| 537 | 537 | if ($result === null) { |
| 538 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 538 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 539 | 539 | } |
| 540 | - } catch(\Exception $e) { |
|
| 540 | + } catch (\Exception $e) { |
|
| 541 | 541 | \OC::$server->getLogger()->logException($e, [ |
| 542 | 542 | 'message' => __METHOD__, |
| 543 | 543 | 'level' => ILogger::ERROR, |
@@ -545,14 +545,14 @@ discard block |
||
| 545 | 545 | ]); |
| 546 | 546 | } |
| 547 | 547 | |
| 548 | - if(!is_null($result)) { |
|
| 548 | + if (!is_null($result)) { |
|
| 549 | 549 | try { |
| 550 | - $stmt = \OC_DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 550 | + $stmt = \OC_DB::prepare('DELETE FROM `'.self::RELATION_TABLE.'` ' |
|
| 551 | 551 | . 'WHERE `categoryid` = ?'); |
| 552 | - while( $row = $result->fetchRow()) { |
|
| 552 | + while ($row = $result->fetchRow()) { |
|
| 553 | 553 | try { |
| 554 | 554 | $stmt->execute(array($row['id'])); |
| 555 | - } catch(\Exception $e) { |
|
| 555 | + } catch (\Exception $e) { |
|
| 556 | 556 | \OC::$server->getLogger()->logException($e, [ |
| 557 | 557 | 'message' => __METHOD__, |
| 558 | 558 | 'level' => ILogger::ERROR, |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | ]); |
| 561 | 561 | } |
| 562 | 562 | } |
| 563 | - } catch(\Exception $e) { |
|
| 563 | + } catch (\Exception $e) { |
|
| 564 | 564 | \OC::$server->getLogger()->logException($e, [ |
| 565 | 565 | 'message' => __METHOD__, |
| 566 | 566 | 'level' => ILogger::ERROR, |
@@ -569,13 +569,13 @@ discard block |
||
| 569 | 569 | } |
| 570 | 570 | } |
| 571 | 571 | try { |
| 572 | - $stmt = \OC_DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` ' |
|
| 572 | + $stmt = \OC_DB::prepare('DELETE FROM `'.self::TAG_TABLE.'` ' |
|
| 573 | 573 | . 'WHERE `uid` = ?'); |
| 574 | 574 | $result = $stmt->execute(array($arguments['uid'])); |
| 575 | 575 | if ($result === null) { |
| 576 | - \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 576 | + \OCP\Util::writeLog('core', __METHOD__.', DB error: '.\OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 577 | 577 | } |
| 578 | - } catch(\Exception $e) { |
|
| 578 | + } catch (\Exception $e) { |
|
| 579 | 579 | \OC::$server->getLogger()->logException($e, [ |
| 580 | 580 | 'message' => __METHOD__, |
| 581 | 581 | 'level' => ILogger::ERROR, |
@@ -591,23 +591,23 @@ discard block |
||
| 591 | 591 | * @return boolean Returns false on error. |
| 592 | 592 | */ |
| 593 | 593 | public function purgeObjects(array $ids) { |
| 594 | - if(count($ids) === 0) { |
|
| 594 | + if (count($ids) === 0) { |
|
| 595 | 595 | // job done ;) |
| 596 | 596 | return true; |
| 597 | 597 | } |
| 598 | 598 | $updates = $ids; |
| 599 | 599 | try { |
| 600 | - $query = 'DELETE FROM `' . self::RELATION_TABLE . '` '; |
|
| 601 | - $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) '; |
|
| 600 | + $query = 'DELETE FROM `'.self::RELATION_TABLE.'` '; |
|
| 601 | + $query .= 'WHERE `objid` IN ('.str_repeat('?,', count($ids) - 1).'?) '; |
|
| 602 | 602 | $query .= 'AND `type`= ?'; |
| 603 | 603 | $updates[] = $this->type; |
| 604 | 604 | $stmt = \OC_DB::prepare($query); |
| 605 | 605 | $result = $stmt->execute($updates); |
| 606 | 606 | if ($result === null) { |
| 607 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 607 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR); |
|
| 608 | 608 | return false; |
| 609 | 609 | } |
| 610 | - } catch(\Exception $e) { |
|
| 610 | + } catch (\Exception $e) { |
|
| 611 | 611 | \OC::$server->getLogger()->logException($e, [ |
| 612 | 612 | 'message' => __METHOD__, |
| 613 | 613 | 'level' => ILogger::ERROR, |
@@ -626,7 +626,7 @@ discard block |
||
| 626 | 626 | public function getFavorites() { |
| 627 | 627 | try { |
| 628 | 628 | return $this->getIdsForTag(self::TAG_FAVORITE); |
| 629 | - } catch(\Exception $e) { |
|
| 629 | + } catch (\Exception $e) { |
|
| 630 | 630 | \OC::$server->getLogger()->logException($e, [ |
| 631 | 631 | 'message' => __METHOD__, |
| 632 | 632 | 'level' => ILogger::ERROR, |
@@ -643,7 +643,7 @@ discard block |
||
| 643 | 643 | * @return boolean |
| 644 | 644 | */ |
| 645 | 645 | public function addToFavorites($objid) { |
| 646 | - if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
| 646 | + if (!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
| 647 | 647 | $this->add(self::TAG_FAVORITE); |
| 648 | 648 | } |
| 649 | 649 | return $this->tagAs($objid, self::TAG_FAVORITE); |
@@ -667,16 +667,16 @@ discard block |
||
| 667 | 667 | * @return boolean Returns false on error. |
| 668 | 668 | */ |
| 669 | 669 | public function tagAs($objid, $tag) { |
| 670 | - if(is_string($tag) && !is_numeric($tag)) { |
|
| 670 | + if (is_string($tag) && !is_numeric($tag)) { |
|
| 671 | 671 | $tag = trim($tag); |
| 672 | - if($tag === '') { |
|
| 672 | + if ($tag === '') { |
|
| 673 | 673 | \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG); |
| 674 | 674 | return false; |
| 675 | 675 | } |
| 676 | - if(!$this->hasTag($tag)) { |
|
| 676 | + if (!$this->hasTag($tag)) { |
|
| 677 | 677 | $this->add($tag); |
| 678 | 678 | } |
| 679 | - $tagId = $this->getTagId($tag); |
|
| 679 | + $tagId = $this->getTagId($tag); |
|
| 680 | 680 | } else { |
| 681 | 681 | $tagId = $tag; |
| 682 | 682 | } |
@@ -687,7 +687,7 @@ discard block |
||
| 687 | 687 | 'categoryid' => $tagId, |
| 688 | 688 | 'type' => $this->type, |
| 689 | 689 | )); |
| 690 | - } catch(\Exception $e) { |
|
| 690 | + } catch (\Exception $e) { |
|
| 691 | 691 | \OC::$server->getLogger()->logException($e, [ |
| 692 | 692 | 'message' => __METHOD__, |
| 693 | 693 | 'level' => ILogger::ERROR, |
@@ -706,23 +706,23 @@ discard block |
||
| 706 | 706 | * @return boolean |
| 707 | 707 | */ |
| 708 | 708 | public function unTag($objid, $tag) { |
| 709 | - if(is_string($tag) && !is_numeric($tag)) { |
|
| 709 | + if (is_string($tag) && !is_numeric($tag)) { |
|
| 710 | 710 | $tag = trim($tag); |
| 711 | - if($tag === '') { |
|
| 711 | + if ($tag === '') { |
|
| 712 | 712 | \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', ILogger::DEBUG); |
| 713 | 713 | return false; |
| 714 | 714 | } |
| 715 | - $tagId = $this->getTagId($tag); |
|
| 715 | + $tagId = $this->getTagId($tag); |
|
| 716 | 716 | } else { |
| 717 | 717 | $tagId = $tag; |
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | try { |
| 721 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 721 | + $sql = 'DELETE FROM `'.self::RELATION_TABLE.'` ' |
|
| 722 | 722 | . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?'; |
| 723 | 723 | $stmt = \OC_DB::prepare($sql); |
| 724 | 724 | $stmt->execute(array($objid, $tagId, $this->type)); |
| 725 | - } catch(\Exception $e) { |
|
| 725 | + } catch (\Exception $e) { |
|
| 726 | 726 | \OC::$server->getLogger()->logException($e, [ |
| 727 | 727 | 'message' => __METHOD__, |
| 728 | 728 | 'level' => ILogger::ERROR, |
@@ -740,16 +740,16 @@ discard block |
||
| 740 | 740 | * @return bool Returns false on error |
| 741 | 741 | */ |
| 742 | 742 | public function delete($names) { |
| 743 | - if(!is_array($names)) { |
|
| 743 | + if (!is_array($names)) { |
|
| 744 | 744 | $names = array($names); |
| 745 | 745 | } |
| 746 | 746 | |
| 747 | 747 | $names = array_map('trim', $names); |
| 748 | 748 | array_filter($names); |
| 749 | 749 | |
| 750 | - \OCP\Util::writeLog('core', __METHOD__ . ', before: ' |
|
| 750 | + \OCP\Util::writeLog('core', __METHOD__.', before: ' |
|
| 751 | 751 | . print_r($this->tags, true), ILogger::DEBUG); |
| 752 | - foreach($names as $name) { |
|
| 752 | + foreach ($names as $name) { |
|
| 753 | 753 | $id = null; |
| 754 | 754 | |
| 755 | 755 | if (is_numeric($name)) { |
@@ -763,22 +763,22 @@ discard block |
||
| 763 | 763 | unset($this->tags[$key]); |
| 764 | 764 | $this->mapper->delete($tag); |
| 765 | 765 | } else { |
| 766 | - \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name |
|
| 766 | + \OCP\Util::writeLog('core', __METHOD__.'Cannot delete tag '.$name |
|
| 767 | 767 | . ': not found.', ILogger::ERROR); |
| 768 | 768 | } |
| 769 | - if(!is_null($id) && $id !== false) { |
|
| 769 | + if (!is_null($id) && $id !== false) { |
|
| 770 | 770 | try { |
| 771 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
| 771 | + $sql = 'DELETE FROM `'.self::RELATION_TABLE.'` ' |
|
| 772 | 772 | . 'WHERE `categoryid` = ?'; |
| 773 | 773 | $stmt = \OC_DB::prepare($sql); |
| 774 | 774 | $result = $stmt->execute(array($id)); |
| 775 | 775 | if ($result === null) { |
| 776 | 776 | \OCP\Util::writeLog('core', |
| 777 | - __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), |
|
| 777 | + __METHOD__.'DB error: '.\OC::$server->getDatabaseConnection()->getError(), |
|
| 778 | 778 | ILogger::ERROR); |
| 779 | 779 | return false; |
| 780 | 780 | } |
| 781 | - } catch(\Exception $e) { |
|
| 781 | + } catch (\Exception $e) { |
|
| 782 | 782 | \OC::$server->getLogger()->logException($e, [ |
| 783 | 783 | 'message' => __METHOD__, |
| 784 | 784 | 'level' => ILogger::ERROR, |
@@ -792,8 +792,8 @@ discard block |
||
| 792 | 792 | } |
| 793 | 793 | |
| 794 | 794 | // case-insensitive array_search |
| 795 | - protected function array_searchi($needle, $haystack, $mem='getName') { |
|
| 796 | - if(!is_array($haystack)) { |
|
| 795 | + protected function array_searchi($needle, $haystack, $mem = 'getName') { |
|
| 796 | + if (!is_array($haystack)) { |
|
| 797 | 797 | return false; |
| 798 | 798 | } |
| 799 | 799 | return array_search(strtolower($needle), array_map( |
@@ -103,7 +103,7 @@ |
||
| 103 | 103 | /** |
| 104 | 104 | * Appends a variable |
| 105 | 105 | * @param string $key key |
| 106 | - * @param mixed $value value |
|
| 106 | + * @param string $value value |
|
| 107 | 107 | * @return boolean|null |
| 108 | 108 | * |
| 109 | 109 | * This function assigns a variable in an array context. If the key already |
@@ -114,8 +114,7 @@ discard block |
||
| 114 | 114 | public function append( $key, $value ) { |
| 115 | 115 | if( array_key_exists( $key, $this->vars )) { |
| 116 | 116 | $this->vars[$key][] = $value; |
| 117 | - } |
|
| 118 | - else{ |
|
| 117 | + } else{ |
|
| 119 | 118 | $this->vars[$key] = array( $value ); |
| 120 | 119 | } |
| 121 | 120 | } |
@@ -130,8 +129,7 @@ discard block |
||
| 130 | 129 | $data = $this->fetchPage(); |
| 131 | 130 | if( $data === false ) { |
| 132 | 131 | return false; |
| 133 | - } |
|
| 134 | - else{ |
|
| 132 | + } else{ |
|
| 135 | 133 | print $data; |
| 136 | 134 | return true; |
| 137 | 135 | } |
@@ -31,161 +31,161 @@ |
||
| 31 | 31 | use OCP\Defaults; |
| 32 | 32 | |
| 33 | 33 | class Base { |
| 34 | - private $template; // The template |
|
| 35 | - private $vars; // Vars |
|
| 36 | - |
|
| 37 | - /** @var \OCP\IL10N */ |
|
| 38 | - private $l10n; |
|
| 39 | - |
|
| 40 | - /** @var Defaults */ |
|
| 41 | - private $theme; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param string $template |
|
| 45 | - * @param string $requestToken |
|
| 46 | - * @param \OCP\IL10N $l10n |
|
| 47 | - * @param Defaults $theme |
|
| 48 | - */ |
|
| 49 | - public function __construct($template, $requestToken, $l10n, $theme ) { |
|
| 50 | - $this->vars = array(); |
|
| 51 | - $this->vars['requesttoken'] = $requestToken; |
|
| 52 | - $this->l10n = $l10n; |
|
| 53 | - $this->template = $template; |
|
| 54 | - $this->theme = $theme; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $serverRoot |
|
| 59 | - * @param string|false $app_dir |
|
| 60 | - * @param string $theme |
|
| 61 | - * @param string $app |
|
| 62 | - * @return string[] |
|
| 63 | - */ |
|
| 64 | - protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
| 65 | - // Check if the app is in the app folder or in the root |
|
| 66 | - if( file_exists($app_dir.'/templates/' )) { |
|
| 67 | - return [ |
|
| 68 | - $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
| 69 | - $app_dir.'/templates/', |
|
| 70 | - ]; |
|
| 71 | - } |
|
| 72 | - return [ |
|
| 73 | - $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
| 74 | - $serverRoot.'/'.$app.'/templates/', |
|
| 75 | - ]; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param string $serverRoot |
|
| 80 | - * @param string $theme |
|
| 81 | - * @return string[] |
|
| 82 | - */ |
|
| 83 | - protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
| 84 | - return [ |
|
| 85 | - $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
| 86 | - $serverRoot.'/core/templates/', |
|
| 87 | - ]; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Assign variables |
|
| 92 | - * @param string $key key |
|
| 93 | - * @param array|bool|integer|string $value value |
|
| 94 | - * @return bool |
|
| 95 | - * |
|
| 96 | - * This function assigns a variable. It can be accessed via $_[$key] in |
|
| 97 | - * the template. |
|
| 98 | - * |
|
| 99 | - * If the key existed before, it will be overwritten |
|
| 100 | - */ |
|
| 101 | - public function assign( $key, $value) { |
|
| 102 | - $this->vars[$key] = $value; |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Appends a variable |
|
| 108 | - * @param string $key key |
|
| 109 | - * @param mixed $value value |
|
| 110 | - * @return boolean|null |
|
| 111 | - * |
|
| 112 | - * This function assigns a variable in an array context. If the key already |
|
| 113 | - * exists, the value will be appended. It can be accessed via |
|
| 114 | - * $_[$key][$position] in the template. |
|
| 115 | - */ |
|
| 116 | - public function append( $key, $value ) { |
|
| 117 | - if( array_key_exists( $key, $this->vars )) { |
|
| 118 | - $this->vars[$key][] = $value; |
|
| 119 | - } |
|
| 120 | - else{ |
|
| 121 | - $this->vars[$key] = array( $value ); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Prints the proceeded template |
|
| 127 | - * @return bool |
|
| 128 | - * |
|
| 129 | - * This function proceeds the template and prints its output. |
|
| 130 | - */ |
|
| 131 | - public function printPage() { |
|
| 132 | - $data = $this->fetchPage(); |
|
| 133 | - if( $data === false ) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - else{ |
|
| 137 | - print $data; |
|
| 138 | - return true; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Process the template |
|
| 144 | - * |
|
| 145 | - * @param array|null $additionalParams |
|
| 146 | - * @return string This function processes the template. |
|
| 147 | - * |
|
| 148 | - * This function processes the template. |
|
| 149 | - */ |
|
| 150 | - public function fetchPage($additionalParams = null) { |
|
| 151 | - return $this->load($this->template, $additionalParams); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * doing the actual work |
|
| 156 | - * |
|
| 157 | - * @param string $file |
|
| 158 | - * @param array|null $additionalParams |
|
| 159 | - * @return string content |
|
| 160 | - * |
|
| 161 | - * Includes the template file, fetches its output |
|
| 162 | - */ |
|
| 163 | - protected function load($file, $additionalParams = null) { |
|
| 164 | - // Register the variables |
|
| 165 | - $_ = $this->vars; |
|
| 166 | - $l = $this->l10n; |
|
| 167 | - $theme = $this->theme; |
|
| 168 | - |
|
| 169 | - if(!is_null($additionalParams)) { |
|
| 170 | - $_ = array_merge( $additionalParams, $this->vars ); |
|
| 171 | - foreach ($_ as $var => $value) { |
|
| 172 | - ${$var} = $value; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Include |
|
| 177 | - ob_start(); |
|
| 178 | - try { |
|
| 179 | - include $file; |
|
| 180 | - $data = ob_get_contents(); |
|
| 181 | - } catch (\Exception $e) { |
|
| 182 | - @ob_end_clean(); |
|
| 183 | - throw $e; |
|
| 184 | - } |
|
| 185 | - @ob_end_clean(); |
|
| 186 | - |
|
| 187 | - // Return data |
|
| 188 | - return $data; |
|
| 189 | - } |
|
| 34 | + private $template; // The template |
|
| 35 | + private $vars; // Vars |
|
| 36 | + |
|
| 37 | + /** @var \OCP\IL10N */ |
|
| 38 | + private $l10n; |
|
| 39 | + |
|
| 40 | + /** @var Defaults */ |
|
| 41 | + private $theme; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param string $template |
|
| 45 | + * @param string $requestToken |
|
| 46 | + * @param \OCP\IL10N $l10n |
|
| 47 | + * @param Defaults $theme |
|
| 48 | + */ |
|
| 49 | + public function __construct($template, $requestToken, $l10n, $theme ) { |
|
| 50 | + $this->vars = array(); |
|
| 51 | + $this->vars['requesttoken'] = $requestToken; |
|
| 52 | + $this->l10n = $l10n; |
|
| 53 | + $this->template = $template; |
|
| 54 | + $this->theme = $theme; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $serverRoot |
|
| 59 | + * @param string|false $app_dir |
|
| 60 | + * @param string $theme |
|
| 61 | + * @param string $app |
|
| 62 | + * @return string[] |
|
| 63 | + */ |
|
| 64 | + protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
| 65 | + // Check if the app is in the app folder or in the root |
|
| 66 | + if( file_exists($app_dir.'/templates/' )) { |
|
| 67 | + return [ |
|
| 68 | + $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
| 69 | + $app_dir.'/templates/', |
|
| 70 | + ]; |
|
| 71 | + } |
|
| 72 | + return [ |
|
| 73 | + $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
| 74 | + $serverRoot.'/'.$app.'/templates/', |
|
| 75 | + ]; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param string $serverRoot |
|
| 80 | + * @param string $theme |
|
| 81 | + * @return string[] |
|
| 82 | + */ |
|
| 83 | + protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
| 84 | + return [ |
|
| 85 | + $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
| 86 | + $serverRoot.'/core/templates/', |
|
| 87 | + ]; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Assign variables |
|
| 92 | + * @param string $key key |
|
| 93 | + * @param array|bool|integer|string $value value |
|
| 94 | + * @return bool |
|
| 95 | + * |
|
| 96 | + * This function assigns a variable. It can be accessed via $_[$key] in |
|
| 97 | + * the template. |
|
| 98 | + * |
|
| 99 | + * If the key existed before, it will be overwritten |
|
| 100 | + */ |
|
| 101 | + public function assign( $key, $value) { |
|
| 102 | + $this->vars[$key] = $value; |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Appends a variable |
|
| 108 | + * @param string $key key |
|
| 109 | + * @param mixed $value value |
|
| 110 | + * @return boolean|null |
|
| 111 | + * |
|
| 112 | + * This function assigns a variable in an array context. If the key already |
|
| 113 | + * exists, the value will be appended. It can be accessed via |
|
| 114 | + * $_[$key][$position] in the template. |
|
| 115 | + */ |
|
| 116 | + public function append( $key, $value ) { |
|
| 117 | + if( array_key_exists( $key, $this->vars )) { |
|
| 118 | + $this->vars[$key][] = $value; |
|
| 119 | + } |
|
| 120 | + else{ |
|
| 121 | + $this->vars[$key] = array( $value ); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Prints the proceeded template |
|
| 127 | + * @return bool |
|
| 128 | + * |
|
| 129 | + * This function proceeds the template and prints its output. |
|
| 130 | + */ |
|
| 131 | + public function printPage() { |
|
| 132 | + $data = $this->fetchPage(); |
|
| 133 | + if( $data === false ) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + else{ |
|
| 137 | + print $data; |
|
| 138 | + return true; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Process the template |
|
| 144 | + * |
|
| 145 | + * @param array|null $additionalParams |
|
| 146 | + * @return string This function processes the template. |
|
| 147 | + * |
|
| 148 | + * This function processes the template. |
|
| 149 | + */ |
|
| 150 | + public function fetchPage($additionalParams = null) { |
|
| 151 | + return $this->load($this->template, $additionalParams); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * doing the actual work |
|
| 156 | + * |
|
| 157 | + * @param string $file |
|
| 158 | + * @param array|null $additionalParams |
|
| 159 | + * @return string content |
|
| 160 | + * |
|
| 161 | + * Includes the template file, fetches its output |
|
| 162 | + */ |
|
| 163 | + protected function load($file, $additionalParams = null) { |
|
| 164 | + // Register the variables |
|
| 165 | + $_ = $this->vars; |
|
| 166 | + $l = $this->l10n; |
|
| 167 | + $theme = $this->theme; |
|
| 168 | + |
|
| 169 | + if(!is_null($additionalParams)) { |
|
| 170 | + $_ = array_merge( $additionalParams, $this->vars ); |
|
| 171 | + foreach ($_ as $var => $value) { |
|
| 172 | + ${$var} = $value; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Include |
|
| 177 | + ob_start(); |
|
| 178 | + try { |
|
| 179 | + include $file; |
|
| 180 | + $data = ob_get_contents(); |
|
| 181 | + } catch (\Exception $e) { |
|
| 182 | + @ob_end_clean(); |
|
| 183 | + throw $e; |
|
| 184 | + } |
|
| 185 | + @ob_end_clean(); |
|
| 186 | + |
|
| 187 | + // Return data |
|
| 188 | + return $data; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | 191 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @param \OCP\IL10N $l10n |
| 47 | 47 | * @param Defaults $theme |
| 48 | 48 | */ |
| 49 | - public function __construct($template, $requestToken, $l10n, $theme ) { |
|
| 49 | + public function __construct($template, $requestToken, $l10n, $theme) { |
|
| 50 | 50 | $this->vars = array(); |
| 51 | 51 | $this->vars['requesttoken'] = $requestToken; |
| 52 | 52 | $this->l10n = $l10n; |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
| 65 | 65 | // Check if the app is in the app folder or in the root |
| 66 | - if( file_exists($app_dir.'/templates/' )) { |
|
| 66 | + if (file_exists($app_dir.'/templates/')) { |
|
| 67 | 67 | return [ |
| 68 | 68 | $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
| 69 | 69 | $app_dir.'/templates/', |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * If the key existed before, it will be overwritten |
| 100 | 100 | */ |
| 101 | - public function assign( $key, $value) { |
|
| 101 | + public function assign($key, $value) { |
|
| 102 | 102 | $this->vars[$key] = $value; |
| 103 | 103 | return true; |
| 104 | 104 | } |
@@ -113,12 +113,12 @@ discard block |
||
| 113 | 113 | * exists, the value will be appended. It can be accessed via |
| 114 | 114 | * $_[$key][$position] in the template. |
| 115 | 115 | */ |
| 116 | - public function append( $key, $value ) { |
|
| 117 | - if( array_key_exists( $key, $this->vars )) { |
|
| 116 | + public function append($key, $value) { |
|
| 117 | + if (array_key_exists($key, $this->vars)) { |
|
| 118 | 118 | $this->vars[$key][] = $value; |
| 119 | 119 | } |
| 120 | - else{ |
|
| 121 | - $this->vars[$key] = array( $value ); |
|
| 120 | + else { |
|
| 121 | + $this->vars[$key] = array($value); |
|
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
@@ -130,10 +130,10 @@ discard block |
||
| 130 | 130 | */ |
| 131 | 131 | public function printPage() { |
| 132 | 132 | $data = $this->fetchPage(); |
| 133 | - if( $data === false ) { |
|
| 133 | + if ($data === false) { |
|
| 134 | 134 | return false; |
| 135 | 135 | } |
| 136 | - else{ |
|
| 136 | + else { |
|
| 137 | 137 | print $data; |
| 138 | 138 | return true; |
| 139 | 139 | } |
@@ -166,8 +166,8 @@ discard block |
||
| 166 | 166 | $l = $this->l10n; |
| 167 | 167 | $theme = $this->theme; |
| 168 | 168 | |
| 169 | - if(!is_null($additionalParams)) { |
|
| 170 | - $_ = array_merge( $additionalParams, $this->vars ); |
|
| 169 | + if (!is_null($additionalParams)) { |
|
| 170 | + $_ = array_merge($additionalParams, $this->vars); |
|
| 171 | 171 | foreach ($_ as $var => $value) { |
| 172 | 172 | ${$var} = $value; |
| 173 | 173 | } |
@@ -32,18 +32,21 @@ discard block |
||
| 32 | 32 | /** |
| 33 | 33 | * @param string $message |
| 34 | 34 | * @since 9.1.0 |
| 35 | + * @return void |
|
| 35 | 36 | */ |
| 36 | 37 | public function info($message); |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | 40 | * @param string $message |
| 40 | 41 | * @since 9.1.0 |
| 42 | + * @return void |
|
| 41 | 43 | */ |
| 42 | 44 | public function warning($message); |
| 43 | 45 | |
| 44 | 46 | /** |
| 45 | 47 | * @param int $max |
| 46 | 48 | * @since 9.1.0 |
| 49 | + * @return void |
|
| 47 | 50 | */ |
| 48 | 51 | public function startProgress($max = 0); |
| 49 | 52 | |
@@ -51,12 +54,13 @@ discard block |
||
| 51 | 54 | * @param int $step |
| 52 | 55 | * @param string $description |
| 53 | 56 | * @since 9.1.0 |
| 57 | + * @return void |
|
| 54 | 58 | */ |
| 55 | 59 | public function advance($step = 1, $description = ''); |
| 56 | 60 | |
| 57 | 61 | /** |
| 58 | - * @param int $max |
|
| 59 | 62 | * @since 9.1.0 |
| 63 | + * @return void |
|
| 60 | 64 | */ |
| 61 | 65 | public function finishProgress(); |
| 62 | 66 | |
@@ -30,35 +30,35 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | interface IOutput { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param string $message |
|
| 35 | - * @since 9.1.0 |
|
| 36 | - */ |
|
| 37 | - public function info($message); |
|
| 33 | + /** |
|
| 34 | + * @param string $message |
|
| 35 | + * @since 9.1.0 |
|
| 36 | + */ |
|
| 37 | + public function info($message); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param string $message |
|
| 41 | - * @since 9.1.0 |
|
| 42 | - */ |
|
| 43 | - public function warning($message); |
|
| 39 | + /** |
|
| 40 | + * @param string $message |
|
| 41 | + * @since 9.1.0 |
|
| 42 | + */ |
|
| 43 | + public function warning($message); |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param int $max |
|
| 47 | - * @since 9.1.0 |
|
| 48 | - */ |
|
| 49 | - public function startProgress($max = 0); |
|
| 45 | + /** |
|
| 46 | + * @param int $max |
|
| 47 | + * @since 9.1.0 |
|
| 48 | + */ |
|
| 49 | + public function startProgress($max = 0); |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param int $step |
|
| 53 | - * @param string $description |
|
| 54 | - * @since 9.1.0 |
|
| 55 | - */ |
|
| 56 | - public function advance($step = 1, $description = ''); |
|
| 51 | + /** |
|
| 52 | + * @param int $step |
|
| 53 | + * @param string $description |
|
| 54 | + * @since 9.1.0 |
|
| 55 | + */ |
|
| 56 | + public function advance($step = 1, $description = ''); |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param int $max |
|
| 60 | - * @since 9.1.0 |
|
| 61 | - */ |
|
| 62 | - public function finishProgress(); |
|
| 58 | + /** |
|
| 59 | + * @param int $max |
|
| 60 | + * @since 9.1.0 |
|
| 61 | + */ |
|
| 62 | + public function finishProgress(); |
|
| 63 | 63 | |
| 64 | 64 | } |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | * and does not take the chroot into account ) |
| 201 | 201 | * |
| 202 | 202 | * @param string $path |
| 203 | - * @return \OCP\Files\Mount\IMountPoint |
|
| 203 | + * @return Mount\MountPoint|null |
|
| 204 | 204 | */ |
| 205 | 205 | public function getMount($path) { |
| 206 | 206 | return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
@@ -963,7 +963,7 @@ discard block |
||
| 963 | 963 | |
| 964 | 964 | /** |
| 965 | 965 | * @param string $path |
| 966 | - * @return bool|string |
|
| 966 | + * @return string|false |
|
| 967 | 967 | * @throws \OCP\Files\InvalidPathException |
| 968 | 968 | */ |
| 969 | 969 | public function toTmpFile($path) { |
@@ -1078,7 +1078,7 @@ discard block |
||
| 1078 | 1078 | * @param string $path |
| 1079 | 1079 | * @param array $hooks (optional) |
| 1080 | 1080 | * @param mixed $extraParam (optional) |
| 1081 | - * @return mixed |
|
| 1081 | + * @return string |
|
| 1082 | 1082 | * @throws \Exception |
| 1083 | 1083 | * |
| 1084 | 1084 | * This method takes requests for basic filesystem functions (e.g. reading & writing |
@@ -2086,7 +2086,7 @@ discard block |
||
| 2086 | 2086 | |
| 2087 | 2087 | /** |
| 2088 | 2088 | * @param string $filename |
| 2089 | - * @return array |
|
| 2089 | + * @return string[] |
|
| 2090 | 2090 | * @throws \OC\User\NoUserException |
| 2091 | 2091 | * @throws NotFoundException |
| 2092 | 2092 | */ |
@@ -81,2086 +81,2086 @@ |
||
| 81 | 81 | * \OC\Files\Storage\Storage object |
| 82 | 82 | */ |
| 83 | 83 | class View { |
| 84 | - /** @var string */ |
|
| 85 | - private $fakeRoot = ''; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @var \OCP\Lock\ILockingProvider |
|
| 89 | - */ |
|
| 90 | - protected $lockingProvider; |
|
| 91 | - |
|
| 92 | - private $lockingEnabled; |
|
| 93 | - |
|
| 94 | - private $updaterEnabled = true; |
|
| 95 | - |
|
| 96 | - /** @var \OC\User\Manager */ |
|
| 97 | - private $userManager; |
|
| 98 | - |
|
| 99 | - /** @var \OCP\ILogger */ |
|
| 100 | - private $logger; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $root |
|
| 104 | - * @throws \Exception If $root contains an invalid path |
|
| 105 | - */ |
|
| 106 | - public function __construct($root = '') { |
|
| 107 | - if (is_null($root)) { |
|
| 108 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 109 | - } |
|
| 110 | - if (!Filesystem::isValidPath($root)) { |
|
| 111 | - throw new \Exception(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $this->fakeRoot = $root; |
|
| 115 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 116 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 117 | - $this->userManager = \OC::$server->getUserManager(); |
|
| 118 | - $this->logger = \OC::$server->getLogger(); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function getAbsolutePath($path = '/') { |
|
| 122 | - if ($path === null) { |
|
| 123 | - return null; |
|
| 124 | - } |
|
| 125 | - $this->assertPathLength($path); |
|
| 126 | - if ($path === '') { |
|
| 127 | - $path = '/'; |
|
| 128 | - } |
|
| 129 | - if ($path[0] !== '/') { |
|
| 130 | - $path = '/' . $path; |
|
| 131 | - } |
|
| 132 | - return $this->fakeRoot . $path; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * change the root to a fake root |
|
| 137 | - * |
|
| 138 | - * @param string $fakeRoot |
|
| 139 | - * @return boolean|null |
|
| 140 | - */ |
|
| 141 | - public function chroot($fakeRoot) { |
|
| 142 | - if (!$fakeRoot == '') { |
|
| 143 | - if ($fakeRoot[0] !== '/') { |
|
| 144 | - $fakeRoot = '/' . $fakeRoot; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - $this->fakeRoot = $fakeRoot; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * get the fake root |
|
| 152 | - * |
|
| 153 | - * @return string |
|
| 154 | - */ |
|
| 155 | - public function getRoot() { |
|
| 156 | - return $this->fakeRoot; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * get path relative to the root of the view |
|
| 161 | - * |
|
| 162 | - * @param string $path |
|
| 163 | - * @return string |
|
| 164 | - */ |
|
| 165 | - public function getRelativePath($path) { |
|
| 166 | - $this->assertPathLength($path); |
|
| 167 | - if ($this->fakeRoot == '') { |
|
| 168 | - return $path; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 172 | - return '/'; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - // missing slashes can cause wrong matches! |
|
| 176 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 177 | - |
|
| 178 | - if (strpos($path, $root) !== 0) { |
|
| 179 | - return null; |
|
| 180 | - } else { |
|
| 181 | - $path = substr($path, strlen($this->fakeRoot)); |
|
| 182 | - if (strlen($path) === 0) { |
|
| 183 | - return '/'; |
|
| 184 | - } else { |
|
| 185 | - return $path; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * get the mountpoint of the storage object for a path |
|
| 192 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 193 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 194 | - * and does not take the chroot into account ) |
|
| 195 | - * |
|
| 196 | - * @param string $path |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - public function getMountPoint($path) { |
|
| 200 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * get the mountpoint of the storage object for a path |
|
| 205 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 206 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 207 | - * and does not take the chroot into account ) |
|
| 208 | - * |
|
| 209 | - * @param string $path |
|
| 210 | - * @return \OCP\Files\Mount\IMountPoint |
|
| 211 | - */ |
|
| 212 | - public function getMount($path) { |
|
| 213 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * resolve a path to a storage and internal path |
|
| 218 | - * |
|
| 219 | - * @param string $path |
|
| 220 | - * @return array an array consisting of the storage and the internal path |
|
| 221 | - */ |
|
| 222 | - public function resolvePath($path) { |
|
| 223 | - $a = $this->getAbsolutePath($path); |
|
| 224 | - $p = Filesystem::normalizePath($a); |
|
| 225 | - return Filesystem::resolvePath($p); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * return the path to a local version of the file |
|
| 230 | - * we need this because we can't know if a file is stored local or not from |
|
| 231 | - * outside the filestorage and for some purposes a local file is needed |
|
| 232 | - * |
|
| 233 | - * @param string $path |
|
| 234 | - * @return string |
|
| 235 | - */ |
|
| 236 | - public function getLocalFile($path) { |
|
| 237 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 238 | - $path = $this->getAbsolutePath($path); |
|
| 239 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 240 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 241 | - return $storage->getLocalFile($internalPath); |
|
| 242 | - } else { |
|
| 243 | - return null; |
|
| 244 | - } |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @param string $path |
|
| 249 | - * @return string |
|
| 250 | - */ |
|
| 251 | - public function getLocalFolder($path) { |
|
| 252 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 253 | - $path = $this->getAbsolutePath($path); |
|
| 254 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 255 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 256 | - return $storage->getLocalFolder($internalPath); |
|
| 257 | - } else { |
|
| 258 | - return null; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * the following functions operate with arguments and return values identical |
|
| 264 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 265 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 266 | - */ |
|
| 267 | - public function mkdir($path) { |
|
| 268 | - return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * remove mount point |
|
| 273 | - * |
|
| 274 | - * @param \OC\Files\Mount\MoveableMount $mount |
|
| 275 | - * @param string $path relative to data/ |
|
| 276 | - * @return boolean |
|
| 277 | - */ |
|
| 278 | - protected function removeMount($mount, $path) { |
|
| 279 | - if ($mount instanceof MoveableMount) { |
|
| 280 | - // cut of /user/files to get the relative path to data/user/files |
|
| 281 | - $pathParts = explode('/', $path, 4); |
|
| 282 | - $relPath = '/' . $pathParts[3]; |
|
| 283 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 284 | - \OC_Hook::emit( |
|
| 285 | - Filesystem::CLASSNAME, "umount", |
|
| 286 | - array(Filesystem::signal_param_path => $relPath) |
|
| 287 | - ); |
|
| 288 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 289 | - $result = $mount->removeMount(); |
|
| 290 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 291 | - if ($result) { |
|
| 292 | - \OC_Hook::emit( |
|
| 293 | - Filesystem::CLASSNAME, "post_umount", |
|
| 294 | - array(Filesystem::signal_param_path => $relPath) |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 298 | - return $result; |
|
| 299 | - } else { |
|
| 300 | - // do not allow deleting the storage's root / the mount point |
|
| 301 | - // because for some storages it might delete the whole contents |
|
| 302 | - // but isn't supposed to work that way |
|
| 303 | - return false; |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - public function disableCacheUpdate() { |
|
| 308 | - $this->updaterEnabled = false; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - public function enableCacheUpdate() { |
|
| 312 | - $this->updaterEnabled = true; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 316 | - if ($this->updaterEnabled) { |
|
| 317 | - if (is_null($time)) { |
|
| 318 | - $time = time(); |
|
| 319 | - } |
|
| 320 | - $storage->getUpdater()->update($internalPath, $time); |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 325 | - if ($this->updaterEnabled) { |
|
| 326 | - $storage->getUpdater()->remove($internalPath); |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 331 | - if ($this->updaterEnabled) { |
|
| 332 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * @param string $path |
|
| 338 | - * @return bool|mixed |
|
| 339 | - */ |
|
| 340 | - public function rmdir($path) { |
|
| 341 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 342 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 343 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 344 | - return $this->removeMount($mount, $absolutePath); |
|
| 345 | - } |
|
| 346 | - if ($this->is_dir($path)) { |
|
| 347 | - $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
| 348 | - } else { |
|
| 349 | - $result = false; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 353 | - $storage = $mount->getStorage(); |
|
| 354 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 355 | - $storage->getUpdater()->remove($internalPath); |
|
| 356 | - } |
|
| 357 | - return $result; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @param string $path |
|
| 362 | - * @return resource |
|
| 363 | - */ |
|
| 364 | - public function opendir($path) { |
|
| 365 | - return $this->basicOperation('opendir', $path, array('read')); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * @param string $path |
|
| 370 | - * @return bool|mixed |
|
| 371 | - */ |
|
| 372 | - public function is_dir($path) { |
|
| 373 | - if ($path == '/') { |
|
| 374 | - return true; |
|
| 375 | - } |
|
| 376 | - return $this->basicOperation('is_dir', $path); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * @param string $path |
|
| 381 | - * @return bool|mixed |
|
| 382 | - */ |
|
| 383 | - public function is_file($path) { |
|
| 384 | - if ($path == '/') { |
|
| 385 | - return false; |
|
| 386 | - } |
|
| 387 | - return $this->basicOperation('is_file', $path); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * @param string $path |
|
| 392 | - * @return mixed |
|
| 393 | - */ |
|
| 394 | - public function stat($path) { |
|
| 395 | - return $this->basicOperation('stat', $path); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * @param string $path |
|
| 400 | - * @return mixed |
|
| 401 | - */ |
|
| 402 | - public function filetype($path) { |
|
| 403 | - return $this->basicOperation('filetype', $path); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * @param string $path |
|
| 408 | - * @return mixed |
|
| 409 | - */ |
|
| 410 | - public function filesize($path) { |
|
| 411 | - return $this->basicOperation('filesize', $path); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * @param string $path |
|
| 416 | - * @return bool|mixed |
|
| 417 | - * @throws \OCP\Files\InvalidPathException |
|
| 418 | - */ |
|
| 419 | - public function readfile($path) { |
|
| 420 | - $this->assertPathLength($path); |
|
| 421 | - @ob_end_clean(); |
|
| 422 | - $handle = $this->fopen($path, 'rb'); |
|
| 423 | - if ($handle) { |
|
| 424 | - $chunkSize = 8192; // 8 kB chunks |
|
| 425 | - while (!feof($handle)) { |
|
| 426 | - echo fread($handle, $chunkSize); |
|
| 427 | - flush(); |
|
| 428 | - } |
|
| 429 | - fclose($handle); |
|
| 430 | - return $this->filesize($path); |
|
| 431 | - } |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * @param string $path |
|
| 437 | - * @param int $from |
|
| 438 | - * @param int $to |
|
| 439 | - * @return bool|mixed |
|
| 440 | - * @throws \OCP\Files\InvalidPathException |
|
| 441 | - * @throws \OCP\Files\UnseekableException |
|
| 442 | - */ |
|
| 443 | - public function readfilePart($path, $from, $to) { |
|
| 444 | - $this->assertPathLength($path); |
|
| 445 | - @ob_end_clean(); |
|
| 446 | - $handle = $this->fopen($path, 'rb'); |
|
| 447 | - if ($handle) { |
|
| 448 | - $chunkSize = 8192; // 8 kB chunks |
|
| 449 | - $startReading = true; |
|
| 450 | - |
|
| 451 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 452 | - // forward file handle via chunked fread because fseek seem to have failed |
|
| 453 | - |
|
| 454 | - $end = $from + 1; |
|
| 455 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 456 | - $len = $from - ftell($handle); |
|
| 457 | - if ($len > $chunkSize) { |
|
| 458 | - $len = $chunkSize; |
|
| 459 | - } |
|
| 460 | - $result = fread($handle, $len); |
|
| 461 | - |
|
| 462 | - if ($result === false) { |
|
| 463 | - $startReading = false; |
|
| 464 | - break; |
|
| 465 | - } |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - if ($startReading) { |
|
| 470 | - $end = $to + 1; |
|
| 471 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 472 | - $len = $end - ftell($handle); |
|
| 473 | - if ($len > $chunkSize) { |
|
| 474 | - $len = $chunkSize; |
|
| 475 | - } |
|
| 476 | - echo fread($handle, $len); |
|
| 477 | - flush(); |
|
| 478 | - } |
|
| 479 | - return ftell($handle) - $from; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 483 | - } |
|
| 484 | - return false; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * @param string $path |
|
| 489 | - * @return mixed |
|
| 490 | - */ |
|
| 491 | - public function isCreatable($path) { |
|
| 492 | - return $this->basicOperation('isCreatable', $path); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * @param string $path |
|
| 497 | - * @return mixed |
|
| 498 | - */ |
|
| 499 | - public function isReadable($path) { |
|
| 500 | - return $this->basicOperation('isReadable', $path); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * @param string $path |
|
| 505 | - * @return mixed |
|
| 506 | - */ |
|
| 507 | - public function isUpdatable($path) { |
|
| 508 | - return $this->basicOperation('isUpdatable', $path); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * @param string $path |
|
| 513 | - * @return bool|mixed |
|
| 514 | - */ |
|
| 515 | - public function isDeletable($path) { |
|
| 516 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 517 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 518 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 519 | - return $mount instanceof MoveableMount; |
|
| 520 | - } |
|
| 521 | - return $this->basicOperation('isDeletable', $path); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * @param string $path |
|
| 526 | - * @return mixed |
|
| 527 | - */ |
|
| 528 | - public function isSharable($path) { |
|
| 529 | - return $this->basicOperation('isSharable', $path); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * @param string $path |
|
| 534 | - * @return bool|mixed |
|
| 535 | - */ |
|
| 536 | - public function file_exists($path) { |
|
| 537 | - if ($path == '/') { |
|
| 538 | - return true; |
|
| 539 | - } |
|
| 540 | - return $this->basicOperation('file_exists', $path); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - /** |
|
| 544 | - * @param string $path |
|
| 545 | - * @return mixed |
|
| 546 | - */ |
|
| 547 | - public function filemtime($path) { |
|
| 548 | - return $this->basicOperation('filemtime', $path); |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * @param string $path |
|
| 553 | - * @param int|string $mtime |
|
| 554 | - * @return bool |
|
| 555 | - */ |
|
| 556 | - public function touch($path, $mtime = null) { |
|
| 557 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 558 | - $mtime = strtotime($mtime); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - $hooks = array('touch'); |
|
| 562 | - |
|
| 563 | - if (!$this->file_exists($path)) { |
|
| 564 | - $hooks[] = 'create'; |
|
| 565 | - $hooks[] = 'write'; |
|
| 566 | - } |
|
| 567 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 568 | - if (!$result) { |
|
| 569 | - // If create file fails because of permissions on external storage like SMB folders, |
|
| 570 | - // check file exists and return false if not. |
|
| 571 | - if (!$this->file_exists($path)) { |
|
| 572 | - return false; |
|
| 573 | - } |
|
| 574 | - if (is_null($mtime)) { |
|
| 575 | - $mtime = time(); |
|
| 576 | - } |
|
| 577 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 578 | - $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
| 579 | - } |
|
| 580 | - return true; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * @param string $path |
|
| 585 | - * @return mixed |
|
| 586 | - */ |
|
| 587 | - public function file_get_contents($path) { |
|
| 588 | - return $this->basicOperation('file_get_contents', $path, array('read')); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - /** |
|
| 592 | - * @param bool $exists |
|
| 593 | - * @param string $path |
|
| 594 | - * @param bool $run |
|
| 595 | - */ |
|
| 596 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 597 | - if (!$exists) { |
|
| 598 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
| 599 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 600 | - Filesystem::signal_param_run => &$run, |
|
| 601 | - )); |
|
| 602 | - } else { |
|
| 603 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
| 604 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 605 | - Filesystem::signal_param_run => &$run, |
|
| 606 | - )); |
|
| 607 | - } |
|
| 608 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
| 609 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 610 | - Filesystem::signal_param_run => &$run, |
|
| 611 | - )); |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - /** |
|
| 615 | - * @param bool $exists |
|
| 616 | - * @param string $path |
|
| 617 | - */ |
|
| 618 | - protected function emit_file_hooks_post($exists, $path) { |
|
| 619 | - if (!$exists) { |
|
| 620 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
| 621 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 622 | - )); |
|
| 623 | - } else { |
|
| 624 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
| 625 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 626 | - )); |
|
| 627 | - } |
|
| 628 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
| 629 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 630 | - )); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * @param string $path |
|
| 635 | - * @param mixed $data |
|
| 636 | - * @return bool|mixed |
|
| 637 | - * @throws \Exception |
|
| 638 | - */ |
|
| 639 | - public function file_put_contents($path, $data) { |
|
| 640 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 641 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 642 | - if (Filesystem::isValidPath($path) |
|
| 643 | - and !Filesystem::isFileBlacklisted($path) |
|
| 644 | - ) { |
|
| 645 | - $path = $this->getRelativePath($absolutePath); |
|
| 646 | - |
|
| 647 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 648 | - |
|
| 649 | - $exists = $this->file_exists($path); |
|
| 650 | - $run = true; |
|
| 651 | - if ($this->shouldEmitHooks($path)) { |
|
| 652 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 653 | - } |
|
| 654 | - if (!$run) { |
|
| 655 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 656 | - return false; |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 660 | - |
|
| 661 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 662 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 663 | - $target = $storage->fopen($internalPath, 'w'); |
|
| 664 | - if ($target) { |
|
| 665 | - list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 666 | - fclose($target); |
|
| 667 | - fclose($data); |
|
| 668 | - |
|
| 669 | - $this->writeUpdate($storage, $internalPath); |
|
| 670 | - |
|
| 671 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 672 | - |
|
| 673 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 674 | - $this->emit_file_hooks_post($exists, $path); |
|
| 675 | - } |
|
| 676 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 677 | - return $result; |
|
| 678 | - } else { |
|
| 679 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 680 | - return false; |
|
| 681 | - } |
|
| 682 | - } else { |
|
| 683 | - return false; |
|
| 684 | - } |
|
| 685 | - } else { |
|
| 686 | - $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
| 687 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 688 | - } |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * @param string $path |
|
| 693 | - * @return bool|mixed |
|
| 694 | - */ |
|
| 695 | - public function unlink($path) { |
|
| 696 | - if ($path === '' || $path === '/') { |
|
| 697 | - // do not allow deleting the root |
|
| 698 | - return false; |
|
| 699 | - } |
|
| 700 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 701 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 702 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 703 | - if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
| 704 | - return $this->removeMount($mount, $absolutePath); |
|
| 705 | - } |
|
| 706 | - if ($this->is_dir($path)) { |
|
| 707 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 708 | - } else { |
|
| 709 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 710 | - } |
|
| 711 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 712 | - $storage = $mount->getStorage(); |
|
| 713 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 714 | - $storage->getUpdater()->remove($internalPath); |
|
| 715 | - return true; |
|
| 716 | - } else { |
|
| 717 | - return $result; |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - /** |
|
| 722 | - * @param string $directory |
|
| 723 | - * @return bool|mixed |
|
| 724 | - */ |
|
| 725 | - public function deleteAll($directory) { |
|
| 726 | - return $this->rmdir($directory); |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - /** |
|
| 730 | - * Rename/move a file or folder from the source path to target path. |
|
| 731 | - * |
|
| 732 | - * @param string $path1 source path |
|
| 733 | - * @param string $path2 target path |
|
| 734 | - * |
|
| 735 | - * @return bool|mixed |
|
| 736 | - */ |
|
| 737 | - public function rename($path1, $path2) { |
|
| 738 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 739 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 740 | - $result = false; |
|
| 741 | - if ( |
|
| 742 | - Filesystem::isValidPath($path2) |
|
| 743 | - and Filesystem::isValidPath($path1) |
|
| 744 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 745 | - ) { |
|
| 746 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 747 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 748 | - $exists = $this->file_exists($path2); |
|
| 749 | - |
|
| 750 | - if ($path1 == null or $path2 == null) { |
|
| 751 | - return false; |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 755 | - try { |
|
| 756 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 757 | - |
|
| 758 | - $run = true; |
|
| 759 | - if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 760 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 761 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 762 | - } elseif ($this->shouldEmitHooks($path1)) { |
|
| 763 | - \OC_Hook::emit( |
|
| 764 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 765 | - array( |
|
| 766 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 767 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 768 | - Filesystem::signal_param_run => &$run |
|
| 769 | - ) |
|
| 770 | - ); |
|
| 771 | - } |
|
| 772 | - if ($run) { |
|
| 773 | - $this->verifyPath(dirname($path2), basename($path2)); |
|
| 774 | - |
|
| 775 | - $manager = Filesystem::getMountManager(); |
|
| 776 | - $mount1 = $this->getMount($path1); |
|
| 777 | - $mount2 = $this->getMount($path2); |
|
| 778 | - $storage1 = $mount1->getStorage(); |
|
| 779 | - $storage2 = $mount2->getStorage(); |
|
| 780 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 781 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 782 | - |
|
| 783 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 784 | - try { |
|
| 785 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 786 | - |
|
| 787 | - if ($internalPath1 === '') { |
|
| 788 | - if ($mount1 instanceof MoveableMount) { |
|
| 789 | - if ($this->isTargetAllowed($absolutePath2)) { |
|
| 790 | - /** |
|
| 791 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 792 | - */ |
|
| 793 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
| 794 | - $result = $mount1->moveMount($absolutePath2); |
|
| 795 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 796 | - } else { |
|
| 797 | - $result = false; |
|
| 798 | - } |
|
| 799 | - } else { |
|
| 800 | - $result = false; |
|
| 801 | - } |
|
| 802 | - // moving a file/folder within the same mount point |
|
| 803 | - } elseif ($storage1 === $storage2) { |
|
| 804 | - if ($storage1) { |
|
| 805 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 806 | - } else { |
|
| 807 | - $result = false; |
|
| 808 | - } |
|
| 809 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 810 | - } else { |
|
| 811 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 815 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 816 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 817 | - } else if ($result) { |
|
| 818 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 819 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 820 | - } |
|
| 821 | - } |
|
| 822 | - } catch(\Exception $e) { |
|
| 823 | - throw $e; |
|
| 824 | - } finally { |
|
| 825 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 826 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 830 | - if ($this->shouldEmitHooks()) { |
|
| 831 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 832 | - } |
|
| 833 | - } elseif ($result) { |
|
| 834 | - if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 835 | - \OC_Hook::emit( |
|
| 836 | - Filesystem::CLASSNAME, |
|
| 837 | - Filesystem::signal_post_rename, |
|
| 838 | - array( |
|
| 839 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 840 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 841 | - ) |
|
| 842 | - ); |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - } catch(\Exception $e) { |
|
| 847 | - throw $e; |
|
| 848 | - } finally { |
|
| 849 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 850 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 851 | - } |
|
| 852 | - } |
|
| 853 | - return $result; |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * Copy a file/folder from the source path to target path |
|
| 858 | - * |
|
| 859 | - * @param string $path1 source path |
|
| 860 | - * @param string $path2 target path |
|
| 861 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 862 | - * |
|
| 863 | - * @return bool|mixed |
|
| 864 | - */ |
|
| 865 | - public function copy($path1, $path2, $preserveMtime = false) { |
|
| 866 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 867 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 868 | - $result = false; |
|
| 869 | - if ( |
|
| 870 | - Filesystem::isValidPath($path2) |
|
| 871 | - and Filesystem::isValidPath($path1) |
|
| 872 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 873 | - ) { |
|
| 874 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 875 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 876 | - |
|
| 877 | - if ($path1 == null or $path2 == null) { |
|
| 878 | - return false; |
|
| 879 | - } |
|
| 880 | - $run = true; |
|
| 881 | - |
|
| 882 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 883 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 884 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 885 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 886 | - |
|
| 887 | - try { |
|
| 888 | - |
|
| 889 | - $exists = $this->file_exists($path2); |
|
| 890 | - if ($this->shouldEmitHooks()) { |
|
| 891 | - \OC_Hook::emit( |
|
| 892 | - Filesystem::CLASSNAME, |
|
| 893 | - Filesystem::signal_copy, |
|
| 894 | - array( |
|
| 895 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 896 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 897 | - Filesystem::signal_param_run => &$run |
|
| 898 | - ) |
|
| 899 | - ); |
|
| 900 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 901 | - } |
|
| 902 | - if ($run) { |
|
| 903 | - $mount1 = $this->getMount($path1); |
|
| 904 | - $mount2 = $this->getMount($path2); |
|
| 905 | - $storage1 = $mount1->getStorage(); |
|
| 906 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 907 | - $storage2 = $mount2->getStorage(); |
|
| 908 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 909 | - |
|
| 910 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 911 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 912 | - |
|
| 913 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 914 | - if ($storage1) { |
|
| 915 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 916 | - } else { |
|
| 917 | - $result = false; |
|
| 918 | - } |
|
| 919 | - } else { |
|
| 920 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 924 | - |
|
| 925 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 926 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 927 | - |
|
| 928 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
| 929 | - \OC_Hook::emit( |
|
| 930 | - Filesystem::CLASSNAME, |
|
| 931 | - Filesystem::signal_post_copy, |
|
| 932 | - array( |
|
| 933 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 934 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 935 | - ) |
|
| 936 | - ); |
|
| 937 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - } |
|
| 941 | - } catch (\Exception $e) { |
|
| 942 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 943 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 944 | - throw $e; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 948 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 949 | - |
|
| 950 | - } |
|
| 951 | - return $result; |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * @param string $path |
|
| 956 | - * @param string $mode 'r' or 'w' |
|
| 957 | - * @return resource |
|
| 958 | - */ |
|
| 959 | - public function fopen($path, $mode) { |
|
| 960 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 961 | - $hooks = array(); |
|
| 962 | - switch ($mode) { |
|
| 963 | - case 'r': |
|
| 964 | - $hooks[] = 'read'; |
|
| 965 | - break; |
|
| 966 | - case 'r+': |
|
| 967 | - case 'w+': |
|
| 968 | - case 'x+': |
|
| 969 | - case 'a+': |
|
| 970 | - $hooks[] = 'read'; |
|
| 971 | - $hooks[] = 'write'; |
|
| 972 | - break; |
|
| 973 | - case 'w': |
|
| 974 | - case 'x': |
|
| 975 | - case 'a': |
|
| 976 | - $hooks[] = 'write'; |
|
| 977 | - break; |
|
| 978 | - default: |
|
| 979 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - if ($mode !== 'r' && $mode !== 'w') { |
|
| 983 | - \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
| 984 | - } |
|
| 985 | - |
|
| 986 | - return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - /** |
|
| 990 | - * @param string $path |
|
| 991 | - * @return bool|string |
|
| 992 | - * @throws \OCP\Files\InvalidPathException |
|
| 993 | - */ |
|
| 994 | - public function toTmpFile($path) { |
|
| 995 | - $this->assertPathLength($path); |
|
| 996 | - if (Filesystem::isValidPath($path)) { |
|
| 997 | - $source = $this->fopen($path, 'r'); |
|
| 998 | - if ($source) { |
|
| 999 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1000 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1001 | - file_put_contents($tmpFile, $source); |
|
| 1002 | - return $tmpFile; |
|
| 1003 | - } else { |
|
| 1004 | - return false; |
|
| 1005 | - } |
|
| 1006 | - } else { |
|
| 1007 | - return false; |
|
| 1008 | - } |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - /** |
|
| 1012 | - * @param string $tmpFile |
|
| 1013 | - * @param string $path |
|
| 1014 | - * @return bool|mixed |
|
| 1015 | - * @throws \OCP\Files\InvalidPathException |
|
| 1016 | - */ |
|
| 1017 | - public function fromTmpFile($tmpFile, $path) { |
|
| 1018 | - $this->assertPathLength($path); |
|
| 1019 | - if (Filesystem::isValidPath($path)) { |
|
| 1020 | - |
|
| 1021 | - // Get directory that the file is going into |
|
| 1022 | - $filePath = dirname($path); |
|
| 1023 | - |
|
| 1024 | - // Create the directories if any |
|
| 1025 | - if (!$this->file_exists($filePath)) { |
|
| 1026 | - $result = $this->createParentDirectories($filePath); |
|
| 1027 | - if ($result === false) { |
|
| 1028 | - return false; |
|
| 1029 | - } |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - $source = fopen($tmpFile, 'r'); |
|
| 1033 | - if ($source) { |
|
| 1034 | - $result = $this->file_put_contents($path, $source); |
|
| 1035 | - // $this->file_put_contents() might have already closed |
|
| 1036 | - // the resource, so we check it, before trying to close it |
|
| 1037 | - // to avoid messages in the error log. |
|
| 1038 | - if (is_resource($source)) { |
|
| 1039 | - fclose($source); |
|
| 1040 | - } |
|
| 1041 | - unlink($tmpFile); |
|
| 1042 | - return $result; |
|
| 1043 | - } else { |
|
| 1044 | - return false; |
|
| 1045 | - } |
|
| 1046 | - } else { |
|
| 1047 | - return false; |
|
| 1048 | - } |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - |
|
| 1052 | - /** |
|
| 1053 | - * @param string $path |
|
| 1054 | - * @return mixed |
|
| 1055 | - * @throws \OCP\Files\InvalidPathException |
|
| 1056 | - */ |
|
| 1057 | - public function getMimeType($path) { |
|
| 1058 | - $this->assertPathLength($path); |
|
| 1059 | - return $this->basicOperation('getMimeType', $path); |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - /** |
|
| 1063 | - * @param string $type |
|
| 1064 | - * @param string $path |
|
| 1065 | - * @param bool $raw |
|
| 1066 | - * @return bool|null|string |
|
| 1067 | - */ |
|
| 1068 | - public function hash($type, $path, $raw = false) { |
|
| 1069 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1070 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1071 | - if (Filesystem::isValidPath($path)) { |
|
| 1072 | - $path = $this->getRelativePath($absolutePath); |
|
| 1073 | - if ($path == null) { |
|
| 1074 | - return false; |
|
| 1075 | - } |
|
| 1076 | - if ($this->shouldEmitHooks($path)) { |
|
| 1077 | - \OC_Hook::emit( |
|
| 1078 | - Filesystem::CLASSNAME, |
|
| 1079 | - Filesystem::signal_read, |
|
| 1080 | - array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
| 1081 | - ); |
|
| 1082 | - } |
|
| 1083 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1084 | - if ($storage) { |
|
| 1085 | - return $storage->hash($type, $internalPath, $raw); |
|
| 1086 | - } |
|
| 1087 | - } |
|
| 1088 | - return null; |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - /** |
|
| 1092 | - * @param string $path |
|
| 1093 | - * @return mixed |
|
| 1094 | - * @throws \OCP\Files\InvalidPathException |
|
| 1095 | - */ |
|
| 1096 | - public function free_space($path = '/') { |
|
| 1097 | - $this->assertPathLength($path); |
|
| 1098 | - $result = $this->basicOperation('free_space', $path); |
|
| 1099 | - if ($result === null) { |
|
| 1100 | - throw new InvalidPathException(); |
|
| 1101 | - } |
|
| 1102 | - return $result; |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - /** |
|
| 1106 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1107 | - * |
|
| 1108 | - * @param string $operation |
|
| 1109 | - * @param string $path |
|
| 1110 | - * @param array $hooks (optional) |
|
| 1111 | - * @param mixed $extraParam (optional) |
|
| 1112 | - * @return mixed |
|
| 1113 | - * @throws \Exception |
|
| 1114 | - * |
|
| 1115 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1116 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1117 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1118 | - */ |
|
| 1119 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1120 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1121 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1122 | - if (Filesystem::isValidPath($path) |
|
| 1123 | - and !Filesystem::isFileBlacklisted($path) |
|
| 1124 | - ) { |
|
| 1125 | - $path = $this->getRelativePath($absolutePath); |
|
| 1126 | - if ($path == null) { |
|
| 1127 | - return false; |
|
| 1128 | - } |
|
| 1129 | - |
|
| 1130 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1131 | - // always a shared lock during pre-hooks so the hook can read the file |
|
| 1132 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1133 | - } |
|
| 1134 | - |
|
| 1135 | - $run = $this->runHooks($hooks, $path); |
|
| 1136 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1137 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1138 | - if ($run and $storage) { |
|
| 1139 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1140 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1141 | - } |
|
| 1142 | - try { |
|
| 1143 | - if (!is_null($extraParam)) { |
|
| 1144 | - $result = $storage->$operation($internalPath, $extraParam); |
|
| 1145 | - } else { |
|
| 1146 | - $result = $storage->$operation($internalPath); |
|
| 1147 | - } |
|
| 1148 | - } catch (\Exception $e) { |
|
| 1149 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1150 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1151 | - } else if (in_array('read', $hooks)) { |
|
| 1152 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1153 | - } |
|
| 1154 | - throw $e; |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - if ($result && in_array('delete', $hooks) and $result) { |
|
| 1158 | - $this->removeUpdate($storage, $internalPath); |
|
| 1159 | - } |
|
| 1160 | - if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
| 1161 | - $this->writeUpdate($storage, $internalPath); |
|
| 1162 | - } |
|
| 1163 | - if ($result && in_array('touch', $hooks)) { |
|
| 1164 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1168 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - $unlockLater = false; |
|
| 1172 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1173 | - $unlockLater = true; |
|
| 1174 | - // make sure our unlocking callback will still be called if connection is aborted |
|
| 1175 | - ignore_user_abort(true); |
|
| 1176 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1177 | - if (in_array('write', $hooks)) { |
|
| 1178 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1179 | - } else if (in_array('read', $hooks)) { |
|
| 1180 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1181 | - } |
|
| 1182 | - }); |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1186 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1187 | - $this->runHooks($hooks, $path, true); |
|
| 1188 | - } |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - if (!$unlockLater |
|
| 1192 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1193 | - ) { |
|
| 1194 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1195 | - } |
|
| 1196 | - return $result; |
|
| 1197 | - } else { |
|
| 1198 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1199 | - } |
|
| 1200 | - } |
|
| 1201 | - return null; |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - /** |
|
| 1205 | - * get the path relative to the default root for hook usage |
|
| 1206 | - * |
|
| 1207 | - * @param string $path |
|
| 1208 | - * @return string |
|
| 1209 | - */ |
|
| 1210 | - private function getHookPath($path) { |
|
| 1211 | - if (!Filesystem::getView()) { |
|
| 1212 | - return $path; |
|
| 1213 | - } |
|
| 1214 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - private function shouldEmitHooks($path = '') { |
|
| 1218 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1219 | - return false; |
|
| 1220 | - } |
|
| 1221 | - if (!Filesystem::$loaded) { |
|
| 1222 | - return false; |
|
| 1223 | - } |
|
| 1224 | - $defaultRoot = Filesystem::getRoot(); |
|
| 1225 | - if ($defaultRoot === null) { |
|
| 1226 | - return false; |
|
| 1227 | - } |
|
| 1228 | - if ($this->fakeRoot === $defaultRoot) { |
|
| 1229 | - return true; |
|
| 1230 | - } |
|
| 1231 | - $fullPath = $this->getAbsolutePath($path); |
|
| 1232 | - |
|
| 1233 | - if ($fullPath === $defaultRoot) { |
|
| 1234 | - return true; |
|
| 1235 | - } |
|
| 1236 | - |
|
| 1237 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1238 | - } |
|
| 1239 | - |
|
| 1240 | - /** |
|
| 1241 | - * @param string[] $hooks |
|
| 1242 | - * @param string $path |
|
| 1243 | - * @param bool $post |
|
| 1244 | - * @return bool |
|
| 1245 | - */ |
|
| 1246 | - private function runHooks($hooks, $path, $post = false) { |
|
| 1247 | - $relativePath = $path; |
|
| 1248 | - $path = $this->getHookPath($path); |
|
| 1249 | - $prefix = $post ? 'post_' : ''; |
|
| 1250 | - $run = true; |
|
| 1251 | - if ($this->shouldEmitHooks($relativePath)) { |
|
| 1252 | - foreach ($hooks as $hook) { |
|
| 1253 | - if ($hook != 'read') { |
|
| 1254 | - \OC_Hook::emit( |
|
| 1255 | - Filesystem::CLASSNAME, |
|
| 1256 | - $prefix . $hook, |
|
| 1257 | - array( |
|
| 1258 | - Filesystem::signal_param_run => &$run, |
|
| 1259 | - Filesystem::signal_param_path => $path |
|
| 1260 | - ) |
|
| 1261 | - ); |
|
| 1262 | - } elseif (!$post) { |
|
| 1263 | - \OC_Hook::emit( |
|
| 1264 | - Filesystem::CLASSNAME, |
|
| 1265 | - $prefix . $hook, |
|
| 1266 | - array( |
|
| 1267 | - Filesystem::signal_param_path => $path |
|
| 1268 | - ) |
|
| 1269 | - ); |
|
| 1270 | - } |
|
| 1271 | - } |
|
| 1272 | - } |
|
| 1273 | - return $run; |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - /** |
|
| 1277 | - * check if a file or folder has been updated since $time |
|
| 1278 | - * |
|
| 1279 | - * @param string $path |
|
| 1280 | - * @param int $time |
|
| 1281 | - * @return bool |
|
| 1282 | - */ |
|
| 1283 | - public function hasUpdated($path, $time) { |
|
| 1284 | - return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - /** |
|
| 1288 | - * @param string $ownerId |
|
| 1289 | - * @return \OC\User\User |
|
| 1290 | - */ |
|
| 1291 | - private function getUserObjectForOwner($ownerId) { |
|
| 1292 | - $owner = $this->userManager->get($ownerId); |
|
| 1293 | - if ($owner instanceof IUser) { |
|
| 1294 | - return $owner; |
|
| 1295 | - } else { |
|
| 1296 | - return new User($ownerId, null); |
|
| 1297 | - } |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - /** |
|
| 1301 | - * Get file info from cache |
|
| 1302 | - * |
|
| 1303 | - * If the file is not in cached it will be scanned |
|
| 1304 | - * If the file has changed on storage the cache will be updated |
|
| 1305 | - * |
|
| 1306 | - * @param \OC\Files\Storage\Storage $storage |
|
| 1307 | - * @param string $internalPath |
|
| 1308 | - * @param string $relativePath |
|
| 1309 | - * @return ICacheEntry|bool |
|
| 1310 | - */ |
|
| 1311 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1312 | - $cache = $storage->getCache($internalPath); |
|
| 1313 | - $data = $cache->get($internalPath); |
|
| 1314 | - $watcher = $storage->getWatcher($internalPath); |
|
| 1315 | - |
|
| 1316 | - try { |
|
| 1317 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1318 | - if (!$data || $data['size'] === -1) { |
|
| 1319 | - if (!$storage->file_exists($internalPath)) { |
|
| 1320 | - return false; |
|
| 1321 | - } |
|
| 1322 | - // don't need to get a lock here since the scanner does it's own locking |
|
| 1323 | - $scanner = $storage->getScanner($internalPath); |
|
| 1324 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1325 | - $data = $cache->get($internalPath); |
|
| 1326 | - } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1327 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1328 | - $watcher->update($internalPath, $data); |
|
| 1329 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1330 | - $data = $cache->get($internalPath); |
|
| 1331 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1332 | - } |
|
| 1333 | - } catch (LockedException $e) { |
|
| 1334 | - // if the file is locked we just use the old cache info |
|
| 1335 | - } |
|
| 1336 | - |
|
| 1337 | - return $data; |
|
| 1338 | - } |
|
| 1339 | - |
|
| 1340 | - /** |
|
| 1341 | - * get the filesystem info |
|
| 1342 | - * |
|
| 1343 | - * @param string $path |
|
| 1344 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1345 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1346 | - * defaults to true |
|
| 1347 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1348 | - */ |
|
| 1349 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1350 | - $this->assertPathLength($path); |
|
| 1351 | - if (!Filesystem::isValidPath($path)) { |
|
| 1352 | - return false; |
|
| 1353 | - } |
|
| 1354 | - if (Cache\Scanner::isPartialFile($path)) { |
|
| 1355 | - return $this->getPartFileInfo($path); |
|
| 1356 | - } |
|
| 1357 | - $relativePath = $path; |
|
| 1358 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1359 | - |
|
| 1360 | - $mount = Filesystem::getMountManager()->find($path); |
|
| 1361 | - if (!$mount) { |
|
| 1362 | - \OC::$server->getLogger()->warning('Mountpoint not found for path: ' . $path); |
|
| 1363 | - return false; |
|
| 1364 | - } |
|
| 1365 | - $storage = $mount->getStorage(); |
|
| 1366 | - $internalPath = $mount->getInternalPath($path); |
|
| 1367 | - if ($storage) { |
|
| 1368 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1369 | - |
|
| 1370 | - if (!$data instanceof ICacheEntry) { |
|
| 1371 | - \OC::$server->getLogger()->debug('No cache entry found for ' . $path . ' (storage: ' . $storage->getId() . ', internalPath: ' . $internalPath . ')'); |
|
| 1372 | - return false; |
|
| 1373 | - } |
|
| 1374 | - |
|
| 1375 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1376 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
| 1380 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1381 | - |
|
| 1382 | - if ($data and isset($data['fileid'])) { |
|
| 1383 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1384 | - //add the sizes of other mount points to the folder |
|
| 1385 | - $extOnly = ($includeMountPoints === 'ext'); |
|
| 1386 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1387 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1388 | - $subStorage = $mount->getStorage(); |
|
| 1389 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1390 | - })); |
|
| 1391 | - } |
|
| 1392 | - } |
|
| 1393 | - |
|
| 1394 | - return $info; |
|
| 1395 | - } else { |
|
| 1396 | - \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint()); |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - return false; |
|
| 1400 | - } |
|
| 1401 | - |
|
| 1402 | - /** |
|
| 1403 | - * get the content of a directory |
|
| 1404 | - * |
|
| 1405 | - * @param string $directory path under datadirectory |
|
| 1406 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1407 | - * @return FileInfo[] |
|
| 1408 | - */ |
|
| 1409 | - public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 1410 | - $this->assertPathLength($directory); |
|
| 1411 | - if (!Filesystem::isValidPath($directory)) { |
|
| 1412 | - return []; |
|
| 1413 | - } |
|
| 1414 | - $path = $this->getAbsolutePath($directory); |
|
| 1415 | - $path = Filesystem::normalizePath($path); |
|
| 1416 | - $mount = $this->getMount($directory); |
|
| 1417 | - if (!$mount) { |
|
| 1418 | - return []; |
|
| 1419 | - } |
|
| 1420 | - $storage = $mount->getStorage(); |
|
| 1421 | - $internalPath = $mount->getInternalPath($path); |
|
| 1422 | - if ($storage) { |
|
| 1423 | - $cache = $storage->getCache($internalPath); |
|
| 1424 | - $user = \OC_User::getUser(); |
|
| 1425 | - |
|
| 1426 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1427 | - |
|
| 1428 | - if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1429 | - return []; |
|
| 1430 | - } |
|
| 1431 | - |
|
| 1432 | - $folderId = $data['fileid']; |
|
| 1433 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1434 | - |
|
| 1435 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1436 | - /** |
|
| 1437 | - * @var \OC\Files\FileInfo[] $files |
|
| 1438 | - */ |
|
| 1439 | - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1440 | - if ($sharingDisabled) { |
|
| 1441 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1442 | - } |
|
| 1443 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1444 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1445 | - }, $contents); |
|
| 1446 | - |
|
| 1447 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1448 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1449 | - $dirLength = strlen($path); |
|
| 1450 | - foreach ($mounts as $mount) { |
|
| 1451 | - $mountPoint = $mount->getMountPoint(); |
|
| 1452 | - $subStorage = $mount->getStorage(); |
|
| 1453 | - if ($subStorage) { |
|
| 1454 | - $subCache = $subStorage->getCache(''); |
|
| 1455 | - |
|
| 1456 | - $rootEntry = $subCache->get(''); |
|
| 1457 | - if (!$rootEntry) { |
|
| 1458 | - $subScanner = $subStorage->getScanner(''); |
|
| 1459 | - try { |
|
| 1460 | - $subScanner->scanFile(''); |
|
| 1461 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1462 | - continue; |
|
| 1463 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1464 | - continue; |
|
| 1465 | - } catch (\Exception $e) { |
|
| 1466 | - // sometimes when the storage is not available it can be any exception |
|
| 1467 | - \OC::$server->getLogger()->logException($e, [ |
|
| 1468 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1469 | - 'level' => ILogger::ERROR, |
|
| 1470 | - 'app' => 'lib', |
|
| 1471 | - ]); |
|
| 1472 | - continue; |
|
| 1473 | - } |
|
| 1474 | - $rootEntry = $subCache->get(''); |
|
| 1475 | - } |
|
| 1476 | - |
|
| 1477 | - if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1478 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1479 | - if ($pos = strpos($relativePath, '/')) { |
|
| 1480 | - //mountpoint inside subfolder add size to the correct folder |
|
| 1481 | - $entryName = substr($relativePath, 0, $pos); |
|
| 1482 | - foreach ($files as &$entry) { |
|
| 1483 | - if ($entry->getName() === $entryName) { |
|
| 1484 | - $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1485 | - } |
|
| 1486 | - } |
|
| 1487 | - } else { //mountpoint in this folder, add an entry for it |
|
| 1488 | - $rootEntry['name'] = $relativePath; |
|
| 1489 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1490 | - $permissions = $rootEntry['permissions']; |
|
| 1491 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1492 | - // for shared files/folders we use the permissions given by the owner |
|
| 1493 | - if ($mount instanceof MoveableMount) { |
|
| 1494 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1495 | - } else { |
|
| 1496 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - //remove any existing entry with the same name |
|
| 1500 | - foreach ($files as $i => $file) { |
|
| 1501 | - if ($file['name'] === $rootEntry['name']) { |
|
| 1502 | - unset($files[$i]); |
|
| 1503 | - break; |
|
| 1504 | - } |
|
| 1505 | - } |
|
| 1506 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1507 | - |
|
| 1508 | - // if sharing was disabled for the user we remove the share permissions |
|
| 1509 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1510 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1511 | - } |
|
| 1512 | - |
|
| 1513 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1514 | - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1515 | - } |
|
| 1516 | - } |
|
| 1517 | - } |
|
| 1518 | - } |
|
| 1519 | - |
|
| 1520 | - if ($mimetype_filter) { |
|
| 1521 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1522 | - if (strpos($mimetype_filter, '/')) { |
|
| 1523 | - return $file->getMimetype() === $mimetype_filter; |
|
| 1524 | - } else { |
|
| 1525 | - return $file->getMimePart() === $mimetype_filter; |
|
| 1526 | - } |
|
| 1527 | - }); |
|
| 1528 | - } |
|
| 1529 | - |
|
| 1530 | - return $files; |
|
| 1531 | - } else { |
|
| 1532 | - return []; |
|
| 1533 | - } |
|
| 1534 | - } |
|
| 1535 | - |
|
| 1536 | - /** |
|
| 1537 | - * change file metadata |
|
| 1538 | - * |
|
| 1539 | - * @param string $path |
|
| 1540 | - * @param array|\OCP\Files\FileInfo $data |
|
| 1541 | - * @return int |
|
| 1542 | - * |
|
| 1543 | - * returns the fileid of the updated file |
|
| 1544 | - */ |
|
| 1545 | - public function putFileInfo($path, $data) { |
|
| 1546 | - $this->assertPathLength($path); |
|
| 1547 | - if ($data instanceof FileInfo) { |
|
| 1548 | - $data = $data->getData(); |
|
| 1549 | - } |
|
| 1550 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1551 | - /** |
|
| 1552 | - * @var \OC\Files\Storage\Storage $storage |
|
| 1553 | - * @var string $internalPath |
|
| 1554 | - */ |
|
| 1555 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 1556 | - if ($storage) { |
|
| 1557 | - $cache = $storage->getCache($path); |
|
| 1558 | - |
|
| 1559 | - if (!$cache->inCache($internalPath)) { |
|
| 1560 | - $scanner = $storage->getScanner($internalPath); |
|
| 1561 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1562 | - } |
|
| 1563 | - |
|
| 1564 | - return $cache->put($internalPath, $data); |
|
| 1565 | - } else { |
|
| 1566 | - return -1; |
|
| 1567 | - } |
|
| 1568 | - } |
|
| 1569 | - |
|
| 1570 | - /** |
|
| 1571 | - * search for files with the name matching $query |
|
| 1572 | - * |
|
| 1573 | - * @param string $query |
|
| 1574 | - * @return FileInfo[] |
|
| 1575 | - */ |
|
| 1576 | - public function search($query) { |
|
| 1577 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1578 | - } |
|
| 1579 | - |
|
| 1580 | - /** |
|
| 1581 | - * search for files with the name matching $query |
|
| 1582 | - * |
|
| 1583 | - * @param string $query |
|
| 1584 | - * @return FileInfo[] |
|
| 1585 | - */ |
|
| 1586 | - public function searchRaw($query) { |
|
| 1587 | - return $this->searchCommon('search', array($query)); |
|
| 1588 | - } |
|
| 1589 | - |
|
| 1590 | - /** |
|
| 1591 | - * search for files by mimetype |
|
| 1592 | - * |
|
| 1593 | - * @param string $mimetype |
|
| 1594 | - * @return FileInfo[] |
|
| 1595 | - */ |
|
| 1596 | - public function searchByMime($mimetype) { |
|
| 1597 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 1598 | - } |
|
| 1599 | - |
|
| 1600 | - /** |
|
| 1601 | - * search for files by tag |
|
| 1602 | - * |
|
| 1603 | - * @param string|int $tag name or tag id |
|
| 1604 | - * @param string $userId owner of the tags |
|
| 1605 | - * @return FileInfo[] |
|
| 1606 | - */ |
|
| 1607 | - public function searchByTag($tag, $userId) { |
|
| 1608 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 1609 | - } |
|
| 1610 | - |
|
| 1611 | - /** |
|
| 1612 | - * @param string $method cache method |
|
| 1613 | - * @param array $args |
|
| 1614 | - * @return FileInfo[] |
|
| 1615 | - */ |
|
| 1616 | - private function searchCommon($method, $args) { |
|
| 1617 | - $files = array(); |
|
| 1618 | - $rootLength = strlen($this->fakeRoot); |
|
| 1619 | - |
|
| 1620 | - $mount = $this->getMount(''); |
|
| 1621 | - $mountPoint = $mount->getMountPoint(); |
|
| 1622 | - $storage = $mount->getStorage(); |
|
| 1623 | - if ($storage) { |
|
| 1624 | - $cache = $storage->getCache(''); |
|
| 1625 | - |
|
| 1626 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 1627 | - foreach ($results as $result) { |
|
| 1628 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1629 | - $internalPath = $result['path']; |
|
| 1630 | - $path = $mountPoint . $result['path']; |
|
| 1631 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1632 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1633 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1634 | - } |
|
| 1635 | - } |
|
| 1636 | - |
|
| 1637 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1638 | - foreach ($mounts as $mount) { |
|
| 1639 | - $mountPoint = $mount->getMountPoint(); |
|
| 1640 | - $storage = $mount->getStorage(); |
|
| 1641 | - if ($storage) { |
|
| 1642 | - $cache = $storage->getCache(''); |
|
| 1643 | - |
|
| 1644 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1645 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 1646 | - if ($results) { |
|
| 1647 | - foreach ($results as $result) { |
|
| 1648 | - $internalPath = $result['path']; |
|
| 1649 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1650 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1651 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1652 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1653 | - } |
|
| 1654 | - } |
|
| 1655 | - } |
|
| 1656 | - } |
|
| 1657 | - } |
|
| 1658 | - return $files; |
|
| 1659 | - } |
|
| 1660 | - |
|
| 1661 | - /** |
|
| 1662 | - * Get the owner for a file or folder |
|
| 1663 | - * |
|
| 1664 | - * @param string $path |
|
| 1665 | - * @return string the user id of the owner |
|
| 1666 | - * @throws NotFoundException |
|
| 1667 | - */ |
|
| 1668 | - public function getOwner($path) { |
|
| 1669 | - $info = $this->getFileInfo($path); |
|
| 1670 | - if (!$info) { |
|
| 1671 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1672 | - } |
|
| 1673 | - return $info->getOwner()->getUID(); |
|
| 1674 | - } |
|
| 1675 | - |
|
| 1676 | - /** |
|
| 1677 | - * get the ETag for a file or folder |
|
| 1678 | - * |
|
| 1679 | - * @param string $path |
|
| 1680 | - * @return string |
|
| 1681 | - */ |
|
| 1682 | - public function getETag($path) { |
|
| 1683 | - /** |
|
| 1684 | - * @var Storage\Storage $storage |
|
| 1685 | - * @var string $internalPath |
|
| 1686 | - */ |
|
| 1687 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1688 | - if ($storage) { |
|
| 1689 | - return $storage->getETag($internalPath); |
|
| 1690 | - } else { |
|
| 1691 | - return null; |
|
| 1692 | - } |
|
| 1693 | - } |
|
| 1694 | - |
|
| 1695 | - /** |
|
| 1696 | - * Get the path of a file by id, relative to the view |
|
| 1697 | - * |
|
| 1698 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1699 | - * |
|
| 1700 | - * @param int $id |
|
| 1701 | - * @throws NotFoundException |
|
| 1702 | - * @return string |
|
| 1703 | - */ |
|
| 1704 | - public function getPath($id) { |
|
| 1705 | - $id = (int)$id; |
|
| 1706 | - $manager = Filesystem::getMountManager(); |
|
| 1707 | - $mounts = $manager->findIn($this->fakeRoot); |
|
| 1708 | - $mounts[] = $manager->find($this->fakeRoot); |
|
| 1709 | - // reverse the array so we start with the storage this view is in |
|
| 1710 | - // which is the most likely to contain the file we're looking for |
|
| 1711 | - $mounts = array_reverse($mounts); |
|
| 1712 | - foreach ($mounts as $mount) { |
|
| 1713 | - /** |
|
| 1714 | - * @var \OC\Files\Mount\MountPoint $mount |
|
| 1715 | - */ |
|
| 1716 | - if ($mount->getStorage()) { |
|
| 1717 | - $cache = $mount->getStorage()->getCache(); |
|
| 1718 | - $internalPath = $cache->getPathById($id); |
|
| 1719 | - if (is_string($internalPath)) { |
|
| 1720 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1721 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1722 | - return $path; |
|
| 1723 | - } |
|
| 1724 | - } |
|
| 1725 | - } |
|
| 1726 | - } |
|
| 1727 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1728 | - } |
|
| 1729 | - |
|
| 1730 | - /** |
|
| 1731 | - * @param string $path |
|
| 1732 | - * @throws InvalidPathException |
|
| 1733 | - */ |
|
| 1734 | - private function assertPathLength($path) { |
|
| 1735 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1736 | - // Check for the string length - performed using isset() instead of strlen() |
|
| 1737 | - // because isset() is about 5x-40x faster. |
|
| 1738 | - if (isset($path[$maxLen])) { |
|
| 1739 | - $pathLen = strlen($path); |
|
| 1740 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1741 | - } |
|
| 1742 | - } |
|
| 1743 | - |
|
| 1744 | - /** |
|
| 1745 | - * check if it is allowed to move a mount point to a given target. |
|
| 1746 | - * It is not allowed to move a mount point into a different mount point or |
|
| 1747 | - * into an already shared folder |
|
| 1748 | - * |
|
| 1749 | - * @param string $target path |
|
| 1750 | - * @return boolean |
|
| 1751 | - */ |
|
| 1752 | - private function isTargetAllowed($target) { |
|
| 1753 | - |
|
| 1754 | - list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
| 1755 | - if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 1756 | - \OCP\Util::writeLog('files', |
|
| 1757 | - 'It is not allowed to move one mount point into another one', |
|
| 1758 | - ILogger::DEBUG); |
|
| 1759 | - return false; |
|
| 1760 | - } |
|
| 1761 | - |
|
| 1762 | - // note: cannot use the view because the target is already locked |
|
| 1763 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1764 | - if ($fileId === -1) { |
|
| 1765 | - // target might not exist, need to check parent instead |
|
| 1766 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1767 | - } |
|
| 1768 | - |
|
| 1769 | - // check if any of the parents were shared by the current owner (include collections) |
|
| 1770 | - $shares = \OCP\Share::getItemShared( |
|
| 1771 | - 'folder', |
|
| 1772 | - $fileId, |
|
| 1773 | - \OCP\Share::FORMAT_NONE, |
|
| 1774 | - null, |
|
| 1775 | - true |
|
| 1776 | - ); |
|
| 1777 | - |
|
| 1778 | - if (count($shares) > 0) { |
|
| 1779 | - \OCP\Util::writeLog('files', |
|
| 1780 | - 'It is not allowed to move one mount point into a shared folder', |
|
| 1781 | - ILogger::DEBUG); |
|
| 1782 | - return false; |
|
| 1783 | - } |
|
| 1784 | - |
|
| 1785 | - return true; |
|
| 1786 | - } |
|
| 1787 | - |
|
| 1788 | - /** |
|
| 1789 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1790 | - * |
|
| 1791 | - * @param string $path |
|
| 1792 | - * @return \OCP\Files\FileInfo |
|
| 1793 | - */ |
|
| 1794 | - private function getPartFileInfo($path) { |
|
| 1795 | - $mount = $this->getMount($path); |
|
| 1796 | - $storage = $mount->getStorage(); |
|
| 1797 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1798 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1799 | - return new FileInfo( |
|
| 1800 | - $this->getAbsolutePath($path), |
|
| 1801 | - $storage, |
|
| 1802 | - $internalPath, |
|
| 1803 | - [ |
|
| 1804 | - 'fileid' => null, |
|
| 1805 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1806 | - 'name' => basename($path), |
|
| 1807 | - 'etag' => null, |
|
| 1808 | - 'size' => $storage->filesize($internalPath), |
|
| 1809 | - 'mtime' => $storage->filemtime($internalPath), |
|
| 1810 | - 'encrypted' => false, |
|
| 1811 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1812 | - ], |
|
| 1813 | - $mount, |
|
| 1814 | - $owner |
|
| 1815 | - ); |
|
| 1816 | - } |
|
| 1817 | - |
|
| 1818 | - /** |
|
| 1819 | - * @param string $path |
|
| 1820 | - * @param string $fileName |
|
| 1821 | - * @throws InvalidPathException |
|
| 1822 | - */ |
|
| 1823 | - public function verifyPath($path, $fileName) { |
|
| 1824 | - try { |
|
| 1825 | - /** @type \OCP\Files\Storage $storage */ |
|
| 1826 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1827 | - $storage->verifyPath($internalPath, $fileName); |
|
| 1828 | - } catch (ReservedWordException $ex) { |
|
| 1829 | - $l = \OC::$server->getL10N('lib'); |
|
| 1830 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1831 | - } catch (InvalidCharacterInPathException $ex) { |
|
| 1832 | - $l = \OC::$server->getL10N('lib'); |
|
| 1833 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1834 | - } catch (FileNameTooLongException $ex) { |
|
| 1835 | - $l = \OC::$server->getL10N('lib'); |
|
| 1836 | - throw new InvalidPathException($l->t('File name is too long')); |
|
| 1837 | - } catch (InvalidDirectoryException $ex) { |
|
| 1838 | - $l = \OC::$server->getL10N('lib'); |
|
| 1839 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1840 | - } catch (EmptyFileNameException $ex) { |
|
| 1841 | - $l = \OC::$server->getL10N('lib'); |
|
| 1842 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1843 | - } |
|
| 1844 | - } |
|
| 1845 | - |
|
| 1846 | - /** |
|
| 1847 | - * get all parent folders of $path |
|
| 1848 | - * |
|
| 1849 | - * @param string $path |
|
| 1850 | - * @return string[] |
|
| 1851 | - */ |
|
| 1852 | - private function getParents($path) { |
|
| 1853 | - $path = trim($path, '/'); |
|
| 1854 | - if (!$path) { |
|
| 1855 | - return []; |
|
| 1856 | - } |
|
| 1857 | - |
|
| 1858 | - $parts = explode('/', $path); |
|
| 1859 | - |
|
| 1860 | - // remove the single file |
|
| 1861 | - array_pop($parts); |
|
| 1862 | - $result = array('/'); |
|
| 1863 | - $resultPath = ''; |
|
| 1864 | - foreach ($parts as $part) { |
|
| 1865 | - if ($part) { |
|
| 1866 | - $resultPath .= '/' . $part; |
|
| 1867 | - $result[] = $resultPath; |
|
| 1868 | - } |
|
| 1869 | - } |
|
| 1870 | - return $result; |
|
| 1871 | - } |
|
| 1872 | - |
|
| 1873 | - /** |
|
| 1874 | - * Returns the mount point for which to lock |
|
| 1875 | - * |
|
| 1876 | - * @param string $absolutePath absolute path |
|
| 1877 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1878 | - * is mounted directly on the given path, false otherwise |
|
| 1879 | - * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
| 1880 | - */ |
|
| 1881 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1882 | - $results = []; |
|
| 1883 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1884 | - if (!$mount) { |
|
| 1885 | - return $results; |
|
| 1886 | - } |
|
| 1887 | - |
|
| 1888 | - if ($useParentMount) { |
|
| 1889 | - // find out if something is mounted directly on the path |
|
| 1890 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1891 | - if ($internalPath === '') { |
|
| 1892 | - // resolve the parent mount instead |
|
| 1893 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1894 | - } |
|
| 1895 | - } |
|
| 1896 | - |
|
| 1897 | - return $mount; |
|
| 1898 | - } |
|
| 1899 | - |
|
| 1900 | - /** |
|
| 1901 | - * Lock the given path |
|
| 1902 | - * |
|
| 1903 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1904 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1905 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1906 | - * |
|
| 1907 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1908 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1909 | - */ |
|
| 1910 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1911 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1912 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1913 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1914 | - return false; |
|
| 1915 | - } |
|
| 1916 | - |
|
| 1917 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1918 | - if ($mount) { |
|
| 1919 | - try { |
|
| 1920 | - $storage = $mount->getStorage(); |
|
| 1921 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1922 | - $storage->acquireLock( |
|
| 1923 | - $mount->getInternalPath($absolutePath), |
|
| 1924 | - $type, |
|
| 1925 | - $this->lockingProvider |
|
| 1926 | - ); |
|
| 1927 | - } |
|
| 1928 | - } catch (\OCP\Lock\LockedException $e) { |
|
| 1929 | - // rethrow with the a human-readable path |
|
| 1930 | - throw new \OCP\Lock\LockedException( |
|
| 1931 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 1932 | - $e |
|
| 1933 | - ); |
|
| 1934 | - } |
|
| 1935 | - } |
|
| 1936 | - |
|
| 1937 | - return true; |
|
| 1938 | - } |
|
| 1939 | - |
|
| 1940 | - /** |
|
| 1941 | - * Change the lock type |
|
| 1942 | - * |
|
| 1943 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1944 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1945 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1946 | - * |
|
| 1947 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1948 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1949 | - */ |
|
| 1950 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1951 | - $path = Filesystem::normalizePath($path); |
|
| 1952 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1953 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1954 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1955 | - return false; |
|
| 1956 | - } |
|
| 1957 | - |
|
| 1958 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1959 | - if ($mount) { |
|
| 1960 | - try { |
|
| 1961 | - $storage = $mount->getStorage(); |
|
| 1962 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1963 | - $storage->changeLock( |
|
| 1964 | - $mount->getInternalPath($absolutePath), |
|
| 1965 | - $type, |
|
| 1966 | - $this->lockingProvider |
|
| 1967 | - ); |
|
| 1968 | - } |
|
| 1969 | - } catch (\OCP\Lock\LockedException $e) { |
|
| 1970 | - try { |
|
| 1971 | - // rethrow with the a human-readable path |
|
| 1972 | - throw new \OCP\Lock\LockedException( |
|
| 1973 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 1974 | - $e |
|
| 1975 | - ); |
|
| 1976 | - } catch (\InvalidArgumentException $e) { |
|
| 1977 | - throw new \OCP\Lock\LockedException( |
|
| 1978 | - $absolutePath, |
|
| 1979 | - $e |
|
| 1980 | - ); |
|
| 1981 | - } |
|
| 1982 | - } |
|
| 1983 | - } |
|
| 1984 | - |
|
| 1985 | - return true; |
|
| 1986 | - } |
|
| 1987 | - |
|
| 1988 | - /** |
|
| 1989 | - * Unlock the given path |
|
| 1990 | - * |
|
| 1991 | - * @param string $path the path of the file to unlock, relative to the view |
|
| 1992 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1993 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1994 | - * |
|
| 1995 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1996 | - */ |
|
| 1997 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 1998 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1999 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2000 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2001 | - return false; |
|
| 2002 | - } |
|
| 2003 | - |
|
| 2004 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2005 | - if ($mount) { |
|
| 2006 | - $storage = $mount->getStorage(); |
|
| 2007 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2008 | - $storage->releaseLock( |
|
| 2009 | - $mount->getInternalPath($absolutePath), |
|
| 2010 | - $type, |
|
| 2011 | - $this->lockingProvider |
|
| 2012 | - ); |
|
| 2013 | - } |
|
| 2014 | - } |
|
| 2015 | - |
|
| 2016 | - return true; |
|
| 2017 | - } |
|
| 2018 | - |
|
| 2019 | - /** |
|
| 2020 | - * Lock a path and all its parents up to the root of the view |
|
| 2021 | - * |
|
| 2022 | - * @param string $path the path of the file to lock relative to the view |
|
| 2023 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2024 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2025 | - * |
|
| 2026 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2027 | - */ |
|
| 2028 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2029 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2030 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2031 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2032 | - return false; |
|
| 2033 | - } |
|
| 2034 | - |
|
| 2035 | - $this->lockPath($path, $type, $lockMountPoint); |
|
| 2036 | - |
|
| 2037 | - $parents = $this->getParents($path); |
|
| 2038 | - foreach ($parents as $parent) { |
|
| 2039 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2040 | - } |
|
| 2041 | - |
|
| 2042 | - return true; |
|
| 2043 | - } |
|
| 2044 | - |
|
| 2045 | - /** |
|
| 2046 | - * Unlock a path and all its parents up to the root of the view |
|
| 2047 | - * |
|
| 2048 | - * @param string $path the path of the file to lock relative to the view |
|
| 2049 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2050 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2051 | - * |
|
| 2052 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2053 | - */ |
|
| 2054 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2055 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2056 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2057 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2058 | - return false; |
|
| 2059 | - } |
|
| 2060 | - |
|
| 2061 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2062 | - |
|
| 2063 | - $parents = $this->getParents($path); |
|
| 2064 | - foreach ($parents as $parent) { |
|
| 2065 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2066 | - } |
|
| 2067 | - |
|
| 2068 | - return true; |
|
| 2069 | - } |
|
| 2070 | - |
|
| 2071 | - /** |
|
| 2072 | - * Only lock files in data/user/files/ |
|
| 2073 | - * |
|
| 2074 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2075 | - * @return bool |
|
| 2076 | - */ |
|
| 2077 | - protected function shouldLockFile($path) { |
|
| 2078 | - $path = Filesystem::normalizePath($path); |
|
| 2079 | - |
|
| 2080 | - $pathSegments = explode('/', $path); |
|
| 2081 | - if (isset($pathSegments[2])) { |
|
| 2082 | - // E.g.: /username/files/path-to-file |
|
| 2083 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2084 | - } |
|
| 2085 | - |
|
| 2086 | - return strpos($path, '/appdata_') !== 0; |
|
| 2087 | - } |
|
| 2088 | - |
|
| 2089 | - /** |
|
| 2090 | - * Shortens the given absolute path to be relative to |
|
| 2091 | - * "$user/files". |
|
| 2092 | - * |
|
| 2093 | - * @param string $absolutePath absolute path which is under "files" |
|
| 2094 | - * |
|
| 2095 | - * @return string path relative to "files" with trimmed slashes or null |
|
| 2096 | - * if the path was NOT relative to files |
|
| 2097 | - * |
|
| 2098 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2099 | - * @since 8.1.0 |
|
| 2100 | - */ |
|
| 2101 | - public function getPathRelativeToFiles($absolutePath) { |
|
| 2102 | - $path = Filesystem::normalizePath($absolutePath); |
|
| 2103 | - $parts = explode('/', trim($path, '/'), 3); |
|
| 2104 | - // "$user", "files", "path/to/dir" |
|
| 2105 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2106 | - $this->logger->error( |
|
| 2107 | - '$absolutePath must be relative to "files", value is "%s"', |
|
| 2108 | - [ |
|
| 2109 | - $absolutePath |
|
| 2110 | - ] |
|
| 2111 | - ); |
|
| 2112 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2113 | - } |
|
| 2114 | - if (isset($parts[2])) { |
|
| 2115 | - return $parts[2]; |
|
| 2116 | - } |
|
| 2117 | - return ''; |
|
| 2118 | - } |
|
| 2119 | - |
|
| 2120 | - /** |
|
| 2121 | - * @param string $filename |
|
| 2122 | - * @return array |
|
| 2123 | - * @throws \OC\User\NoUserException |
|
| 2124 | - * @throws NotFoundException |
|
| 2125 | - */ |
|
| 2126 | - public function getUidAndFilename($filename) { |
|
| 2127 | - $info = $this->getFileInfo($filename); |
|
| 2128 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2129 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2130 | - } |
|
| 2131 | - $uid = $info->getOwner()->getUID(); |
|
| 2132 | - if ($uid != \OCP\User::getUser()) { |
|
| 2133 | - Filesystem::initMountPoints($uid); |
|
| 2134 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2135 | - try { |
|
| 2136 | - $filename = $ownerView->getPath($info['fileid']); |
|
| 2137 | - } catch (NotFoundException $e) { |
|
| 2138 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2139 | - } |
|
| 2140 | - } |
|
| 2141 | - return [$uid, $filename]; |
|
| 2142 | - } |
|
| 2143 | - |
|
| 2144 | - /** |
|
| 2145 | - * Creates parent non-existing folders |
|
| 2146 | - * |
|
| 2147 | - * @param string $filePath |
|
| 2148 | - * @return bool |
|
| 2149 | - */ |
|
| 2150 | - private function createParentDirectories($filePath) { |
|
| 2151 | - $directoryParts = explode('/', $filePath); |
|
| 2152 | - $directoryParts = array_filter($directoryParts); |
|
| 2153 | - foreach ($directoryParts as $key => $part) { |
|
| 2154 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2155 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2156 | - if ($this->is_file($currentPath)) { |
|
| 2157 | - return false; |
|
| 2158 | - } |
|
| 2159 | - if (!$this->file_exists($currentPath)) { |
|
| 2160 | - $this->mkdir($currentPath); |
|
| 2161 | - } |
|
| 2162 | - } |
|
| 2163 | - |
|
| 2164 | - return true; |
|
| 2165 | - } |
|
| 84 | + /** @var string */ |
|
| 85 | + private $fakeRoot = ''; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @var \OCP\Lock\ILockingProvider |
|
| 89 | + */ |
|
| 90 | + protected $lockingProvider; |
|
| 91 | + |
|
| 92 | + private $lockingEnabled; |
|
| 93 | + |
|
| 94 | + private $updaterEnabled = true; |
|
| 95 | + |
|
| 96 | + /** @var \OC\User\Manager */ |
|
| 97 | + private $userManager; |
|
| 98 | + |
|
| 99 | + /** @var \OCP\ILogger */ |
|
| 100 | + private $logger; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $root |
|
| 104 | + * @throws \Exception If $root contains an invalid path |
|
| 105 | + */ |
|
| 106 | + public function __construct($root = '') { |
|
| 107 | + if (is_null($root)) { |
|
| 108 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 109 | + } |
|
| 110 | + if (!Filesystem::isValidPath($root)) { |
|
| 111 | + throw new \Exception(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $this->fakeRoot = $root; |
|
| 115 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 116 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 117 | + $this->userManager = \OC::$server->getUserManager(); |
|
| 118 | + $this->logger = \OC::$server->getLogger(); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function getAbsolutePath($path = '/') { |
|
| 122 | + if ($path === null) { |
|
| 123 | + return null; |
|
| 124 | + } |
|
| 125 | + $this->assertPathLength($path); |
|
| 126 | + if ($path === '') { |
|
| 127 | + $path = '/'; |
|
| 128 | + } |
|
| 129 | + if ($path[0] !== '/') { |
|
| 130 | + $path = '/' . $path; |
|
| 131 | + } |
|
| 132 | + return $this->fakeRoot . $path; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * change the root to a fake root |
|
| 137 | + * |
|
| 138 | + * @param string $fakeRoot |
|
| 139 | + * @return boolean|null |
|
| 140 | + */ |
|
| 141 | + public function chroot($fakeRoot) { |
|
| 142 | + if (!$fakeRoot == '') { |
|
| 143 | + if ($fakeRoot[0] !== '/') { |
|
| 144 | + $fakeRoot = '/' . $fakeRoot; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + $this->fakeRoot = $fakeRoot; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * get the fake root |
|
| 152 | + * |
|
| 153 | + * @return string |
|
| 154 | + */ |
|
| 155 | + public function getRoot() { |
|
| 156 | + return $this->fakeRoot; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * get path relative to the root of the view |
|
| 161 | + * |
|
| 162 | + * @param string $path |
|
| 163 | + * @return string |
|
| 164 | + */ |
|
| 165 | + public function getRelativePath($path) { |
|
| 166 | + $this->assertPathLength($path); |
|
| 167 | + if ($this->fakeRoot == '') { |
|
| 168 | + return $path; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 172 | + return '/'; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + // missing slashes can cause wrong matches! |
|
| 176 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 177 | + |
|
| 178 | + if (strpos($path, $root) !== 0) { |
|
| 179 | + return null; |
|
| 180 | + } else { |
|
| 181 | + $path = substr($path, strlen($this->fakeRoot)); |
|
| 182 | + if (strlen($path) === 0) { |
|
| 183 | + return '/'; |
|
| 184 | + } else { |
|
| 185 | + return $path; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * get the mountpoint of the storage object for a path |
|
| 192 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 193 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 194 | + * and does not take the chroot into account ) |
|
| 195 | + * |
|
| 196 | + * @param string $path |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + public function getMountPoint($path) { |
|
| 200 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * get the mountpoint of the storage object for a path |
|
| 205 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 206 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 207 | + * and does not take the chroot into account ) |
|
| 208 | + * |
|
| 209 | + * @param string $path |
|
| 210 | + * @return \OCP\Files\Mount\IMountPoint |
|
| 211 | + */ |
|
| 212 | + public function getMount($path) { |
|
| 213 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * resolve a path to a storage and internal path |
|
| 218 | + * |
|
| 219 | + * @param string $path |
|
| 220 | + * @return array an array consisting of the storage and the internal path |
|
| 221 | + */ |
|
| 222 | + public function resolvePath($path) { |
|
| 223 | + $a = $this->getAbsolutePath($path); |
|
| 224 | + $p = Filesystem::normalizePath($a); |
|
| 225 | + return Filesystem::resolvePath($p); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * return the path to a local version of the file |
|
| 230 | + * we need this because we can't know if a file is stored local or not from |
|
| 231 | + * outside the filestorage and for some purposes a local file is needed |
|
| 232 | + * |
|
| 233 | + * @param string $path |
|
| 234 | + * @return string |
|
| 235 | + */ |
|
| 236 | + public function getLocalFile($path) { |
|
| 237 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 238 | + $path = $this->getAbsolutePath($path); |
|
| 239 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 240 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 241 | + return $storage->getLocalFile($internalPath); |
|
| 242 | + } else { |
|
| 243 | + return null; |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @param string $path |
|
| 249 | + * @return string |
|
| 250 | + */ |
|
| 251 | + public function getLocalFolder($path) { |
|
| 252 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 253 | + $path = $this->getAbsolutePath($path); |
|
| 254 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 255 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 256 | + return $storage->getLocalFolder($internalPath); |
|
| 257 | + } else { |
|
| 258 | + return null; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * the following functions operate with arguments and return values identical |
|
| 264 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 265 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 266 | + */ |
|
| 267 | + public function mkdir($path) { |
|
| 268 | + return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * remove mount point |
|
| 273 | + * |
|
| 274 | + * @param \OC\Files\Mount\MoveableMount $mount |
|
| 275 | + * @param string $path relative to data/ |
|
| 276 | + * @return boolean |
|
| 277 | + */ |
|
| 278 | + protected function removeMount($mount, $path) { |
|
| 279 | + if ($mount instanceof MoveableMount) { |
|
| 280 | + // cut of /user/files to get the relative path to data/user/files |
|
| 281 | + $pathParts = explode('/', $path, 4); |
|
| 282 | + $relPath = '/' . $pathParts[3]; |
|
| 283 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 284 | + \OC_Hook::emit( |
|
| 285 | + Filesystem::CLASSNAME, "umount", |
|
| 286 | + array(Filesystem::signal_param_path => $relPath) |
|
| 287 | + ); |
|
| 288 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 289 | + $result = $mount->removeMount(); |
|
| 290 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 291 | + if ($result) { |
|
| 292 | + \OC_Hook::emit( |
|
| 293 | + Filesystem::CLASSNAME, "post_umount", |
|
| 294 | + array(Filesystem::signal_param_path => $relPath) |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 298 | + return $result; |
|
| 299 | + } else { |
|
| 300 | + // do not allow deleting the storage's root / the mount point |
|
| 301 | + // because for some storages it might delete the whole contents |
|
| 302 | + // but isn't supposed to work that way |
|
| 303 | + return false; |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + public function disableCacheUpdate() { |
|
| 308 | + $this->updaterEnabled = false; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + public function enableCacheUpdate() { |
|
| 312 | + $this->updaterEnabled = true; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 316 | + if ($this->updaterEnabled) { |
|
| 317 | + if (is_null($time)) { |
|
| 318 | + $time = time(); |
|
| 319 | + } |
|
| 320 | + $storage->getUpdater()->update($internalPath, $time); |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 325 | + if ($this->updaterEnabled) { |
|
| 326 | + $storage->getUpdater()->remove($internalPath); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 331 | + if ($this->updaterEnabled) { |
|
| 332 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * @param string $path |
|
| 338 | + * @return bool|mixed |
|
| 339 | + */ |
|
| 340 | + public function rmdir($path) { |
|
| 341 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 342 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 343 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 344 | + return $this->removeMount($mount, $absolutePath); |
|
| 345 | + } |
|
| 346 | + if ($this->is_dir($path)) { |
|
| 347 | + $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
| 348 | + } else { |
|
| 349 | + $result = false; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 353 | + $storage = $mount->getStorage(); |
|
| 354 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 355 | + $storage->getUpdater()->remove($internalPath); |
|
| 356 | + } |
|
| 357 | + return $result; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @param string $path |
|
| 362 | + * @return resource |
|
| 363 | + */ |
|
| 364 | + public function opendir($path) { |
|
| 365 | + return $this->basicOperation('opendir', $path, array('read')); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * @param string $path |
|
| 370 | + * @return bool|mixed |
|
| 371 | + */ |
|
| 372 | + public function is_dir($path) { |
|
| 373 | + if ($path == '/') { |
|
| 374 | + return true; |
|
| 375 | + } |
|
| 376 | + return $this->basicOperation('is_dir', $path); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * @param string $path |
|
| 381 | + * @return bool|mixed |
|
| 382 | + */ |
|
| 383 | + public function is_file($path) { |
|
| 384 | + if ($path == '/') { |
|
| 385 | + return false; |
|
| 386 | + } |
|
| 387 | + return $this->basicOperation('is_file', $path); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * @param string $path |
|
| 392 | + * @return mixed |
|
| 393 | + */ |
|
| 394 | + public function stat($path) { |
|
| 395 | + return $this->basicOperation('stat', $path); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * @param string $path |
|
| 400 | + * @return mixed |
|
| 401 | + */ |
|
| 402 | + public function filetype($path) { |
|
| 403 | + return $this->basicOperation('filetype', $path); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * @param string $path |
|
| 408 | + * @return mixed |
|
| 409 | + */ |
|
| 410 | + public function filesize($path) { |
|
| 411 | + return $this->basicOperation('filesize', $path); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * @param string $path |
|
| 416 | + * @return bool|mixed |
|
| 417 | + * @throws \OCP\Files\InvalidPathException |
|
| 418 | + */ |
|
| 419 | + public function readfile($path) { |
|
| 420 | + $this->assertPathLength($path); |
|
| 421 | + @ob_end_clean(); |
|
| 422 | + $handle = $this->fopen($path, 'rb'); |
|
| 423 | + if ($handle) { |
|
| 424 | + $chunkSize = 8192; // 8 kB chunks |
|
| 425 | + while (!feof($handle)) { |
|
| 426 | + echo fread($handle, $chunkSize); |
|
| 427 | + flush(); |
|
| 428 | + } |
|
| 429 | + fclose($handle); |
|
| 430 | + return $this->filesize($path); |
|
| 431 | + } |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * @param string $path |
|
| 437 | + * @param int $from |
|
| 438 | + * @param int $to |
|
| 439 | + * @return bool|mixed |
|
| 440 | + * @throws \OCP\Files\InvalidPathException |
|
| 441 | + * @throws \OCP\Files\UnseekableException |
|
| 442 | + */ |
|
| 443 | + public function readfilePart($path, $from, $to) { |
|
| 444 | + $this->assertPathLength($path); |
|
| 445 | + @ob_end_clean(); |
|
| 446 | + $handle = $this->fopen($path, 'rb'); |
|
| 447 | + if ($handle) { |
|
| 448 | + $chunkSize = 8192; // 8 kB chunks |
|
| 449 | + $startReading = true; |
|
| 450 | + |
|
| 451 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 452 | + // forward file handle via chunked fread because fseek seem to have failed |
|
| 453 | + |
|
| 454 | + $end = $from + 1; |
|
| 455 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 456 | + $len = $from - ftell($handle); |
|
| 457 | + if ($len > $chunkSize) { |
|
| 458 | + $len = $chunkSize; |
|
| 459 | + } |
|
| 460 | + $result = fread($handle, $len); |
|
| 461 | + |
|
| 462 | + if ($result === false) { |
|
| 463 | + $startReading = false; |
|
| 464 | + break; |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + if ($startReading) { |
|
| 470 | + $end = $to + 1; |
|
| 471 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 472 | + $len = $end - ftell($handle); |
|
| 473 | + if ($len > $chunkSize) { |
|
| 474 | + $len = $chunkSize; |
|
| 475 | + } |
|
| 476 | + echo fread($handle, $len); |
|
| 477 | + flush(); |
|
| 478 | + } |
|
| 479 | + return ftell($handle) - $from; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 483 | + } |
|
| 484 | + return false; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * @param string $path |
|
| 489 | + * @return mixed |
|
| 490 | + */ |
|
| 491 | + public function isCreatable($path) { |
|
| 492 | + return $this->basicOperation('isCreatable', $path); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * @param string $path |
|
| 497 | + * @return mixed |
|
| 498 | + */ |
|
| 499 | + public function isReadable($path) { |
|
| 500 | + return $this->basicOperation('isReadable', $path); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * @param string $path |
|
| 505 | + * @return mixed |
|
| 506 | + */ |
|
| 507 | + public function isUpdatable($path) { |
|
| 508 | + return $this->basicOperation('isUpdatable', $path); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * @param string $path |
|
| 513 | + * @return bool|mixed |
|
| 514 | + */ |
|
| 515 | + public function isDeletable($path) { |
|
| 516 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 517 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 518 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 519 | + return $mount instanceof MoveableMount; |
|
| 520 | + } |
|
| 521 | + return $this->basicOperation('isDeletable', $path); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * @param string $path |
|
| 526 | + * @return mixed |
|
| 527 | + */ |
|
| 528 | + public function isSharable($path) { |
|
| 529 | + return $this->basicOperation('isSharable', $path); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * @param string $path |
|
| 534 | + * @return bool|mixed |
|
| 535 | + */ |
|
| 536 | + public function file_exists($path) { |
|
| 537 | + if ($path == '/') { |
|
| 538 | + return true; |
|
| 539 | + } |
|
| 540 | + return $this->basicOperation('file_exists', $path); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + /** |
|
| 544 | + * @param string $path |
|
| 545 | + * @return mixed |
|
| 546 | + */ |
|
| 547 | + public function filemtime($path) { |
|
| 548 | + return $this->basicOperation('filemtime', $path); |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * @param string $path |
|
| 553 | + * @param int|string $mtime |
|
| 554 | + * @return bool |
|
| 555 | + */ |
|
| 556 | + public function touch($path, $mtime = null) { |
|
| 557 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 558 | + $mtime = strtotime($mtime); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + $hooks = array('touch'); |
|
| 562 | + |
|
| 563 | + if (!$this->file_exists($path)) { |
|
| 564 | + $hooks[] = 'create'; |
|
| 565 | + $hooks[] = 'write'; |
|
| 566 | + } |
|
| 567 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 568 | + if (!$result) { |
|
| 569 | + // If create file fails because of permissions on external storage like SMB folders, |
|
| 570 | + // check file exists and return false if not. |
|
| 571 | + if (!$this->file_exists($path)) { |
|
| 572 | + return false; |
|
| 573 | + } |
|
| 574 | + if (is_null($mtime)) { |
|
| 575 | + $mtime = time(); |
|
| 576 | + } |
|
| 577 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 578 | + $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
| 579 | + } |
|
| 580 | + return true; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * @param string $path |
|
| 585 | + * @return mixed |
|
| 586 | + */ |
|
| 587 | + public function file_get_contents($path) { |
|
| 588 | + return $this->basicOperation('file_get_contents', $path, array('read')); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + /** |
|
| 592 | + * @param bool $exists |
|
| 593 | + * @param string $path |
|
| 594 | + * @param bool $run |
|
| 595 | + */ |
|
| 596 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 597 | + if (!$exists) { |
|
| 598 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
| 599 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 600 | + Filesystem::signal_param_run => &$run, |
|
| 601 | + )); |
|
| 602 | + } else { |
|
| 603 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
| 604 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 605 | + Filesystem::signal_param_run => &$run, |
|
| 606 | + )); |
|
| 607 | + } |
|
| 608 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
| 609 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 610 | + Filesystem::signal_param_run => &$run, |
|
| 611 | + )); |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + /** |
|
| 615 | + * @param bool $exists |
|
| 616 | + * @param string $path |
|
| 617 | + */ |
|
| 618 | + protected function emit_file_hooks_post($exists, $path) { |
|
| 619 | + if (!$exists) { |
|
| 620 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
| 621 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 622 | + )); |
|
| 623 | + } else { |
|
| 624 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
| 625 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 626 | + )); |
|
| 627 | + } |
|
| 628 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
| 629 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 630 | + )); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * @param string $path |
|
| 635 | + * @param mixed $data |
|
| 636 | + * @return bool|mixed |
|
| 637 | + * @throws \Exception |
|
| 638 | + */ |
|
| 639 | + public function file_put_contents($path, $data) { |
|
| 640 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 641 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 642 | + if (Filesystem::isValidPath($path) |
|
| 643 | + and !Filesystem::isFileBlacklisted($path) |
|
| 644 | + ) { |
|
| 645 | + $path = $this->getRelativePath($absolutePath); |
|
| 646 | + |
|
| 647 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 648 | + |
|
| 649 | + $exists = $this->file_exists($path); |
|
| 650 | + $run = true; |
|
| 651 | + if ($this->shouldEmitHooks($path)) { |
|
| 652 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 653 | + } |
|
| 654 | + if (!$run) { |
|
| 655 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 656 | + return false; |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 660 | + |
|
| 661 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 662 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 663 | + $target = $storage->fopen($internalPath, 'w'); |
|
| 664 | + if ($target) { |
|
| 665 | + list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 666 | + fclose($target); |
|
| 667 | + fclose($data); |
|
| 668 | + |
|
| 669 | + $this->writeUpdate($storage, $internalPath); |
|
| 670 | + |
|
| 671 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 672 | + |
|
| 673 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 674 | + $this->emit_file_hooks_post($exists, $path); |
|
| 675 | + } |
|
| 676 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 677 | + return $result; |
|
| 678 | + } else { |
|
| 679 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 680 | + return false; |
|
| 681 | + } |
|
| 682 | + } else { |
|
| 683 | + return false; |
|
| 684 | + } |
|
| 685 | + } else { |
|
| 686 | + $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write'); |
|
| 687 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 688 | + } |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * @param string $path |
|
| 693 | + * @return bool|mixed |
|
| 694 | + */ |
|
| 695 | + public function unlink($path) { |
|
| 696 | + if ($path === '' || $path === '/') { |
|
| 697 | + // do not allow deleting the root |
|
| 698 | + return false; |
|
| 699 | + } |
|
| 700 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 701 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 702 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 703 | + if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
| 704 | + return $this->removeMount($mount, $absolutePath); |
|
| 705 | + } |
|
| 706 | + if ($this->is_dir($path)) { |
|
| 707 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 708 | + } else { |
|
| 709 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 710 | + } |
|
| 711 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 712 | + $storage = $mount->getStorage(); |
|
| 713 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 714 | + $storage->getUpdater()->remove($internalPath); |
|
| 715 | + return true; |
|
| 716 | + } else { |
|
| 717 | + return $result; |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + /** |
|
| 722 | + * @param string $directory |
|
| 723 | + * @return bool|mixed |
|
| 724 | + */ |
|
| 725 | + public function deleteAll($directory) { |
|
| 726 | + return $this->rmdir($directory); |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + /** |
|
| 730 | + * Rename/move a file or folder from the source path to target path. |
|
| 731 | + * |
|
| 732 | + * @param string $path1 source path |
|
| 733 | + * @param string $path2 target path |
|
| 734 | + * |
|
| 735 | + * @return bool|mixed |
|
| 736 | + */ |
|
| 737 | + public function rename($path1, $path2) { |
|
| 738 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 739 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 740 | + $result = false; |
|
| 741 | + if ( |
|
| 742 | + Filesystem::isValidPath($path2) |
|
| 743 | + and Filesystem::isValidPath($path1) |
|
| 744 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 745 | + ) { |
|
| 746 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 747 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 748 | + $exists = $this->file_exists($path2); |
|
| 749 | + |
|
| 750 | + if ($path1 == null or $path2 == null) { |
|
| 751 | + return false; |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 755 | + try { |
|
| 756 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 757 | + |
|
| 758 | + $run = true; |
|
| 759 | + if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 760 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 761 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 762 | + } elseif ($this->shouldEmitHooks($path1)) { |
|
| 763 | + \OC_Hook::emit( |
|
| 764 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 765 | + array( |
|
| 766 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 767 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 768 | + Filesystem::signal_param_run => &$run |
|
| 769 | + ) |
|
| 770 | + ); |
|
| 771 | + } |
|
| 772 | + if ($run) { |
|
| 773 | + $this->verifyPath(dirname($path2), basename($path2)); |
|
| 774 | + |
|
| 775 | + $manager = Filesystem::getMountManager(); |
|
| 776 | + $mount1 = $this->getMount($path1); |
|
| 777 | + $mount2 = $this->getMount($path2); |
|
| 778 | + $storage1 = $mount1->getStorage(); |
|
| 779 | + $storage2 = $mount2->getStorage(); |
|
| 780 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 781 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 782 | + |
|
| 783 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 784 | + try { |
|
| 785 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 786 | + |
|
| 787 | + if ($internalPath1 === '') { |
|
| 788 | + if ($mount1 instanceof MoveableMount) { |
|
| 789 | + if ($this->isTargetAllowed($absolutePath2)) { |
|
| 790 | + /** |
|
| 791 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 792 | + */ |
|
| 793 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
| 794 | + $result = $mount1->moveMount($absolutePath2); |
|
| 795 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 796 | + } else { |
|
| 797 | + $result = false; |
|
| 798 | + } |
|
| 799 | + } else { |
|
| 800 | + $result = false; |
|
| 801 | + } |
|
| 802 | + // moving a file/folder within the same mount point |
|
| 803 | + } elseif ($storage1 === $storage2) { |
|
| 804 | + if ($storage1) { |
|
| 805 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 806 | + } else { |
|
| 807 | + $result = false; |
|
| 808 | + } |
|
| 809 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 810 | + } else { |
|
| 811 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 815 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 816 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 817 | + } else if ($result) { |
|
| 818 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 819 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 820 | + } |
|
| 821 | + } |
|
| 822 | + } catch(\Exception $e) { |
|
| 823 | + throw $e; |
|
| 824 | + } finally { |
|
| 825 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 826 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 830 | + if ($this->shouldEmitHooks()) { |
|
| 831 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 832 | + } |
|
| 833 | + } elseif ($result) { |
|
| 834 | + if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 835 | + \OC_Hook::emit( |
|
| 836 | + Filesystem::CLASSNAME, |
|
| 837 | + Filesystem::signal_post_rename, |
|
| 838 | + array( |
|
| 839 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 840 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 841 | + ) |
|
| 842 | + ); |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + } catch(\Exception $e) { |
|
| 847 | + throw $e; |
|
| 848 | + } finally { |
|
| 849 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 850 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 851 | + } |
|
| 852 | + } |
|
| 853 | + return $result; |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * Copy a file/folder from the source path to target path |
|
| 858 | + * |
|
| 859 | + * @param string $path1 source path |
|
| 860 | + * @param string $path2 target path |
|
| 861 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 862 | + * |
|
| 863 | + * @return bool|mixed |
|
| 864 | + */ |
|
| 865 | + public function copy($path1, $path2, $preserveMtime = false) { |
|
| 866 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 867 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 868 | + $result = false; |
|
| 869 | + if ( |
|
| 870 | + Filesystem::isValidPath($path2) |
|
| 871 | + and Filesystem::isValidPath($path1) |
|
| 872 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 873 | + ) { |
|
| 874 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 875 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 876 | + |
|
| 877 | + if ($path1 == null or $path2 == null) { |
|
| 878 | + return false; |
|
| 879 | + } |
|
| 880 | + $run = true; |
|
| 881 | + |
|
| 882 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 883 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 884 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 885 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 886 | + |
|
| 887 | + try { |
|
| 888 | + |
|
| 889 | + $exists = $this->file_exists($path2); |
|
| 890 | + if ($this->shouldEmitHooks()) { |
|
| 891 | + \OC_Hook::emit( |
|
| 892 | + Filesystem::CLASSNAME, |
|
| 893 | + Filesystem::signal_copy, |
|
| 894 | + array( |
|
| 895 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 896 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 897 | + Filesystem::signal_param_run => &$run |
|
| 898 | + ) |
|
| 899 | + ); |
|
| 900 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 901 | + } |
|
| 902 | + if ($run) { |
|
| 903 | + $mount1 = $this->getMount($path1); |
|
| 904 | + $mount2 = $this->getMount($path2); |
|
| 905 | + $storage1 = $mount1->getStorage(); |
|
| 906 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 907 | + $storage2 = $mount2->getStorage(); |
|
| 908 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 909 | + |
|
| 910 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 911 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 912 | + |
|
| 913 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 914 | + if ($storage1) { |
|
| 915 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 916 | + } else { |
|
| 917 | + $result = false; |
|
| 918 | + } |
|
| 919 | + } else { |
|
| 920 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 924 | + |
|
| 925 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 926 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 927 | + |
|
| 928 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
| 929 | + \OC_Hook::emit( |
|
| 930 | + Filesystem::CLASSNAME, |
|
| 931 | + Filesystem::signal_post_copy, |
|
| 932 | + array( |
|
| 933 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 934 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 935 | + ) |
|
| 936 | + ); |
|
| 937 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + } |
|
| 941 | + } catch (\Exception $e) { |
|
| 942 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 943 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 944 | + throw $e; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 948 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 949 | + |
|
| 950 | + } |
|
| 951 | + return $result; |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * @param string $path |
|
| 956 | + * @param string $mode 'r' or 'w' |
|
| 957 | + * @return resource |
|
| 958 | + */ |
|
| 959 | + public function fopen($path, $mode) { |
|
| 960 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 961 | + $hooks = array(); |
|
| 962 | + switch ($mode) { |
|
| 963 | + case 'r': |
|
| 964 | + $hooks[] = 'read'; |
|
| 965 | + break; |
|
| 966 | + case 'r+': |
|
| 967 | + case 'w+': |
|
| 968 | + case 'x+': |
|
| 969 | + case 'a+': |
|
| 970 | + $hooks[] = 'read'; |
|
| 971 | + $hooks[] = 'write'; |
|
| 972 | + break; |
|
| 973 | + case 'w': |
|
| 974 | + case 'x': |
|
| 975 | + case 'a': |
|
| 976 | + $hooks[] = 'write'; |
|
| 977 | + break; |
|
| 978 | + default: |
|
| 979 | + \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + if ($mode !== 'r' && $mode !== 'w') { |
|
| 983 | + \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
| 984 | + } |
|
| 985 | + |
|
| 986 | + return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * @param string $path |
|
| 991 | + * @return bool|string |
|
| 992 | + * @throws \OCP\Files\InvalidPathException |
|
| 993 | + */ |
|
| 994 | + public function toTmpFile($path) { |
|
| 995 | + $this->assertPathLength($path); |
|
| 996 | + if (Filesystem::isValidPath($path)) { |
|
| 997 | + $source = $this->fopen($path, 'r'); |
|
| 998 | + if ($source) { |
|
| 999 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 1000 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1001 | + file_put_contents($tmpFile, $source); |
|
| 1002 | + return $tmpFile; |
|
| 1003 | + } else { |
|
| 1004 | + return false; |
|
| 1005 | + } |
|
| 1006 | + } else { |
|
| 1007 | + return false; |
|
| 1008 | + } |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + /** |
|
| 1012 | + * @param string $tmpFile |
|
| 1013 | + * @param string $path |
|
| 1014 | + * @return bool|mixed |
|
| 1015 | + * @throws \OCP\Files\InvalidPathException |
|
| 1016 | + */ |
|
| 1017 | + public function fromTmpFile($tmpFile, $path) { |
|
| 1018 | + $this->assertPathLength($path); |
|
| 1019 | + if (Filesystem::isValidPath($path)) { |
|
| 1020 | + |
|
| 1021 | + // Get directory that the file is going into |
|
| 1022 | + $filePath = dirname($path); |
|
| 1023 | + |
|
| 1024 | + // Create the directories if any |
|
| 1025 | + if (!$this->file_exists($filePath)) { |
|
| 1026 | + $result = $this->createParentDirectories($filePath); |
|
| 1027 | + if ($result === false) { |
|
| 1028 | + return false; |
|
| 1029 | + } |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + $source = fopen($tmpFile, 'r'); |
|
| 1033 | + if ($source) { |
|
| 1034 | + $result = $this->file_put_contents($path, $source); |
|
| 1035 | + // $this->file_put_contents() might have already closed |
|
| 1036 | + // the resource, so we check it, before trying to close it |
|
| 1037 | + // to avoid messages in the error log. |
|
| 1038 | + if (is_resource($source)) { |
|
| 1039 | + fclose($source); |
|
| 1040 | + } |
|
| 1041 | + unlink($tmpFile); |
|
| 1042 | + return $result; |
|
| 1043 | + } else { |
|
| 1044 | + return false; |
|
| 1045 | + } |
|
| 1046 | + } else { |
|
| 1047 | + return false; |
|
| 1048 | + } |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + |
|
| 1052 | + /** |
|
| 1053 | + * @param string $path |
|
| 1054 | + * @return mixed |
|
| 1055 | + * @throws \OCP\Files\InvalidPathException |
|
| 1056 | + */ |
|
| 1057 | + public function getMimeType($path) { |
|
| 1058 | + $this->assertPathLength($path); |
|
| 1059 | + return $this->basicOperation('getMimeType', $path); |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + /** |
|
| 1063 | + * @param string $type |
|
| 1064 | + * @param string $path |
|
| 1065 | + * @param bool $raw |
|
| 1066 | + * @return bool|null|string |
|
| 1067 | + */ |
|
| 1068 | + public function hash($type, $path, $raw = false) { |
|
| 1069 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1070 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1071 | + if (Filesystem::isValidPath($path)) { |
|
| 1072 | + $path = $this->getRelativePath($absolutePath); |
|
| 1073 | + if ($path == null) { |
|
| 1074 | + return false; |
|
| 1075 | + } |
|
| 1076 | + if ($this->shouldEmitHooks($path)) { |
|
| 1077 | + \OC_Hook::emit( |
|
| 1078 | + Filesystem::CLASSNAME, |
|
| 1079 | + Filesystem::signal_read, |
|
| 1080 | + array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
| 1081 | + ); |
|
| 1082 | + } |
|
| 1083 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1084 | + if ($storage) { |
|
| 1085 | + return $storage->hash($type, $internalPath, $raw); |
|
| 1086 | + } |
|
| 1087 | + } |
|
| 1088 | + return null; |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + /** |
|
| 1092 | + * @param string $path |
|
| 1093 | + * @return mixed |
|
| 1094 | + * @throws \OCP\Files\InvalidPathException |
|
| 1095 | + */ |
|
| 1096 | + public function free_space($path = '/') { |
|
| 1097 | + $this->assertPathLength($path); |
|
| 1098 | + $result = $this->basicOperation('free_space', $path); |
|
| 1099 | + if ($result === null) { |
|
| 1100 | + throw new InvalidPathException(); |
|
| 1101 | + } |
|
| 1102 | + return $result; |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + /** |
|
| 1106 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1107 | + * |
|
| 1108 | + * @param string $operation |
|
| 1109 | + * @param string $path |
|
| 1110 | + * @param array $hooks (optional) |
|
| 1111 | + * @param mixed $extraParam (optional) |
|
| 1112 | + * @return mixed |
|
| 1113 | + * @throws \Exception |
|
| 1114 | + * |
|
| 1115 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1116 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1117 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1118 | + */ |
|
| 1119 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1120 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1121 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1122 | + if (Filesystem::isValidPath($path) |
|
| 1123 | + and !Filesystem::isFileBlacklisted($path) |
|
| 1124 | + ) { |
|
| 1125 | + $path = $this->getRelativePath($absolutePath); |
|
| 1126 | + if ($path == null) { |
|
| 1127 | + return false; |
|
| 1128 | + } |
|
| 1129 | + |
|
| 1130 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1131 | + // always a shared lock during pre-hooks so the hook can read the file |
|
| 1132 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1133 | + } |
|
| 1134 | + |
|
| 1135 | + $run = $this->runHooks($hooks, $path); |
|
| 1136 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1137 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1138 | + if ($run and $storage) { |
|
| 1139 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1140 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1141 | + } |
|
| 1142 | + try { |
|
| 1143 | + if (!is_null($extraParam)) { |
|
| 1144 | + $result = $storage->$operation($internalPath, $extraParam); |
|
| 1145 | + } else { |
|
| 1146 | + $result = $storage->$operation($internalPath); |
|
| 1147 | + } |
|
| 1148 | + } catch (\Exception $e) { |
|
| 1149 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1150 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1151 | + } else if (in_array('read', $hooks)) { |
|
| 1152 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1153 | + } |
|
| 1154 | + throw $e; |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + if ($result && in_array('delete', $hooks) and $result) { |
|
| 1158 | + $this->removeUpdate($storage, $internalPath); |
|
| 1159 | + } |
|
| 1160 | + if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
| 1161 | + $this->writeUpdate($storage, $internalPath); |
|
| 1162 | + } |
|
| 1163 | + if ($result && in_array('touch', $hooks)) { |
|
| 1164 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1168 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + $unlockLater = false; |
|
| 1172 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1173 | + $unlockLater = true; |
|
| 1174 | + // make sure our unlocking callback will still be called if connection is aborted |
|
| 1175 | + ignore_user_abort(true); |
|
| 1176 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1177 | + if (in_array('write', $hooks)) { |
|
| 1178 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1179 | + } else if (in_array('read', $hooks)) { |
|
| 1180 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1181 | + } |
|
| 1182 | + }); |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1186 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1187 | + $this->runHooks($hooks, $path, true); |
|
| 1188 | + } |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + if (!$unlockLater |
|
| 1192 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1193 | + ) { |
|
| 1194 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1195 | + } |
|
| 1196 | + return $result; |
|
| 1197 | + } else { |
|
| 1198 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1199 | + } |
|
| 1200 | + } |
|
| 1201 | + return null; |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + /** |
|
| 1205 | + * get the path relative to the default root for hook usage |
|
| 1206 | + * |
|
| 1207 | + * @param string $path |
|
| 1208 | + * @return string |
|
| 1209 | + */ |
|
| 1210 | + private function getHookPath($path) { |
|
| 1211 | + if (!Filesystem::getView()) { |
|
| 1212 | + return $path; |
|
| 1213 | + } |
|
| 1214 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + private function shouldEmitHooks($path = '') { |
|
| 1218 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1219 | + return false; |
|
| 1220 | + } |
|
| 1221 | + if (!Filesystem::$loaded) { |
|
| 1222 | + return false; |
|
| 1223 | + } |
|
| 1224 | + $defaultRoot = Filesystem::getRoot(); |
|
| 1225 | + if ($defaultRoot === null) { |
|
| 1226 | + return false; |
|
| 1227 | + } |
|
| 1228 | + if ($this->fakeRoot === $defaultRoot) { |
|
| 1229 | + return true; |
|
| 1230 | + } |
|
| 1231 | + $fullPath = $this->getAbsolutePath($path); |
|
| 1232 | + |
|
| 1233 | + if ($fullPath === $defaultRoot) { |
|
| 1234 | + return true; |
|
| 1235 | + } |
|
| 1236 | + |
|
| 1237 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1238 | + } |
|
| 1239 | + |
|
| 1240 | + /** |
|
| 1241 | + * @param string[] $hooks |
|
| 1242 | + * @param string $path |
|
| 1243 | + * @param bool $post |
|
| 1244 | + * @return bool |
|
| 1245 | + */ |
|
| 1246 | + private function runHooks($hooks, $path, $post = false) { |
|
| 1247 | + $relativePath = $path; |
|
| 1248 | + $path = $this->getHookPath($path); |
|
| 1249 | + $prefix = $post ? 'post_' : ''; |
|
| 1250 | + $run = true; |
|
| 1251 | + if ($this->shouldEmitHooks($relativePath)) { |
|
| 1252 | + foreach ($hooks as $hook) { |
|
| 1253 | + if ($hook != 'read') { |
|
| 1254 | + \OC_Hook::emit( |
|
| 1255 | + Filesystem::CLASSNAME, |
|
| 1256 | + $prefix . $hook, |
|
| 1257 | + array( |
|
| 1258 | + Filesystem::signal_param_run => &$run, |
|
| 1259 | + Filesystem::signal_param_path => $path |
|
| 1260 | + ) |
|
| 1261 | + ); |
|
| 1262 | + } elseif (!$post) { |
|
| 1263 | + \OC_Hook::emit( |
|
| 1264 | + Filesystem::CLASSNAME, |
|
| 1265 | + $prefix . $hook, |
|
| 1266 | + array( |
|
| 1267 | + Filesystem::signal_param_path => $path |
|
| 1268 | + ) |
|
| 1269 | + ); |
|
| 1270 | + } |
|
| 1271 | + } |
|
| 1272 | + } |
|
| 1273 | + return $run; |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + /** |
|
| 1277 | + * check if a file or folder has been updated since $time |
|
| 1278 | + * |
|
| 1279 | + * @param string $path |
|
| 1280 | + * @param int $time |
|
| 1281 | + * @return bool |
|
| 1282 | + */ |
|
| 1283 | + public function hasUpdated($path, $time) { |
|
| 1284 | + return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + /** |
|
| 1288 | + * @param string $ownerId |
|
| 1289 | + * @return \OC\User\User |
|
| 1290 | + */ |
|
| 1291 | + private function getUserObjectForOwner($ownerId) { |
|
| 1292 | + $owner = $this->userManager->get($ownerId); |
|
| 1293 | + if ($owner instanceof IUser) { |
|
| 1294 | + return $owner; |
|
| 1295 | + } else { |
|
| 1296 | + return new User($ownerId, null); |
|
| 1297 | + } |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + /** |
|
| 1301 | + * Get file info from cache |
|
| 1302 | + * |
|
| 1303 | + * If the file is not in cached it will be scanned |
|
| 1304 | + * If the file has changed on storage the cache will be updated |
|
| 1305 | + * |
|
| 1306 | + * @param \OC\Files\Storage\Storage $storage |
|
| 1307 | + * @param string $internalPath |
|
| 1308 | + * @param string $relativePath |
|
| 1309 | + * @return ICacheEntry|bool |
|
| 1310 | + */ |
|
| 1311 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1312 | + $cache = $storage->getCache($internalPath); |
|
| 1313 | + $data = $cache->get($internalPath); |
|
| 1314 | + $watcher = $storage->getWatcher($internalPath); |
|
| 1315 | + |
|
| 1316 | + try { |
|
| 1317 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1318 | + if (!$data || $data['size'] === -1) { |
|
| 1319 | + if (!$storage->file_exists($internalPath)) { |
|
| 1320 | + return false; |
|
| 1321 | + } |
|
| 1322 | + // don't need to get a lock here since the scanner does it's own locking |
|
| 1323 | + $scanner = $storage->getScanner($internalPath); |
|
| 1324 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1325 | + $data = $cache->get($internalPath); |
|
| 1326 | + } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1327 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1328 | + $watcher->update($internalPath, $data); |
|
| 1329 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1330 | + $data = $cache->get($internalPath); |
|
| 1331 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1332 | + } |
|
| 1333 | + } catch (LockedException $e) { |
|
| 1334 | + // if the file is locked we just use the old cache info |
|
| 1335 | + } |
|
| 1336 | + |
|
| 1337 | + return $data; |
|
| 1338 | + } |
|
| 1339 | + |
|
| 1340 | + /** |
|
| 1341 | + * get the filesystem info |
|
| 1342 | + * |
|
| 1343 | + * @param string $path |
|
| 1344 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1345 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1346 | + * defaults to true |
|
| 1347 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1348 | + */ |
|
| 1349 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1350 | + $this->assertPathLength($path); |
|
| 1351 | + if (!Filesystem::isValidPath($path)) { |
|
| 1352 | + return false; |
|
| 1353 | + } |
|
| 1354 | + if (Cache\Scanner::isPartialFile($path)) { |
|
| 1355 | + return $this->getPartFileInfo($path); |
|
| 1356 | + } |
|
| 1357 | + $relativePath = $path; |
|
| 1358 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1359 | + |
|
| 1360 | + $mount = Filesystem::getMountManager()->find($path); |
|
| 1361 | + if (!$mount) { |
|
| 1362 | + \OC::$server->getLogger()->warning('Mountpoint not found for path: ' . $path); |
|
| 1363 | + return false; |
|
| 1364 | + } |
|
| 1365 | + $storage = $mount->getStorage(); |
|
| 1366 | + $internalPath = $mount->getInternalPath($path); |
|
| 1367 | + if ($storage) { |
|
| 1368 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1369 | + |
|
| 1370 | + if (!$data instanceof ICacheEntry) { |
|
| 1371 | + \OC::$server->getLogger()->debug('No cache entry found for ' . $path . ' (storage: ' . $storage->getId() . ', internalPath: ' . $internalPath . ')'); |
|
| 1372 | + return false; |
|
| 1373 | + } |
|
| 1374 | + |
|
| 1375 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1376 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
| 1380 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1381 | + |
|
| 1382 | + if ($data and isset($data['fileid'])) { |
|
| 1383 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1384 | + //add the sizes of other mount points to the folder |
|
| 1385 | + $extOnly = ($includeMountPoints === 'ext'); |
|
| 1386 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1387 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1388 | + $subStorage = $mount->getStorage(); |
|
| 1389 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1390 | + })); |
|
| 1391 | + } |
|
| 1392 | + } |
|
| 1393 | + |
|
| 1394 | + return $info; |
|
| 1395 | + } else { |
|
| 1396 | + \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint()); |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + return false; |
|
| 1400 | + } |
|
| 1401 | + |
|
| 1402 | + /** |
|
| 1403 | + * get the content of a directory |
|
| 1404 | + * |
|
| 1405 | + * @param string $directory path under datadirectory |
|
| 1406 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1407 | + * @return FileInfo[] |
|
| 1408 | + */ |
|
| 1409 | + public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 1410 | + $this->assertPathLength($directory); |
|
| 1411 | + if (!Filesystem::isValidPath($directory)) { |
|
| 1412 | + return []; |
|
| 1413 | + } |
|
| 1414 | + $path = $this->getAbsolutePath($directory); |
|
| 1415 | + $path = Filesystem::normalizePath($path); |
|
| 1416 | + $mount = $this->getMount($directory); |
|
| 1417 | + if (!$mount) { |
|
| 1418 | + return []; |
|
| 1419 | + } |
|
| 1420 | + $storage = $mount->getStorage(); |
|
| 1421 | + $internalPath = $mount->getInternalPath($path); |
|
| 1422 | + if ($storage) { |
|
| 1423 | + $cache = $storage->getCache($internalPath); |
|
| 1424 | + $user = \OC_User::getUser(); |
|
| 1425 | + |
|
| 1426 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1427 | + |
|
| 1428 | + if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1429 | + return []; |
|
| 1430 | + } |
|
| 1431 | + |
|
| 1432 | + $folderId = $data['fileid']; |
|
| 1433 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1434 | + |
|
| 1435 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1436 | + /** |
|
| 1437 | + * @var \OC\Files\FileInfo[] $files |
|
| 1438 | + */ |
|
| 1439 | + $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1440 | + if ($sharingDisabled) { |
|
| 1441 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1442 | + } |
|
| 1443 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1444 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1445 | + }, $contents); |
|
| 1446 | + |
|
| 1447 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1448 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1449 | + $dirLength = strlen($path); |
|
| 1450 | + foreach ($mounts as $mount) { |
|
| 1451 | + $mountPoint = $mount->getMountPoint(); |
|
| 1452 | + $subStorage = $mount->getStorage(); |
|
| 1453 | + if ($subStorage) { |
|
| 1454 | + $subCache = $subStorage->getCache(''); |
|
| 1455 | + |
|
| 1456 | + $rootEntry = $subCache->get(''); |
|
| 1457 | + if (!$rootEntry) { |
|
| 1458 | + $subScanner = $subStorage->getScanner(''); |
|
| 1459 | + try { |
|
| 1460 | + $subScanner->scanFile(''); |
|
| 1461 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1462 | + continue; |
|
| 1463 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1464 | + continue; |
|
| 1465 | + } catch (\Exception $e) { |
|
| 1466 | + // sometimes when the storage is not available it can be any exception |
|
| 1467 | + \OC::$server->getLogger()->logException($e, [ |
|
| 1468 | + 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1469 | + 'level' => ILogger::ERROR, |
|
| 1470 | + 'app' => 'lib', |
|
| 1471 | + ]); |
|
| 1472 | + continue; |
|
| 1473 | + } |
|
| 1474 | + $rootEntry = $subCache->get(''); |
|
| 1475 | + } |
|
| 1476 | + |
|
| 1477 | + if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1478 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1479 | + if ($pos = strpos($relativePath, '/')) { |
|
| 1480 | + //mountpoint inside subfolder add size to the correct folder |
|
| 1481 | + $entryName = substr($relativePath, 0, $pos); |
|
| 1482 | + foreach ($files as &$entry) { |
|
| 1483 | + if ($entry->getName() === $entryName) { |
|
| 1484 | + $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1485 | + } |
|
| 1486 | + } |
|
| 1487 | + } else { //mountpoint in this folder, add an entry for it |
|
| 1488 | + $rootEntry['name'] = $relativePath; |
|
| 1489 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1490 | + $permissions = $rootEntry['permissions']; |
|
| 1491 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1492 | + // for shared files/folders we use the permissions given by the owner |
|
| 1493 | + if ($mount instanceof MoveableMount) { |
|
| 1494 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1495 | + } else { |
|
| 1496 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + //remove any existing entry with the same name |
|
| 1500 | + foreach ($files as $i => $file) { |
|
| 1501 | + if ($file['name'] === $rootEntry['name']) { |
|
| 1502 | + unset($files[$i]); |
|
| 1503 | + break; |
|
| 1504 | + } |
|
| 1505 | + } |
|
| 1506 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1507 | + |
|
| 1508 | + // if sharing was disabled for the user we remove the share permissions |
|
| 1509 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1510 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1511 | + } |
|
| 1512 | + |
|
| 1513 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1514 | + $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1515 | + } |
|
| 1516 | + } |
|
| 1517 | + } |
|
| 1518 | + } |
|
| 1519 | + |
|
| 1520 | + if ($mimetype_filter) { |
|
| 1521 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1522 | + if (strpos($mimetype_filter, '/')) { |
|
| 1523 | + return $file->getMimetype() === $mimetype_filter; |
|
| 1524 | + } else { |
|
| 1525 | + return $file->getMimePart() === $mimetype_filter; |
|
| 1526 | + } |
|
| 1527 | + }); |
|
| 1528 | + } |
|
| 1529 | + |
|
| 1530 | + return $files; |
|
| 1531 | + } else { |
|
| 1532 | + return []; |
|
| 1533 | + } |
|
| 1534 | + } |
|
| 1535 | + |
|
| 1536 | + /** |
|
| 1537 | + * change file metadata |
|
| 1538 | + * |
|
| 1539 | + * @param string $path |
|
| 1540 | + * @param array|\OCP\Files\FileInfo $data |
|
| 1541 | + * @return int |
|
| 1542 | + * |
|
| 1543 | + * returns the fileid of the updated file |
|
| 1544 | + */ |
|
| 1545 | + public function putFileInfo($path, $data) { |
|
| 1546 | + $this->assertPathLength($path); |
|
| 1547 | + if ($data instanceof FileInfo) { |
|
| 1548 | + $data = $data->getData(); |
|
| 1549 | + } |
|
| 1550 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1551 | + /** |
|
| 1552 | + * @var \OC\Files\Storage\Storage $storage |
|
| 1553 | + * @var string $internalPath |
|
| 1554 | + */ |
|
| 1555 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 1556 | + if ($storage) { |
|
| 1557 | + $cache = $storage->getCache($path); |
|
| 1558 | + |
|
| 1559 | + if (!$cache->inCache($internalPath)) { |
|
| 1560 | + $scanner = $storage->getScanner($internalPath); |
|
| 1561 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1562 | + } |
|
| 1563 | + |
|
| 1564 | + return $cache->put($internalPath, $data); |
|
| 1565 | + } else { |
|
| 1566 | + return -1; |
|
| 1567 | + } |
|
| 1568 | + } |
|
| 1569 | + |
|
| 1570 | + /** |
|
| 1571 | + * search for files with the name matching $query |
|
| 1572 | + * |
|
| 1573 | + * @param string $query |
|
| 1574 | + * @return FileInfo[] |
|
| 1575 | + */ |
|
| 1576 | + public function search($query) { |
|
| 1577 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1578 | + } |
|
| 1579 | + |
|
| 1580 | + /** |
|
| 1581 | + * search for files with the name matching $query |
|
| 1582 | + * |
|
| 1583 | + * @param string $query |
|
| 1584 | + * @return FileInfo[] |
|
| 1585 | + */ |
|
| 1586 | + public function searchRaw($query) { |
|
| 1587 | + return $this->searchCommon('search', array($query)); |
|
| 1588 | + } |
|
| 1589 | + |
|
| 1590 | + /** |
|
| 1591 | + * search for files by mimetype |
|
| 1592 | + * |
|
| 1593 | + * @param string $mimetype |
|
| 1594 | + * @return FileInfo[] |
|
| 1595 | + */ |
|
| 1596 | + public function searchByMime($mimetype) { |
|
| 1597 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 1598 | + } |
|
| 1599 | + |
|
| 1600 | + /** |
|
| 1601 | + * search for files by tag |
|
| 1602 | + * |
|
| 1603 | + * @param string|int $tag name or tag id |
|
| 1604 | + * @param string $userId owner of the tags |
|
| 1605 | + * @return FileInfo[] |
|
| 1606 | + */ |
|
| 1607 | + public function searchByTag($tag, $userId) { |
|
| 1608 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 1609 | + } |
|
| 1610 | + |
|
| 1611 | + /** |
|
| 1612 | + * @param string $method cache method |
|
| 1613 | + * @param array $args |
|
| 1614 | + * @return FileInfo[] |
|
| 1615 | + */ |
|
| 1616 | + private function searchCommon($method, $args) { |
|
| 1617 | + $files = array(); |
|
| 1618 | + $rootLength = strlen($this->fakeRoot); |
|
| 1619 | + |
|
| 1620 | + $mount = $this->getMount(''); |
|
| 1621 | + $mountPoint = $mount->getMountPoint(); |
|
| 1622 | + $storage = $mount->getStorage(); |
|
| 1623 | + if ($storage) { |
|
| 1624 | + $cache = $storage->getCache(''); |
|
| 1625 | + |
|
| 1626 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 1627 | + foreach ($results as $result) { |
|
| 1628 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1629 | + $internalPath = $result['path']; |
|
| 1630 | + $path = $mountPoint . $result['path']; |
|
| 1631 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1632 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1633 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1634 | + } |
|
| 1635 | + } |
|
| 1636 | + |
|
| 1637 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1638 | + foreach ($mounts as $mount) { |
|
| 1639 | + $mountPoint = $mount->getMountPoint(); |
|
| 1640 | + $storage = $mount->getStorage(); |
|
| 1641 | + if ($storage) { |
|
| 1642 | + $cache = $storage->getCache(''); |
|
| 1643 | + |
|
| 1644 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1645 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 1646 | + if ($results) { |
|
| 1647 | + foreach ($results as $result) { |
|
| 1648 | + $internalPath = $result['path']; |
|
| 1649 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1650 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1651 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1652 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1653 | + } |
|
| 1654 | + } |
|
| 1655 | + } |
|
| 1656 | + } |
|
| 1657 | + } |
|
| 1658 | + return $files; |
|
| 1659 | + } |
|
| 1660 | + |
|
| 1661 | + /** |
|
| 1662 | + * Get the owner for a file or folder |
|
| 1663 | + * |
|
| 1664 | + * @param string $path |
|
| 1665 | + * @return string the user id of the owner |
|
| 1666 | + * @throws NotFoundException |
|
| 1667 | + */ |
|
| 1668 | + public function getOwner($path) { |
|
| 1669 | + $info = $this->getFileInfo($path); |
|
| 1670 | + if (!$info) { |
|
| 1671 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1672 | + } |
|
| 1673 | + return $info->getOwner()->getUID(); |
|
| 1674 | + } |
|
| 1675 | + |
|
| 1676 | + /** |
|
| 1677 | + * get the ETag for a file or folder |
|
| 1678 | + * |
|
| 1679 | + * @param string $path |
|
| 1680 | + * @return string |
|
| 1681 | + */ |
|
| 1682 | + public function getETag($path) { |
|
| 1683 | + /** |
|
| 1684 | + * @var Storage\Storage $storage |
|
| 1685 | + * @var string $internalPath |
|
| 1686 | + */ |
|
| 1687 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1688 | + if ($storage) { |
|
| 1689 | + return $storage->getETag($internalPath); |
|
| 1690 | + } else { |
|
| 1691 | + return null; |
|
| 1692 | + } |
|
| 1693 | + } |
|
| 1694 | + |
|
| 1695 | + /** |
|
| 1696 | + * Get the path of a file by id, relative to the view |
|
| 1697 | + * |
|
| 1698 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1699 | + * |
|
| 1700 | + * @param int $id |
|
| 1701 | + * @throws NotFoundException |
|
| 1702 | + * @return string |
|
| 1703 | + */ |
|
| 1704 | + public function getPath($id) { |
|
| 1705 | + $id = (int)$id; |
|
| 1706 | + $manager = Filesystem::getMountManager(); |
|
| 1707 | + $mounts = $manager->findIn($this->fakeRoot); |
|
| 1708 | + $mounts[] = $manager->find($this->fakeRoot); |
|
| 1709 | + // reverse the array so we start with the storage this view is in |
|
| 1710 | + // which is the most likely to contain the file we're looking for |
|
| 1711 | + $mounts = array_reverse($mounts); |
|
| 1712 | + foreach ($mounts as $mount) { |
|
| 1713 | + /** |
|
| 1714 | + * @var \OC\Files\Mount\MountPoint $mount |
|
| 1715 | + */ |
|
| 1716 | + if ($mount->getStorage()) { |
|
| 1717 | + $cache = $mount->getStorage()->getCache(); |
|
| 1718 | + $internalPath = $cache->getPathById($id); |
|
| 1719 | + if (is_string($internalPath)) { |
|
| 1720 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1721 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1722 | + return $path; |
|
| 1723 | + } |
|
| 1724 | + } |
|
| 1725 | + } |
|
| 1726 | + } |
|
| 1727 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1728 | + } |
|
| 1729 | + |
|
| 1730 | + /** |
|
| 1731 | + * @param string $path |
|
| 1732 | + * @throws InvalidPathException |
|
| 1733 | + */ |
|
| 1734 | + private function assertPathLength($path) { |
|
| 1735 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1736 | + // Check for the string length - performed using isset() instead of strlen() |
|
| 1737 | + // because isset() is about 5x-40x faster. |
|
| 1738 | + if (isset($path[$maxLen])) { |
|
| 1739 | + $pathLen = strlen($path); |
|
| 1740 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1741 | + } |
|
| 1742 | + } |
|
| 1743 | + |
|
| 1744 | + /** |
|
| 1745 | + * check if it is allowed to move a mount point to a given target. |
|
| 1746 | + * It is not allowed to move a mount point into a different mount point or |
|
| 1747 | + * into an already shared folder |
|
| 1748 | + * |
|
| 1749 | + * @param string $target path |
|
| 1750 | + * @return boolean |
|
| 1751 | + */ |
|
| 1752 | + private function isTargetAllowed($target) { |
|
| 1753 | + |
|
| 1754 | + list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
| 1755 | + if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 1756 | + \OCP\Util::writeLog('files', |
|
| 1757 | + 'It is not allowed to move one mount point into another one', |
|
| 1758 | + ILogger::DEBUG); |
|
| 1759 | + return false; |
|
| 1760 | + } |
|
| 1761 | + |
|
| 1762 | + // note: cannot use the view because the target is already locked |
|
| 1763 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1764 | + if ($fileId === -1) { |
|
| 1765 | + // target might not exist, need to check parent instead |
|
| 1766 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1767 | + } |
|
| 1768 | + |
|
| 1769 | + // check if any of the parents were shared by the current owner (include collections) |
|
| 1770 | + $shares = \OCP\Share::getItemShared( |
|
| 1771 | + 'folder', |
|
| 1772 | + $fileId, |
|
| 1773 | + \OCP\Share::FORMAT_NONE, |
|
| 1774 | + null, |
|
| 1775 | + true |
|
| 1776 | + ); |
|
| 1777 | + |
|
| 1778 | + if (count($shares) > 0) { |
|
| 1779 | + \OCP\Util::writeLog('files', |
|
| 1780 | + 'It is not allowed to move one mount point into a shared folder', |
|
| 1781 | + ILogger::DEBUG); |
|
| 1782 | + return false; |
|
| 1783 | + } |
|
| 1784 | + |
|
| 1785 | + return true; |
|
| 1786 | + } |
|
| 1787 | + |
|
| 1788 | + /** |
|
| 1789 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1790 | + * |
|
| 1791 | + * @param string $path |
|
| 1792 | + * @return \OCP\Files\FileInfo |
|
| 1793 | + */ |
|
| 1794 | + private function getPartFileInfo($path) { |
|
| 1795 | + $mount = $this->getMount($path); |
|
| 1796 | + $storage = $mount->getStorage(); |
|
| 1797 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1798 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1799 | + return new FileInfo( |
|
| 1800 | + $this->getAbsolutePath($path), |
|
| 1801 | + $storage, |
|
| 1802 | + $internalPath, |
|
| 1803 | + [ |
|
| 1804 | + 'fileid' => null, |
|
| 1805 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1806 | + 'name' => basename($path), |
|
| 1807 | + 'etag' => null, |
|
| 1808 | + 'size' => $storage->filesize($internalPath), |
|
| 1809 | + 'mtime' => $storage->filemtime($internalPath), |
|
| 1810 | + 'encrypted' => false, |
|
| 1811 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1812 | + ], |
|
| 1813 | + $mount, |
|
| 1814 | + $owner |
|
| 1815 | + ); |
|
| 1816 | + } |
|
| 1817 | + |
|
| 1818 | + /** |
|
| 1819 | + * @param string $path |
|
| 1820 | + * @param string $fileName |
|
| 1821 | + * @throws InvalidPathException |
|
| 1822 | + */ |
|
| 1823 | + public function verifyPath($path, $fileName) { |
|
| 1824 | + try { |
|
| 1825 | + /** @type \OCP\Files\Storage $storage */ |
|
| 1826 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1827 | + $storage->verifyPath($internalPath, $fileName); |
|
| 1828 | + } catch (ReservedWordException $ex) { |
|
| 1829 | + $l = \OC::$server->getL10N('lib'); |
|
| 1830 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1831 | + } catch (InvalidCharacterInPathException $ex) { |
|
| 1832 | + $l = \OC::$server->getL10N('lib'); |
|
| 1833 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1834 | + } catch (FileNameTooLongException $ex) { |
|
| 1835 | + $l = \OC::$server->getL10N('lib'); |
|
| 1836 | + throw new InvalidPathException($l->t('File name is too long')); |
|
| 1837 | + } catch (InvalidDirectoryException $ex) { |
|
| 1838 | + $l = \OC::$server->getL10N('lib'); |
|
| 1839 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1840 | + } catch (EmptyFileNameException $ex) { |
|
| 1841 | + $l = \OC::$server->getL10N('lib'); |
|
| 1842 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1843 | + } |
|
| 1844 | + } |
|
| 1845 | + |
|
| 1846 | + /** |
|
| 1847 | + * get all parent folders of $path |
|
| 1848 | + * |
|
| 1849 | + * @param string $path |
|
| 1850 | + * @return string[] |
|
| 1851 | + */ |
|
| 1852 | + private function getParents($path) { |
|
| 1853 | + $path = trim($path, '/'); |
|
| 1854 | + if (!$path) { |
|
| 1855 | + return []; |
|
| 1856 | + } |
|
| 1857 | + |
|
| 1858 | + $parts = explode('/', $path); |
|
| 1859 | + |
|
| 1860 | + // remove the single file |
|
| 1861 | + array_pop($parts); |
|
| 1862 | + $result = array('/'); |
|
| 1863 | + $resultPath = ''; |
|
| 1864 | + foreach ($parts as $part) { |
|
| 1865 | + if ($part) { |
|
| 1866 | + $resultPath .= '/' . $part; |
|
| 1867 | + $result[] = $resultPath; |
|
| 1868 | + } |
|
| 1869 | + } |
|
| 1870 | + return $result; |
|
| 1871 | + } |
|
| 1872 | + |
|
| 1873 | + /** |
|
| 1874 | + * Returns the mount point for which to lock |
|
| 1875 | + * |
|
| 1876 | + * @param string $absolutePath absolute path |
|
| 1877 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1878 | + * is mounted directly on the given path, false otherwise |
|
| 1879 | + * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
| 1880 | + */ |
|
| 1881 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1882 | + $results = []; |
|
| 1883 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1884 | + if (!$mount) { |
|
| 1885 | + return $results; |
|
| 1886 | + } |
|
| 1887 | + |
|
| 1888 | + if ($useParentMount) { |
|
| 1889 | + // find out if something is mounted directly on the path |
|
| 1890 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1891 | + if ($internalPath === '') { |
|
| 1892 | + // resolve the parent mount instead |
|
| 1893 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1894 | + } |
|
| 1895 | + } |
|
| 1896 | + |
|
| 1897 | + return $mount; |
|
| 1898 | + } |
|
| 1899 | + |
|
| 1900 | + /** |
|
| 1901 | + * Lock the given path |
|
| 1902 | + * |
|
| 1903 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1904 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1905 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1906 | + * |
|
| 1907 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1908 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1909 | + */ |
|
| 1910 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1911 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1912 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1913 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1914 | + return false; |
|
| 1915 | + } |
|
| 1916 | + |
|
| 1917 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1918 | + if ($mount) { |
|
| 1919 | + try { |
|
| 1920 | + $storage = $mount->getStorage(); |
|
| 1921 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1922 | + $storage->acquireLock( |
|
| 1923 | + $mount->getInternalPath($absolutePath), |
|
| 1924 | + $type, |
|
| 1925 | + $this->lockingProvider |
|
| 1926 | + ); |
|
| 1927 | + } |
|
| 1928 | + } catch (\OCP\Lock\LockedException $e) { |
|
| 1929 | + // rethrow with the a human-readable path |
|
| 1930 | + throw new \OCP\Lock\LockedException( |
|
| 1931 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 1932 | + $e |
|
| 1933 | + ); |
|
| 1934 | + } |
|
| 1935 | + } |
|
| 1936 | + |
|
| 1937 | + return true; |
|
| 1938 | + } |
|
| 1939 | + |
|
| 1940 | + /** |
|
| 1941 | + * Change the lock type |
|
| 1942 | + * |
|
| 1943 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1944 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1945 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1946 | + * |
|
| 1947 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1948 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1949 | + */ |
|
| 1950 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1951 | + $path = Filesystem::normalizePath($path); |
|
| 1952 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1953 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1954 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1955 | + return false; |
|
| 1956 | + } |
|
| 1957 | + |
|
| 1958 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1959 | + if ($mount) { |
|
| 1960 | + try { |
|
| 1961 | + $storage = $mount->getStorage(); |
|
| 1962 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1963 | + $storage->changeLock( |
|
| 1964 | + $mount->getInternalPath($absolutePath), |
|
| 1965 | + $type, |
|
| 1966 | + $this->lockingProvider |
|
| 1967 | + ); |
|
| 1968 | + } |
|
| 1969 | + } catch (\OCP\Lock\LockedException $e) { |
|
| 1970 | + try { |
|
| 1971 | + // rethrow with the a human-readable path |
|
| 1972 | + throw new \OCP\Lock\LockedException( |
|
| 1973 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 1974 | + $e |
|
| 1975 | + ); |
|
| 1976 | + } catch (\InvalidArgumentException $e) { |
|
| 1977 | + throw new \OCP\Lock\LockedException( |
|
| 1978 | + $absolutePath, |
|
| 1979 | + $e |
|
| 1980 | + ); |
|
| 1981 | + } |
|
| 1982 | + } |
|
| 1983 | + } |
|
| 1984 | + |
|
| 1985 | + return true; |
|
| 1986 | + } |
|
| 1987 | + |
|
| 1988 | + /** |
|
| 1989 | + * Unlock the given path |
|
| 1990 | + * |
|
| 1991 | + * @param string $path the path of the file to unlock, relative to the view |
|
| 1992 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1993 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1994 | + * |
|
| 1995 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1996 | + */ |
|
| 1997 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 1998 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1999 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2000 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2001 | + return false; |
|
| 2002 | + } |
|
| 2003 | + |
|
| 2004 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2005 | + if ($mount) { |
|
| 2006 | + $storage = $mount->getStorage(); |
|
| 2007 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2008 | + $storage->releaseLock( |
|
| 2009 | + $mount->getInternalPath($absolutePath), |
|
| 2010 | + $type, |
|
| 2011 | + $this->lockingProvider |
|
| 2012 | + ); |
|
| 2013 | + } |
|
| 2014 | + } |
|
| 2015 | + |
|
| 2016 | + return true; |
|
| 2017 | + } |
|
| 2018 | + |
|
| 2019 | + /** |
|
| 2020 | + * Lock a path and all its parents up to the root of the view |
|
| 2021 | + * |
|
| 2022 | + * @param string $path the path of the file to lock relative to the view |
|
| 2023 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2024 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2025 | + * |
|
| 2026 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2027 | + */ |
|
| 2028 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2029 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2030 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2031 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2032 | + return false; |
|
| 2033 | + } |
|
| 2034 | + |
|
| 2035 | + $this->lockPath($path, $type, $lockMountPoint); |
|
| 2036 | + |
|
| 2037 | + $parents = $this->getParents($path); |
|
| 2038 | + foreach ($parents as $parent) { |
|
| 2039 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2040 | + } |
|
| 2041 | + |
|
| 2042 | + return true; |
|
| 2043 | + } |
|
| 2044 | + |
|
| 2045 | + /** |
|
| 2046 | + * Unlock a path and all its parents up to the root of the view |
|
| 2047 | + * |
|
| 2048 | + * @param string $path the path of the file to lock relative to the view |
|
| 2049 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2050 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2051 | + * |
|
| 2052 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2053 | + */ |
|
| 2054 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2055 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2056 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2057 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2058 | + return false; |
|
| 2059 | + } |
|
| 2060 | + |
|
| 2061 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2062 | + |
|
| 2063 | + $parents = $this->getParents($path); |
|
| 2064 | + foreach ($parents as $parent) { |
|
| 2065 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2066 | + } |
|
| 2067 | + |
|
| 2068 | + return true; |
|
| 2069 | + } |
|
| 2070 | + |
|
| 2071 | + /** |
|
| 2072 | + * Only lock files in data/user/files/ |
|
| 2073 | + * |
|
| 2074 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2075 | + * @return bool |
|
| 2076 | + */ |
|
| 2077 | + protected function shouldLockFile($path) { |
|
| 2078 | + $path = Filesystem::normalizePath($path); |
|
| 2079 | + |
|
| 2080 | + $pathSegments = explode('/', $path); |
|
| 2081 | + if (isset($pathSegments[2])) { |
|
| 2082 | + // E.g.: /username/files/path-to-file |
|
| 2083 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2084 | + } |
|
| 2085 | + |
|
| 2086 | + return strpos($path, '/appdata_') !== 0; |
|
| 2087 | + } |
|
| 2088 | + |
|
| 2089 | + /** |
|
| 2090 | + * Shortens the given absolute path to be relative to |
|
| 2091 | + * "$user/files". |
|
| 2092 | + * |
|
| 2093 | + * @param string $absolutePath absolute path which is under "files" |
|
| 2094 | + * |
|
| 2095 | + * @return string path relative to "files" with trimmed slashes or null |
|
| 2096 | + * if the path was NOT relative to files |
|
| 2097 | + * |
|
| 2098 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2099 | + * @since 8.1.0 |
|
| 2100 | + */ |
|
| 2101 | + public function getPathRelativeToFiles($absolutePath) { |
|
| 2102 | + $path = Filesystem::normalizePath($absolutePath); |
|
| 2103 | + $parts = explode('/', trim($path, '/'), 3); |
|
| 2104 | + // "$user", "files", "path/to/dir" |
|
| 2105 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2106 | + $this->logger->error( |
|
| 2107 | + '$absolutePath must be relative to "files", value is "%s"', |
|
| 2108 | + [ |
|
| 2109 | + $absolutePath |
|
| 2110 | + ] |
|
| 2111 | + ); |
|
| 2112 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2113 | + } |
|
| 2114 | + if (isset($parts[2])) { |
|
| 2115 | + return $parts[2]; |
|
| 2116 | + } |
|
| 2117 | + return ''; |
|
| 2118 | + } |
|
| 2119 | + |
|
| 2120 | + /** |
|
| 2121 | + * @param string $filename |
|
| 2122 | + * @return array |
|
| 2123 | + * @throws \OC\User\NoUserException |
|
| 2124 | + * @throws NotFoundException |
|
| 2125 | + */ |
|
| 2126 | + public function getUidAndFilename($filename) { |
|
| 2127 | + $info = $this->getFileInfo($filename); |
|
| 2128 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2129 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2130 | + } |
|
| 2131 | + $uid = $info->getOwner()->getUID(); |
|
| 2132 | + if ($uid != \OCP\User::getUser()) { |
|
| 2133 | + Filesystem::initMountPoints($uid); |
|
| 2134 | + $ownerView = new View('/' . $uid . '/files'); |
|
| 2135 | + try { |
|
| 2136 | + $filename = $ownerView->getPath($info['fileid']); |
|
| 2137 | + } catch (NotFoundException $e) { |
|
| 2138 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2139 | + } |
|
| 2140 | + } |
|
| 2141 | + return [$uid, $filename]; |
|
| 2142 | + } |
|
| 2143 | + |
|
| 2144 | + /** |
|
| 2145 | + * Creates parent non-existing folders |
|
| 2146 | + * |
|
| 2147 | + * @param string $filePath |
|
| 2148 | + * @return bool |
|
| 2149 | + */ |
|
| 2150 | + private function createParentDirectories($filePath) { |
|
| 2151 | + $directoryParts = explode('/', $filePath); |
|
| 2152 | + $directoryParts = array_filter($directoryParts); |
|
| 2153 | + foreach ($directoryParts as $key => $part) { |
|
| 2154 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2155 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2156 | + if ($this->is_file($currentPath)) { |
|
| 2157 | + return false; |
|
| 2158 | + } |
|
| 2159 | + if (!$this->file_exists($currentPath)) { |
|
| 2160 | + $this->mkdir($currentPath); |
|
| 2161 | + } |
|
| 2162 | + } |
|
| 2163 | + |
|
| 2164 | + return true; |
|
| 2165 | + } |
|
| 2166 | 2166 | } |
@@ -127,9 +127,9 @@ discard block |
||
| 127 | 127 | $path = '/'; |
| 128 | 128 | } |
| 129 | 129 | if ($path[0] !== '/') { |
| 130 | - $path = '/' . $path; |
|
| 130 | + $path = '/'.$path; |
|
| 131 | 131 | } |
| 132 | - return $this->fakeRoot . $path; |
|
| 132 | + return $this->fakeRoot.$path; |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /** |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | public function chroot($fakeRoot) { |
| 142 | 142 | if (!$fakeRoot == '') { |
| 143 | 143 | if ($fakeRoot[0] !== '/') { |
| 144 | - $fakeRoot = '/' . $fakeRoot; |
|
| 144 | + $fakeRoot = '/'.$fakeRoot; |
|
| 145 | 145 | } |
| 146 | 146 | } |
| 147 | 147 | $this->fakeRoot = $fakeRoot; |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | // missing slashes can cause wrong matches! |
| 176 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 176 | + $root = rtrim($this->fakeRoot, '/').'/'; |
|
| 177 | 177 | |
| 178 | 178 | if (strpos($path, $root) !== 0) { |
| 179 | 179 | return null; |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | if ($mount instanceof MoveableMount) { |
| 280 | 280 | // cut of /user/files to get the relative path to data/user/files |
| 281 | 281 | $pathParts = explode('/', $path, 4); |
| 282 | - $relPath = '/' . $pathParts[3]; |
|
| 282 | + $relPath = '/'.$pathParts[3]; |
|
| 283 | 283 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
| 284 | 284 | \OC_Hook::emit( |
| 285 | 285 | Filesystem::CLASSNAME, "umount", |
@@ -699,7 +699,7 @@ discard block |
||
| 699 | 699 | } |
| 700 | 700 | $postFix = (substr($path, -1) === '/') ? '/' : ''; |
| 701 | 701 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
| 702 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 702 | + $mount = Filesystem::getMountManager()->find($absolutePath.$postFix); |
|
| 703 | 703 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
| 704 | 704 | return $this->removeMount($mount, $absolutePath); |
| 705 | 705 | } |
@@ -819,7 +819,7 @@ discard block |
||
| 819 | 819 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
| 820 | 820 | } |
| 821 | 821 | } |
| 822 | - } catch(\Exception $e) { |
|
| 822 | + } catch (\Exception $e) { |
|
| 823 | 823 | throw $e; |
| 824 | 824 | } finally { |
| 825 | 825 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -843,7 +843,7 @@ discard block |
||
| 843 | 843 | } |
| 844 | 844 | } |
| 845 | 845 | } |
| 846 | - } catch(\Exception $e) { |
|
| 846 | + } catch (\Exception $e) { |
|
| 847 | 847 | throw $e; |
| 848 | 848 | } finally { |
| 849 | 849 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -976,7 +976,7 @@ discard block |
||
| 976 | 976 | $hooks[] = 'write'; |
| 977 | 977 | break; |
| 978 | 978 | default: |
| 979 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR); |
|
| 979 | + \OCP\Util::writeLog('core', 'invalid mode ('.$mode.') for '.$path, ILogger::ERROR); |
|
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | if ($mode !== 'r' && $mode !== 'w') { |
@@ -1080,7 +1080,7 @@ discard block |
||
| 1080 | 1080 | array(Filesystem::signal_param_path => $this->getHookPath($path)) |
| 1081 | 1081 | ); |
| 1082 | 1082 | } |
| 1083 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1083 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1084 | 1084 | if ($storage) { |
| 1085 | 1085 | return $storage->hash($type, $internalPath, $raw); |
| 1086 | 1086 | } |
@@ -1134,7 +1134,7 @@ discard block |
||
| 1134 | 1134 | |
| 1135 | 1135 | $run = $this->runHooks($hooks, $path); |
| 1136 | 1136 | /** @var \OC\Files\Storage\Storage $storage */ |
| 1137 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1137 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1138 | 1138 | if ($run and $storage) { |
| 1139 | 1139 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
| 1140 | 1140 | $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
@@ -1173,7 +1173,7 @@ discard block |
||
| 1173 | 1173 | $unlockLater = true; |
| 1174 | 1174 | // make sure our unlocking callback will still be called if connection is aborted |
| 1175 | 1175 | ignore_user_abort(true); |
| 1176 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1176 | + $result = CallbackWrapper::wrap($result, null, null, function() use ($hooks, $path) { |
|
| 1177 | 1177 | if (in_array('write', $hooks)) { |
| 1178 | 1178 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
| 1179 | 1179 | } else if (in_array('read', $hooks)) { |
@@ -1234,7 +1234,7 @@ discard block |
||
| 1234 | 1234 | return true; |
| 1235 | 1235 | } |
| 1236 | 1236 | |
| 1237 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1237 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot.'/'); |
|
| 1238 | 1238 | } |
| 1239 | 1239 | |
| 1240 | 1240 | /** |
@@ -1253,7 +1253,7 @@ discard block |
||
| 1253 | 1253 | if ($hook != 'read') { |
| 1254 | 1254 | \OC_Hook::emit( |
| 1255 | 1255 | Filesystem::CLASSNAME, |
| 1256 | - $prefix . $hook, |
|
| 1256 | + $prefix.$hook, |
|
| 1257 | 1257 | array( |
| 1258 | 1258 | Filesystem::signal_param_run => &$run, |
| 1259 | 1259 | Filesystem::signal_param_path => $path |
@@ -1262,7 +1262,7 @@ discard block |
||
| 1262 | 1262 | } elseif (!$post) { |
| 1263 | 1263 | \OC_Hook::emit( |
| 1264 | 1264 | Filesystem::CLASSNAME, |
| 1265 | - $prefix . $hook, |
|
| 1265 | + $prefix.$hook, |
|
| 1266 | 1266 | array( |
| 1267 | 1267 | Filesystem::signal_param_path => $path |
| 1268 | 1268 | ) |
@@ -1355,11 +1355,11 @@ discard block |
||
| 1355 | 1355 | return $this->getPartFileInfo($path); |
| 1356 | 1356 | } |
| 1357 | 1357 | $relativePath = $path; |
| 1358 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1358 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1359 | 1359 | |
| 1360 | 1360 | $mount = Filesystem::getMountManager()->find($path); |
| 1361 | 1361 | if (!$mount) { |
| 1362 | - \OC::$server->getLogger()->warning('Mountpoint not found for path: ' . $path); |
|
| 1362 | + \OC::$server->getLogger()->warning('Mountpoint not found for path: '.$path); |
|
| 1363 | 1363 | return false; |
| 1364 | 1364 | } |
| 1365 | 1365 | $storage = $mount->getStorage(); |
@@ -1368,7 +1368,7 @@ discard block |
||
| 1368 | 1368 | $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
| 1369 | 1369 | |
| 1370 | 1370 | if (!$data instanceof ICacheEntry) { |
| 1371 | - \OC::$server->getLogger()->debug('No cache entry found for ' . $path . ' (storage: ' . $storage->getId() . ', internalPath: ' . $internalPath . ')'); |
|
| 1371 | + \OC::$server->getLogger()->debug('No cache entry found for '.$path.' (storage: '.$storage->getId().', internalPath: '.$internalPath.')'); |
|
| 1372 | 1372 | return false; |
| 1373 | 1373 | } |
| 1374 | 1374 | |
@@ -1384,7 +1384,7 @@ discard block |
||
| 1384 | 1384 | //add the sizes of other mount points to the folder |
| 1385 | 1385 | $extOnly = ($includeMountPoints === 'ext'); |
| 1386 | 1386 | $mounts = Filesystem::getMountManager()->findIn($path); |
| 1387 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1387 | + $info->setSubMounts(array_filter($mounts, function(IMountPoint $mount) use ($extOnly) { |
|
| 1388 | 1388 | $subStorage = $mount->getStorage(); |
| 1389 | 1389 | return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
| 1390 | 1390 | })); |
@@ -1393,7 +1393,7 @@ discard block |
||
| 1393 | 1393 | |
| 1394 | 1394 | return $info; |
| 1395 | 1395 | } else { |
| 1396 | - \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint()); |
|
| 1396 | + \OC::$server->getLogger()->warning('Storage not valid for mountpoint: '.$mount->getMountPoint()); |
|
| 1397 | 1397 | } |
| 1398 | 1398 | |
| 1399 | 1399 | return false; |
@@ -1436,12 +1436,12 @@ discard block |
||
| 1436 | 1436 | /** |
| 1437 | 1437 | * @var \OC\Files\FileInfo[] $files |
| 1438 | 1438 | */ |
| 1439 | - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1439 | + $files = array_map(function(ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1440 | 1440 | if ($sharingDisabled) { |
| 1441 | 1441 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
| 1442 | 1442 | } |
| 1443 | 1443 | $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
| 1444 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1444 | + return new FileInfo($path.'/'.$content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1445 | 1445 | }, $contents); |
| 1446 | 1446 | |
| 1447 | 1447 | //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
@@ -1465,7 +1465,7 @@ discard block |
||
| 1465 | 1465 | } catch (\Exception $e) { |
| 1466 | 1466 | // sometimes when the storage is not available it can be any exception |
| 1467 | 1467 | \OC::$server->getLogger()->logException($e, [ |
| 1468 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1468 | + 'message' => 'Exception while scanning storage "'.$subStorage->getId().'"', |
|
| 1469 | 1469 | 'level' => ILogger::ERROR, |
| 1470 | 1470 | 'app' => 'lib', |
| 1471 | 1471 | ]); |
@@ -1503,7 +1503,7 @@ discard block |
||
| 1503 | 1503 | break; |
| 1504 | 1504 | } |
| 1505 | 1505 | } |
| 1506 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1506 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path.'/'.$rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1507 | 1507 | |
| 1508 | 1508 | // if sharing was disabled for the user we remove the share permissions |
| 1509 | 1509 | if (\OCP\Util::isSharingDisabledForUser()) { |
@@ -1511,14 +1511,14 @@ discard block |
||
| 1511 | 1511 | } |
| 1512 | 1512 | |
| 1513 | 1513 | $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
| 1514 | - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1514 | + $files[] = new FileInfo($path.'/'.$rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1515 | 1515 | } |
| 1516 | 1516 | } |
| 1517 | 1517 | } |
| 1518 | 1518 | } |
| 1519 | 1519 | |
| 1520 | 1520 | if ($mimetype_filter) { |
| 1521 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1521 | + $files = array_filter($files, function(FileInfo $file) use ($mimetype_filter) { |
|
| 1522 | 1522 | if (strpos($mimetype_filter, '/')) { |
| 1523 | 1523 | return $file->getMimetype() === $mimetype_filter; |
| 1524 | 1524 | } else { |
@@ -1547,7 +1547,7 @@ discard block |
||
| 1547 | 1547 | if ($data instanceof FileInfo) { |
| 1548 | 1548 | $data = $data->getData(); |
| 1549 | 1549 | } |
| 1550 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1550 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1551 | 1551 | /** |
| 1552 | 1552 | * @var \OC\Files\Storage\Storage $storage |
| 1553 | 1553 | * @var string $internalPath |
@@ -1574,7 +1574,7 @@ discard block |
||
| 1574 | 1574 | * @return FileInfo[] |
| 1575 | 1575 | */ |
| 1576 | 1576 | public function search($query) { |
| 1577 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1577 | + return $this->searchCommon('search', array('%'.$query.'%')); |
|
| 1578 | 1578 | } |
| 1579 | 1579 | |
| 1580 | 1580 | /** |
@@ -1625,10 +1625,10 @@ discard block |
||
| 1625 | 1625 | |
| 1626 | 1626 | $results = call_user_func_array(array($cache, $method), $args); |
| 1627 | 1627 | foreach ($results as $result) { |
| 1628 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1628 | + if (substr($mountPoint.$result['path'], 0, $rootLength + 1) === $this->fakeRoot.'/') { |
|
| 1629 | 1629 | $internalPath = $result['path']; |
| 1630 | - $path = $mountPoint . $result['path']; |
|
| 1631 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1630 | + $path = $mountPoint.$result['path']; |
|
| 1631 | + $result['path'] = substr($mountPoint.$result['path'], $rootLength); |
|
| 1632 | 1632 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
| 1633 | 1633 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
| 1634 | 1634 | } |
@@ -1646,8 +1646,8 @@ discard block |
||
| 1646 | 1646 | if ($results) { |
| 1647 | 1647 | foreach ($results as $result) { |
| 1648 | 1648 | $internalPath = $result['path']; |
| 1649 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1650 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1649 | + $result['path'] = rtrim($relativeMountPoint.$result['path'], '/'); |
|
| 1650 | + $path = rtrim($mountPoint.$internalPath, '/'); |
|
| 1651 | 1651 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
| 1652 | 1652 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
| 1653 | 1653 | } |
@@ -1668,7 +1668,7 @@ discard block |
||
| 1668 | 1668 | public function getOwner($path) { |
| 1669 | 1669 | $info = $this->getFileInfo($path); |
| 1670 | 1670 | if (!$info) { |
| 1671 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1671 | + throw new NotFoundException($path.' not found while trying to get owner'); |
|
| 1672 | 1672 | } |
| 1673 | 1673 | return $info->getOwner()->getUID(); |
| 1674 | 1674 | } |
@@ -1702,7 +1702,7 @@ discard block |
||
| 1702 | 1702 | * @return string |
| 1703 | 1703 | */ |
| 1704 | 1704 | public function getPath($id) { |
| 1705 | - $id = (int)$id; |
|
| 1705 | + $id = (int) $id; |
|
| 1706 | 1706 | $manager = Filesystem::getMountManager(); |
| 1707 | 1707 | $mounts = $manager->findIn($this->fakeRoot); |
| 1708 | 1708 | $mounts[] = $manager->find($this->fakeRoot); |
@@ -1717,7 +1717,7 @@ discard block |
||
| 1717 | 1717 | $cache = $mount->getStorage()->getCache(); |
| 1718 | 1718 | $internalPath = $cache->getPathById($id); |
| 1719 | 1719 | if (is_string($internalPath)) { |
| 1720 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1720 | + $fullPath = $mount->getMountPoint().$internalPath; |
|
| 1721 | 1721 | if (!is_null($path = $this->getRelativePath($fullPath))) { |
| 1722 | 1722 | return $path; |
| 1723 | 1723 | } |
@@ -1760,10 +1760,10 @@ discard block |
||
| 1760 | 1760 | } |
| 1761 | 1761 | |
| 1762 | 1762 | // note: cannot use the view because the target is already locked |
| 1763 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1763 | + $fileId = (int) $targetStorage->getCache()->getId($targetInternalPath); |
|
| 1764 | 1764 | if ($fileId === -1) { |
| 1765 | 1765 | // target might not exist, need to check parent instead |
| 1766 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1766 | + $fileId = (int) $targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1767 | 1767 | } |
| 1768 | 1768 | |
| 1769 | 1769 | // check if any of the parents were shared by the current owner (include collections) |
@@ -1863,7 +1863,7 @@ discard block |
||
| 1863 | 1863 | $resultPath = ''; |
| 1864 | 1864 | foreach ($parts as $part) { |
| 1865 | 1865 | if ($part) { |
| 1866 | - $resultPath .= '/' . $part; |
|
| 1866 | + $resultPath .= '/'.$part; |
|
| 1867 | 1867 | $result[] = $resultPath; |
| 1868 | 1868 | } |
| 1869 | 1869 | } |
@@ -2126,16 +2126,16 @@ discard block |
||
| 2126 | 2126 | public function getUidAndFilename($filename) { |
| 2127 | 2127 | $info = $this->getFileInfo($filename); |
| 2128 | 2128 | if (!$info instanceof \OCP\Files\FileInfo) { |
| 2129 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2129 | + throw new NotFoundException($this->getAbsolutePath($filename).' not found'); |
|
| 2130 | 2130 | } |
| 2131 | 2131 | $uid = $info->getOwner()->getUID(); |
| 2132 | 2132 | if ($uid != \OCP\User::getUser()) { |
| 2133 | 2133 | Filesystem::initMountPoints($uid); |
| 2134 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2134 | + $ownerView = new View('/'.$uid.'/files'); |
|
| 2135 | 2135 | try { |
| 2136 | 2136 | $filename = $ownerView->getPath($info['fileid']); |
| 2137 | 2137 | } catch (NotFoundException $e) { |
| 2138 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2138 | + throw new NotFoundException('File with id '.$info['fileid'].' not found for user '.$uid); |
|
| 2139 | 2139 | } |
| 2140 | 2140 | } |
| 2141 | 2141 | return [$uid, $filename]; |
@@ -2152,7 +2152,7 @@ discard block |
||
| 2152 | 2152 | $directoryParts = array_filter($directoryParts); |
| 2153 | 2153 | foreach ($directoryParts as $key => $part) { |
| 2154 | 2154 | $currentPathElements = array_slice($directoryParts, 0, $key); |
| 2155 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2155 | + $currentPath = '/'.implode('/', $currentPathElements); |
|
| 2156 | 2156 | if ($this->is_file($currentPath)) { |
| 2157 | 2157 | return false; |
| 2158 | 2158 | } |