@@ -318,6 +318,11 @@ |
||
| 318 | 318 | */ |
| 319 | 319 | // FIXME This method is only public, until OC_L10N does not need it anymore, |
| 320 | 320 | // FIXME This is also the reason, why it is not in the public interface |
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @param string $app |
|
| 324 | + * @param string $lang |
|
| 325 | + */ |
|
| 321 | 326 | public function getL10nFilesForApp($app, $lang) { |
| 322 | 327 | $languageFiles = []; |
| 323 | 328 | |
@@ -131,9 +131,9 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @link https://github.com/owncloud/core/issues/21955 |
| 133 | 133 | */ |
| 134 | - if($this->config->getSystemValue('installed', false)) { |
|
| 135 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 136 | - if(!is_null($userId)) { |
|
| 134 | + if ($this->config->getSystemValue('installed', false)) { |
|
| 135 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 136 | + if (!is_null($userId)) { |
|
| 137 | 137 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
| 138 | 138 | } else { |
| 139 | 139 | $userLang = null; |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | // merge with translations from theme |
| 203 | 203 | $theme = $this->config->getSystemValue('theme'); |
| 204 | 204 | if (!empty($theme)) { |
| 205 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 205 | + $themeDir = $this->serverRoot.'/themes/'.$theme.substr($dir, strlen($this->serverRoot)); |
|
| 206 | 206 | |
| 207 | 207 | if (is_dir($themeDir)) { |
| 208 | 208 | $files = scandir($themeDir); |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | */ |
| 298 | 298 | private function isSubDirectory($sub, $parent) { |
| 299 | 299 | // Check whether $sub contains no ".." |
| 300 | - if(strpos($sub, '..') !== false) { |
|
| 300 | + if (strpos($sub, '..') !== false) { |
|
| 301 | 301 | return false; |
| 302 | 302 | } |
| 303 | 303 | |
@@ -322,12 +322,12 @@ discard block |
||
| 322 | 322 | $languageFiles = []; |
| 323 | 323 | |
| 324 | 324 | $i18nDir = $this->findL10nDir($app); |
| 325 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 325 | + $transFile = strip_tags($i18nDir).strip_tags($lang).'.json'; |
|
| 326 | 326 | |
| 327 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 328 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 329 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 330 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 327 | + if (($this->isSubDirectory($transFile, $this->serverRoot.'/core/l10n/') |
|
| 328 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/lib/l10n/') |
|
| 329 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/settings/l10n/') |
|
| 330 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app).'/l10n/') |
|
| 331 | 331 | ) |
| 332 | 332 | && file_exists($transFile)) { |
| 333 | 333 | // load the translations file |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | // merge with translations from theme |
| 338 | 338 | $theme = $this->config->getSystemValue('theme'); |
| 339 | 339 | if (!empty($theme)) { |
| 340 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 340 | + $transFile = $this->serverRoot.'/themes/'.$theme.substr($transFile, strlen($this->serverRoot)); |
|
| 341 | 341 | if (file_exists($transFile)) { |
| 342 | 342 | $languageFiles[] = $transFile; |
| 343 | 343 | } |
@@ -354,14 +354,14 @@ discard block |
||
| 354 | 354 | */ |
| 355 | 355 | protected function findL10nDir($app = null) { |
| 356 | 356 | if (in_array($app, ['core', 'lib', 'settings'])) { |
| 357 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 358 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 357 | + if (file_exists($this->serverRoot.'/'.$app.'/l10n/')) { |
|
| 358 | + return $this->serverRoot.'/'.$app.'/l10n/'; |
|
| 359 | 359 | } |
| 360 | 360 | } else if ($app && \OC_App::getAppPath($app) !== false) { |
| 361 | 361 | // Check if the app is in the app folder |
| 362 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 362 | + return \OC_App::getAppPath($app).'/l10n/'; |
|
| 363 | 363 | } |
| 364 | - return $this->serverRoot . '/core/l10n/'; |
|
| 364 | + return $this->serverRoot.'/core/l10n/'; |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | |
@@ -378,15 +378,15 @@ discard block |
||
| 378 | 378 | return $this->pluralFunctions[$string]; |
| 379 | 379 | } |
| 380 | 380 | |
| 381 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 381 | + if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 382 | 382 | // sanitize |
| 383 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 384 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 383 | + $nplurals = preg_replace('/[^0-9]/', '', $matches[1]); |
|
| 384 | + $plural = preg_replace('#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2]); |
|
| 385 | 385 | |
| 386 | 386 | $body = str_replace( |
| 387 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 388 | - array( '$plural', '$n', '$nplurals', ), |
|
| 389 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 387 | + array('plural', 'n', '$n$plurals',), |
|
| 388 | + array('$plural', '$n', '$nplurals',), |
|
| 389 | + 'nplurals='.$nplurals.'; plural='.$plural |
|
| 390 | 390 | ); |
| 391 | 391 | |
| 392 | 392 | // add parents |
@@ -394,9 +394,9 @@ discard block |
||
| 394 | 394 | $body .= ';'; |
| 395 | 395 | $res = ''; |
| 396 | 396 | $p = 0; |
| 397 | - for($i = 0; $i < strlen($body); $i++) { |
|
| 397 | + for ($i = 0; $i < strlen($body); $i++) { |
|
| 398 | 398 | $ch = $body[$i]; |
| 399 | - switch ( $ch ) { |
|
| 399 | + switch ($ch) { |
|
| 400 | 400 | case '?': |
| 401 | 401 | $res .= ' ? ('; |
| 402 | 402 | $p++; |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | $res .= ') : ('; |
| 406 | 406 | break; |
| 407 | 407 | case ';': |
| 408 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 408 | + $res .= str_repeat(')', $p).';'; |
|
| 409 | 409 | $p = 0; |
| 410 | 410 | break; |
| 411 | 411 | default: |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | } |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 416 | + $body = $res.'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 417 | 417 | $function = create_function('$n', $body); |
| 418 | 418 | $this->pluralFunctions[$string] = $function; |
| 419 | 419 | return $function; |
@@ -40,409 +40,409 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | class Factory implements IFactory { |
| 42 | 42 | |
| 43 | - /** @var string */ |
|
| 44 | - protected $requestLanguage = ''; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * cached instances |
|
| 48 | - * @var array Structure: Lang => App => \OCP\IL10N |
|
| 49 | - */ |
|
| 50 | - protected $instances = []; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var array Structure: App => string[] |
|
| 54 | - */ |
|
| 55 | - protected $availableLanguages = []; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var array Structure: string => callable |
|
| 59 | - */ |
|
| 60 | - protected $pluralFunctions = []; |
|
| 61 | - |
|
| 62 | - /** @var IConfig */ |
|
| 63 | - protected $config; |
|
| 64 | - |
|
| 65 | - /** @var IRequest */ |
|
| 66 | - protected $request; |
|
| 67 | - |
|
| 68 | - /** @var IUserSession */ |
|
| 69 | - protected $userSession; |
|
| 70 | - |
|
| 71 | - /** @var string */ |
|
| 72 | - protected $serverRoot; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param IConfig $config |
|
| 76 | - * @param IRequest $request |
|
| 77 | - * @param IUserSession $userSession |
|
| 78 | - * @param string $serverRoot |
|
| 79 | - */ |
|
| 80 | - public function __construct(IConfig $config, |
|
| 81 | - IRequest $request, |
|
| 82 | - IUserSession $userSession, |
|
| 83 | - $serverRoot) { |
|
| 84 | - $this->config = $config; |
|
| 85 | - $this->request = $request; |
|
| 86 | - $this->userSession = $userSession; |
|
| 87 | - $this->serverRoot = $serverRoot; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get a language instance |
|
| 92 | - * |
|
| 93 | - * @param string $app |
|
| 94 | - * @param string|null $lang |
|
| 95 | - * @return \OCP\IL10N |
|
| 96 | - */ |
|
| 97 | - public function get($app, $lang = null) { |
|
| 98 | - $app = \OC_App::cleanAppId($app); |
|
| 99 | - if ($lang !== null) { |
|
| 100 | - $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 104 | - if (is_string($forceLang)) { |
|
| 105 | - $lang = $forceLang; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 109 | - $lang = $this->findLanguage($app); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if (!isset($this->instances[$lang][$app])) { |
|
| 113 | - $this->instances[$lang][$app] = new L10N( |
|
| 114 | - $this, $app, $lang, |
|
| 115 | - $this->getL10nFilesForApp($app, $lang) |
|
| 116 | - ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $this->instances[$lang][$app]; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Find the best language |
|
| 124 | - * |
|
| 125 | - * @param string|null $app App id or null for core |
|
| 126 | - * @return string language If nothing works it returns 'en' |
|
| 127 | - */ |
|
| 128 | - public function findLanguage($app = null) { |
|
| 129 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 130 | - if (is_string($forceLang)) { |
|
| 131 | - $this->requestLanguage = $forceLang; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 135 | - return $this->requestLanguage; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * At this point Nextcloud might not yet be installed and thus the lookup |
|
| 140 | - * in the preferences table might fail. For this reason we need to check |
|
| 141 | - * whether the instance has already been installed |
|
| 142 | - * |
|
| 143 | - * @link https://github.com/owncloud/core/issues/21955 |
|
| 144 | - */ |
|
| 145 | - if($this->config->getSystemValue('installed', false)) { |
|
| 146 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 147 | - if(!is_null($userId)) { |
|
| 148 | - $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 149 | - } else { |
|
| 150 | - $userLang = null; |
|
| 151 | - } |
|
| 152 | - } else { |
|
| 153 | - $userId = null; |
|
| 154 | - $userLang = null; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if ($userLang) { |
|
| 158 | - $this->requestLanguage = $userLang; |
|
| 159 | - if ($this->languageExists($app, $userLang)) { |
|
| 160 | - return $userLang; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - try { |
|
| 165 | - // Try to get the language from the Request |
|
| 166 | - $lang = $this->getLanguageFromRequest($app); |
|
| 167 | - if ($userId !== null && $app === null && !$userLang) { |
|
| 168 | - $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 169 | - } |
|
| 170 | - return $lang; |
|
| 171 | - } catch (LanguageNotFoundException $e) { |
|
| 172 | - // Finding language from request failed fall back to default language |
|
| 173 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 174 | - if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 175 | - return $defaultLanguage; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // We could not find any language so fall back to english |
|
| 180 | - return 'en'; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Find all available languages for an app |
|
| 185 | - * |
|
| 186 | - * @param string|null $app App id or null for core |
|
| 187 | - * @return array an array of available languages |
|
| 188 | - */ |
|
| 189 | - public function findAvailableLanguages($app = null) { |
|
| 190 | - $key = $app; |
|
| 191 | - if ($key === null) { |
|
| 192 | - $key = 'null'; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // also works with null as key |
|
| 196 | - if (!empty($this->availableLanguages[$key])) { |
|
| 197 | - return $this->availableLanguages[$key]; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $available = ['en']; //english is always available |
|
| 201 | - $dir = $this->findL10nDir($app); |
|
| 202 | - if (is_dir($dir)) { |
|
| 203 | - $files = scandir($dir); |
|
| 204 | - if ($files !== false) { |
|
| 205 | - foreach ($files as $file) { |
|
| 206 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 207 | - $available[] = substr($file, 0, -5); |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - // merge with translations from theme |
|
| 214 | - $theme = $this->config->getSystemValue('theme'); |
|
| 215 | - if (!empty($theme)) { |
|
| 216 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 217 | - |
|
| 218 | - if (is_dir($themeDir)) { |
|
| 219 | - $files = scandir($themeDir); |
|
| 220 | - if ($files !== false) { |
|
| 221 | - foreach ($files as $file) { |
|
| 222 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 223 | - $available[] = substr($file, 0, -5); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $this->availableLanguages[$key] = $available; |
|
| 231 | - return $available; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @param string|null $app App id or null for core |
|
| 236 | - * @param string $lang |
|
| 237 | - * @return bool |
|
| 238 | - */ |
|
| 239 | - public function languageExists($app, $lang) { |
|
| 240 | - if ($lang === 'en') {//english is always available |
|
| 241 | - return true; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $languages = $this->findAvailableLanguages($app); |
|
| 245 | - return array_search($lang, $languages) !== false; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * @param string|null $app |
|
| 250 | - * @return string |
|
| 251 | - * @throws LanguageNotFoundException |
|
| 252 | - */ |
|
| 253 | - private function getLanguageFromRequest($app) { |
|
| 254 | - $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 255 | - if ($header) { |
|
| 256 | - $available = $this->findAvailableLanguages($app); |
|
| 257 | - |
|
| 258 | - // E.g. make sure that 'de' is before 'de_DE'. |
|
| 259 | - sort($available); |
|
| 260 | - |
|
| 261 | - $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 262 | - foreach ($preferences as $preference) { |
|
| 263 | - list($preferred_language) = explode(';', $preference); |
|
| 264 | - $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 265 | - |
|
| 266 | - foreach ($available as $available_language) { |
|
| 267 | - if ($preferred_language === strtolower($available_language)) { |
|
| 268 | - return $this->respectDefaultLanguage($app, $available_language); |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // Fallback from de_De to de |
|
| 273 | - foreach ($available as $available_language) { |
|
| 274 | - if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 275 | - return $available_language; |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - throw new LanguageNotFoundException(); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * if default language is set to de_DE (formal German) this should be |
|
| 286 | - * preferred to 'de' (non-formal German) if possible |
|
| 287 | - * |
|
| 288 | - * @param string|null $app |
|
| 289 | - * @param string $lang |
|
| 290 | - * @return string |
|
| 291 | - */ |
|
| 292 | - protected function respectDefaultLanguage($app, $lang) { |
|
| 293 | - $result = $lang; |
|
| 294 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 295 | - |
|
| 296 | - // use formal version of german ("Sie" instead of "Du") if the default |
|
| 297 | - // language is set to 'de_DE' if possible |
|
| 298 | - if (is_string($defaultLanguage) && |
|
| 299 | - strtolower($lang) === 'de' && |
|
| 300 | - strtolower($defaultLanguage) === 'de_de' && |
|
| 301 | - $this->languageExists($app, 'de_DE') |
|
| 302 | - ) { |
|
| 303 | - $result = 'de_DE'; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - return $result; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Checks if $sub is a subdirectory of $parent |
|
| 311 | - * |
|
| 312 | - * @param string $sub |
|
| 313 | - * @param string $parent |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - private function isSubDirectory($sub, $parent) { |
|
| 317 | - // Check whether $sub contains no ".." |
|
| 318 | - if(strpos($sub, '..') !== false) { |
|
| 319 | - return false; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - // Check whether $sub is a subdirectory of $parent |
|
| 323 | - if (strpos($sub, $parent) === 0) { |
|
| 324 | - return true; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return false; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get a list of language files that should be loaded |
|
| 332 | - * |
|
| 333 | - * @param string $app |
|
| 334 | - * @param string $lang |
|
| 335 | - * @return string[] |
|
| 336 | - */ |
|
| 337 | - // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 338 | - // FIXME This is also the reason, why it is not in the public interface |
|
| 339 | - public function getL10nFilesForApp($app, $lang) { |
|
| 340 | - $languageFiles = []; |
|
| 341 | - |
|
| 342 | - $i18nDir = $this->findL10nDir($app); |
|
| 343 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 344 | - |
|
| 345 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 346 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 347 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 348 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 349 | - ) |
|
| 350 | - && file_exists($transFile)) { |
|
| 351 | - // load the translations file |
|
| 352 | - $languageFiles[] = $transFile; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - // merge with translations from theme |
|
| 356 | - $theme = $this->config->getSystemValue('theme'); |
|
| 357 | - if (!empty($theme)) { |
|
| 358 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 359 | - if (file_exists($transFile)) { |
|
| 360 | - $languageFiles[] = $transFile; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return $languageFiles; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * find the l10n directory |
|
| 369 | - * |
|
| 370 | - * @param string $app App id or empty string for core |
|
| 371 | - * @return string directory |
|
| 372 | - */ |
|
| 373 | - protected function findL10nDir($app = null) { |
|
| 374 | - if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 375 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 376 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 377 | - } |
|
| 378 | - } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 379 | - // Check if the app is in the app folder |
|
| 380 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 381 | - } |
|
| 382 | - return $this->serverRoot . '/core/l10n/'; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Creates a function from the plural string |
|
| 388 | - * |
|
| 389 | - * Parts of the code is copied from Habari: |
|
| 390 | - * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 391 | - * @param string $string |
|
| 392 | - * @return string |
|
| 393 | - */ |
|
| 394 | - public function createPluralFunction($string) { |
|
| 395 | - if (isset($this->pluralFunctions[$string])) { |
|
| 396 | - return $this->pluralFunctions[$string]; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 400 | - // sanitize |
|
| 401 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 402 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 403 | - |
|
| 404 | - $body = str_replace( |
|
| 405 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 406 | - array( '$plural', '$n', '$nplurals', ), |
|
| 407 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 408 | - ); |
|
| 409 | - |
|
| 410 | - // add parents |
|
| 411 | - // important since PHP's ternary evaluates from left to right |
|
| 412 | - $body .= ';'; |
|
| 413 | - $res = ''; |
|
| 414 | - $p = 0; |
|
| 415 | - for($i = 0; $i < strlen($body); $i++) { |
|
| 416 | - $ch = $body[$i]; |
|
| 417 | - switch ( $ch ) { |
|
| 418 | - case '?': |
|
| 419 | - $res .= ' ? ('; |
|
| 420 | - $p++; |
|
| 421 | - break; |
|
| 422 | - case ':': |
|
| 423 | - $res .= ') : ('; |
|
| 424 | - break; |
|
| 425 | - case ';': |
|
| 426 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 427 | - $p = 0; |
|
| 428 | - break; |
|
| 429 | - default: |
|
| 430 | - $res .= $ch; |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 435 | - $function = create_function('$n', $body); |
|
| 436 | - $this->pluralFunctions[$string] = $function; |
|
| 437 | - return $function; |
|
| 438 | - } else { |
|
| 439 | - // default: one plural form for all cases but n==1 (english) |
|
| 440 | - $function = create_function( |
|
| 441 | - '$n', |
|
| 442 | - '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 443 | - ); |
|
| 444 | - $this->pluralFunctions[$string] = $function; |
|
| 445 | - return $function; |
|
| 446 | - } |
|
| 447 | - } |
|
| 43 | + /** @var string */ |
|
| 44 | + protected $requestLanguage = ''; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * cached instances |
|
| 48 | + * @var array Structure: Lang => App => \OCP\IL10N |
|
| 49 | + */ |
|
| 50 | + protected $instances = []; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var array Structure: App => string[] |
|
| 54 | + */ |
|
| 55 | + protected $availableLanguages = []; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var array Structure: string => callable |
|
| 59 | + */ |
|
| 60 | + protected $pluralFunctions = []; |
|
| 61 | + |
|
| 62 | + /** @var IConfig */ |
|
| 63 | + protected $config; |
|
| 64 | + |
|
| 65 | + /** @var IRequest */ |
|
| 66 | + protected $request; |
|
| 67 | + |
|
| 68 | + /** @var IUserSession */ |
|
| 69 | + protected $userSession; |
|
| 70 | + |
|
| 71 | + /** @var string */ |
|
| 72 | + protected $serverRoot; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param IConfig $config |
|
| 76 | + * @param IRequest $request |
|
| 77 | + * @param IUserSession $userSession |
|
| 78 | + * @param string $serverRoot |
|
| 79 | + */ |
|
| 80 | + public function __construct(IConfig $config, |
|
| 81 | + IRequest $request, |
|
| 82 | + IUserSession $userSession, |
|
| 83 | + $serverRoot) { |
|
| 84 | + $this->config = $config; |
|
| 85 | + $this->request = $request; |
|
| 86 | + $this->userSession = $userSession; |
|
| 87 | + $this->serverRoot = $serverRoot; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get a language instance |
|
| 92 | + * |
|
| 93 | + * @param string $app |
|
| 94 | + * @param string|null $lang |
|
| 95 | + * @return \OCP\IL10N |
|
| 96 | + */ |
|
| 97 | + public function get($app, $lang = null) { |
|
| 98 | + $app = \OC_App::cleanAppId($app); |
|
| 99 | + if ($lang !== null) { |
|
| 100 | + $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 104 | + if (is_string($forceLang)) { |
|
| 105 | + $lang = $forceLang; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 109 | + $lang = $this->findLanguage($app); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if (!isset($this->instances[$lang][$app])) { |
|
| 113 | + $this->instances[$lang][$app] = new L10N( |
|
| 114 | + $this, $app, $lang, |
|
| 115 | + $this->getL10nFilesForApp($app, $lang) |
|
| 116 | + ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $this->instances[$lang][$app]; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Find the best language |
|
| 124 | + * |
|
| 125 | + * @param string|null $app App id or null for core |
|
| 126 | + * @return string language If nothing works it returns 'en' |
|
| 127 | + */ |
|
| 128 | + public function findLanguage($app = null) { |
|
| 129 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 130 | + if (is_string($forceLang)) { |
|
| 131 | + $this->requestLanguage = $forceLang; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 135 | + return $this->requestLanguage; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * At this point Nextcloud might not yet be installed and thus the lookup |
|
| 140 | + * in the preferences table might fail. For this reason we need to check |
|
| 141 | + * whether the instance has already been installed |
|
| 142 | + * |
|
| 143 | + * @link https://github.com/owncloud/core/issues/21955 |
|
| 144 | + */ |
|
| 145 | + if($this->config->getSystemValue('installed', false)) { |
|
| 146 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 147 | + if(!is_null($userId)) { |
|
| 148 | + $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 149 | + } else { |
|
| 150 | + $userLang = null; |
|
| 151 | + } |
|
| 152 | + } else { |
|
| 153 | + $userId = null; |
|
| 154 | + $userLang = null; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if ($userLang) { |
|
| 158 | + $this->requestLanguage = $userLang; |
|
| 159 | + if ($this->languageExists($app, $userLang)) { |
|
| 160 | + return $userLang; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + try { |
|
| 165 | + // Try to get the language from the Request |
|
| 166 | + $lang = $this->getLanguageFromRequest($app); |
|
| 167 | + if ($userId !== null && $app === null && !$userLang) { |
|
| 168 | + $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 169 | + } |
|
| 170 | + return $lang; |
|
| 171 | + } catch (LanguageNotFoundException $e) { |
|
| 172 | + // Finding language from request failed fall back to default language |
|
| 173 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 174 | + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 175 | + return $defaultLanguage; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // We could not find any language so fall back to english |
|
| 180 | + return 'en'; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Find all available languages for an app |
|
| 185 | + * |
|
| 186 | + * @param string|null $app App id or null for core |
|
| 187 | + * @return array an array of available languages |
|
| 188 | + */ |
|
| 189 | + public function findAvailableLanguages($app = null) { |
|
| 190 | + $key = $app; |
|
| 191 | + if ($key === null) { |
|
| 192 | + $key = 'null'; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // also works with null as key |
|
| 196 | + if (!empty($this->availableLanguages[$key])) { |
|
| 197 | + return $this->availableLanguages[$key]; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $available = ['en']; //english is always available |
|
| 201 | + $dir = $this->findL10nDir($app); |
|
| 202 | + if (is_dir($dir)) { |
|
| 203 | + $files = scandir($dir); |
|
| 204 | + if ($files !== false) { |
|
| 205 | + foreach ($files as $file) { |
|
| 206 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 207 | + $available[] = substr($file, 0, -5); |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + // merge with translations from theme |
|
| 214 | + $theme = $this->config->getSystemValue('theme'); |
|
| 215 | + if (!empty($theme)) { |
|
| 216 | + $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 217 | + |
|
| 218 | + if (is_dir($themeDir)) { |
|
| 219 | + $files = scandir($themeDir); |
|
| 220 | + if ($files !== false) { |
|
| 221 | + foreach ($files as $file) { |
|
| 222 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 223 | + $available[] = substr($file, 0, -5); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $this->availableLanguages[$key] = $available; |
|
| 231 | + return $available; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @param string|null $app App id or null for core |
|
| 236 | + * @param string $lang |
|
| 237 | + * @return bool |
|
| 238 | + */ |
|
| 239 | + public function languageExists($app, $lang) { |
|
| 240 | + if ($lang === 'en') {//english is always available |
|
| 241 | + return true; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $languages = $this->findAvailableLanguages($app); |
|
| 245 | + return array_search($lang, $languages) !== false; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * @param string|null $app |
|
| 250 | + * @return string |
|
| 251 | + * @throws LanguageNotFoundException |
|
| 252 | + */ |
|
| 253 | + private function getLanguageFromRequest($app) { |
|
| 254 | + $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 255 | + if ($header) { |
|
| 256 | + $available = $this->findAvailableLanguages($app); |
|
| 257 | + |
|
| 258 | + // E.g. make sure that 'de' is before 'de_DE'. |
|
| 259 | + sort($available); |
|
| 260 | + |
|
| 261 | + $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 262 | + foreach ($preferences as $preference) { |
|
| 263 | + list($preferred_language) = explode(';', $preference); |
|
| 264 | + $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 265 | + |
|
| 266 | + foreach ($available as $available_language) { |
|
| 267 | + if ($preferred_language === strtolower($available_language)) { |
|
| 268 | + return $this->respectDefaultLanguage($app, $available_language); |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // Fallback from de_De to de |
|
| 273 | + foreach ($available as $available_language) { |
|
| 274 | + if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 275 | + return $available_language; |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + throw new LanguageNotFoundException(); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * if default language is set to de_DE (formal German) this should be |
|
| 286 | + * preferred to 'de' (non-formal German) if possible |
|
| 287 | + * |
|
| 288 | + * @param string|null $app |
|
| 289 | + * @param string $lang |
|
| 290 | + * @return string |
|
| 291 | + */ |
|
| 292 | + protected function respectDefaultLanguage($app, $lang) { |
|
| 293 | + $result = $lang; |
|
| 294 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 295 | + |
|
| 296 | + // use formal version of german ("Sie" instead of "Du") if the default |
|
| 297 | + // language is set to 'de_DE' if possible |
|
| 298 | + if (is_string($defaultLanguage) && |
|
| 299 | + strtolower($lang) === 'de' && |
|
| 300 | + strtolower($defaultLanguage) === 'de_de' && |
|
| 301 | + $this->languageExists($app, 'de_DE') |
|
| 302 | + ) { |
|
| 303 | + $result = 'de_DE'; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + return $result; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Checks if $sub is a subdirectory of $parent |
|
| 311 | + * |
|
| 312 | + * @param string $sub |
|
| 313 | + * @param string $parent |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + private function isSubDirectory($sub, $parent) { |
|
| 317 | + // Check whether $sub contains no ".." |
|
| 318 | + if(strpos($sub, '..') !== false) { |
|
| 319 | + return false; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + // Check whether $sub is a subdirectory of $parent |
|
| 323 | + if (strpos($sub, $parent) === 0) { |
|
| 324 | + return true; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return false; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get a list of language files that should be loaded |
|
| 332 | + * |
|
| 333 | + * @param string $app |
|
| 334 | + * @param string $lang |
|
| 335 | + * @return string[] |
|
| 336 | + */ |
|
| 337 | + // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 338 | + // FIXME This is also the reason, why it is not in the public interface |
|
| 339 | + public function getL10nFilesForApp($app, $lang) { |
|
| 340 | + $languageFiles = []; |
|
| 341 | + |
|
| 342 | + $i18nDir = $this->findL10nDir($app); |
|
| 343 | + $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 344 | + |
|
| 345 | + if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 346 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 347 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 348 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 349 | + ) |
|
| 350 | + && file_exists($transFile)) { |
|
| 351 | + // load the translations file |
|
| 352 | + $languageFiles[] = $transFile; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + // merge with translations from theme |
|
| 356 | + $theme = $this->config->getSystemValue('theme'); |
|
| 357 | + if (!empty($theme)) { |
|
| 358 | + $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 359 | + if (file_exists($transFile)) { |
|
| 360 | + $languageFiles[] = $transFile; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return $languageFiles; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * find the l10n directory |
|
| 369 | + * |
|
| 370 | + * @param string $app App id or empty string for core |
|
| 371 | + * @return string directory |
|
| 372 | + */ |
|
| 373 | + protected function findL10nDir($app = null) { |
|
| 374 | + if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 375 | + if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 376 | + return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 377 | + } |
|
| 378 | + } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 379 | + // Check if the app is in the app folder |
|
| 380 | + return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 381 | + } |
|
| 382 | + return $this->serverRoot . '/core/l10n/'; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Creates a function from the plural string |
|
| 388 | + * |
|
| 389 | + * Parts of the code is copied from Habari: |
|
| 390 | + * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 391 | + * @param string $string |
|
| 392 | + * @return string |
|
| 393 | + */ |
|
| 394 | + public function createPluralFunction($string) { |
|
| 395 | + if (isset($this->pluralFunctions[$string])) { |
|
| 396 | + return $this->pluralFunctions[$string]; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 400 | + // sanitize |
|
| 401 | + $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 402 | + $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 403 | + |
|
| 404 | + $body = str_replace( |
|
| 405 | + array( 'plural', 'n', '$n$plurals', ), |
|
| 406 | + array( '$plural', '$n', '$nplurals', ), |
|
| 407 | + 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 408 | + ); |
|
| 409 | + |
|
| 410 | + // add parents |
|
| 411 | + // important since PHP's ternary evaluates from left to right |
|
| 412 | + $body .= ';'; |
|
| 413 | + $res = ''; |
|
| 414 | + $p = 0; |
|
| 415 | + for($i = 0; $i < strlen($body); $i++) { |
|
| 416 | + $ch = $body[$i]; |
|
| 417 | + switch ( $ch ) { |
|
| 418 | + case '?': |
|
| 419 | + $res .= ' ? ('; |
|
| 420 | + $p++; |
|
| 421 | + break; |
|
| 422 | + case ':': |
|
| 423 | + $res .= ') : ('; |
|
| 424 | + break; |
|
| 425 | + case ';': |
|
| 426 | + $res .= str_repeat( ')', $p ) . ';'; |
|
| 427 | + $p = 0; |
|
| 428 | + break; |
|
| 429 | + default: |
|
| 430 | + $res .= $ch; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 435 | + $function = create_function('$n', $body); |
|
| 436 | + $this->pluralFunctions[$string] = $function; |
|
| 437 | + return $function; |
|
| 438 | + } else { |
|
| 439 | + // default: one plural form for all cases but n==1 (english) |
|
| 440 | + $function = create_function( |
|
| 441 | + '$n', |
|
| 442 | + '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 443 | + ); |
|
| 444 | + $this->pluralFunctions[$string] = $function; |
|
| 445 | + return $function; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | 448 | } |
@@ -360,6 +360,9 @@ discard block |
||
| 360 | 360 | } |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | + /** |
|
| 364 | + * @param string $root |
|
| 365 | + */ |
|
| 363 | 366 | static public function init($user, $root) { |
| 364 | 367 | if (self::$defaultInstance) { |
| 365 | 368 | return false; |
@@ -528,7 +531,7 @@ discard block |
||
| 528 | 531 | /** |
| 529 | 532 | * mount an \OC\Files\Storage\Storage in our virtual filesystem |
| 530 | 533 | * |
| 531 | - * @param \OC\Files\Storage\Storage|string $class |
|
| 534 | + * @param string $class |
|
| 532 | 535 | * @param array $arguments |
| 533 | 536 | * @param string $mountpoint |
| 534 | 537 | */ |
@@ -660,6 +663,9 @@ discard block |
||
| 660 | 663 | return self::$defaultInstance->is_dir($path); |
| 661 | 664 | } |
| 662 | 665 | |
| 666 | + /** |
|
| 667 | + * @param string $path |
|
| 668 | + */ |
|
| 663 | 669 | static public function is_file($path) { |
| 664 | 670 | return self::$defaultInstance->is_file($path); |
| 665 | 671 | } |
@@ -672,6 +678,9 @@ discard block |
||
| 672 | 678 | return self::$defaultInstance->filetype($path); |
| 673 | 679 | } |
| 674 | 680 | |
| 681 | + /** |
|
| 682 | + * @param string $path |
|
| 683 | + */ |
|
| 675 | 684 | static public function filesize($path) { |
| 676 | 685 | return self::$defaultInstance->filesize($path); |
| 677 | 686 | } |
@@ -684,6 +693,9 @@ discard block |
||
| 684 | 693 | return self::$defaultInstance->isCreatable($path); |
| 685 | 694 | } |
| 686 | 695 | |
| 696 | + /** |
|
| 697 | + * @param string $path |
|
| 698 | + */ |
|
| 687 | 699 | static public function isReadable($path) { |
| 688 | 700 | return self::$defaultInstance->isReadable($path); |
| 689 | 701 | } |
@@ -696,6 +708,9 @@ discard block |
||
| 696 | 708 | return self::$defaultInstance->isDeletable($path); |
| 697 | 709 | } |
| 698 | 710 | |
| 711 | + /** |
|
| 712 | + * @param string $path |
|
| 713 | + */ |
|
| 699 | 714 | static public function isSharable($path) { |
| 700 | 715 | return self::$defaultInstance->isSharable($path); |
| 701 | 716 | } |
@@ -704,6 +719,9 @@ discard block |
||
| 704 | 719 | return self::$defaultInstance->file_exists($path); |
| 705 | 720 | } |
| 706 | 721 | |
| 722 | + /** |
|
| 723 | + * @param string $path |
|
| 724 | + */ |
|
| 707 | 725 | static public function filemtime($path) { |
| 708 | 726 | return self::$defaultInstance->filemtime($path); |
| 709 | 727 | } |
@@ -713,6 +731,7 @@ discard block |
||
| 713 | 731 | } |
| 714 | 732 | |
| 715 | 733 | /** |
| 734 | + * @param string $path |
|
| 716 | 735 | * @return string |
| 717 | 736 | */ |
| 718 | 737 | static public function file_get_contents($path) { |
@@ -735,6 +754,10 @@ discard block |
||
| 735 | 754 | return self::$defaultInstance->copy($path1, $path2); |
| 736 | 755 | } |
| 737 | 756 | |
| 757 | + /** |
|
| 758 | + * @param string $path |
|
| 759 | + * @param string $mode |
|
| 760 | + */ |
|
| 738 | 761 | static public function fopen($path, $mode) { |
| 739 | 762 | return self::$defaultInstance->fopen($path, $mode); |
| 740 | 763 | } |
@@ -750,6 +773,9 @@ discard block |
||
| 750 | 773 | return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
| 751 | 774 | } |
| 752 | 775 | |
| 776 | + /** |
|
| 777 | + * @param string $path |
|
| 778 | + */ |
|
| 753 | 779 | static public function getMimeType($path) { |
| 754 | 780 | return self::$defaultInstance->getMimeType($path); |
| 755 | 781 | } |
@@ -762,6 +788,9 @@ discard block |
||
| 762 | 788 | return self::$defaultInstance->free_space($path); |
| 763 | 789 | } |
| 764 | 790 | |
| 791 | + /** |
|
| 792 | + * @param string $query |
|
| 793 | + */ |
|
| 765 | 794 | static public function search($query) { |
| 766 | 795 | return self::$defaultInstance->search($query); |
| 767 | 796 | } |
@@ -870,7 +899,7 @@ discard block |
||
| 870 | 899 | * @param string $path |
| 871 | 900 | * @param boolean $includeMountPoints whether to add mountpoint sizes, |
| 872 | 901 | * defaults to true |
| 873 | - * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 902 | + * @return \OCP\Files\FileInfo|null False if file does not exist |
|
| 874 | 903 | */ |
| 875 | 904 | public static function getFileInfo($path, $includeMountPoints = true) { |
| 876 | 905 | return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
@@ -409,17 +409,17 @@ discard block |
||
| 409 | 409 | $userObject = $userManager->get($user); |
| 410 | 410 | |
| 411 | 411 | if (is_null($userObject)) { |
| 412 | - \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR); |
|
| 412 | + \OCP\Util::writeLog('files', ' Backends provided no user object for '.$user, \OCP\Util::ERROR); |
|
| 413 | 413 | // reset flag, this will make it possible to rethrow the exception if called again |
| 414 | 414 | unset(self::$usersSetup[$user]); |
| 415 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 415 | + throw new \OC\User\NoUserException('Backends provided no user object for '.$user); |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | $realUid = $userObject->getUID(); |
| 419 | 419 | // workaround in case of different casings |
| 420 | 420 | if ($user !== $realUid) { |
| 421 | 421 | $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
| 422 | - \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, \OCP\Util::WARN); |
|
| 422 | + \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "'.$realUid.'" got "'.$user.'". Stack: '.$stack, \OCP\Util::WARN); |
|
| 423 | 423 | $user = $realUid; |
| 424 | 424 | |
| 425 | 425 | // again with the correct casing |
@@ -453,11 +453,11 @@ discard block |
||
| 453 | 453 | } else { |
| 454 | 454 | self::getMountManager()->addMount(new MountPoint( |
| 455 | 455 | new NullStorage([]), |
| 456 | - '/' . $user |
|
| 456 | + '/'.$user |
|
| 457 | 457 | )); |
| 458 | 458 | self::getMountManager()->addMount(new MountPoint( |
| 459 | 459 | new NullStorage([]), |
| 460 | - '/' . $user . '/files' |
|
| 460 | + '/'.$user.'/files' |
|
| 461 | 461 | )); |
| 462 | 462 | } |
| 463 | 463 | \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
@@ -472,7 +472,7 @@ discard block |
||
| 472 | 472 | private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
| 473 | 473 | if (!self::$listeningForProviders) { |
| 474 | 474 | self::$listeningForProviders = true; |
| 475 | - $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 475 | + $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function(IMountProvider $provider) use ($userManager) { |
|
| 476 | 476 | foreach (Filesystem::$usersSetup as $user => $setup) { |
| 477 | 477 | $userObject = $userManager->get($user); |
| 478 | 478 | if ($userObject) { |
@@ -567,7 +567,7 @@ discard block |
||
| 567 | 567 | * @return string |
| 568 | 568 | */ |
| 569 | 569 | static public function getLocalPath($path) { |
| 570 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 570 | + $datadir = \OC_User::getHome(\OC_User::getUser()).'/files'; |
|
| 571 | 571 | $newpath = $path; |
| 572 | 572 | if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
| 573 | 573 | $newpath = substr($path, strlen($datadir)); |
@@ -584,7 +584,7 @@ discard block |
||
| 584 | 584 | static public function isValidPath($path) { |
| 585 | 585 | $path = self::normalizePath($path); |
| 586 | 586 | if (!$path || $path[0] !== '/') { |
| 587 | - $path = '/' . $path; |
|
| 587 | + $path = '/'.$path; |
|
| 588 | 588 | } |
| 589 | 589 | if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
| 590 | 590 | return false; |
@@ -813,7 +813,7 @@ discard block |
||
| 813 | 813 | * conversion should get removed as soon as all existing |
| 814 | 814 | * function calls have been fixed. |
| 815 | 815 | */ |
| 816 | - $path = (string)$path; |
|
| 816 | + $path = (string) $path; |
|
| 817 | 817 | |
| 818 | 818 | $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
| 819 | 819 | |
@@ -835,7 +835,7 @@ discard block |
||
| 835 | 835 | |
| 836 | 836 | //add leading slash |
| 837 | 837 | if ($path[0] !== '/') { |
| 838 | - $path = '/' . $path; |
|
| 838 | + $path = '/'.$path; |
|
| 839 | 839 | } |
| 840 | 840 | |
| 841 | 841 | // remove '/./' |
@@ -69,857 +69,857 @@ |
||
| 69 | 69 | |
| 70 | 70 | class Filesystem { |
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @var Mount\Manager $mounts |
|
| 74 | - */ |
|
| 75 | - private static $mounts; |
|
| 76 | - |
|
| 77 | - public static $loaded = false; |
|
| 78 | - /** |
|
| 79 | - * @var \OC\Files\View $defaultInstance |
|
| 80 | - */ |
|
| 81 | - static private $defaultInstance; |
|
| 82 | - |
|
| 83 | - static private $usersSetup = array(); |
|
| 84 | - |
|
| 85 | - static private $normalizedPathCache = null; |
|
| 86 | - |
|
| 87 | - static private $listeningForProviders = false; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * classname which used for hooks handling |
|
| 91 | - * used as signalclass in OC_Hooks::emit() |
|
| 92 | - */ |
|
| 93 | - const CLASSNAME = 'OC_Filesystem'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * signalname emitted before file renaming |
|
| 97 | - * |
|
| 98 | - * @param string $oldpath |
|
| 99 | - * @param string $newpath |
|
| 100 | - */ |
|
| 101 | - const signal_rename = 'rename'; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * signal emitted after file renaming |
|
| 105 | - * |
|
| 106 | - * @param string $oldpath |
|
| 107 | - * @param string $newpath |
|
| 108 | - */ |
|
| 109 | - const signal_post_rename = 'post_rename'; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * signal emitted before file/dir creation |
|
| 113 | - * |
|
| 114 | - * @param string $path |
|
| 115 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 116 | - */ |
|
| 117 | - const signal_create = 'create'; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * signal emitted after file/dir creation |
|
| 121 | - * |
|
| 122 | - * @param string $path |
|
| 123 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 124 | - */ |
|
| 125 | - const signal_post_create = 'post_create'; |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * signal emits before file/dir copy |
|
| 129 | - * |
|
| 130 | - * @param string $oldpath |
|
| 131 | - * @param string $newpath |
|
| 132 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 133 | - */ |
|
| 134 | - const signal_copy = 'copy'; |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * signal emits after file/dir copy |
|
| 138 | - * |
|
| 139 | - * @param string $oldpath |
|
| 140 | - * @param string $newpath |
|
| 141 | - */ |
|
| 142 | - const signal_post_copy = 'post_copy'; |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * signal emits before file/dir save |
|
| 146 | - * |
|
| 147 | - * @param string $path |
|
| 148 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 149 | - */ |
|
| 150 | - const signal_write = 'write'; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * signal emits after file/dir save |
|
| 154 | - * |
|
| 155 | - * @param string $path |
|
| 156 | - */ |
|
| 157 | - const signal_post_write = 'post_write'; |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * signal emitted before file/dir update |
|
| 161 | - * |
|
| 162 | - * @param string $path |
|
| 163 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 164 | - */ |
|
| 165 | - const signal_update = 'update'; |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * signal emitted after file/dir update |
|
| 169 | - * |
|
| 170 | - * @param string $path |
|
| 171 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 172 | - */ |
|
| 173 | - const signal_post_update = 'post_update'; |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * signal emits when reading file/dir |
|
| 177 | - * |
|
| 178 | - * @param string $path |
|
| 179 | - */ |
|
| 180 | - const signal_read = 'read'; |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * signal emits when removing file/dir |
|
| 184 | - * |
|
| 185 | - * @param string $path |
|
| 186 | - */ |
|
| 187 | - const signal_delete = 'delete'; |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * parameters definitions for signals |
|
| 191 | - */ |
|
| 192 | - const signal_param_path = 'path'; |
|
| 193 | - const signal_param_oldpath = 'oldpath'; |
|
| 194 | - const signal_param_newpath = 'newpath'; |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * run - changing this flag to false in hook handler will cancel event |
|
| 198 | - */ |
|
| 199 | - const signal_param_run = 'run'; |
|
| 200 | - |
|
| 201 | - const signal_create_mount = 'create_mount'; |
|
| 202 | - const signal_delete_mount = 'delete_mount'; |
|
| 203 | - const signal_param_mount_type = 'mounttype'; |
|
| 204 | - const signal_param_users = 'users'; |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @var \OC\Files\Storage\StorageFactory $loader |
|
| 208 | - */ |
|
| 209 | - private static $loader; |
|
| 210 | - |
|
| 211 | - /** @var bool */ |
|
| 212 | - private static $logWarningWhenAddingStorageWrapper = true; |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * @param bool $shouldLog |
|
| 216 | - * @return bool previous value |
|
| 217 | - * @internal |
|
| 218 | - */ |
|
| 219 | - public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
| 220 | - $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 221 | - self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
| 222 | - return $previousValue; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @param string $wrapperName |
|
| 227 | - * @param callable $wrapper |
|
| 228 | - * @param int $priority |
|
| 229 | - */ |
|
| 230 | - public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 231 | - if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 232 | - \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 233 | - 'wrapper' => $wrapperName, |
|
| 234 | - 'app' => 'filesystem', |
|
| 235 | - ]); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $mounts = self::getMountManager()->getAll(); |
|
| 239 | - if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 240 | - // do not re-wrap if storage with this name already existed |
|
| 241 | - return; |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Returns the storage factory |
|
| 247 | - * |
|
| 248 | - * @return \OCP\Files\Storage\IStorageFactory |
|
| 249 | - */ |
|
| 250 | - public static function getLoader() { |
|
| 251 | - if (!self::$loader) { |
|
| 252 | - self::$loader = new StorageFactory(); |
|
| 253 | - } |
|
| 254 | - return self::$loader; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Returns the mount manager |
|
| 259 | - * |
|
| 260 | - * @return \OC\Files\Mount\Manager |
|
| 261 | - */ |
|
| 262 | - public static function getMountManager($user = '') { |
|
| 263 | - if (!self::$mounts) { |
|
| 264 | - \OC_Util::setupFS($user); |
|
| 265 | - } |
|
| 266 | - return self::$mounts; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * get the mountpoint of the storage object for a path |
|
| 271 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 272 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 273 | - * and doesn't take the chroot into account ) |
|
| 274 | - * |
|
| 275 | - * @param string $path |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - static public function getMountPoint($path) { |
|
| 279 | - if (!self::$mounts) { |
|
| 280 | - \OC_Util::setupFS(); |
|
| 281 | - } |
|
| 282 | - $mount = self::$mounts->find($path); |
|
| 283 | - if ($mount) { |
|
| 284 | - return $mount->getMountPoint(); |
|
| 285 | - } else { |
|
| 286 | - return ''; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * get a list of all mount points in a directory |
|
| 292 | - * |
|
| 293 | - * @param string $path |
|
| 294 | - * @return string[] |
|
| 295 | - */ |
|
| 296 | - static public function getMountPoints($path) { |
|
| 297 | - if (!self::$mounts) { |
|
| 298 | - \OC_Util::setupFS(); |
|
| 299 | - } |
|
| 300 | - $result = array(); |
|
| 301 | - $mounts = self::$mounts->findIn($path); |
|
| 302 | - foreach ($mounts as $mount) { |
|
| 303 | - $result[] = $mount->getMountPoint(); |
|
| 304 | - } |
|
| 305 | - return $result; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * get the storage mounted at $mountPoint |
|
| 310 | - * |
|
| 311 | - * @param string $mountPoint |
|
| 312 | - * @return \OC\Files\Storage\Storage |
|
| 313 | - */ |
|
| 314 | - public static function getStorage($mountPoint) { |
|
| 315 | - if (!self::$mounts) { |
|
| 316 | - \OC_Util::setupFS(); |
|
| 317 | - } |
|
| 318 | - $mount = self::$mounts->find($mountPoint); |
|
| 319 | - return $mount->getStorage(); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * @param string $id |
|
| 324 | - * @return Mount\MountPoint[] |
|
| 325 | - */ |
|
| 326 | - public static function getMountByStorageId($id) { |
|
| 327 | - if (!self::$mounts) { |
|
| 328 | - \OC_Util::setupFS(); |
|
| 329 | - } |
|
| 330 | - return self::$mounts->findByStorageId($id); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * @param int $id |
|
| 335 | - * @return Mount\MountPoint[] |
|
| 336 | - */ |
|
| 337 | - public static function getMountByNumericId($id) { |
|
| 338 | - if (!self::$mounts) { |
|
| 339 | - \OC_Util::setupFS(); |
|
| 340 | - } |
|
| 341 | - return self::$mounts->findByNumericId($id); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * resolve a path to a storage and internal path |
|
| 346 | - * |
|
| 347 | - * @param string $path |
|
| 348 | - * @return array an array consisting of the storage and the internal path |
|
| 349 | - */ |
|
| 350 | - static public function resolvePath($path) { |
|
| 351 | - if (!self::$mounts) { |
|
| 352 | - \OC_Util::setupFS(); |
|
| 353 | - } |
|
| 354 | - $mount = self::$mounts->find($path); |
|
| 355 | - if ($mount) { |
|
| 356 | - return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
| 357 | - } else { |
|
| 358 | - return array(null, null); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - static public function init($user, $root) { |
|
| 363 | - if (self::$defaultInstance) { |
|
| 364 | - return false; |
|
| 365 | - } |
|
| 366 | - self::getLoader(); |
|
| 367 | - self::$defaultInstance = new View($root); |
|
| 368 | - |
|
| 369 | - if (!self::$mounts) { |
|
| 370 | - self::$mounts = \OC::$server->getMountManager(); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - //load custom mount config |
|
| 374 | - self::initMountPoints($user); |
|
| 375 | - |
|
| 376 | - self::$loaded = true; |
|
| 377 | - |
|
| 378 | - return true; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - static public function initMountManager() { |
|
| 382 | - if (!self::$mounts) { |
|
| 383 | - self::$mounts = \OC::$server->getMountManager(); |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Initialize system and personal mount points for a user |
|
| 389 | - * |
|
| 390 | - * @param string $user |
|
| 391 | - * @throws \OC\User\NoUserException if the user is not available |
|
| 392 | - */ |
|
| 393 | - public static function initMountPoints($user = '') { |
|
| 394 | - if ($user == '') { |
|
| 395 | - $user = \OC_User::getUser(); |
|
| 396 | - } |
|
| 397 | - if ($user === null || $user === false || $user === '') { |
|
| 398 | - throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - if (isset(self::$usersSetup[$user])) { |
|
| 402 | - return; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - self::$usersSetup[$user] = true; |
|
| 406 | - |
|
| 407 | - $userManager = \OC::$server->getUserManager(); |
|
| 408 | - $userObject = $userManager->get($user); |
|
| 409 | - |
|
| 410 | - if (is_null($userObject)) { |
|
| 411 | - \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR); |
|
| 412 | - // reset flag, this will make it possible to rethrow the exception if called again |
|
| 413 | - unset(self::$usersSetup[$user]); |
|
| 414 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - $realUid = $userObject->getUID(); |
|
| 418 | - // workaround in case of different casings |
|
| 419 | - if ($user !== $realUid) { |
|
| 420 | - $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
| 421 | - \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, \OCP\Util::WARN); |
|
| 422 | - $user = $realUid; |
|
| 423 | - |
|
| 424 | - // again with the correct casing |
|
| 425 | - if (isset(self::$usersSetup[$user])) { |
|
| 426 | - return; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - self::$usersSetup[$user] = true; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
| 433 | - /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
| 434 | - $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
| 435 | - |
|
| 436 | - // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
| 437 | - $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
| 438 | - |
|
| 439 | - self::getMountManager()->addMount($homeMount); |
|
| 440 | - |
|
| 441 | - \OC\Files\Filesystem::getStorage($user); |
|
| 442 | - |
|
| 443 | - // Chance to mount for other storages |
|
| 444 | - if ($userObject) { |
|
| 445 | - $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
| 446 | - $mounts[] = $homeMount; |
|
| 447 | - $mountConfigManager->registerMounts($userObject, $mounts); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
| 451 | - } else { |
|
| 452 | - self::getMountManager()->addMount(new MountPoint( |
|
| 453 | - new NullStorage([]), |
|
| 454 | - '/' . $user |
|
| 455 | - )); |
|
| 456 | - self::getMountManager()->addMount(new MountPoint( |
|
| 457 | - new NullStorage([]), |
|
| 458 | - '/' . $user . '/files' |
|
| 459 | - )); |
|
| 460 | - } |
|
| 461 | - \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get mounts from mount providers that are registered after setup |
|
| 466 | - * |
|
| 467 | - * @param MountProviderCollection $mountConfigManager |
|
| 468 | - * @param IUserManager $userManager |
|
| 469 | - */ |
|
| 470 | - private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
| 471 | - if (!self::$listeningForProviders) { |
|
| 472 | - self::$listeningForProviders = true; |
|
| 473 | - $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 474 | - foreach (Filesystem::$usersSetup as $user => $setup) { |
|
| 475 | - $userObject = $userManager->get($user); |
|
| 476 | - if ($userObject) { |
|
| 477 | - $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
| 478 | - array_walk($mounts, array(self::$mounts, 'addMount')); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - }); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * get the default filesystem view |
|
| 487 | - * |
|
| 488 | - * @return View |
|
| 489 | - */ |
|
| 490 | - static public function getView() { |
|
| 491 | - return self::$defaultInstance; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * tear down the filesystem, removing all storage providers |
|
| 496 | - */ |
|
| 497 | - static public function tearDown() { |
|
| 498 | - self::clearMounts(); |
|
| 499 | - self::$defaultInstance = null; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * get the relative path of the root data directory for the current user |
|
| 504 | - * |
|
| 505 | - * @return string |
|
| 506 | - * |
|
| 507 | - * Returns path like /admin/files |
|
| 508 | - */ |
|
| 509 | - static public function getRoot() { |
|
| 510 | - if (!self::$defaultInstance) { |
|
| 511 | - return null; |
|
| 512 | - } |
|
| 513 | - return self::$defaultInstance->getRoot(); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * clear all mounts and storage backends |
|
| 518 | - */ |
|
| 519 | - public static function clearMounts() { |
|
| 520 | - if (self::$mounts) { |
|
| 521 | - self::$usersSetup = array(); |
|
| 522 | - self::$mounts->clear(); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 528 | - * |
|
| 529 | - * @param \OC\Files\Storage\Storage|string $class |
|
| 530 | - * @param array $arguments |
|
| 531 | - * @param string $mountpoint |
|
| 532 | - */ |
|
| 533 | - static public function mount($class, $arguments, $mountpoint) { |
|
| 534 | - if (!self::$mounts) { |
|
| 535 | - \OC_Util::setupFS(); |
|
| 536 | - } |
|
| 537 | - $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 538 | - self::$mounts->addMount($mount); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * return the path to a local version of the file |
|
| 543 | - * we need this because we can't know if a file is stored local or not from |
|
| 544 | - * outside the filestorage and for some purposes a local file is needed |
|
| 545 | - * |
|
| 546 | - * @param string $path |
|
| 547 | - * @return string |
|
| 548 | - */ |
|
| 549 | - static public function getLocalFile($path) { |
|
| 550 | - return self::$defaultInstance->getLocalFile($path); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * @param string $path |
|
| 555 | - * @return string |
|
| 556 | - */ |
|
| 557 | - static public function getLocalFolder($path) { |
|
| 558 | - return self::$defaultInstance->getLocalFolder($path); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * return path to file which reflects one visible in browser |
|
| 563 | - * |
|
| 564 | - * @param string $path |
|
| 565 | - * @return string |
|
| 566 | - */ |
|
| 567 | - static public function getLocalPath($path) { |
|
| 568 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 569 | - $newpath = $path; |
|
| 570 | - if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 571 | - $newpath = substr($path, strlen($datadir)); |
|
| 572 | - } |
|
| 573 | - return $newpath; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * check if the requested path is valid |
|
| 578 | - * |
|
| 579 | - * @param string $path |
|
| 580 | - * @return bool |
|
| 581 | - */ |
|
| 582 | - static public function isValidPath($path) { |
|
| 583 | - $path = self::normalizePath($path); |
|
| 584 | - if (!$path || $path[0] !== '/') { |
|
| 585 | - $path = '/' . $path; |
|
| 586 | - } |
|
| 587 | - if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
| 588 | - return false; |
|
| 589 | - } |
|
| 590 | - return true; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * checks if a file is blacklisted for storage in the filesystem |
|
| 595 | - * Listens to write and rename hooks |
|
| 596 | - * |
|
| 597 | - * @param array $data from hook |
|
| 598 | - */ |
|
| 599 | - static public function isBlacklisted($data) { |
|
| 600 | - if (isset($data['path'])) { |
|
| 601 | - $path = $data['path']; |
|
| 602 | - } else if (isset($data['newpath'])) { |
|
| 603 | - $path = $data['newpath']; |
|
| 604 | - } |
|
| 605 | - if (isset($path)) { |
|
| 606 | - if (self::isFileBlacklisted($path)) { |
|
| 607 | - $data['run'] = false; |
|
| 608 | - } |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * @param string $filename |
|
| 614 | - * @return bool |
|
| 615 | - */ |
|
| 616 | - static public function isFileBlacklisted($filename) { |
|
| 617 | - $filename = self::normalizePath($filename); |
|
| 618 | - |
|
| 619 | - $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
| 620 | - $filename = strtolower(basename($filename)); |
|
| 621 | - return in_array($filename, $blacklist); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * check if the directory should be ignored when scanning |
|
| 626 | - * NOTE: the special directories . and .. would cause never ending recursion |
|
| 627 | - * |
|
| 628 | - * @param String $dir |
|
| 629 | - * @return boolean |
|
| 630 | - */ |
|
| 631 | - static public function isIgnoredDir($dir) { |
|
| 632 | - if ($dir === '.' || $dir === '..') { |
|
| 633 | - return true; |
|
| 634 | - } |
|
| 635 | - return false; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 640 | - */ |
|
| 641 | - static public function mkdir($path) { |
|
| 642 | - return self::$defaultInstance->mkdir($path); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - static public function rmdir($path) { |
|
| 646 | - return self::$defaultInstance->rmdir($path); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - static public function is_dir($path) { |
|
| 650 | - return self::$defaultInstance->is_dir($path); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - static public function is_file($path) { |
|
| 654 | - return self::$defaultInstance->is_file($path); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - static public function stat($path) { |
|
| 658 | - return self::$defaultInstance->stat($path); |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - static public function filetype($path) { |
|
| 662 | - return self::$defaultInstance->filetype($path); |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - static public function filesize($path) { |
|
| 666 | - return self::$defaultInstance->filesize($path); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - static public function readfile($path) { |
|
| 670 | - return self::$defaultInstance->readfile($path); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - static public function isCreatable($path) { |
|
| 674 | - return self::$defaultInstance->isCreatable($path); |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - static public function isReadable($path) { |
|
| 678 | - return self::$defaultInstance->isReadable($path); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - static public function isUpdatable($path) { |
|
| 682 | - return self::$defaultInstance->isUpdatable($path); |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - static public function isDeletable($path) { |
|
| 686 | - return self::$defaultInstance->isDeletable($path); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - static public function isSharable($path) { |
|
| 690 | - return self::$defaultInstance->isSharable($path); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - static public function file_exists($path) { |
|
| 694 | - return self::$defaultInstance->file_exists($path); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - static public function filemtime($path) { |
|
| 698 | - return self::$defaultInstance->filemtime($path); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - static public function touch($path, $mtime = null) { |
|
| 702 | - return self::$defaultInstance->touch($path, $mtime); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * @return string |
|
| 707 | - */ |
|
| 708 | - static public function file_get_contents($path) { |
|
| 709 | - return self::$defaultInstance->file_get_contents($path); |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - static public function file_put_contents($path, $data) { |
|
| 713 | - return self::$defaultInstance->file_put_contents($path, $data); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - static public function unlink($path) { |
|
| 717 | - return self::$defaultInstance->unlink($path); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - static public function rename($path1, $path2) { |
|
| 721 | - return self::$defaultInstance->rename($path1, $path2); |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - static public function copy($path1, $path2) { |
|
| 725 | - return self::$defaultInstance->copy($path1, $path2); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - static public function fopen($path, $mode) { |
|
| 729 | - return self::$defaultInstance->fopen($path, $mode); |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - /** |
|
| 733 | - * @return string |
|
| 734 | - */ |
|
| 735 | - static public function toTmpFile($path) { |
|
| 736 | - return self::$defaultInstance->toTmpFile($path); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - static public function fromTmpFile($tmpFile, $path) { |
|
| 740 | - return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - static public function getMimeType($path) { |
|
| 744 | - return self::$defaultInstance->getMimeType($path); |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - static public function hash($type, $path, $raw = false) { |
|
| 748 | - return self::$defaultInstance->hash($type, $path, $raw); |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - static public function free_space($path = '/') { |
|
| 752 | - return self::$defaultInstance->free_space($path); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - static public function search($query) { |
|
| 756 | - return self::$defaultInstance->search($query); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - /** |
|
| 760 | - * @param string $query |
|
| 761 | - */ |
|
| 762 | - static public function searchByMime($query) { |
|
| 763 | - return self::$defaultInstance->searchByMime($query); |
|
| 764 | - } |
|
| 765 | - |
|
| 766 | - /** |
|
| 767 | - * @param string|int $tag name or tag id |
|
| 768 | - * @param string $userId owner of the tags |
|
| 769 | - * @return FileInfo[] array or file info |
|
| 770 | - */ |
|
| 771 | - static public function searchByTag($tag, $userId) { |
|
| 772 | - return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - /** |
|
| 776 | - * check if a file or folder has been updated since $time |
|
| 777 | - * |
|
| 778 | - * @param string $path |
|
| 779 | - * @param int $time |
|
| 780 | - * @return bool |
|
| 781 | - */ |
|
| 782 | - static public function hasUpdated($path, $time) { |
|
| 783 | - return self::$defaultInstance->hasUpdated($path, $time); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Fix common problems with a file path |
|
| 788 | - * |
|
| 789 | - * @param string $path |
|
| 790 | - * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 791 | - * @param bool $isAbsolutePath whether the given path is absolute |
|
| 792 | - * @param bool $keepUnicode true to disable unicode normalization |
|
| 793 | - * @return string |
|
| 794 | - */ |
|
| 795 | - public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 796 | - if (is_null(self::$normalizedPathCache)) { |
|
| 797 | - self::$normalizedPathCache = new CappedMemoryCache(); |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * FIXME: This is a workaround for existing classes and files which call |
|
| 802 | - * this function with another type than a valid string. This |
|
| 803 | - * conversion should get removed as soon as all existing |
|
| 804 | - * function calls have been fixed. |
|
| 805 | - */ |
|
| 806 | - $path = (string)$path; |
|
| 807 | - |
|
| 808 | - $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 809 | - |
|
| 810 | - if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 811 | - return self::$normalizedPathCache[$cacheKey]; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - if ($path == '') { |
|
| 815 | - return '/'; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - //normalize unicode if possible |
|
| 819 | - if (!$keepUnicode) { |
|
| 820 | - $path = \OC_Util::normalizeUnicode($path); |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - //no windows style slashes |
|
| 824 | - $path = str_replace('\\', '/', $path); |
|
| 825 | - |
|
| 826 | - //add leading slash |
|
| 827 | - if ($path[0] !== '/') { |
|
| 828 | - $path = '/' . $path; |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - // remove '/./' |
|
| 832 | - // ugly, but str_replace() can't replace them all in one go |
|
| 833 | - // as the replacement itself is part of the search string |
|
| 834 | - // which will only be found during the next iteration |
|
| 835 | - while (strpos($path, '/./') !== false) { |
|
| 836 | - $path = str_replace('/./', '/', $path); |
|
| 837 | - } |
|
| 838 | - // remove sequences of slashes |
|
| 839 | - $path = preg_replace('#/{2,}#', '/', $path); |
|
| 840 | - |
|
| 841 | - //remove trailing slash |
|
| 842 | - if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') { |
|
| 843 | - $path = substr($path, 0, -1); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - // remove trailing '/.' |
|
| 847 | - if (substr($path, -2) == '/.') { |
|
| 848 | - $path = substr($path, 0, -2); |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - $normalizedPath = $path; |
|
| 852 | - self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
| 853 | - |
|
| 854 | - return $normalizedPath; |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * get the filesystem info |
|
| 859 | - * |
|
| 860 | - * @param string $path |
|
| 861 | - * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
| 862 | - * defaults to true |
|
| 863 | - * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 864 | - */ |
|
| 865 | - public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 866 | - return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - /** |
|
| 870 | - * change file metadata |
|
| 871 | - * |
|
| 872 | - * @param string $path |
|
| 873 | - * @param array $data |
|
| 874 | - * @return int |
|
| 875 | - * |
|
| 876 | - * returns the fileid of the updated file |
|
| 877 | - */ |
|
| 878 | - public static function putFileInfo($path, $data) { |
|
| 879 | - return self::$defaultInstance->putFileInfo($path, $data); |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * get the content of a directory |
|
| 884 | - * |
|
| 885 | - * @param string $directory path under datadirectory |
|
| 886 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 887 | - * @return \OC\Files\FileInfo[] |
|
| 888 | - */ |
|
| 889 | - public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 890 | - return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - /** |
|
| 894 | - * Get the path of a file by id |
|
| 895 | - * |
|
| 896 | - * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 897 | - * |
|
| 898 | - * @param int $id |
|
| 899 | - * @throws NotFoundException |
|
| 900 | - * @return string |
|
| 901 | - */ |
|
| 902 | - public static function getPath($id) { |
|
| 903 | - return self::$defaultInstance->getPath($id); |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Get the owner for a file or folder |
|
| 908 | - * |
|
| 909 | - * @param string $path |
|
| 910 | - * @return string |
|
| 911 | - */ |
|
| 912 | - public static function getOwner($path) { |
|
| 913 | - return self::$defaultInstance->getOwner($path); |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - /** |
|
| 917 | - * get the ETag for a file or folder |
|
| 918 | - * |
|
| 919 | - * @param string $path |
|
| 920 | - * @return string |
|
| 921 | - */ |
|
| 922 | - static public function getETag($path) { |
|
| 923 | - return self::$defaultInstance->getETag($path); |
|
| 924 | - } |
|
| 72 | + /** |
|
| 73 | + * @var Mount\Manager $mounts |
|
| 74 | + */ |
|
| 75 | + private static $mounts; |
|
| 76 | + |
|
| 77 | + public static $loaded = false; |
|
| 78 | + /** |
|
| 79 | + * @var \OC\Files\View $defaultInstance |
|
| 80 | + */ |
|
| 81 | + static private $defaultInstance; |
|
| 82 | + |
|
| 83 | + static private $usersSetup = array(); |
|
| 84 | + |
|
| 85 | + static private $normalizedPathCache = null; |
|
| 86 | + |
|
| 87 | + static private $listeningForProviders = false; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * classname which used for hooks handling |
|
| 91 | + * used as signalclass in OC_Hooks::emit() |
|
| 92 | + */ |
|
| 93 | + const CLASSNAME = 'OC_Filesystem'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * signalname emitted before file renaming |
|
| 97 | + * |
|
| 98 | + * @param string $oldpath |
|
| 99 | + * @param string $newpath |
|
| 100 | + */ |
|
| 101 | + const signal_rename = 'rename'; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * signal emitted after file renaming |
|
| 105 | + * |
|
| 106 | + * @param string $oldpath |
|
| 107 | + * @param string $newpath |
|
| 108 | + */ |
|
| 109 | + const signal_post_rename = 'post_rename'; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * signal emitted before file/dir creation |
|
| 113 | + * |
|
| 114 | + * @param string $path |
|
| 115 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 116 | + */ |
|
| 117 | + const signal_create = 'create'; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * signal emitted after file/dir creation |
|
| 121 | + * |
|
| 122 | + * @param string $path |
|
| 123 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 124 | + */ |
|
| 125 | + const signal_post_create = 'post_create'; |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * signal emits before file/dir copy |
|
| 129 | + * |
|
| 130 | + * @param string $oldpath |
|
| 131 | + * @param string $newpath |
|
| 132 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 133 | + */ |
|
| 134 | + const signal_copy = 'copy'; |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * signal emits after file/dir copy |
|
| 138 | + * |
|
| 139 | + * @param string $oldpath |
|
| 140 | + * @param string $newpath |
|
| 141 | + */ |
|
| 142 | + const signal_post_copy = 'post_copy'; |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * signal emits before file/dir save |
|
| 146 | + * |
|
| 147 | + * @param string $path |
|
| 148 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 149 | + */ |
|
| 150 | + const signal_write = 'write'; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * signal emits after file/dir save |
|
| 154 | + * |
|
| 155 | + * @param string $path |
|
| 156 | + */ |
|
| 157 | + const signal_post_write = 'post_write'; |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * signal emitted before file/dir update |
|
| 161 | + * |
|
| 162 | + * @param string $path |
|
| 163 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 164 | + */ |
|
| 165 | + const signal_update = 'update'; |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * signal emitted after file/dir update |
|
| 169 | + * |
|
| 170 | + * @param string $path |
|
| 171 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 172 | + */ |
|
| 173 | + const signal_post_update = 'post_update'; |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * signal emits when reading file/dir |
|
| 177 | + * |
|
| 178 | + * @param string $path |
|
| 179 | + */ |
|
| 180 | + const signal_read = 'read'; |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * signal emits when removing file/dir |
|
| 184 | + * |
|
| 185 | + * @param string $path |
|
| 186 | + */ |
|
| 187 | + const signal_delete = 'delete'; |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * parameters definitions for signals |
|
| 191 | + */ |
|
| 192 | + const signal_param_path = 'path'; |
|
| 193 | + const signal_param_oldpath = 'oldpath'; |
|
| 194 | + const signal_param_newpath = 'newpath'; |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * run - changing this flag to false in hook handler will cancel event |
|
| 198 | + */ |
|
| 199 | + const signal_param_run = 'run'; |
|
| 200 | + |
|
| 201 | + const signal_create_mount = 'create_mount'; |
|
| 202 | + const signal_delete_mount = 'delete_mount'; |
|
| 203 | + const signal_param_mount_type = 'mounttype'; |
|
| 204 | + const signal_param_users = 'users'; |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @var \OC\Files\Storage\StorageFactory $loader |
|
| 208 | + */ |
|
| 209 | + private static $loader; |
|
| 210 | + |
|
| 211 | + /** @var bool */ |
|
| 212 | + private static $logWarningWhenAddingStorageWrapper = true; |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * @param bool $shouldLog |
|
| 216 | + * @return bool previous value |
|
| 217 | + * @internal |
|
| 218 | + */ |
|
| 219 | + public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
| 220 | + $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 221 | + self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
| 222 | + return $previousValue; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @param string $wrapperName |
|
| 227 | + * @param callable $wrapper |
|
| 228 | + * @param int $priority |
|
| 229 | + */ |
|
| 230 | + public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 231 | + if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 232 | + \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 233 | + 'wrapper' => $wrapperName, |
|
| 234 | + 'app' => 'filesystem', |
|
| 235 | + ]); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $mounts = self::getMountManager()->getAll(); |
|
| 239 | + if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 240 | + // do not re-wrap if storage with this name already existed |
|
| 241 | + return; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Returns the storage factory |
|
| 247 | + * |
|
| 248 | + * @return \OCP\Files\Storage\IStorageFactory |
|
| 249 | + */ |
|
| 250 | + public static function getLoader() { |
|
| 251 | + if (!self::$loader) { |
|
| 252 | + self::$loader = new StorageFactory(); |
|
| 253 | + } |
|
| 254 | + return self::$loader; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Returns the mount manager |
|
| 259 | + * |
|
| 260 | + * @return \OC\Files\Mount\Manager |
|
| 261 | + */ |
|
| 262 | + public static function getMountManager($user = '') { |
|
| 263 | + if (!self::$mounts) { |
|
| 264 | + \OC_Util::setupFS($user); |
|
| 265 | + } |
|
| 266 | + return self::$mounts; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * get the mountpoint of the storage object for a path |
|
| 271 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 272 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 273 | + * and doesn't take the chroot into account ) |
|
| 274 | + * |
|
| 275 | + * @param string $path |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + static public function getMountPoint($path) { |
|
| 279 | + if (!self::$mounts) { |
|
| 280 | + \OC_Util::setupFS(); |
|
| 281 | + } |
|
| 282 | + $mount = self::$mounts->find($path); |
|
| 283 | + if ($mount) { |
|
| 284 | + return $mount->getMountPoint(); |
|
| 285 | + } else { |
|
| 286 | + return ''; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * get a list of all mount points in a directory |
|
| 292 | + * |
|
| 293 | + * @param string $path |
|
| 294 | + * @return string[] |
|
| 295 | + */ |
|
| 296 | + static public function getMountPoints($path) { |
|
| 297 | + if (!self::$mounts) { |
|
| 298 | + \OC_Util::setupFS(); |
|
| 299 | + } |
|
| 300 | + $result = array(); |
|
| 301 | + $mounts = self::$mounts->findIn($path); |
|
| 302 | + foreach ($mounts as $mount) { |
|
| 303 | + $result[] = $mount->getMountPoint(); |
|
| 304 | + } |
|
| 305 | + return $result; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * get the storage mounted at $mountPoint |
|
| 310 | + * |
|
| 311 | + * @param string $mountPoint |
|
| 312 | + * @return \OC\Files\Storage\Storage |
|
| 313 | + */ |
|
| 314 | + public static function getStorage($mountPoint) { |
|
| 315 | + if (!self::$mounts) { |
|
| 316 | + \OC_Util::setupFS(); |
|
| 317 | + } |
|
| 318 | + $mount = self::$mounts->find($mountPoint); |
|
| 319 | + return $mount->getStorage(); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @param string $id |
|
| 324 | + * @return Mount\MountPoint[] |
|
| 325 | + */ |
|
| 326 | + public static function getMountByStorageId($id) { |
|
| 327 | + if (!self::$mounts) { |
|
| 328 | + \OC_Util::setupFS(); |
|
| 329 | + } |
|
| 330 | + return self::$mounts->findByStorageId($id); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * @param int $id |
|
| 335 | + * @return Mount\MountPoint[] |
|
| 336 | + */ |
|
| 337 | + public static function getMountByNumericId($id) { |
|
| 338 | + if (!self::$mounts) { |
|
| 339 | + \OC_Util::setupFS(); |
|
| 340 | + } |
|
| 341 | + return self::$mounts->findByNumericId($id); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * resolve a path to a storage and internal path |
|
| 346 | + * |
|
| 347 | + * @param string $path |
|
| 348 | + * @return array an array consisting of the storage and the internal path |
|
| 349 | + */ |
|
| 350 | + static public function resolvePath($path) { |
|
| 351 | + if (!self::$mounts) { |
|
| 352 | + \OC_Util::setupFS(); |
|
| 353 | + } |
|
| 354 | + $mount = self::$mounts->find($path); |
|
| 355 | + if ($mount) { |
|
| 356 | + return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
| 357 | + } else { |
|
| 358 | + return array(null, null); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + static public function init($user, $root) { |
|
| 363 | + if (self::$defaultInstance) { |
|
| 364 | + return false; |
|
| 365 | + } |
|
| 366 | + self::getLoader(); |
|
| 367 | + self::$defaultInstance = new View($root); |
|
| 368 | + |
|
| 369 | + if (!self::$mounts) { |
|
| 370 | + self::$mounts = \OC::$server->getMountManager(); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + //load custom mount config |
|
| 374 | + self::initMountPoints($user); |
|
| 375 | + |
|
| 376 | + self::$loaded = true; |
|
| 377 | + |
|
| 378 | + return true; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + static public function initMountManager() { |
|
| 382 | + if (!self::$mounts) { |
|
| 383 | + self::$mounts = \OC::$server->getMountManager(); |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Initialize system and personal mount points for a user |
|
| 389 | + * |
|
| 390 | + * @param string $user |
|
| 391 | + * @throws \OC\User\NoUserException if the user is not available |
|
| 392 | + */ |
|
| 393 | + public static function initMountPoints($user = '') { |
|
| 394 | + if ($user == '') { |
|
| 395 | + $user = \OC_User::getUser(); |
|
| 396 | + } |
|
| 397 | + if ($user === null || $user === false || $user === '') { |
|
| 398 | + throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + if (isset(self::$usersSetup[$user])) { |
|
| 402 | + return; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + self::$usersSetup[$user] = true; |
|
| 406 | + |
|
| 407 | + $userManager = \OC::$server->getUserManager(); |
|
| 408 | + $userObject = $userManager->get($user); |
|
| 409 | + |
|
| 410 | + if (is_null($userObject)) { |
|
| 411 | + \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR); |
|
| 412 | + // reset flag, this will make it possible to rethrow the exception if called again |
|
| 413 | + unset(self::$usersSetup[$user]); |
|
| 414 | + throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + $realUid = $userObject->getUID(); |
|
| 418 | + // workaround in case of different casings |
|
| 419 | + if ($user !== $realUid) { |
|
| 420 | + $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
| 421 | + \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, \OCP\Util::WARN); |
|
| 422 | + $user = $realUid; |
|
| 423 | + |
|
| 424 | + // again with the correct casing |
|
| 425 | + if (isset(self::$usersSetup[$user])) { |
|
| 426 | + return; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + self::$usersSetup[$user] = true; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
| 433 | + /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
| 434 | + $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
| 435 | + |
|
| 436 | + // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
| 437 | + $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
| 438 | + |
|
| 439 | + self::getMountManager()->addMount($homeMount); |
|
| 440 | + |
|
| 441 | + \OC\Files\Filesystem::getStorage($user); |
|
| 442 | + |
|
| 443 | + // Chance to mount for other storages |
|
| 444 | + if ($userObject) { |
|
| 445 | + $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
| 446 | + $mounts[] = $homeMount; |
|
| 447 | + $mountConfigManager->registerMounts($userObject, $mounts); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
| 451 | + } else { |
|
| 452 | + self::getMountManager()->addMount(new MountPoint( |
|
| 453 | + new NullStorage([]), |
|
| 454 | + '/' . $user |
|
| 455 | + )); |
|
| 456 | + self::getMountManager()->addMount(new MountPoint( |
|
| 457 | + new NullStorage([]), |
|
| 458 | + '/' . $user . '/files' |
|
| 459 | + )); |
|
| 460 | + } |
|
| 461 | + \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get mounts from mount providers that are registered after setup |
|
| 466 | + * |
|
| 467 | + * @param MountProviderCollection $mountConfigManager |
|
| 468 | + * @param IUserManager $userManager |
|
| 469 | + */ |
|
| 470 | + private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
| 471 | + if (!self::$listeningForProviders) { |
|
| 472 | + self::$listeningForProviders = true; |
|
| 473 | + $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 474 | + foreach (Filesystem::$usersSetup as $user => $setup) { |
|
| 475 | + $userObject = $userManager->get($user); |
|
| 476 | + if ($userObject) { |
|
| 477 | + $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
| 478 | + array_walk($mounts, array(self::$mounts, 'addMount')); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + }); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * get the default filesystem view |
|
| 487 | + * |
|
| 488 | + * @return View |
|
| 489 | + */ |
|
| 490 | + static public function getView() { |
|
| 491 | + return self::$defaultInstance; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * tear down the filesystem, removing all storage providers |
|
| 496 | + */ |
|
| 497 | + static public function tearDown() { |
|
| 498 | + self::clearMounts(); |
|
| 499 | + self::$defaultInstance = null; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * get the relative path of the root data directory for the current user |
|
| 504 | + * |
|
| 505 | + * @return string |
|
| 506 | + * |
|
| 507 | + * Returns path like /admin/files |
|
| 508 | + */ |
|
| 509 | + static public function getRoot() { |
|
| 510 | + if (!self::$defaultInstance) { |
|
| 511 | + return null; |
|
| 512 | + } |
|
| 513 | + return self::$defaultInstance->getRoot(); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * clear all mounts and storage backends |
|
| 518 | + */ |
|
| 519 | + public static function clearMounts() { |
|
| 520 | + if (self::$mounts) { |
|
| 521 | + self::$usersSetup = array(); |
|
| 522 | + self::$mounts->clear(); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 528 | + * |
|
| 529 | + * @param \OC\Files\Storage\Storage|string $class |
|
| 530 | + * @param array $arguments |
|
| 531 | + * @param string $mountpoint |
|
| 532 | + */ |
|
| 533 | + static public function mount($class, $arguments, $mountpoint) { |
|
| 534 | + if (!self::$mounts) { |
|
| 535 | + \OC_Util::setupFS(); |
|
| 536 | + } |
|
| 537 | + $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 538 | + self::$mounts->addMount($mount); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * return the path to a local version of the file |
|
| 543 | + * we need this because we can't know if a file is stored local or not from |
|
| 544 | + * outside the filestorage and for some purposes a local file is needed |
|
| 545 | + * |
|
| 546 | + * @param string $path |
|
| 547 | + * @return string |
|
| 548 | + */ |
|
| 549 | + static public function getLocalFile($path) { |
|
| 550 | + return self::$defaultInstance->getLocalFile($path); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * @param string $path |
|
| 555 | + * @return string |
|
| 556 | + */ |
|
| 557 | + static public function getLocalFolder($path) { |
|
| 558 | + return self::$defaultInstance->getLocalFolder($path); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * return path to file which reflects one visible in browser |
|
| 563 | + * |
|
| 564 | + * @param string $path |
|
| 565 | + * @return string |
|
| 566 | + */ |
|
| 567 | + static public function getLocalPath($path) { |
|
| 568 | + $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 569 | + $newpath = $path; |
|
| 570 | + if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 571 | + $newpath = substr($path, strlen($datadir)); |
|
| 572 | + } |
|
| 573 | + return $newpath; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * check if the requested path is valid |
|
| 578 | + * |
|
| 579 | + * @param string $path |
|
| 580 | + * @return bool |
|
| 581 | + */ |
|
| 582 | + static public function isValidPath($path) { |
|
| 583 | + $path = self::normalizePath($path); |
|
| 584 | + if (!$path || $path[0] !== '/') { |
|
| 585 | + $path = '/' . $path; |
|
| 586 | + } |
|
| 587 | + if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
| 588 | + return false; |
|
| 589 | + } |
|
| 590 | + return true; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * checks if a file is blacklisted for storage in the filesystem |
|
| 595 | + * Listens to write and rename hooks |
|
| 596 | + * |
|
| 597 | + * @param array $data from hook |
|
| 598 | + */ |
|
| 599 | + static public function isBlacklisted($data) { |
|
| 600 | + if (isset($data['path'])) { |
|
| 601 | + $path = $data['path']; |
|
| 602 | + } else if (isset($data['newpath'])) { |
|
| 603 | + $path = $data['newpath']; |
|
| 604 | + } |
|
| 605 | + if (isset($path)) { |
|
| 606 | + if (self::isFileBlacklisted($path)) { |
|
| 607 | + $data['run'] = false; |
|
| 608 | + } |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * @param string $filename |
|
| 614 | + * @return bool |
|
| 615 | + */ |
|
| 616 | + static public function isFileBlacklisted($filename) { |
|
| 617 | + $filename = self::normalizePath($filename); |
|
| 618 | + |
|
| 619 | + $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
| 620 | + $filename = strtolower(basename($filename)); |
|
| 621 | + return in_array($filename, $blacklist); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * check if the directory should be ignored when scanning |
|
| 626 | + * NOTE: the special directories . and .. would cause never ending recursion |
|
| 627 | + * |
|
| 628 | + * @param String $dir |
|
| 629 | + * @return boolean |
|
| 630 | + */ |
|
| 631 | + static public function isIgnoredDir($dir) { |
|
| 632 | + if ($dir === '.' || $dir === '..') { |
|
| 633 | + return true; |
|
| 634 | + } |
|
| 635 | + return false; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 640 | + */ |
|
| 641 | + static public function mkdir($path) { |
|
| 642 | + return self::$defaultInstance->mkdir($path); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + static public function rmdir($path) { |
|
| 646 | + return self::$defaultInstance->rmdir($path); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + static public function is_dir($path) { |
|
| 650 | + return self::$defaultInstance->is_dir($path); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + static public function is_file($path) { |
|
| 654 | + return self::$defaultInstance->is_file($path); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + static public function stat($path) { |
|
| 658 | + return self::$defaultInstance->stat($path); |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + static public function filetype($path) { |
|
| 662 | + return self::$defaultInstance->filetype($path); |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + static public function filesize($path) { |
|
| 666 | + return self::$defaultInstance->filesize($path); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + static public function readfile($path) { |
|
| 670 | + return self::$defaultInstance->readfile($path); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + static public function isCreatable($path) { |
|
| 674 | + return self::$defaultInstance->isCreatable($path); |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + static public function isReadable($path) { |
|
| 678 | + return self::$defaultInstance->isReadable($path); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + static public function isUpdatable($path) { |
|
| 682 | + return self::$defaultInstance->isUpdatable($path); |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + static public function isDeletable($path) { |
|
| 686 | + return self::$defaultInstance->isDeletable($path); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + static public function isSharable($path) { |
|
| 690 | + return self::$defaultInstance->isSharable($path); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + static public function file_exists($path) { |
|
| 694 | + return self::$defaultInstance->file_exists($path); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + static public function filemtime($path) { |
|
| 698 | + return self::$defaultInstance->filemtime($path); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + static public function touch($path, $mtime = null) { |
|
| 702 | + return self::$defaultInstance->touch($path, $mtime); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * @return string |
|
| 707 | + */ |
|
| 708 | + static public function file_get_contents($path) { |
|
| 709 | + return self::$defaultInstance->file_get_contents($path); |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + static public function file_put_contents($path, $data) { |
|
| 713 | + return self::$defaultInstance->file_put_contents($path, $data); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + static public function unlink($path) { |
|
| 717 | + return self::$defaultInstance->unlink($path); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + static public function rename($path1, $path2) { |
|
| 721 | + return self::$defaultInstance->rename($path1, $path2); |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + static public function copy($path1, $path2) { |
|
| 725 | + return self::$defaultInstance->copy($path1, $path2); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + static public function fopen($path, $mode) { |
|
| 729 | + return self::$defaultInstance->fopen($path, $mode); |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** |
|
| 733 | + * @return string |
|
| 734 | + */ |
|
| 735 | + static public function toTmpFile($path) { |
|
| 736 | + return self::$defaultInstance->toTmpFile($path); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + static public function fromTmpFile($tmpFile, $path) { |
|
| 740 | + return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + static public function getMimeType($path) { |
|
| 744 | + return self::$defaultInstance->getMimeType($path); |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + static public function hash($type, $path, $raw = false) { |
|
| 748 | + return self::$defaultInstance->hash($type, $path, $raw); |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + static public function free_space($path = '/') { |
|
| 752 | + return self::$defaultInstance->free_space($path); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + static public function search($query) { |
|
| 756 | + return self::$defaultInstance->search($query); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + /** |
|
| 760 | + * @param string $query |
|
| 761 | + */ |
|
| 762 | + static public function searchByMime($query) { |
|
| 763 | + return self::$defaultInstance->searchByMime($query); |
|
| 764 | + } |
|
| 765 | + |
|
| 766 | + /** |
|
| 767 | + * @param string|int $tag name or tag id |
|
| 768 | + * @param string $userId owner of the tags |
|
| 769 | + * @return FileInfo[] array or file info |
|
| 770 | + */ |
|
| 771 | + static public function searchByTag($tag, $userId) { |
|
| 772 | + return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + /** |
|
| 776 | + * check if a file or folder has been updated since $time |
|
| 777 | + * |
|
| 778 | + * @param string $path |
|
| 779 | + * @param int $time |
|
| 780 | + * @return bool |
|
| 781 | + */ |
|
| 782 | + static public function hasUpdated($path, $time) { |
|
| 783 | + return self::$defaultInstance->hasUpdated($path, $time); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Fix common problems with a file path |
|
| 788 | + * |
|
| 789 | + * @param string $path |
|
| 790 | + * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 791 | + * @param bool $isAbsolutePath whether the given path is absolute |
|
| 792 | + * @param bool $keepUnicode true to disable unicode normalization |
|
| 793 | + * @return string |
|
| 794 | + */ |
|
| 795 | + public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 796 | + if (is_null(self::$normalizedPathCache)) { |
|
| 797 | + self::$normalizedPathCache = new CappedMemoryCache(); |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * FIXME: This is a workaround for existing classes and files which call |
|
| 802 | + * this function with another type than a valid string. This |
|
| 803 | + * conversion should get removed as soon as all existing |
|
| 804 | + * function calls have been fixed. |
|
| 805 | + */ |
|
| 806 | + $path = (string)$path; |
|
| 807 | + |
|
| 808 | + $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 809 | + |
|
| 810 | + if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 811 | + return self::$normalizedPathCache[$cacheKey]; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + if ($path == '') { |
|
| 815 | + return '/'; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + //normalize unicode if possible |
|
| 819 | + if (!$keepUnicode) { |
|
| 820 | + $path = \OC_Util::normalizeUnicode($path); |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + //no windows style slashes |
|
| 824 | + $path = str_replace('\\', '/', $path); |
|
| 825 | + |
|
| 826 | + //add leading slash |
|
| 827 | + if ($path[0] !== '/') { |
|
| 828 | + $path = '/' . $path; |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + // remove '/./' |
|
| 832 | + // ugly, but str_replace() can't replace them all in one go |
|
| 833 | + // as the replacement itself is part of the search string |
|
| 834 | + // which will only be found during the next iteration |
|
| 835 | + while (strpos($path, '/./') !== false) { |
|
| 836 | + $path = str_replace('/./', '/', $path); |
|
| 837 | + } |
|
| 838 | + // remove sequences of slashes |
|
| 839 | + $path = preg_replace('#/{2,}#', '/', $path); |
|
| 840 | + |
|
| 841 | + //remove trailing slash |
|
| 842 | + if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') { |
|
| 843 | + $path = substr($path, 0, -1); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + // remove trailing '/.' |
|
| 847 | + if (substr($path, -2) == '/.') { |
|
| 848 | + $path = substr($path, 0, -2); |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + $normalizedPath = $path; |
|
| 852 | + self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
| 853 | + |
|
| 854 | + return $normalizedPath; |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * get the filesystem info |
|
| 859 | + * |
|
| 860 | + * @param string $path |
|
| 861 | + * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
| 862 | + * defaults to true |
|
| 863 | + * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 864 | + */ |
|
| 865 | + public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 866 | + return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + /** |
|
| 870 | + * change file metadata |
|
| 871 | + * |
|
| 872 | + * @param string $path |
|
| 873 | + * @param array $data |
|
| 874 | + * @return int |
|
| 875 | + * |
|
| 876 | + * returns the fileid of the updated file |
|
| 877 | + */ |
|
| 878 | + public static function putFileInfo($path, $data) { |
|
| 879 | + return self::$defaultInstance->putFileInfo($path, $data); |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * get the content of a directory |
|
| 884 | + * |
|
| 885 | + * @param string $directory path under datadirectory |
|
| 886 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 887 | + * @return \OC\Files\FileInfo[] |
|
| 888 | + */ |
|
| 889 | + public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 890 | + return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + /** |
|
| 894 | + * Get the path of a file by id |
|
| 895 | + * |
|
| 896 | + * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 897 | + * |
|
| 898 | + * @param int $id |
|
| 899 | + * @throws NotFoundException |
|
| 900 | + * @return string |
|
| 901 | + */ |
|
| 902 | + public static function getPath($id) { |
|
| 903 | + return self::$defaultInstance->getPath($id); |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Get the owner for a file or folder |
|
| 908 | + * |
|
| 909 | + * @param string $path |
|
| 910 | + * @return string |
|
| 911 | + */ |
|
| 912 | + public static function getOwner($path) { |
|
| 913 | + return self::$defaultInstance->getOwner($path); |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + /** |
|
| 917 | + * get the ETag for a file or folder |
|
| 918 | + * |
|
| 919 | + * @param string $path |
|
| 920 | + * @return string |
|
| 921 | + */ |
|
| 922 | + static public function getETag($path) { |
|
| 923 | + return self::$defaultInstance->getETag($path); |
|
| 924 | + } |
|
| 925 | 925 | } |
@@ -984,7 +984,7 @@ discard block |
||
| 984 | 984 | /** |
| 985 | 985 | * check if path points to a files version |
| 986 | 986 | * |
| 987 | - * @param $path |
|
| 987 | + * @param string $path |
|
| 988 | 988 | * @return bool |
| 989 | 989 | */ |
| 990 | 990 | protected function isVersion($path) { |
@@ -995,7 +995,7 @@ discard block |
||
| 995 | 995 | /** |
| 996 | 996 | * check if the given storage should be encrypted or not |
| 997 | 997 | * |
| 998 | - * @param $path |
|
| 998 | + * @param string $path |
|
| 999 | 999 | * @return bool |
| 1000 | 1000 | */ |
| 1001 | 1001 | protected function shouldEncrypt($path) { |
@@ -370,8 +370,8 @@ discard block |
||
| 370 | 370 | // check if the file is stored in the array cache, this means that we |
| 371 | 371 | // copy a file over to the versions folder, in this case we don't want to |
| 372 | 372 | // decrypt it |
| 373 | - if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
| 374 | - $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
| 373 | + if ($this->arrayCache->hasKey('encryption_copy_version_'.$path)) { |
|
| 374 | + $this->arrayCache->remove('encryption_copy_version_'.$path); |
|
| 375 | 375 | return $this->storage->fopen($path, $mode); |
| 376 | 376 | } |
| 377 | 377 | |
@@ -441,8 +441,8 @@ discard block |
||
| 441 | 441 | } |
| 442 | 442 | } |
| 443 | 443 | } catch (ModuleDoesNotExistsException $e) { |
| 444 | - $this->logger->warning('Encryption module "' . $encryptionModuleId . |
|
| 445 | - '" not found, file will be stored unencrypted (' . $e->getMessage() . ')'); |
|
| 444 | + $this->logger->warning('Encryption module "'.$encryptionModuleId. |
|
| 445 | + '" not found, file will be stored unencrypted ('.$e->getMessage().')'); |
|
| 446 | 446 | } |
| 447 | 447 | |
| 448 | 448 | // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | try { |
| 495 | 495 | $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
| 496 | 496 | } catch (\Exception $e) { |
| 497 | - $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
| 497 | + $this->logger->error('Couldn\'t re-calculate unencrypted size for '.$path); |
|
| 498 | 498 | $this->logger->logException($e); |
| 499 | 499 | } |
| 500 | 500 | unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
@@ -523,7 +523,7 @@ discard block |
||
| 523 | 523 | |
| 524 | 524 | // if we couldn't open the file we return the old unencrypted size |
| 525 | 525 | if (!is_resource($stream)) { |
| 526 | - $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
| 526 | + $this->logger->error('Could not open '.$path.'. Recalculation of unencrypted size aborted.'); |
|
| 527 | 527 | return $unencryptedSize; |
| 528 | 528 | } |
| 529 | 529 | |
@@ -548,7 +548,7 @@ discard block |
||
| 548 | 548 | // next highest is end of chunks, one subtracted is last one |
| 549 | 549 | // we have to read the last chunk, we can't just calculate it (because of padding etc) |
| 550 | 550 | |
| 551 | - $lastChunkNr = ceil($size/ $blockSize)-1; |
|
| 551 | + $lastChunkNr = ceil($size / $blockSize) - 1; |
|
| 552 | 552 | // calculate last chunk position |
| 553 | 553 | $lastChunkPos = ($lastChunkNr * $blockSize); |
| 554 | 554 | // try to fseek to the last chunk, if it fails we have to read the whole file |
@@ -556,16 +556,16 @@ discard block |
||
| 556 | 556 | $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
| 557 | 557 | } |
| 558 | 558 | |
| 559 | - $lastChunkContentEncrypted=''; |
|
| 559 | + $lastChunkContentEncrypted = ''; |
|
| 560 | 560 | $count = $blockSize; |
| 561 | 561 | |
| 562 | 562 | while ($count > 0) { |
| 563 | - $data=fread($stream, $blockSize); |
|
| 564 | - $count=strlen($data); |
|
| 563 | + $data = fread($stream, $blockSize); |
|
| 564 | + $count = strlen($data); |
|
| 565 | 565 | $lastChunkContentEncrypted .= $data; |
| 566 | - if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 566 | + if (strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 567 | 567 | $newUnencryptedSize += $unencryptedBlockSize; |
| 568 | - $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
| 568 | + $lastChunkContentEncrypted = substr($lastChunkContentEncrypted, $blockSize); |
|
| 569 | 569 | } |
| 570 | 570 | } |
| 571 | 571 | |
@@ -573,8 +573,8 @@ discard block |
||
| 573 | 573 | |
| 574 | 574 | // we have to decrypt the last chunk to get it actual size |
| 575 | 575 | $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
| 576 | - $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
| 577 | - $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
| 576 | + $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr.'end'); |
|
| 577 | + $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr.'end'); |
|
| 578 | 578 | |
| 579 | 579 | // calc the real file size with the size of the last chunk |
| 580 | 580 | $newUnencryptedSize += strlen($decryptedLastChunk); |
@@ -658,7 +658,7 @@ discard block |
||
| 658 | 658 | $cacheInformation = [ |
| 659 | 659 | 'encrypted' => $isEncrypted, |
| 660 | 660 | ]; |
| 661 | - if($isEncrypted) { |
|
| 661 | + if ($isEncrypted) { |
|
| 662 | 662 | $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
| 663 | 663 | |
| 664 | 664 | // In case of a move operation from an unencrypted to an encrypted |
@@ -666,7 +666,7 @@ discard block |
||
| 666 | 666 | // correct value would be "1". Thus we manually set the value to "1" |
| 667 | 667 | // for those cases. |
| 668 | 668 | // See also https://github.com/owncloud/core/issues/23078 |
| 669 | - if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 669 | + if ($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 670 | 670 | $encryptedVersion = 1; |
| 671 | 671 | } |
| 672 | 672 | |
@@ -702,9 +702,9 @@ discard block |
||
| 702 | 702 | // remember that we try to create a version so that we can detect it during |
| 703 | 703 | // fopen($sourceInternalPath) and by-pass the encryption in order to |
| 704 | 704 | // create a 1:1 copy of the file |
| 705 | - $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
| 705 | + $this->arrayCache->set('encryption_copy_version_'.$sourceInternalPath, true); |
|
| 706 | 706 | $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
| 707 | - $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
| 707 | + $this->arrayCache->remove('encryption_copy_version_'.$sourceInternalPath); |
|
| 708 | 708 | if ($result) { |
| 709 | 709 | $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
| 710 | 710 | // make sure that we update the unencrypted size for the version |
@@ -724,7 +724,7 @@ discard block |
||
| 724 | 724 | $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
| 725 | 725 | if (count($mount) === 1) { |
| 726 | 726 | $mountPoint = $mount[0]->getMountPoint(); |
| 727 | - $source = $mountPoint . '/' . $sourceInternalPath; |
|
| 727 | + $source = $mountPoint.'/'.$sourceInternalPath; |
|
| 728 | 728 | $target = $this->getFullPath($targetInternalPath); |
| 729 | 729 | $this->copyKeys($source, $target); |
| 730 | 730 | } else { |
@@ -737,7 +737,7 @@ discard block |
||
| 737 | 737 | if (is_resource($dh)) { |
| 738 | 738 | while ($result and ($file = readdir($dh)) !== false) { |
| 739 | 739 | if (!Filesystem::isIgnoredDir($file)) { |
| 740 | - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
| 740 | + $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath.'/'.$file, $targetInternalPath.'/'.$file, false, $isRename); |
|
| 741 | 741 | } |
| 742 | 742 | } |
| 743 | 743 | } |
@@ -753,7 +753,7 @@ discard block |
||
| 753 | 753 | fclose($target); |
| 754 | 754 | throw $e; |
| 755 | 755 | } |
| 756 | - if($result) { |
|
| 756 | + if ($result) { |
|
| 757 | 757 | if ($preserveMtime) { |
| 758 | 758 | $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
| 759 | 759 | } |
@@ -765,7 +765,7 @@ discard block |
||
| 765 | 765 | $this->getCache()->remove($targetInternalPath); |
| 766 | 766 | } |
| 767 | 767 | } |
| 768 | - return (bool)$result; |
|
| 768 | + return (bool) $result; |
|
| 769 | 769 | |
| 770 | 770 | } |
| 771 | 771 | |
@@ -836,7 +836,7 @@ discard block |
||
| 836 | 836 | * @return string full path including mount point |
| 837 | 837 | */ |
| 838 | 838 | protected function getFullPath($path) { |
| 839 | - return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 839 | + return Filesystem::normalizePath($this->mountPoint.'/'.$path); |
|
| 840 | 840 | } |
| 841 | 841 | |
| 842 | 842 | /** |
@@ -892,7 +892,7 @@ discard block |
||
| 892 | 892 | $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
| 893 | 893 | |
| 894 | 894 | // +1 to not start with an ':' which would result in empty element at the beginning |
| 895 | - $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
| 895 | + $exploded = explode(':', substr($header, strlen(Util::HEADER_START) + 1)); |
|
| 896 | 896 | |
| 897 | 897 | $element = array_shift($exploded); |
| 898 | 898 | while ($element !== Util::HEADER_END) { |
@@ -955,7 +955,7 @@ discard block |
||
| 955 | 955 | try { |
| 956 | 956 | $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
| 957 | 957 | } catch (ModuleDoesNotExistsException $e) { |
| 958 | - $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
| 958 | + $this->logger->critical('Encryption module defined in "'.$path.'" not loaded!'); |
|
| 959 | 959 | throw $e; |
| 960 | 960 | } |
| 961 | 961 | } |
@@ -48,984 +48,984 @@ |
||
| 48 | 48 | |
| 49 | 49 | class Encryption extends Wrapper { |
| 50 | 50 | |
| 51 | - use LocalTempFileTrait; |
|
| 52 | - |
|
| 53 | - /** @var string */ |
|
| 54 | - private $mountPoint; |
|
| 55 | - |
|
| 56 | - /** @var \OC\Encryption\Util */ |
|
| 57 | - private $util; |
|
| 58 | - |
|
| 59 | - /** @var \OCP\Encryption\IManager */ |
|
| 60 | - private $encryptionManager; |
|
| 61 | - |
|
| 62 | - /** @var \OCP\ILogger */ |
|
| 63 | - private $logger; |
|
| 64 | - |
|
| 65 | - /** @var string */ |
|
| 66 | - private $uid; |
|
| 67 | - |
|
| 68 | - /** @var array */ |
|
| 69 | - protected $unencryptedSize; |
|
| 70 | - |
|
| 71 | - /** @var \OCP\Encryption\IFile */ |
|
| 72 | - private $fileHelper; |
|
| 73 | - |
|
| 74 | - /** @var IMountPoint */ |
|
| 75 | - private $mount; |
|
| 76 | - |
|
| 77 | - /** @var IStorage */ |
|
| 78 | - private $keyStorage; |
|
| 79 | - |
|
| 80 | - /** @var Update */ |
|
| 81 | - private $update; |
|
| 82 | - |
|
| 83 | - /** @var Manager */ |
|
| 84 | - private $mountManager; |
|
| 85 | - |
|
| 86 | - /** @var array remember for which path we execute the repair step to avoid recursions */ |
|
| 87 | - private $fixUnencryptedSizeOf = array(); |
|
| 88 | - |
|
| 89 | - /** @var ArrayCache */ |
|
| 90 | - private $arrayCache; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param array $parameters |
|
| 94 | - * @param IManager $encryptionManager |
|
| 95 | - * @param Util $util |
|
| 96 | - * @param ILogger $logger |
|
| 97 | - * @param IFile $fileHelper |
|
| 98 | - * @param string $uid |
|
| 99 | - * @param IStorage $keyStorage |
|
| 100 | - * @param Update $update |
|
| 101 | - * @param Manager $mountManager |
|
| 102 | - * @param ArrayCache $arrayCache |
|
| 103 | - */ |
|
| 104 | - public function __construct( |
|
| 105 | - $parameters, |
|
| 106 | - IManager $encryptionManager = null, |
|
| 107 | - Util $util = null, |
|
| 108 | - ILogger $logger = null, |
|
| 109 | - IFile $fileHelper = null, |
|
| 110 | - $uid = null, |
|
| 111 | - IStorage $keyStorage = null, |
|
| 112 | - Update $update = null, |
|
| 113 | - Manager $mountManager = null, |
|
| 114 | - ArrayCache $arrayCache = null |
|
| 115 | - ) { |
|
| 116 | - |
|
| 117 | - $this->mountPoint = $parameters['mountPoint']; |
|
| 118 | - $this->mount = $parameters['mount']; |
|
| 119 | - $this->encryptionManager = $encryptionManager; |
|
| 120 | - $this->util = $util; |
|
| 121 | - $this->logger = $logger; |
|
| 122 | - $this->uid = $uid; |
|
| 123 | - $this->fileHelper = $fileHelper; |
|
| 124 | - $this->keyStorage = $keyStorage; |
|
| 125 | - $this->unencryptedSize = array(); |
|
| 126 | - $this->update = $update; |
|
| 127 | - $this->mountManager = $mountManager; |
|
| 128 | - $this->arrayCache = $arrayCache; |
|
| 129 | - parent::__construct($parameters); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * see http://php.net/manual/en/function.filesize.php |
|
| 134 | - * The result for filesize when called on a folder is required to be 0 |
|
| 135 | - * |
|
| 136 | - * @param string $path |
|
| 137 | - * @return int |
|
| 138 | - */ |
|
| 139 | - public function filesize($path) { |
|
| 140 | - $fullPath = $this->getFullPath($path); |
|
| 141 | - |
|
| 142 | - /** @var CacheEntry $info */ |
|
| 143 | - $info = $this->getCache()->get($path); |
|
| 144 | - if (isset($this->unencryptedSize[$fullPath])) { |
|
| 145 | - $size = $this->unencryptedSize[$fullPath]; |
|
| 146 | - // update file cache |
|
| 147 | - if ($info instanceof ICacheEntry) { |
|
| 148 | - $info = $info->getData(); |
|
| 149 | - $info['encrypted'] = $info['encryptedVersion']; |
|
| 150 | - } else { |
|
| 151 | - if (!is_array($info)) { |
|
| 152 | - $info = []; |
|
| 153 | - } |
|
| 154 | - $info['encrypted'] = true; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $info['size'] = $size; |
|
| 158 | - $this->getCache()->put($path, $info); |
|
| 159 | - |
|
| 160 | - return $size; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - if (isset($info['fileid']) && $info['encrypted']) { |
|
| 164 | - return $this->verifyUnencryptedSize($path, $info['size']); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $this->storage->filesize($path); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param string $path |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - public function getMetaData($path) { |
|
| 175 | - $data = $this->storage->getMetaData($path); |
|
| 176 | - if (is_null($data)) { |
|
| 177 | - return null; |
|
| 178 | - } |
|
| 179 | - $fullPath = $this->getFullPath($path); |
|
| 180 | - $info = $this->getCache()->get($path); |
|
| 181 | - |
|
| 182 | - if (isset($this->unencryptedSize[$fullPath])) { |
|
| 183 | - $data['encrypted'] = true; |
|
| 184 | - $data['size'] = $this->unencryptedSize[$fullPath]; |
|
| 185 | - } else { |
|
| 186 | - if (isset($info['fileid']) && $info['encrypted']) { |
|
| 187 | - $data['size'] = $this->verifyUnencryptedSize($path, $info['size']); |
|
| 188 | - $data['encrypted'] = true; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) { |
|
| 193 | - $data['encryptedVersion'] = $info['encryptedVersion']; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $data; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * see http://php.net/manual/en/function.file_get_contents.php |
|
| 201 | - * |
|
| 202 | - * @param string $path |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - public function file_get_contents($path) { |
|
| 206 | - |
|
| 207 | - $encryptionModule = $this->getEncryptionModule($path); |
|
| 208 | - |
|
| 209 | - if ($encryptionModule) { |
|
| 210 | - $handle = $this->fopen($path, "r"); |
|
| 211 | - if (!$handle) { |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - $data = stream_get_contents($handle); |
|
| 215 | - fclose($handle); |
|
| 216 | - return $data; |
|
| 217 | - } |
|
| 218 | - return $this->storage->file_get_contents($path); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
| 223 | - * |
|
| 224 | - * @param string $path |
|
| 225 | - * @param string $data |
|
| 226 | - * @return bool |
|
| 227 | - */ |
|
| 228 | - public function file_put_contents($path, $data) { |
|
| 229 | - // file put content will always be translated to a stream write |
|
| 230 | - $handle = $this->fopen($path, 'w'); |
|
| 231 | - if (is_resource($handle)) { |
|
| 232 | - $written = fwrite($handle, $data); |
|
| 233 | - fclose($handle); |
|
| 234 | - return $written; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * see http://php.net/manual/en/function.unlink.php |
|
| 242 | - * |
|
| 243 | - * @param string $path |
|
| 244 | - * @return bool |
|
| 245 | - */ |
|
| 246 | - public function unlink($path) { |
|
| 247 | - $fullPath = $this->getFullPath($path); |
|
| 248 | - if ($this->util->isExcluded($fullPath)) { |
|
| 249 | - return $this->storage->unlink($path); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $encryptionModule = $this->getEncryptionModule($path); |
|
| 253 | - if ($encryptionModule) { |
|
| 254 | - $this->keyStorage->deleteAllFileKeys($this->getFullPath($path)); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return $this->storage->unlink($path); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * see http://php.net/manual/en/function.rename.php |
|
| 262 | - * |
|
| 263 | - * @param string $path1 |
|
| 264 | - * @param string $path2 |
|
| 265 | - * @return bool |
|
| 266 | - */ |
|
| 267 | - public function rename($path1, $path2) { |
|
| 268 | - |
|
| 269 | - $result = $this->storage->rename($path1, $path2); |
|
| 270 | - |
|
| 271 | - if ($result && |
|
| 272 | - // versions always use the keys from the original file, so we can skip |
|
| 273 | - // this step for versions |
|
| 274 | - $this->isVersion($path2) === false && |
|
| 275 | - $this->encryptionManager->isEnabled()) { |
|
| 276 | - $source = $this->getFullPath($path1); |
|
| 277 | - if (!$this->util->isExcluded($source)) { |
|
| 278 | - $target = $this->getFullPath($path2); |
|
| 279 | - if (isset($this->unencryptedSize[$source])) { |
|
| 280 | - $this->unencryptedSize[$target] = $this->unencryptedSize[$source]; |
|
| 281 | - } |
|
| 282 | - $this->keyStorage->renameKeys($source, $target); |
|
| 283 | - $module = $this->getEncryptionModule($path2); |
|
| 284 | - if ($module) { |
|
| 285 | - $module->update($target, $this->uid, []); |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - return $result; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * see http://php.net/manual/en/function.rmdir.php |
|
| 295 | - * |
|
| 296 | - * @param string $path |
|
| 297 | - * @return bool |
|
| 298 | - */ |
|
| 299 | - public function rmdir($path) { |
|
| 300 | - $result = $this->storage->rmdir($path); |
|
| 301 | - $fullPath = $this->getFullPath($path); |
|
| 302 | - if ($result && |
|
| 303 | - $this->util->isExcluded($fullPath) === false && |
|
| 304 | - $this->encryptionManager->isEnabled() |
|
| 305 | - ) { |
|
| 306 | - $this->keyStorage->deleteAllFileKeys($fullPath); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - return $result; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * check if a file can be read |
|
| 314 | - * |
|
| 315 | - * @param string $path |
|
| 316 | - * @return bool |
|
| 317 | - */ |
|
| 318 | - public function isReadable($path) { |
|
| 319 | - |
|
| 320 | - $isReadable = true; |
|
| 321 | - |
|
| 322 | - $metaData = $this->getMetaData($path); |
|
| 323 | - if ( |
|
| 324 | - !$this->is_dir($path) && |
|
| 325 | - isset($metaData['encrypted']) && |
|
| 326 | - $metaData['encrypted'] === true |
|
| 327 | - ) { |
|
| 328 | - $fullPath = $this->getFullPath($path); |
|
| 329 | - $module = $this->getEncryptionModule($path); |
|
| 330 | - $isReadable = $module->isReadable($fullPath, $this->uid); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - return $this->storage->isReadable($path) && $isReadable; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * see http://php.net/manual/en/function.copy.php |
|
| 338 | - * |
|
| 339 | - * @param string $path1 |
|
| 340 | - * @param string $path2 |
|
| 341 | - * @return bool |
|
| 342 | - */ |
|
| 343 | - public function copy($path1, $path2) { |
|
| 344 | - |
|
| 345 | - $source = $this->getFullPath($path1); |
|
| 346 | - |
|
| 347 | - if ($this->util->isExcluded($source)) { |
|
| 348 | - return $this->storage->copy($path1, $path2); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - // need to stream copy file by file in case we copy between a encrypted |
|
| 352 | - // and a unencrypted storage |
|
| 353 | - $this->unlink($path2); |
|
| 354 | - $result = $this->copyFromStorage($this, $path1, $path2); |
|
| 355 | - |
|
| 356 | - return $result; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * see http://php.net/manual/en/function.fopen.php |
|
| 361 | - * |
|
| 362 | - * @param string $path |
|
| 363 | - * @param string $mode |
|
| 364 | - * @return resource|bool |
|
| 365 | - * @throws GenericEncryptionException |
|
| 366 | - * @throws ModuleDoesNotExistsException |
|
| 367 | - */ |
|
| 368 | - public function fopen($path, $mode) { |
|
| 369 | - |
|
| 370 | - // check if the file is stored in the array cache, this means that we |
|
| 371 | - // copy a file over to the versions folder, in this case we don't want to |
|
| 372 | - // decrypt it |
|
| 373 | - if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
| 374 | - $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
| 375 | - return $this->storage->fopen($path, $mode); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - $encryptionEnabled = $this->encryptionManager->isEnabled(); |
|
| 379 | - $shouldEncrypt = false; |
|
| 380 | - $encryptionModule = null; |
|
| 381 | - $header = $this->getHeader($path); |
|
| 382 | - $signed = (isset($header['signed']) && $header['signed'] === 'true') ? true : false; |
|
| 383 | - $fullPath = $this->getFullPath($path); |
|
| 384 | - $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 385 | - |
|
| 386 | - if ($this->util->isExcluded($fullPath) === false) { |
|
| 387 | - |
|
| 388 | - $size = $unencryptedSize = 0; |
|
| 389 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
| 390 | - $targetExists = $this->file_exists($realFile) || $this->file_exists($path); |
|
| 391 | - $targetIsEncrypted = false; |
|
| 392 | - if ($targetExists) { |
|
| 393 | - // in case the file exists we require the explicit module as |
|
| 394 | - // specified in the file header - otherwise we need to fail hard to |
|
| 395 | - // prevent data loss on client side |
|
| 396 | - if (!empty($encryptionModuleId)) { |
|
| 397 | - $targetIsEncrypted = true; |
|
| 398 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - if ($this->file_exists($path)) { |
|
| 402 | - $size = $this->storage->filesize($path); |
|
| 403 | - $unencryptedSize = $this->filesize($path); |
|
| 404 | - } else { |
|
| 405 | - $size = $unencryptedSize = 0; |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - try { |
|
| 410 | - |
|
| 411 | - if ( |
|
| 412 | - $mode === 'w' |
|
| 413 | - || $mode === 'w+' |
|
| 414 | - || $mode === 'wb' |
|
| 415 | - || $mode === 'wb+' |
|
| 416 | - ) { |
|
| 417 | - // if we update a encrypted file with a un-encrypted one we change the db flag |
|
| 418 | - if ($targetIsEncrypted && $encryptionEnabled === false) { |
|
| 419 | - $cache = $this->storage->getCache(); |
|
| 420 | - if ($cache) { |
|
| 421 | - $entry = $cache->get($path); |
|
| 422 | - $cache->update($entry->getId(), ['encrypted' => 0]); |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - if ($encryptionEnabled) { |
|
| 426 | - // if $encryptionModuleId is empty, the default module will be used |
|
| 427 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 428 | - $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); |
|
| 429 | - $signed = true; |
|
| 430 | - } |
|
| 431 | - } else { |
|
| 432 | - $info = $this->getCache()->get($path); |
|
| 433 | - // only get encryption module if we found one in the header |
|
| 434 | - // or if file should be encrypted according to the file cache |
|
| 435 | - if (!empty($encryptionModuleId)) { |
|
| 436 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 437 | - $shouldEncrypt = true; |
|
| 438 | - } else if (empty($encryptionModuleId) && $info['encrypted'] === true) { |
|
| 439 | - // we come from a old installation. No header and/or no module defined |
|
| 440 | - // but the file is encrypted. In this case we need to use the |
|
| 441 | - // OC_DEFAULT_MODULE to read the file |
|
| 442 | - $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE'); |
|
| 443 | - $shouldEncrypt = true; |
|
| 444 | - $targetIsEncrypted = true; |
|
| 445 | - } |
|
| 446 | - } |
|
| 447 | - } catch (ModuleDoesNotExistsException $e) { |
|
| 448 | - $this->logger->warning('Encryption module "' . $encryptionModuleId . |
|
| 449 | - '" not found, file will be stored unencrypted (' . $e->getMessage() . ')'); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
|
| 453 | - if (!$encryptionEnabled || !$this->shouldEncrypt($path)) { |
|
| 454 | - if (!$targetExists || !$targetIsEncrypted) { |
|
| 455 | - $shouldEncrypt = false; |
|
| 456 | - } |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - if ($shouldEncrypt === true && $encryptionModule !== null) { |
|
| 460 | - $headerSize = $this->getHeaderSize($path); |
|
| 461 | - $source = $this->storage->fopen($path, $mode); |
|
| 462 | - if (!is_resource($source)) { |
|
| 463 | - return false; |
|
| 464 | - } |
|
| 465 | - $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, |
|
| 466 | - $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, |
|
| 467 | - $size, $unencryptedSize, $headerSize, $signed); |
|
| 468 | - return $handle; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - return $this->storage->fopen($path, $mode); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * perform some plausibility checks if the the unencrypted size is correct. |
|
| 479 | - * If not, we calculate the correct unencrypted size and return it |
|
| 480 | - * |
|
| 481 | - * @param string $path internal path relative to the storage root |
|
| 482 | - * @param int $unencryptedSize size of the unencrypted file |
|
| 483 | - * |
|
| 484 | - * @return int unencrypted size |
|
| 485 | - */ |
|
| 486 | - protected function verifyUnencryptedSize($path, $unencryptedSize) { |
|
| 487 | - |
|
| 488 | - $size = $this->storage->filesize($path); |
|
| 489 | - $result = $unencryptedSize; |
|
| 490 | - |
|
| 491 | - if ($unencryptedSize < 0 || |
|
| 492 | - ($size > 0 && $unencryptedSize === $size) |
|
| 493 | - ) { |
|
| 494 | - // check if we already calculate the unencrypted size for the |
|
| 495 | - // given path to avoid recursions |
|
| 496 | - if (isset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]) === false) { |
|
| 497 | - $this->fixUnencryptedSizeOf[$this->getFullPath($path)] = true; |
|
| 498 | - try { |
|
| 499 | - $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
|
| 500 | - } catch (\Exception $e) { |
|
| 501 | - $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
| 502 | - $this->logger->logException($e); |
|
| 503 | - } |
|
| 504 | - unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - return $result; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * calculate the unencrypted size |
|
| 513 | - * |
|
| 514 | - * @param string $path internal path relative to the storage root |
|
| 515 | - * @param int $size size of the physical file |
|
| 516 | - * @param int $unencryptedSize size of the unencrypted file |
|
| 517 | - * |
|
| 518 | - * @return int calculated unencrypted size |
|
| 519 | - */ |
|
| 520 | - protected function fixUnencryptedSize($path, $size, $unencryptedSize) { |
|
| 521 | - |
|
| 522 | - $headerSize = $this->getHeaderSize($path); |
|
| 523 | - $header = $this->getHeader($path); |
|
| 524 | - $encryptionModule = $this->getEncryptionModule($path); |
|
| 525 | - |
|
| 526 | - $stream = $this->storage->fopen($path, 'r'); |
|
| 527 | - |
|
| 528 | - // if we couldn't open the file we return the old unencrypted size |
|
| 529 | - if (!is_resource($stream)) { |
|
| 530 | - $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
| 531 | - return $unencryptedSize; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - $newUnencryptedSize = 0; |
|
| 535 | - $size -= $headerSize; |
|
| 536 | - $blockSize = $this->util->getBlockSize(); |
|
| 537 | - |
|
| 538 | - // if a header exists we skip it |
|
| 539 | - if ($headerSize > 0) { |
|
| 540 | - fread($stream, $headerSize); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - // fast path, else the calculation for $lastChunkNr is bogus |
|
| 544 | - if ($size === 0) { |
|
| 545 | - return 0; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - $signed = (isset($header['signed']) && $header['signed'] === 'true') ? true : false; |
|
| 549 | - $unencryptedBlockSize = $encryptionModule->getUnencryptedBlockSize($signed); |
|
| 550 | - |
|
| 551 | - // calculate last chunk nr |
|
| 552 | - // next highest is end of chunks, one subtracted is last one |
|
| 553 | - // we have to read the last chunk, we can't just calculate it (because of padding etc) |
|
| 554 | - |
|
| 555 | - $lastChunkNr = ceil($size/ $blockSize)-1; |
|
| 556 | - // calculate last chunk position |
|
| 557 | - $lastChunkPos = ($lastChunkNr * $blockSize); |
|
| 558 | - // try to fseek to the last chunk, if it fails we have to read the whole file |
|
| 559 | - if (@fseek($stream, $lastChunkPos, SEEK_CUR) === 0) { |
|
| 560 | - $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - $lastChunkContentEncrypted=''; |
|
| 564 | - $count = $blockSize; |
|
| 565 | - |
|
| 566 | - while ($count > 0) { |
|
| 567 | - $data=fread($stream, $blockSize); |
|
| 568 | - $count=strlen($data); |
|
| 569 | - $lastChunkContentEncrypted .= $data; |
|
| 570 | - if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 571 | - $newUnencryptedSize += $unencryptedBlockSize; |
|
| 572 | - $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - fclose($stream); |
|
| 577 | - |
|
| 578 | - // we have to decrypt the last chunk to get it actual size |
|
| 579 | - $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
|
| 580 | - $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
| 581 | - $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
| 582 | - |
|
| 583 | - // calc the real file size with the size of the last chunk |
|
| 584 | - $newUnencryptedSize += strlen($decryptedLastChunk); |
|
| 585 | - |
|
| 586 | - $this->updateUnencryptedSize($this->getFullPath($path), $newUnencryptedSize); |
|
| 587 | - |
|
| 588 | - // write to cache if applicable |
|
| 589 | - $cache = $this->storage->getCache(); |
|
| 590 | - if ($cache) { |
|
| 591 | - $entry = $cache->get($path); |
|
| 592 | - $cache->update($entry['fileid'], ['size' => $newUnencryptedSize]); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - return $newUnencryptedSize; |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * @param Storage\IStorage $sourceStorage |
|
| 600 | - * @param string $sourceInternalPath |
|
| 601 | - * @param string $targetInternalPath |
|
| 602 | - * @param bool $preserveMtime |
|
| 603 | - * @return bool |
|
| 604 | - */ |
|
| 605 | - public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) { |
|
| 606 | - if ($sourceStorage === $this) { |
|
| 607 | - return $this->rename($sourceInternalPath, $targetInternalPath); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 611 | - // - call $this->storage->moveFromStorage() instead of $this->copyBetweenStorage |
|
| 612 | - // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 613 | - // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 614 | - // - remove $this->copyBetweenStorage |
|
| 615 | - |
|
| 616 | - if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
| 617 | - return false; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true); |
|
| 621 | - if ($result) { |
|
| 622 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 623 | - $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
| 624 | - } else { |
|
| 625 | - $result &= $sourceStorage->unlink($sourceInternalPath); |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - return $result; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * @param Storage\IStorage $sourceStorage |
|
| 634 | - * @param string $sourceInternalPath |
|
| 635 | - * @param string $targetInternalPath |
|
| 636 | - * @param bool $preserveMtime |
|
| 637 | - * @param bool $isRename |
|
| 638 | - * @return bool |
|
| 639 | - */ |
|
| 640 | - public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) { |
|
| 641 | - |
|
| 642 | - // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 643 | - // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage |
|
| 644 | - // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 645 | - // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 646 | - // - remove $this->copyBetweenStorage |
|
| 647 | - |
|
| 648 | - return $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Update the encrypted cache version in the database |
|
| 653 | - * |
|
| 654 | - * @param Storage\IStorage $sourceStorage |
|
| 655 | - * @param string $sourceInternalPath |
|
| 656 | - * @param string $targetInternalPath |
|
| 657 | - * @param bool $isRename |
|
| 658 | - * @param bool $keepEncryptionVersion |
|
| 659 | - */ |
|
| 660 | - private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, $keepEncryptionVersion) { |
|
| 661 | - $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath); |
|
| 662 | - $cacheInformation = [ |
|
| 663 | - 'encrypted' => $isEncrypted, |
|
| 664 | - ]; |
|
| 665 | - if($isEncrypted) { |
|
| 666 | - $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
|
| 667 | - |
|
| 668 | - // In case of a move operation from an unencrypted to an encrypted |
|
| 669 | - // storage the old encrypted version would stay with "0" while the |
|
| 670 | - // correct value would be "1". Thus we manually set the value to "1" |
|
| 671 | - // for those cases. |
|
| 672 | - // See also https://github.com/owncloud/core/issues/23078 |
|
| 673 | - if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 674 | - $encryptedVersion = 1; |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - $cacheInformation['encryptedVersion'] = $encryptedVersion; |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - // in case of a rename we need to manipulate the source cache because |
|
| 681 | - // this information will be kept for the new target |
|
| 682 | - if ($isRename) { |
|
| 683 | - $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation); |
|
| 684 | - } else { |
|
| 685 | - $this->getCache()->put($targetInternalPath, $cacheInformation); |
|
| 686 | - } |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * copy file between two storages |
|
| 691 | - * |
|
| 692 | - * @param Storage\IStorage $sourceStorage |
|
| 693 | - * @param string $sourceInternalPath |
|
| 694 | - * @param string $targetInternalPath |
|
| 695 | - * @param bool $preserveMtime |
|
| 696 | - * @param bool $isRename |
|
| 697 | - * @return bool |
|
| 698 | - * @throws \Exception |
|
| 699 | - */ |
|
| 700 | - private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) { |
|
| 701 | - |
|
| 702 | - // for versions we have nothing to do, because versions should always use the |
|
| 703 | - // key from the original file. Just create a 1:1 copy and done |
|
| 704 | - if ($this->isVersion($targetInternalPath) || |
|
| 705 | - $this->isVersion($sourceInternalPath)) { |
|
| 706 | - // remember that we try to create a version so that we can detect it during |
|
| 707 | - // fopen($sourceInternalPath) and by-pass the encryption in order to |
|
| 708 | - // create a 1:1 copy of the file |
|
| 709 | - $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
| 710 | - $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 711 | - $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
| 712 | - if ($result) { |
|
| 713 | - $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
|
| 714 | - // make sure that we update the unencrypted size for the version |
|
| 715 | - if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 716 | - $this->updateUnencryptedSize( |
|
| 717 | - $this->getFullPath($targetInternalPath), |
|
| 718 | - $info['size'] |
|
| 719 | - ); |
|
| 720 | - } |
|
| 721 | - $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true); |
|
| 722 | - } |
|
| 723 | - return $result; |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - // first copy the keys that we reuse the existing file key on the target location |
|
| 727 | - // and don't create a new one which would break versions for example. |
|
| 728 | - $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
|
| 729 | - if (count($mount) === 1) { |
|
| 730 | - $mountPoint = $mount[0]->getMountPoint(); |
|
| 731 | - $source = $mountPoint . '/' . $sourceInternalPath; |
|
| 732 | - $target = $this->getFullPath($targetInternalPath); |
|
| 733 | - $this->copyKeys($source, $target); |
|
| 734 | - } else { |
|
| 735 | - $this->logger->error('Could not find mount point, can\'t keep encryption keys'); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 739 | - $dh = $sourceStorage->opendir($sourceInternalPath); |
|
| 740 | - $result = $this->mkdir($targetInternalPath); |
|
| 741 | - if (is_resource($dh)) { |
|
| 742 | - while ($result and ($file = readdir($dh)) !== false) { |
|
| 743 | - if (!Filesystem::isIgnoredDir($file)) { |
|
| 744 | - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
| 745 | - } |
|
| 746 | - } |
|
| 747 | - } |
|
| 748 | - } else { |
|
| 749 | - try { |
|
| 750 | - $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
| 751 | - $target = $this->fopen($targetInternalPath, 'w'); |
|
| 752 | - list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
| 753 | - fclose($source); |
|
| 754 | - fclose($target); |
|
| 755 | - } catch (\Exception $e) { |
|
| 756 | - fclose($source); |
|
| 757 | - fclose($target); |
|
| 758 | - throw $e; |
|
| 759 | - } |
|
| 760 | - if($result) { |
|
| 761 | - if ($preserveMtime) { |
|
| 762 | - $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
| 763 | - } |
|
| 764 | - $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, false); |
|
| 765 | - } else { |
|
| 766 | - // delete partially written target file |
|
| 767 | - $this->unlink($targetInternalPath); |
|
| 768 | - // delete cache entry that was created by fopen |
|
| 769 | - $this->getCache()->remove($targetInternalPath); |
|
| 770 | - } |
|
| 771 | - } |
|
| 772 | - return (bool)$result; |
|
| 773 | - |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - /** |
|
| 777 | - * get the path to a local version of the file. |
|
| 778 | - * The local version of the file can be temporary and doesn't have to be persistent across requests |
|
| 779 | - * |
|
| 780 | - * @param string $path |
|
| 781 | - * @return string |
|
| 782 | - */ |
|
| 783 | - public function getLocalFile($path) { |
|
| 784 | - if ($this->encryptionManager->isEnabled()) { |
|
| 785 | - $cachedFile = $this->getCachedFile($path); |
|
| 786 | - if (is_string($cachedFile)) { |
|
| 787 | - return $cachedFile; |
|
| 788 | - } |
|
| 789 | - } |
|
| 790 | - return $this->storage->getLocalFile($path); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - /** |
|
| 794 | - * Returns the wrapped storage's value for isLocal() |
|
| 795 | - * |
|
| 796 | - * @return bool wrapped storage's isLocal() value |
|
| 797 | - */ |
|
| 798 | - public function isLocal() { |
|
| 799 | - if ($this->encryptionManager->isEnabled()) { |
|
| 800 | - return false; |
|
| 801 | - } |
|
| 802 | - return $this->storage->isLocal(); |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * see http://php.net/manual/en/function.stat.php |
|
| 807 | - * only the following keys are required in the result: size and mtime |
|
| 808 | - * |
|
| 809 | - * @param string $path |
|
| 810 | - * @return array |
|
| 811 | - */ |
|
| 812 | - public function stat($path) { |
|
| 813 | - $stat = $this->storage->stat($path); |
|
| 814 | - $fileSize = $this->filesize($path); |
|
| 815 | - $stat['size'] = $fileSize; |
|
| 816 | - $stat[7] = $fileSize; |
|
| 817 | - return $stat; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * see http://php.net/manual/en/function.hash.php |
|
| 822 | - * |
|
| 823 | - * @param string $type |
|
| 824 | - * @param string $path |
|
| 825 | - * @param bool $raw |
|
| 826 | - * @return string |
|
| 827 | - */ |
|
| 828 | - public function hash($type, $path, $raw = false) { |
|
| 829 | - $fh = $this->fopen($path, 'rb'); |
|
| 830 | - $ctx = hash_init($type); |
|
| 831 | - hash_update_stream($ctx, $fh); |
|
| 832 | - fclose($fh); |
|
| 833 | - return hash_final($ctx, $raw); |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - /** |
|
| 837 | - * return full path, including mount point |
|
| 838 | - * |
|
| 839 | - * @param string $path relative to mount point |
|
| 840 | - * @return string full path including mount point |
|
| 841 | - */ |
|
| 842 | - protected function getFullPath($path) { |
|
| 843 | - return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * read first block of encrypted file, typically this will contain the |
|
| 848 | - * encryption header |
|
| 849 | - * |
|
| 850 | - * @param string $path |
|
| 851 | - * @return string |
|
| 852 | - */ |
|
| 853 | - protected function readFirstBlock($path) { |
|
| 854 | - $firstBlock = ''; |
|
| 855 | - if ($this->storage->file_exists($path)) { |
|
| 856 | - $handle = $this->storage->fopen($path, 'r'); |
|
| 857 | - $firstBlock = fread($handle, $this->util->getHeaderSize()); |
|
| 858 | - fclose($handle); |
|
| 859 | - } |
|
| 860 | - return $firstBlock; |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * return header size of given file |
|
| 865 | - * |
|
| 866 | - * @param string $path |
|
| 867 | - * @return int |
|
| 868 | - */ |
|
| 869 | - protected function getHeaderSize($path) { |
|
| 870 | - $headerSize = 0; |
|
| 871 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
| 872 | - if ($this->storage->file_exists($realFile)) { |
|
| 873 | - $path = $realFile; |
|
| 874 | - } |
|
| 875 | - $firstBlock = $this->readFirstBlock($path); |
|
| 876 | - |
|
| 877 | - if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 878 | - $headerSize = $this->util->getHeaderSize(); |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - return $headerSize; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - /** |
|
| 885 | - * parse raw header to array |
|
| 886 | - * |
|
| 887 | - * @param string $rawHeader |
|
| 888 | - * @return array |
|
| 889 | - */ |
|
| 890 | - protected function parseRawHeader($rawHeader) { |
|
| 891 | - $result = array(); |
|
| 892 | - if (substr($rawHeader, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 893 | - $header = $rawHeader; |
|
| 894 | - $endAt = strpos($header, Util::HEADER_END); |
|
| 895 | - if ($endAt !== false) { |
|
| 896 | - $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
|
| 897 | - |
|
| 898 | - // +1 to not start with an ':' which would result in empty element at the beginning |
|
| 899 | - $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
| 900 | - |
|
| 901 | - $element = array_shift($exploded); |
|
| 902 | - while ($element !== Util::HEADER_END) { |
|
| 903 | - $result[$element] = array_shift($exploded); |
|
| 904 | - $element = array_shift($exploded); |
|
| 905 | - } |
|
| 906 | - } |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - return $result; |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - /** |
|
| 913 | - * read header from file |
|
| 914 | - * |
|
| 915 | - * @param string $path |
|
| 916 | - * @return array |
|
| 917 | - */ |
|
| 918 | - protected function getHeader($path) { |
|
| 919 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
| 920 | - $exists = $this->storage->file_exists($realFile); |
|
| 921 | - if ($exists) { |
|
| 922 | - $path = $realFile; |
|
| 923 | - } |
|
| 924 | - |
|
| 925 | - $firstBlock = $this->readFirstBlock($path); |
|
| 926 | - $result = $this->parseRawHeader($firstBlock); |
|
| 927 | - |
|
| 928 | - // if the header doesn't contain a encryption module we check if it is a |
|
| 929 | - // legacy file. If true, we add the default encryption module |
|
| 930 | - if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY])) { |
|
| 931 | - if (!empty($result)) { |
|
| 932 | - $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 933 | - } else if ($exists) { |
|
| 934 | - // if the header was empty we have to check first if it is a encrypted file at all |
|
| 935 | - // We would do query to filecache only if we know that entry in filecache exists |
|
| 936 | - $info = $this->getCache()->get($path); |
|
| 937 | - if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 938 | - $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 939 | - } |
|
| 940 | - } |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - return $result; |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - /** |
|
| 947 | - * read encryption module needed to read/write the file located at $path |
|
| 948 | - * |
|
| 949 | - * @param string $path |
|
| 950 | - * @return null|\OCP\Encryption\IEncryptionModule |
|
| 951 | - * @throws ModuleDoesNotExistsException |
|
| 952 | - * @throws \Exception |
|
| 953 | - */ |
|
| 954 | - protected function getEncryptionModule($path) { |
|
| 955 | - $encryptionModule = null; |
|
| 956 | - $header = $this->getHeader($path); |
|
| 957 | - $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 958 | - if (!empty($encryptionModuleId)) { |
|
| 959 | - try { |
|
| 960 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 961 | - } catch (ModuleDoesNotExistsException $e) { |
|
| 962 | - $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
| 963 | - throw $e; |
|
| 964 | - } |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - return $encryptionModule; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - /** |
|
| 971 | - * @param string $path |
|
| 972 | - * @param int $unencryptedSize |
|
| 973 | - */ |
|
| 974 | - public function updateUnencryptedSize($path, $unencryptedSize) { |
|
| 975 | - $this->unencryptedSize[$path] = $unencryptedSize; |
|
| 976 | - } |
|
| 977 | - |
|
| 978 | - /** |
|
| 979 | - * copy keys to new location |
|
| 980 | - * |
|
| 981 | - * @param string $source path relative to data/ |
|
| 982 | - * @param string $target path relative to data/ |
|
| 983 | - * @return bool |
|
| 984 | - */ |
|
| 985 | - protected function copyKeys($source, $target) { |
|
| 986 | - if (!$this->util->isExcluded($source)) { |
|
| 987 | - return $this->keyStorage->copyKeys($source, $target); |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - return false; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - /** |
|
| 994 | - * check if path points to a files version |
|
| 995 | - * |
|
| 996 | - * @param $path |
|
| 997 | - * @return bool |
|
| 998 | - */ |
|
| 999 | - protected function isVersion($path) { |
|
| 1000 | - $normalized = Filesystem::normalizePath($path); |
|
| 1001 | - return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/'; |
|
| 1002 | - } |
|
| 1003 | - |
|
| 1004 | - /** |
|
| 1005 | - * check if the given storage should be encrypted or not |
|
| 1006 | - * |
|
| 1007 | - * @param $path |
|
| 1008 | - * @return bool |
|
| 1009 | - */ |
|
| 1010 | - protected function shouldEncrypt($path) { |
|
| 1011 | - $fullPath = $this->getFullPath($path); |
|
| 1012 | - $mountPointConfig = $this->mount->getOption('encrypt', true); |
|
| 1013 | - if ($mountPointConfig === false) { |
|
| 1014 | - return false; |
|
| 1015 | - } |
|
| 1016 | - |
|
| 1017 | - try { |
|
| 1018 | - $encryptionModule = $this->getEncryptionModule($fullPath); |
|
| 1019 | - } catch (ModuleDoesNotExistsException $e) { |
|
| 1020 | - return false; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - if ($encryptionModule === null) { |
|
| 1024 | - $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - return $encryptionModule->shouldEncrypt($fullPath); |
|
| 1028 | - |
|
| 1029 | - } |
|
| 51 | + use LocalTempFileTrait; |
|
| 52 | + |
|
| 53 | + /** @var string */ |
|
| 54 | + private $mountPoint; |
|
| 55 | + |
|
| 56 | + /** @var \OC\Encryption\Util */ |
|
| 57 | + private $util; |
|
| 58 | + |
|
| 59 | + /** @var \OCP\Encryption\IManager */ |
|
| 60 | + private $encryptionManager; |
|
| 61 | + |
|
| 62 | + /** @var \OCP\ILogger */ |
|
| 63 | + private $logger; |
|
| 64 | + |
|
| 65 | + /** @var string */ |
|
| 66 | + private $uid; |
|
| 67 | + |
|
| 68 | + /** @var array */ |
|
| 69 | + protected $unencryptedSize; |
|
| 70 | + |
|
| 71 | + /** @var \OCP\Encryption\IFile */ |
|
| 72 | + private $fileHelper; |
|
| 73 | + |
|
| 74 | + /** @var IMountPoint */ |
|
| 75 | + private $mount; |
|
| 76 | + |
|
| 77 | + /** @var IStorage */ |
|
| 78 | + private $keyStorage; |
|
| 79 | + |
|
| 80 | + /** @var Update */ |
|
| 81 | + private $update; |
|
| 82 | + |
|
| 83 | + /** @var Manager */ |
|
| 84 | + private $mountManager; |
|
| 85 | + |
|
| 86 | + /** @var array remember for which path we execute the repair step to avoid recursions */ |
|
| 87 | + private $fixUnencryptedSizeOf = array(); |
|
| 88 | + |
|
| 89 | + /** @var ArrayCache */ |
|
| 90 | + private $arrayCache; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param array $parameters |
|
| 94 | + * @param IManager $encryptionManager |
|
| 95 | + * @param Util $util |
|
| 96 | + * @param ILogger $logger |
|
| 97 | + * @param IFile $fileHelper |
|
| 98 | + * @param string $uid |
|
| 99 | + * @param IStorage $keyStorage |
|
| 100 | + * @param Update $update |
|
| 101 | + * @param Manager $mountManager |
|
| 102 | + * @param ArrayCache $arrayCache |
|
| 103 | + */ |
|
| 104 | + public function __construct( |
|
| 105 | + $parameters, |
|
| 106 | + IManager $encryptionManager = null, |
|
| 107 | + Util $util = null, |
|
| 108 | + ILogger $logger = null, |
|
| 109 | + IFile $fileHelper = null, |
|
| 110 | + $uid = null, |
|
| 111 | + IStorage $keyStorage = null, |
|
| 112 | + Update $update = null, |
|
| 113 | + Manager $mountManager = null, |
|
| 114 | + ArrayCache $arrayCache = null |
|
| 115 | + ) { |
|
| 116 | + |
|
| 117 | + $this->mountPoint = $parameters['mountPoint']; |
|
| 118 | + $this->mount = $parameters['mount']; |
|
| 119 | + $this->encryptionManager = $encryptionManager; |
|
| 120 | + $this->util = $util; |
|
| 121 | + $this->logger = $logger; |
|
| 122 | + $this->uid = $uid; |
|
| 123 | + $this->fileHelper = $fileHelper; |
|
| 124 | + $this->keyStorage = $keyStorage; |
|
| 125 | + $this->unencryptedSize = array(); |
|
| 126 | + $this->update = $update; |
|
| 127 | + $this->mountManager = $mountManager; |
|
| 128 | + $this->arrayCache = $arrayCache; |
|
| 129 | + parent::__construct($parameters); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * see http://php.net/manual/en/function.filesize.php |
|
| 134 | + * The result for filesize when called on a folder is required to be 0 |
|
| 135 | + * |
|
| 136 | + * @param string $path |
|
| 137 | + * @return int |
|
| 138 | + */ |
|
| 139 | + public function filesize($path) { |
|
| 140 | + $fullPath = $this->getFullPath($path); |
|
| 141 | + |
|
| 142 | + /** @var CacheEntry $info */ |
|
| 143 | + $info = $this->getCache()->get($path); |
|
| 144 | + if (isset($this->unencryptedSize[$fullPath])) { |
|
| 145 | + $size = $this->unencryptedSize[$fullPath]; |
|
| 146 | + // update file cache |
|
| 147 | + if ($info instanceof ICacheEntry) { |
|
| 148 | + $info = $info->getData(); |
|
| 149 | + $info['encrypted'] = $info['encryptedVersion']; |
|
| 150 | + } else { |
|
| 151 | + if (!is_array($info)) { |
|
| 152 | + $info = []; |
|
| 153 | + } |
|
| 154 | + $info['encrypted'] = true; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $info['size'] = $size; |
|
| 158 | + $this->getCache()->put($path, $info); |
|
| 159 | + |
|
| 160 | + return $size; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + if (isset($info['fileid']) && $info['encrypted']) { |
|
| 164 | + return $this->verifyUnencryptedSize($path, $info['size']); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $this->storage->filesize($path); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param string $path |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + public function getMetaData($path) { |
|
| 175 | + $data = $this->storage->getMetaData($path); |
|
| 176 | + if (is_null($data)) { |
|
| 177 | + return null; |
|
| 178 | + } |
|
| 179 | + $fullPath = $this->getFullPath($path); |
|
| 180 | + $info = $this->getCache()->get($path); |
|
| 181 | + |
|
| 182 | + if (isset($this->unencryptedSize[$fullPath])) { |
|
| 183 | + $data['encrypted'] = true; |
|
| 184 | + $data['size'] = $this->unencryptedSize[$fullPath]; |
|
| 185 | + } else { |
|
| 186 | + if (isset($info['fileid']) && $info['encrypted']) { |
|
| 187 | + $data['size'] = $this->verifyUnencryptedSize($path, $info['size']); |
|
| 188 | + $data['encrypted'] = true; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) { |
|
| 193 | + $data['encryptedVersion'] = $info['encryptedVersion']; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $data; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * see http://php.net/manual/en/function.file_get_contents.php |
|
| 201 | + * |
|
| 202 | + * @param string $path |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + public function file_get_contents($path) { |
|
| 206 | + |
|
| 207 | + $encryptionModule = $this->getEncryptionModule($path); |
|
| 208 | + |
|
| 209 | + if ($encryptionModule) { |
|
| 210 | + $handle = $this->fopen($path, "r"); |
|
| 211 | + if (!$handle) { |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + $data = stream_get_contents($handle); |
|
| 215 | + fclose($handle); |
|
| 216 | + return $data; |
|
| 217 | + } |
|
| 218 | + return $this->storage->file_get_contents($path); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
| 223 | + * |
|
| 224 | + * @param string $path |
|
| 225 | + * @param string $data |
|
| 226 | + * @return bool |
|
| 227 | + */ |
|
| 228 | + public function file_put_contents($path, $data) { |
|
| 229 | + // file put content will always be translated to a stream write |
|
| 230 | + $handle = $this->fopen($path, 'w'); |
|
| 231 | + if (is_resource($handle)) { |
|
| 232 | + $written = fwrite($handle, $data); |
|
| 233 | + fclose($handle); |
|
| 234 | + return $written; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * see http://php.net/manual/en/function.unlink.php |
|
| 242 | + * |
|
| 243 | + * @param string $path |
|
| 244 | + * @return bool |
|
| 245 | + */ |
|
| 246 | + public function unlink($path) { |
|
| 247 | + $fullPath = $this->getFullPath($path); |
|
| 248 | + if ($this->util->isExcluded($fullPath)) { |
|
| 249 | + return $this->storage->unlink($path); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $encryptionModule = $this->getEncryptionModule($path); |
|
| 253 | + if ($encryptionModule) { |
|
| 254 | + $this->keyStorage->deleteAllFileKeys($this->getFullPath($path)); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return $this->storage->unlink($path); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * see http://php.net/manual/en/function.rename.php |
|
| 262 | + * |
|
| 263 | + * @param string $path1 |
|
| 264 | + * @param string $path2 |
|
| 265 | + * @return bool |
|
| 266 | + */ |
|
| 267 | + public function rename($path1, $path2) { |
|
| 268 | + |
|
| 269 | + $result = $this->storage->rename($path1, $path2); |
|
| 270 | + |
|
| 271 | + if ($result && |
|
| 272 | + // versions always use the keys from the original file, so we can skip |
|
| 273 | + // this step for versions |
|
| 274 | + $this->isVersion($path2) === false && |
|
| 275 | + $this->encryptionManager->isEnabled()) { |
|
| 276 | + $source = $this->getFullPath($path1); |
|
| 277 | + if (!$this->util->isExcluded($source)) { |
|
| 278 | + $target = $this->getFullPath($path2); |
|
| 279 | + if (isset($this->unencryptedSize[$source])) { |
|
| 280 | + $this->unencryptedSize[$target] = $this->unencryptedSize[$source]; |
|
| 281 | + } |
|
| 282 | + $this->keyStorage->renameKeys($source, $target); |
|
| 283 | + $module = $this->getEncryptionModule($path2); |
|
| 284 | + if ($module) { |
|
| 285 | + $module->update($target, $this->uid, []); |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + return $result; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * see http://php.net/manual/en/function.rmdir.php |
|
| 295 | + * |
|
| 296 | + * @param string $path |
|
| 297 | + * @return bool |
|
| 298 | + */ |
|
| 299 | + public function rmdir($path) { |
|
| 300 | + $result = $this->storage->rmdir($path); |
|
| 301 | + $fullPath = $this->getFullPath($path); |
|
| 302 | + if ($result && |
|
| 303 | + $this->util->isExcluded($fullPath) === false && |
|
| 304 | + $this->encryptionManager->isEnabled() |
|
| 305 | + ) { |
|
| 306 | + $this->keyStorage->deleteAllFileKeys($fullPath); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + return $result; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * check if a file can be read |
|
| 314 | + * |
|
| 315 | + * @param string $path |
|
| 316 | + * @return bool |
|
| 317 | + */ |
|
| 318 | + public function isReadable($path) { |
|
| 319 | + |
|
| 320 | + $isReadable = true; |
|
| 321 | + |
|
| 322 | + $metaData = $this->getMetaData($path); |
|
| 323 | + if ( |
|
| 324 | + !$this->is_dir($path) && |
|
| 325 | + isset($metaData['encrypted']) && |
|
| 326 | + $metaData['encrypted'] === true |
|
| 327 | + ) { |
|
| 328 | + $fullPath = $this->getFullPath($path); |
|
| 329 | + $module = $this->getEncryptionModule($path); |
|
| 330 | + $isReadable = $module->isReadable($fullPath, $this->uid); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + return $this->storage->isReadable($path) && $isReadable; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * see http://php.net/manual/en/function.copy.php |
|
| 338 | + * |
|
| 339 | + * @param string $path1 |
|
| 340 | + * @param string $path2 |
|
| 341 | + * @return bool |
|
| 342 | + */ |
|
| 343 | + public function copy($path1, $path2) { |
|
| 344 | + |
|
| 345 | + $source = $this->getFullPath($path1); |
|
| 346 | + |
|
| 347 | + if ($this->util->isExcluded($source)) { |
|
| 348 | + return $this->storage->copy($path1, $path2); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + // need to stream copy file by file in case we copy between a encrypted |
|
| 352 | + // and a unencrypted storage |
|
| 353 | + $this->unlink($path2); |
|
| 354 | + $result = $this->copyFromStorage($this, $path1, $path2); |
|
| 355 | + |
|
| 356 | + return $result; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * see http://php.net/manual/en/function.fopen.php |
|
| 361 | + * |
|
| 362 | + * @param string $path |
|
| 363 | + * @param string $mode |
|
| 364 | + * @return resource|bool |
|
| 365 | + * @throws GenericEncryptionException |
|
| 366 | + * @throws ModuleDoesNotExistsException |
|
| 367 | + */ |
|
| 368 | + public function fopen($path, $mode) { |
|
| 369 | + |
|
| 370 | + // check if the file is stored in the array cache, this means that we |
|
| 371 | + // copy a file over to the versions folder, in this case we don't want to |
|
| 372 | + // decrypt it |
|
| 373 | + if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
| 374 | + $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
| 375 | + return $this->storage->fopen($path, $mode); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + $encryptionEnabled = $this->encryptionManager->isEnabled(); |
|
| 379 | + $shouldEncrypt = false; |
|
| 380 | + $encryptionModule = null; |
|
| 381 | + $header = $this->getHeader($path); |
|
| 382 | + $signed = (isset($header['signed']) && $header['signed'] === 'true') ? true : false; |
|
| 383 | + $fullPath = $this->getFullPath($path); |
|
| 384 | + $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 385 | + |
|
| 386 | + if ($this->util->isExcluded($fullPath) === false) { |
|
| 387 | + |
|
| 388 | + $size = $unencryptedSize = 0; |
|
| 389 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
| 390 | + $targetExists = $this->file_exists($realFile) || $this->file_exists($path); |
|
| 391 | + $targetIsEncrypted = false; |
|
| 392 | + if ($targetExists) { |
|
| 393 | + // in case the file exists we require the explicit module as |
|
| 394 | + // specified in the file header - otherwise we need to fail hard to |
|
| 395 | + // prevent data loss on client side |
|
| 396 | + if (!empty($encryptionModuleId)) { |
|
| 397 | + $targetIsEncrypted = true; |
|
| 398 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + if ($this->file_exists($path)) { |
|
| 402 | + $size = $this->storage->filesize($path); |
|
| 403 | + $unencryptedSize = $this->filesize($path); |
|
| 404 | + } else { |
|
| 405 | + $size = $unencryptedSize = 0; |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + try { |
|
| 410 | + |
|
| 411 | + if ( |
|
| 412 | + $mode === 'w' |
|
| 413 | + || $mode === 'w+' |
|
| 414 | + || $mode === 'wb' |
|
| 415 | + || $mode === 'wb+' |
|
| 416 | + ) { |
|
| 417 | + // if we update a encrypted file with a un-encrypted one we change the db flag |
|
| 418 | + if ($targetIsEncrypted && $encryptionEnabled === false) { |
|
| 419 | + $cache = $this->storage->getCache(); |
|
| 420 | + if ($cache) { |
|
| 421 | + $entry = $cache->get($path); |
|
| 422 | + $cache->update($entry->getId(), ['encrypted' => 0]); |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + if ($encryptionEnabled) { |
|
| 426 | + // if $encryptionModuleId is empty, the default module will be used |
|
| 427 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 428 | + $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); |
|
| 429 | + $signed = true; |
|
| 430 | + } |
|
| 431 | + } else { |
|
| 432 | + $info = $this->getCache()->get($path); |
|
| 433 | + // only get encryption module if we found one in the header |
|
| 434 | + // or if file should be encrypted according to the file cache |
|
| 435 | + if (!empty($encryptionModuleId)) { |
|
| 436 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 437 | + $shouldEncrypt = true; |
|
| 438 | + } else if (empty($encryptionModuleId) && $info['encrypted'] === true) { |
|
| 439 | + // we come from a old installation. No header and/or no module defined |
|
| 440 | + // but the file is encrypted. In this case we need to use the |
|
| 441 | + // OC_DEFAULT_MODULE to read the file |
|
| 442 | + $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE'); |
|
| 443 | + $shouldEncrypt = true; |
|
| 444 | + $targetIsEncrypted = true; |
|
| 445 | + } |
|
| 446 | + } |
|
| 447 | + } catch (ModuleDoesNotExistsException $e) { |
|
| 448 | + $this->logger->warning('Encryption module "' . $encryptionModuleId . |
|
| 449 | + '" not found, file will be stored unencrypted (' . $e->getMessage() . ')'); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
|
| 453 | + if (!$encryptionEnabled || !$this->shouldEncrypt($path)) { |
|
| 454 | + if (!$targetExists || !$targetIsEncrypted) { |
|
| 455 | + $shouldEncrypt = false; |
|
| 456 | + } |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + if ($shouldEncrypt === true && $encryptionModule !== null) { |
|
| 460 | + $headerSize = $this->getHeaderSize($path); |
|
| 461 | + $source = $this->storage->fopen($path, $mode); |
|
| 462 | + if (!is_resource($source)) { |
|
| 463 | + return false; |
|
| 464 | + } |
|
| 465 | + $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, |
|
| 466 | + $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, |
|
| 467 | + $size, $unencryptedSize, $headerSize, $signed); |
|
| 468 | + return $handle; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + return $this->storage->fopen($path, $mode); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * perform some plausibility checks if the the unencrypted size is correct. |
|
| 479 | + * If not, we calculate the correct unencrypted size and return it |
|
| 480 | + * |
|
| 481 | + * @param string $path internal path relative to the storage root |
|
| 482 | + * @param int $unencryptedSize size of the unencrypted file |
|
| 483 | + * |
|
| 484 | + * @return int unencrypted size |
|
| 485 | + */ |
|
| 486 | + protected function verifyUnencryptedSize($path, $unencryptedSize) { |
|
| 487 | + |
|
| 488 | + $size = $this->storage->filesize($path); |
|
| 489 | + $result = $unencryptedSize; |
|
| 490 | + |
|
| 491 | + if ($unencryptedSize < 0 || |
|
| 492 | + ($size > 0 && $unencryptedSize === $size) |
|
| 493 | + ) { |
|
| 494 | + // check if we already calculate the unencrypted size for the |
|
| 495 | + // given path to avoid recursions |
|
| 496 | + if (isset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]) === false) { |
|
| 497 | + $this->fixUnencryptedSizeOf[$this->getFullPath($path)] = true; |
|
| 498 | + try { |
|
| 499 | + $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
|
| 500 | + } catch (\Exception $e) { |
|
| 501 | + $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
| 502 | + $this->logger->logException($e); |
|
| 503 | + } |
|
| 504 | + unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + return $result; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * calculate the unencrypted size |
|
| 513 | + * |
|
| 514 | + * @param string $path internal path relative to the storage root |
|
| 515 | + * @param int $size size of the physical file |
|
| 516 | + * @param int $unencryptedSize size of the unencrypted file |
|
| 517 | + * |
|
| 518 | + * @return int calculated unencrypted size |
|
| 519 | + */ |
|
| 520 | + protected function fixUnencryptedSize($path, $size, $unencryptedSize) { |
|
| 521 | + |
|
| 522 | + $headerSize = $this->getHeaderSize($path); |
|
| 523 | + $header = $this->getHeader($path); |
|
| 524 | + $encryptionModule = $this->getEncryptionModule($path); |
|
| 525 | + |
|
| 526 | + $stream = $this->storage->fopen($path, 'r'); |
|
| 527 | + |
|
| 528 | + // if we couldn't open the file we return the old unencrypted size |
|
| 529 | + if (!is_resource($stream)) { |
|
| 530 | + $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
| 531 | + return $unencryptedSize; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + $newUnencryptedSize = 0; |
|
| 535 | + $size -= $headerSize; |
|
| 536 | + $blockSize = $this->util->getBlockSize(); |
|
| 537 | + |
|
| 538 | + // if a header exists we skip it |
|
| 539 | + if ($headerSize > 0) { |
|
| 540 | + fread($stream, $headerSize); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + // fast path, else the calculation for $lastChunkNr is bogus |
|
| 544 | + if ($size === 0) { |
|
| 545 | + return 0; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + $signed = (isset($header['signed']) && $header['signed'] === 'true') ? true : false; |
|
| 549 | + $unencryptedBlockSize = $encryptionModule->getUnencryptedBlockSize($signed); |
|
| 550 | + |
|
| 551 | + // calculate last chunk nr |
|
| 552 | + // next highest is end of chunks, one subtracted is last one |
|
| 553 | + // we have to read the last chunk, we can't just calculate it (because of padding etc) |
|
| 554 | + |
|
| 555 | + $lastChunkNr = ceil($size/ $blockSize)-1; |
|
| 556 | + // calculate last chunk position |
|
| 557 | + $lastChunkPos = ($lastChunkNr * $blockSize); |
|
| 558 | + // try to fseek to the last chunk, if it fails we have to read the whole file |
|
| 559 | + if (@fseek($stream, $lastChunkPos, SEEK_CUR) === 0) { |
|
| 560 | + $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + $lastChunkContentEncrypted=''; |
|
| 564 | + $count = $blockSize; |
|
| 565 | + |
|
| 566 | + while ($count > 0) { |
|
| 567 | + $data=fread($stream, $blockSize); |
|
| 568 | + $count=strlen($data); |
|
| 569 | + $lastChunkContentEncrypted .= $data; |
|
| 570 | + if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 571 | + $newUnencryptedSize += $unencryptedBlockSize; |
|
| 572 | + $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + fclose($stream); |
|
| 577 | + |
|
| 578 | + // we have to decrypt the last chunk to get it actual size |
|
| 579 | + $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
|
| 580 | + $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
| 581 | + $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
| 582 | + |
|
| 583 | + // calc the real file size with the size of the last chunk |
|
| 584 | + $newUnencryptedSize += strlen($decryptedLastChunk); |
|
| 585 | + |
|
| 586 | + $this->updateUnencryptedSize($this->getFullPath($path), $newUnencryptedSize); |
|
| 587 | + |
|
| 588 | + // write to cache if applicable |
|
| 589 | + $cache = $this->storage->getCache(); |
|
| 590 | + if ($cache) { |
|
| 591 | + $entry = $cache->get($path); |
|
| 592 | + $cache->update($entry['fileid'], ['size' => $newUnencryptedSize]); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + return $newUnencryptedSize; |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * @param Storage\IStorage $sourceStorage |
|
| 600 | + * @param string $sourceInternalPath |
|
| 601 | + * @param string $targetInternalPath |
|
| 602 | + * @param bool $preserveMtime |
|
| 603 | + * @return bool |
|
| 604 | + */ |
|
| 605 | + public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) { |
|
| 606 | + if ($sourceStorage === $this) { |
|
| 607 | + return $this->rename($sourceInternalPath, $targetInternalPath); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 611 | + // - call $this->storage->moveFromStorage() instead of $this->copyBetweenStorage |
|
| 612 | + // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 613 | + // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 614 | + // - remove $this->copyBetweenStorage |
|
| 615 | + |
|
| 616 | + if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
| 617 | + return false; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true); |
|
| 621 | + if ($result) { |
|
| 622 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 623 | + $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
| 624 | + } else { |
|
| 625 | + $result &= $sourceStorage->unlink($sourceInternalPath); |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + return $result; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * @param Storage\IStorage $sourceStorage |
|
| 634 | + * @param string $sourceInternalPath |
|
| 635 | + * @param string $targetInternalPath |
|
| 636 | + * @param bool $preserveMtime |
|
| 637 | + * @param bool $isRename |
|
| 638 | + * @return bool |
|
| 639 | + */ |
|
| 640 | + public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) { |
|
| 641 | + |
|
| 642 | + // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 643 | + // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage |
|
| 644 | + // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 645 | + // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 646 | + // - remove $this->copyBetweenStorage |
|
| 647 | + |
|
| 648 | + return $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Update the encrypted cache version in the database |
|
| 653 | + * |
|
| 654 | + * @param Storage\IStorage $sourceStorage |
|
| 655 | + * @param string $sourceInternalPath |
|
| 656 | + * @param string $targetInternalPath |
|
| 657 | + * @param bool $isRename |
|
| 658 | + * @param bool $keepEncryptionVersion |
|
| 659 | + */ |
|
| 660 | + private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, $keepEncryptionVersion) { |
|
| 661 | + $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath); |
|
| 662 | + $cacheInformation = [ |
|
| 663 | + 'encrypted' => $isEncrypted, |
|
| 664 | + ]; |
|
| 665 | + if($isEncrypted) { |
|
| 666 | + $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
|
| 667 | + |
|
| 668 | + // In case of a move operation from an unencrypted to an encrypted |
|
| 669 | + // storage the old encrypted version would stay with "0" while the |
|
| 670 | + // correct value would be "1". Thus we manually set the value to "1" |
|
| 671 | + // for those cases. |
|
| 672 | + // See also https://github.com/owncloud/core/issues/23078 |
|
| 673 | + if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 674 | + $encryptedVersion = 1; |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + $cacheInformation['encryptedVersion'] = $encryptedVersion; |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + // in case of a rename we need to manipulate the source cache because |
|
| 681 | + // this information will be kept for the new target |
|
| 682 | + if ($isRename) { |
|
| 683 | + $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation); |
|
| 684 | + } else { |
|
| 685 | + $this->getCache()->put($targetInternalPath, $cacheInformation); |
|
| 686 | + } |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * copy file between two storages |
|
| 691 | + * |
|
| 692 | + * @param Storage\IStorage $sourceStorage |
|
| 693 | + * @param string $sourceInternalPath |
|
| 694 | + * @param string $targetInternalPath |
|
| 695 | + * @param bool $preserveMtime |
|
| 696 | + * @param bool $isRename |
|
| 697 | + * @return bool |
|
| 698 | + * @throws \Exception |
|
| 699 | + */ |
|
| 700 | + private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) { |
|
| 701 | + |
|
| 702 | + // for versions we have nothing to do, because versions should always use the |
|
| 703 | + // key from the original file. Just create a 1:1 copy and done |
|
| 704 | + if ($this->isVersion($targetInternalPath) || |
|
| 705 | + $this->isVersion($sourceInternalPath)) { |
|
| 706 | + // remember that we try to create a version so that we can detect it during |
|
| 707 | + // fopen($sourceInternalPath) and by-pass the encryption in order to |
|
| 708 | + // create a 1:1 copy of the file |
|
| 709 | + $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
| 710 | + $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 711 | + $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
| 712 | + if ($result) { |
|
| 713 | + $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
|
| 714 | + // make sure that we update the unencrypted size for the version |
|
| 715 | + if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 716 | + $this->updateUnencryptedSize( |
|
| 717 | + $this->getFullPath($targetInternalPath), |
|
| 718 | + $info['size'] |
|
| 719 | + ); |
|
| 720 | + } |
|
| 721 | + $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true); |
|
| 722 | + } |
|
| 723 | + return $result; |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + // first copy the keys that we reuse the existing file key on the target location |
|
| 727 | + // and don't create a new one which would break versions for example. |
|
| 728 | + $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
|
| 729 | + if (count($mount) === 1) { |
|
| 730 | + $mountPoint = $mount[0]->getMountPoint(); |
|
| 731 | + $source = $mountPoint . '/' . $sourceInternalPath; |
|
| 732 | + $target = $this->getFullPath($targetInternalPath); |
|
| 733 | + $this->copyKeys($source, $target); |
|
| 734 | + } else { |
|
| 735 | + $this->logger->error('Could not find mount point, can\'t keep encryption keys'); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 739 | + $dh = $sourceStorage->opendir($sourceInternalPath); |
|
| 740 | + $result = $this->mkdir($targetInternalPath); |
|
| 741 | + if (is_resource($dh)) { |
|
| 742 | + while ($result and ($file = readdir($dh)) !== false) { |
|
| 743 | + if (!Filesystem::isIgnoredDir($file)) { |
|
| 744 | + $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
| 745 | + } |
|
| 746 | + } |
|
| 747 | + } |
|
| 748 | + } else { |
|
| 749 | + try { |
|
| 750 | + $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
| 751 | + $target = $this->fopen($targetInternalPath, 'w'); |
|
| 752 | + list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
| 753 | + fclose($source); |
|
| 754 | + fclose($target); |
|
| 755 | + } catch (\Exception $e) { |
|
| 756 | + fclose($source); |
|
| 757 | + fclose($target); |
|
| 758 | + throw $e; |
|
| 759 | + } |
|
| 760 | + if($result) { |
|
| 761 | + if ($preserveMtime) { |
|
| 762 | + $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
| 763 | + } |
|
| 764 | + $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, false); |
|
| 765 | + } else { |
|
| 766 | + // delete partially written target file |
|
| 767 | + $this->unlink($targetInternalPath); |
|
| 768 | + // delete cache entry that was created by fopen |
|
| 769 | + $this->getCache()->remove($targetInternalPath); |
|
| 770 | + } |
|
| 771 | + } |
|
| 772 | + return (bool)$result; |
|
| 773 | + |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + /** |
|
| 777 | + * get the path to a local version of the file. |
|
| 778 | + * The local version of the file can be temporary and doesn't have to be persistent across requests |
|
| 779 | + * |
|
| 780 | + * @param string $path |
|
| 781 | + * @return string |
|
| 782 | + */ |
|
| 783 | + public function getLocalFile($path) { |
|
| 784 | + if ($this->encryptionManager->isEnabled()) { |
|
| 785 | + $cachedFile = $this->getCachedFile($path); |
|
| 786 | + if (is_string($cachedFile)) { |
|
| 787 | + return $cachedFile; |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | + return $this->storage->getLocalFile($path); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + /** |
|
| 794 | + * Returns the wrapped storage's value for isLocal() |
|
| 795 | + * |
|
| 796 | + * @return bool wrapped storage's isLocal() value |
|
| 797 | + */ |
|
| 798 | + public function isLocal() { |
|
| 799 | + if ($this->encryptionManager->isEnabled()) { |
|
| 800 | + return false; |
|
| 801 | + } |
|
| 802 | + return $this->storage->isLocal(); |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * see http://php.net/manual/en/function.stat.php |
|
| 807 | + * only the following keys are required in the result: size and mtime |
|
| 808 | + * |
|
| 809 | + * @param string $path |
|
| 810 | + * @return array |
|
| 811 | + */ |
|
| 812 | + public function stat($path) { |
|
| 813 | + $stat = $this->storage->stat($path); |
|
| 814 | + $fileSize = $this->filesize($path); |
|
| 815 | + $stat['size'] = $fileSize; |
|
| 816 | + $stat[7] = $fileSize; |
|
| 817 | + return $stat; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * see http://php.net/manual/en/function.hash.php |
|
| 822 | + * |
|
| 823 | + * @param string $type |
|
| 824 | + * @param string $path |
|
| 825 | + * @param bool $raw |
|
| 826 | + * @return string |
|
| 827 | + */ |
|
| 828 | + public function hash($type, $path, $raw = false) { |
|
| 829 | + $fh = $this->fopen($path, 'rb'); |
|
| 830 | + $ctx = hash_init($type); |
|
| 831 | + hash_update_stream($ctx, $fh); |
|
| 832 | + fclose($fh); |
|
| 833 | + return hash_final($ctx, $raw); |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + /** |
|
| 837 | + * return full path, including mount point |
|
| 838 | + * |
|
| 839 | + * @param string $path relative to mount point |
|
| 840 | + * @return string full path including mount point |
|
| 841 | + */ |
|
| 842 | + protected function getFullPath($path) { |
|
| 843 | + return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * read first block of encrypted file, typically this will contain the |
|
| 848 | + * encryption header |
|
| 849 | + * |
|
| 850 | + * @param string $path |
|
| 851 | + * @return string |
|
| 852 | + */ |
|
| 853 | + protected function readFirstBlock($path) { |
|
| 854 | + $firstBlock = ''; |
|
| 855 | + if ($this->storage->file_exists($path)) { |
|
| 856 | + $handle = $this->storage->fopen($path, 'r'); |
|
| 857 | + $firstBlock = fread($handle, $this->util->getHeaderSize()); |
|
| 858 | + fclose($handle); |
|
| 859 | + } |
|
| 860 | + return $firstBlock; |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * return header size of given file |
|
| 865 | + * |
|
| 866 | + * @param string $path |
|
| 867 | + * @return int |
|
| 868 | + */ |
|
| 869 | + protected function getHeaderSize($path) { |
|
| 870 | + $headerSize = 0; |
|
| 871 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
| 872 | + if ($this->storage->file_exists($realFile)) { |
|
| 873 | + $path = $realFile; |
|
| 874 | + } |
|
| 875 | + $firstBlock = $this->readFirstBlock($path); |
|
| 876 | + |
|
| 877 | + if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 878 | + $headerSize = $this->util->getHeaderSize(); |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + return $headerSize; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + /** |
|
| 885 | + * parse raw header to array |
|
| 886 | + * |
|
| 887 | + * @param string $rawHeader |
|
| 888 | + * @return array |
|
| 889 | + */ |
|
| 890 | + protected function parseRawHeader($rawHeader) { |
|
| 891 | + $result = array(); |
|
| 892 | + if (substr($rawHeader, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 893 | + $header = $rawHeader; |
|
| 894 | + $endAt = strpos($header, Util::HEADER_END); |
|
| 895 | + if ($endAt !== false) { |
|
| 896 | + $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
|
| 897 | + |
|
| 898 | + // +1 to not start with an ':' which would result in empty element at the beginning |
|
| 899 | + $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
| 900 | + |
|
| 901 | + $element = array_shift($exploded); |
|
| 902 | + while ($element !== Util::HEADER_END) { |
|
| 903 | + $result[$element] = array_shift($exploded); |
|
| 904 | + $element = array_shift($exploded); |
|
| 905 | + } |
|
| 906 | + } |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + return $result; |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + /** |
|
| 913 | + * read header from file |
|
| 914 | + * |
|
| 915 | + * @param string $path |
|
| 916 | + * @return array |
|
| 917 | + */ |
|
| 918 | + protected function getHeader($path) { |
|
| 919 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
| 920 | + $exists = $this->storage->file_exists($realFile); |
|
| 921 | + if ($exists) { |
|
| 922 | + $path = $realFile; |
|
| 923 | + } |
|
| 924 | + |
|
| 925 | + $firstBlock = $this->readFirstBlock($path); |
|
| 926 | + $result = $this->parseRawHeader($firstBlock); |
|
| 927 | + |
|
| 928 | + // if the header doesn't contain a encryption module we check if it is a |
|
| 929 | + // legacy file. If true, we add the default encryption module |
|
| 930 | + if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY])) { |
|
| 931 | + if (!empty($result)) { |
|
| 932 | + $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 933 | + } else if ($exists) { |
|
| 934 | + // if the header was empty we have to check first if it is a encrypted file at all |
|
| 935 | + // We would do query to filecache only if we know that entry in filecache exists |
|
| 936 | + $info = $this->getCache()->get($path); |
|
| 937 | + if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 938 | + $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 939 | + } |
|
| 940 | + } |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + return $result; |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + /** |
|
| 947 | + * read encryption module needed to read/write the file located at $path |
|
| 948 | + * |
|
| 949 | + * @param string $path |
|
| 950 | + * @return null|\OCP\Encryption\IEncryptionModule |
|
| 951 | + * @throws ModuleDoesNotExistsException |
|
| 952 | + * @throws \Exception |
|
| 953 | + */ |
|
| 954 | + protected function getEncryptionModule($path) { |
|
| 955 | + $encryptionModule = null; |
|
| 956 | + $header = $this->getHeader($path); |
|
| 957 | + $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 958 | + if (!empty($encryptionModuleId)) { |
|
| 959 | + try { |
|
| 960 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 961 | + } catch (ModuleDoesNotExistsException $e) { |
|
| 962 | + $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
| 963 | + throw $e; |
|
| 964 | + } |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + return $encryptionModule; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + /** |
|
| 971 | + * @param string $path |
|
| 972 | + * @param int $unencryptedSize |
|
| 973 | + */ |
|
| 974 | + public function updateUnencryptedSize($path, $unencryptedSize) { |
|
| 975 | + $this->unencryptedSize[$path] = $unencryptedSize; |
|
| 976 | + } |
|
| 977 | + |
|
| 978 | + /** |
|
| 979 | + * copy keys to new location |
|
| 980 | + * |
|
| 981 | + * @param string $source path relative to data/ |
|
| 982 | + * @param string $target path relative to data/ |
|
| 983 | + * @return bool |
|
| 984 | + */ |
|
| 985 | + protected function copyKeys($source, $target) { |
|
| 986 | + if (!$this->util->isExcluded($source)) { |
|
| 987 | + return $this->keyStorage->copyKeys($source, $target); |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + return false; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + /** |
|
| 994 | + * check if path points to a files version |
|
| 995 | + * |
|
| 996 | + * @param $path |
|
| 997 | + * @return bool |
|
| 998 | + */ |
|
| 999 | + protected function isVersion($path) { |
|
| 1000 | + $normalized = Filesystem::normalizePath($path); |
|
| 1001 | + return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/'; |
|
| 1002 | + } |
|
| 1003 | + |
|
| 1004 | + /** |
|
| 1005 | + * check if the given storage should be encrypted or not |
|
| 1006 | + * |
|
| 1007 | + * @param $path |
|
| 1008 | + * @return bool |
|
| 1009 | + */ |
|
| 1010 | + protected function shouldEncrypt($path) { |
|
| 1011 | + $fullPath = $this->getFullPath($path); |
|
| 1012 | + $mountPointConfig = $this->mount->getOption('encrypt', true); |
|
| 1013 | + if ($mountPointConfig === false) { |
|
| 1014 | + return false; |
|
| 1015 | + } |
|
| 1016 | + |
|
| 1017 | + try { |
|
| 1018 | + $encryptionModule = $this->getEncryptionModule($fullPath); |
|
| 1019 | + } catch (ModuleDoesNotExistsException $e) { |
|
| 1020 | + return false; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + if ($encryptionModule === null) { |
|
| 1024 | + $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + return $encryptionModule->shouldEncrypt($fullPath); |
|
| 1028 | + |
|
| 1029 | + } |
|
| 1030 | 1030 | |
| 1031 | 1031 | } |
@@ -162,6 +162,9 @@ discard block |
||
| 162 | 162 | return true; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | + /** |
|
| 166 | + * @param string $path |
|
| 167 | + */ |
|
| 165 | 168 | private function rmObjects($path) { |
| 166 | 169 | $children = $this->getCache()->getFolderContents($path); |
| 167 | 170 | foreach ($children as $child) { |
@@ -364,6 +367,9 @@ discard block |
||
| 364 | 367 | return true; |
| 365 | 368 | } |
| 366 | 369 | |
| 370 | + /** |
|
| 371 | + * @param string $path |
|
| 372 | + */ |
|
| 367 | 373 | public function writeBack($tmpFile, $path) { |
| 368 | 374 | $stat = $this->stat($path); |
| 369 | 375 | if (empty($stat)) { |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | throw new \Exception('missing IObjectStore instance'); |
| 56 | 56 | } |
| 57 | 57 | if (isset($params['storageid'])) { |
| 58 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 58 | + $this->id = 'object::store:'.$params['storageid']; |
|
| 59 | 59 | } else { |
| 60 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 60 | + $this->id = 'object::store:'.$this->objectStore->getStorageId(); |
|
| 61 | 61 | } |
| 62 | 62 | if (isset($params['objectPrefix'])) { |
| 63 | 63 | $this->objectPrefix = $params['objectPrefix']; |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | if ($ex->getCode() !== 404) { |
| 192 | 192 | $this->logger->logException($ex, [ |
| 193 | 193 | 'app' => 'objectstore', |
| 194 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 194 | + 'message' => 'Could not delete object '.$this->getURN($stat['fileid']).' for '.$path, |
|
| 195 | 195 | ]); |
| 196 | 196 | return false; |
| 197 | 197 | } else { |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | protected function getURN($fileId) { |
| 226 | 226 | if (is_numeric($fileId)) { |
| 227 | - return $this->objectPrefix . $fileId; |
|
| 227 | + return $this->objectPrefix.$fileId; |
|
| 228 | 228 | } |
| 229 | 229 | return null; |
| 230 | 230 | } |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | } catch (\Exception $ex) { |
| 273 | 273 | $this->logger->logException($ex, [ |
| 274 | 274 | 'app' => 'objectstore', |
| 275 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 275 | + 'message' => 'Count not get object '.$this->getURN($stat['fileid']).' for file '.$path, |
|
| 276 | 276 | ]); |
| 277 | 277 | return false; |
| 278 | 278 | } |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | file_put_contents($tmpFile, $source); |
| 303 | 303 | } |
| 304 | 304 | $handle = fopen($tmpFile, $mode); |
| 305 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 305 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 306 | 306 | $this->writeBack($tmpFile, $path); |
| 307 | 307 | }); |
| 308 | 308 | } |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | |
| 312 | 312 | public function file_exists($path) { |
| 313 | 313 | $path = $this->normalizePath($path); |
| 314 | - return (bool)$this->stat($path); |
|
| 314 | + return (bool) $this->stat($path); |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | public function rename($source, $target) { |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | } catch (\Exception $ex) { |
| 368 | 368 | $this->logger->logException($ex, [ |
| 369 | 369 | 'app' => 'objectstore', |
| 370 | - 'message' => 'Could not create object for ' . $path, |
|
| 370 | + 'message' => 'Could not create object for '.$path, |
|
| 371 | 371 | ]); |
| 372 | 372 | return false; |
| 373 | 373 | } |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | $this->getCache()->remove($path); |
| 408 | 408 | $this->logger->logException($ex, [ |
| 409 | 409 | 'app' => 'objectstore', |
| 410 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 410 | + 'message' => 'Could not create object '.$this->getURN($fileId).' for '.$path, |
|
| 411 | 411 | ]); |
| 412 | 412 | throw $ex; // make this bubble up |
| 413 | 413 | } |
@@ -31,401 +31,401 @@ |
||
| 31 | 31 | use OCP\Files\ObjectStore\IObjectStore; |
| 32 | 32 | |
| 33 | 33 | class ObjectStoreStorage extends \OC\Files\Storage\Common { |
| 34 | - /** |
|
| 35 | - * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 36 | - */ |
|
| 37 | - protected $objectStore; |
|
| 38 | - /** |
|
| 39 | - * @var string $id |
|
| 40 | - */ |
|
| 41 | - protected $id; |
|
| 42 | - /** |
|
| 43 | - * @var \OC\User\User $user |
|
| 44 | - */ |
|
| 45 | - protected $user; |
|
| 46 | - |
|
| 47 | - private $objectPrefix = 'urn:oid:'; |
|
| 48 | - |
|
| 49 | - private $logger; |
|
| 50 | - |
|
| 51 | - public function __construct($params) { |
|
| 52 | - if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 53 | - $this->objectStore = $params['objectstore']; |
|
| 54 | - } else { |
|
| 55 | - throw new \Exception('missing IObjectStore instance'); |
|
| 56 | - } |
|
| 57 | - if (isset($params['storageid'])) { |
|
| 58 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 59 | - } else { |
|
| 60 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 61 | - } |
|
| 62 | - if (isset($params['objectPrefix'])) { |
|
| 63 | - $this->objectPrefix = $params['objectPrefix']; |
|
| 64 | - } |
|
| 65 | - //initialize cache with root directory in cache |
|
| 66 | - if (!$this->is_dir('/')) { |
|
| 67 | - $this->mkdir('/'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->logger = \OC::$server->getLogger(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function mkdir($path) { |
|
| 74 | - $path = $this->normalizePath($path); |
|
| 75 | - |
|
| 76 | - if ($this->file_exists($path)) { |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $mTime = time(); |
|
| 81 | - $data = [ |
|
| 82 | - 'mimetype' => 'httpd/unix-directory', |
|
| 83 | - 'size' => 0, |
|
| 84 | - 'mtime' => $mTime, |
|
| 85 | - 'storage_mtime' => $mTime, |
|
| 86 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 87 | - ]; |
|
| 88 | - if ($path === '') { |
|
| 89 | - //create root on the fly |
|
| 90 | - $data['etag'] = $this->getETag(''); |
|
| 91 | - $this->getCache()->put('', $data); |
|
| 92 | - return true; |
|
| 93 | - } else { |
|
| 94 | - // if parent does not exist, create it |
|
| 95 | - $parent = $this->normalizePath(dirname($path)); |
|
| 96 | - $parentType = $this->filetype($parent); |
|
| 97 | - if ($parentType === false) { |
|
| 98 | - if (!$this->mkdir($parent)) { |
|
| 99 | - // something went wrong |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - } else if ($parentType === 'file') { |
|
| 103 | - // parent is a file |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - // finally create the new dir |
|
| 107 | - $mTime = time(); // update mtime |
|
| 108 | - $data['mtime'] = $mTime; |
|
| 109 | - $data['storage_mtime'] = $mTime; |
|
| 110 | - $data['etag'] = $this->getETag($path); |
|
| 111 | - $this->getCache()->put($path, $data); |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param string $path |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - private function normalizePath($path) { |
|
| 121 | - $path = trim($path, '/'); |
|
| 122 | - //FIXME why do we sometimes get a path like 'files//username'? |
|
| 123 | - $path = str_replace('//', '/', $path); |
|
| 124 | - |
|
| 125 | - // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 126 | - if (!$path || $path === '.') { |
|
| 127 | - $path = ''; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - return $path; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 135 | - * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 136 | - * |
|
| 137 | - * @param string $path |
|
| 138 | - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 139 | - * @return \OC\Files\ObjectStore\NoopScanner |
|
| 140 | - */ |
|
| 141 | - public function getScanner($path = '', $storage = null) { |
|
| 142 | - if (!$storage) { |
|
| 143 | - $storage = $this; |
|
| 144 | - } |
|
| 145 | - if (!isset($this->scanner)) { |
|
| 146 | - $this->scanner = new NoopScanner($storage); |
|
| 147 | - } |
|
| 148 | - return $this->scanner; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - public function getId() { |
|
| 152 | - return $this->id; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - public function rmdir($path) { |
|
| 156 | - $path = $this->normalizePath($path); |
|
| 157 | - |
|
| 158 | - if (!$this->is_dir($path)) { |
|
| 159 | - return false; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $this->rmObjects($path); |
|
| 163 | - |
|
| 164 | - $this->getCache()->remove($path); |
|
| 165 | - |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - private function rmObjects($path) { |
|
| 170 | - $children = $this->getCache()->getFolderContents($path); |
|
| 171 | - foreach ($children as $child) { |
|
| 172 | - if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 173 | - $this->rmObjects($child['path']); |
|
| 174 | - } else { |
|
| 175 | - $this->unlink($child['path']); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - public function unlink($path) { |
|
| 181 | - $path = $this->normalizePath($path); |
|
| 182 | - $stat = $this->stat($path); |
|
| 183 | - |
|
| 184 | - if ($stat && isset($stat['fileid'])) { |
|
| 185 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 186 | - return $this->rmdir($path); |
|
| 187 | - } |
|
| 188 | - try { |
|
| 189 | - $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 190 | - } catch (\Exception $ex) { |
|
| 191 | - if ($ex->getCode() !== 404) { |
|
| 192 | - $this->logger->logException($ex, [ |
|
| 193 | - 'app' => 'objectstore', |
|
| 194 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 195 | - ]); |
|
| 196 | - return false; |
|
| 197 | - } else { |
|
| 198 | - //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - $this->getCache()->remove($path); |
|
| 202 | - return true; |
|
| 203 | - } |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - public function stat($path) { |
|
| 208 | - $path = $this->normalizePath($path); |
|
| 209 | - $cacheEntry = $this->getCache()->get($path); |
|
| 210 | - if ($cacheEntry instanceof CacheEntry) { |
|
| 211 | - return $cacheEntry->getData(); |
|
| 212 | - } else { |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 219 | - * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 220 | - * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 221 | - * |
|
| 222 | - * @param int $fileId the fileid |
|
| 223 | - * @return null|string the unified resource name used to identify the object |
|
| 224 | - */ |
|
| 225 | - protected function getURN($fileId) { |
|
| 226 | - if (is_numeric($fileId)) { |
|
| 227 | - return $this->objectPrefix . $fileId; |
|
| 228 | - } |
|
| 229 | - return null; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - public function opendir($path) { |
|
| 233 | - $path = $this->normalizePath($path); |
|
| 234 | - |
|
| 235 | - try { |
|
| 236 | - $files = array(); |
|
| 237 | - $folderContents = $this->getCache()->getFolderContents($path); |
|
| 238 | - foreach ($folderContents as $file) { |
|
| 239 | - $files[] = $file['name']; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - return IteratorDirectory::wrap($files); |
|
| 243 | - } catch (\Exception $e) { |
|
| 244 | - $this->logger->logException($e); |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - public function filetype($path) { |
|
| 250 | - $path = $this->normalizePath($path); |
|
| 251 | - $stat = $this->stat($path); |
|
| 252 | - if ($stat) { |
|
| 253 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 254 | - return 'dir'; |
|
| 255 | - } |
|
| 256 | - return 'file'; |
|
| 257 | - } else { |
|
| 258 | - return false; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - public function fopen($path, $mode) { |
|
| 263 | - $path = $this->normalizePath($path); |
|
| 264 | - |
|
| 265 | - switch ($mode) { |
|
| 266 | - case 'r': |
|
| 267 | - case 'rb': |
|
| 268 | - $stat = $this->stat($path); |
|
| 269 | - if (is_array($stat)) { |
|
| 270 | - try { |
|
| 271 | - return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 272 | - } catch (\Exception $ex) { |
|
| 273 | - $this->logger->logException($ex, [ |
|
| 274 | - 'app' => 'objectstore', |
|
| 275 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 276 | - ]); |
|
| 277 | - return false; |
|
| 278 | - } |
|
| 279 | - } else { |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - case 'w': |
|
| 283 | - case 'wb': |
|
| 284 | - case 'a': |
|
| 285 | - case 'ab': |
|
| 286 | - case 'r+': |
|
| 287 | - case 'w+': |
|
| 288 | - case 'wb+': |
|
| 289 | - case 'a+': |
|
| 290 | - case 'x': |
|
| 291 | - case 'x+': |
|
| 292 | - case 'c': |
|
| 293 | - case 'c+': |
|
| 294 | - if (strrpos($path, '.') !== false) { |
|
| 295 | - $ext = substr($path, strrpos($path, '.')); |
|
| 296 | - } else { |
|
| 297 | - $ext = ''; |
|
| 298 | - } |
|
| 299 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 300 | - if ($this->file_exists($path)) { |
|
| 301 | - $source = $this->fopen($path, 'r'); |
|
| 302 | - file_put_contents($tmpFile, $source); |
|
| 303 | - } |
|
| 304 | - $handle = fopen($tmpFile, $mode); |
|
| 305 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 306 | - $this->writeBack($tmpFile, $path); |
|
| 307 | - }); |
|
| 308 | - } |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - public function file_exists($path) { |
|
| 313 | - $path = $this->normalizePath($path); |
|
| 314 | - return (bool)$this->stat($path); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - public function rename($source, $target) { |
|
| 318 | - $source = $this->normalizePath($source); |
|
| 319 | - $target = $this->normalizePath($target); |
|
| 320 | - $this->remove($target); |
|
| 321 | - $this->getCache()->move($source, $target); |
|
| 322 | - $this->touch(dirname($target)); |
|
| 323 | - return true; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - public function getMimeType($path) { |
|
| 327 | - $path = $this->normalizePath($path); |
|
| 328 | - $stat = $this->stat($path); |
|
| 329 | - if (is_array($stat)) { |
|
| 330 | - return $stat['mimetype']; |
|
| 331 | - } else { |
|
| 332 | - return false; |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - public function touch($path, $mtime = null) { |
|
| 337 | - if (is_null($mtime)) { |
|
| 338 | - $mtime = time(); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - $path = $this->normalizePath($path); |
|
| 342 | - $dirName = dirname($path); |
|
| 343 | - $parentExists = $this->is_dir($dirName); |
|
| 344 | - if (!$parentExists) { |
|
| 345 | - return false; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $stat = $this->stat($path); |
|
| 349 | - if (is_array($stat)) { |
|
| 350 | - // update existing mtime in db |
|
| 351 | - $stat['mtime'] = $mtime; |
|
| 352 | - $this->getCache()->update($stat['fileid'], $stat); |
|
| 353 | - } else { |
|
| 354 | - try { |
|
| 355 | - //create a empty file, need to have at least on char to make it |
|
| 356 | - // work with all object storage implementations |
|
| 357 | - $this->file_put_contents($path, ' '); |
|
| 358 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 359 | - $stat = array( |
|
| 360 | - 'etag' => $this->getETag($path), |
|
| 361 | - 'mimetype' => $mimeType, |
|
| 362 | - 'size' => 0, |
|
| 363 | - 'mtime' => $mtime, |
|
| 364 | - 'storage_mtime' => $mtime, |
|
| 365 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 366 | - ); |
|
| 367 | - $this->getCache()->put($path, $stat); |
|
| 368 | - } catch (\Exception $ex) { |
|
| 369 | - $this->logger->logException($ex, [ |
|
| 370 | - 'app' => 'objectstore', |
|
| 371 | - 'message' => 'Could not create object for ' . $path, |
|
| 372 | - ]); |
|
| 373 | - return false; |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - return true; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - public function writeBack($tmpFile, $path) { |
|
| 380 | - $stat = $this->stat($path); |
|
| 381 | - if (empty($stat)) { |
|
| 382 | - // create new file |
|
| 383 | - $stat = array( |
|
| 384 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 385 | - ); |
|
| 386 | - } |
|
| 387 | - // update stat with new data |
|
| 388 | - $mTime = time(); |
|
| 389 | - $stat['size'] = filesize($tmpFile); |
|
| 390 | - $stat['mtime'] = $mTime; |
|
| 391 | - $stat['storage_mtime'] = $mTime; |
|
| 392 | - |
|
| 393 | - // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 394 | - $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 395 | - $mimetype = $mimetypeDetector->detectPath($path); |
|
| 396 | - if ($mimetype === 'application/octet-stream') { |
|
| 397 | - $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - $stat['mimetype'] = $mimetype; |
|
| 401 | - $stat['etag'] = $this->getETag($path); |
|
| 402 | - |
|
| 403 | - $fileId = $this->getCache()->put($path, $stat); |
|
| 404 | - try { |
|
| 405 | - //upload to object storage |
|
| 406 | - $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 407 | - } catch (\Exception $ex) { |
|
| 408 | - $this->getCache()->remove($path); |
|
| 409 | - $this->logger->logException($ex, [ |
|
| 410 | - 'app' => 'objectstore', |
|
| 411 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 412 | - ]); |
|
| 413 | - throw $ex; // make this bubble up |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * external changes are not supported, exclusive access to the object storage is assumed |
|
| 419 | - * |
|
| 420 | - * @param string $path |
|
| 421 | - * @param int $time |
|
| 422 | - * @return false |
|
| 423 | - */ |
|
| 424 | - public function hasUpdated($path, $time) { |
|
| 425 | - return false; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - public function needsPartFile() { |
|
| 429 | - return false; |
|
| 430 | - } |
|
| 34 | + /** |
|
| 35 | + * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 36 | + */ |
|
| 37 | + protected $objectStore; |
|
| 38 | + /** |
|
| 39 | + * @var string $id |
|
| 40 | + */ |
|
| 41 | + protected $id; |
|
| 42 | + /** |
|
| 43 | + * @var \OC\User\User $user |
|
| 44 | + */ |
|
| 45 | + protected $user; |
|
| 46 | + |
|
| 47 | + private $objectPrefix = 'urn:oid:'; |
|
| 48 | + |
|
| 49 | + private $logger; |
|
| 50 | + |
|
| 51 | + public function __construct($params) { |
|
| 52 | + if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 53 | + $this->objectStore = $params['objectstore']; |
|
| 54 | + } else { |
|
| 55 | + throw new \Exception('missing IObjectStore instance'); |
|
| 56 | + } |
|
| 57 | + if (isset($params['storageid'])) { |
|
| 58 | + $this->id = 'object::store:' . $params['storageid']; |
|
| 59 | + } else { |
|
| 60 | + $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 61 | + } |
|
| 62 | + if (isset($params['objectPrefix'])) { |
|
| 63 | + $this->objectPrefix = $params['objectPrefix']; |
|
| 64 | + } |
|
| 65 | + //initialize cache with root directory in cache |
|
| 66 | + if (!$this->is_dir('/')) { |
|
| 67 | + $this->mkdir('/'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->logger = \OC::$server->getLogger(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function mkdir($path) { |
|
| 74 | + $path = $this->normalizePath($path); |
|
| 75 | + |
|
| 76 | + if ($this->file_exists($path)) { |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $mTime = time(); |
|
| 81 | + $data = [ |
|
| 82 | + 'mimetype' => 'httpd/unix-directory', |
|
| 83 | + 'size' => 0, |
|
| 84 | + 'mtime' => $mTime, |
|
| 85 | + 'storage_mtime' => $mTime, |
|
| 86 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 87 | + ]; |
|
| 88 | + if ($path === '') { |
|
| 89 | + //create root on the fly |
|
| 90 | + $data['etag'] = $this->getETag(''); |
|
| 91 | + $this->getCache()->put('', $data); |
|
| 92 | + return true; |
|
| 93 | + } else { |
|
| 94 | + // if parent does not exist, create it |
|
| 95 | + $parent = $this->normalizePath(dirname($path)); |
|
| 96 | + $parentType = $this->filetype($parent); |
|
| 97 | + if ($parentType === false) { |
|
| 98 | + if (!$this->mkdir($parent)) { |
|
| 99 | + // something went wrong |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + } else if ($parentType === 'file') { |
|
| 103 | + // parent is a file |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + // finally create the new dir |
|
| 107 | + $mTime = time(); // update mtime |
|
| 108 | + $data['mtime'] = $mTime; |
|
| 109 | + $data['storage_mtime'] = $mTime; |
|
| 110 | + $data['etag'] = $this->getETag($path); |
|
| 111 | + $this->getCache()->put($path, $data); |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param string $path |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + private function normalizePath($path) { |
|
| 121 | + $path = trim($path, '/'); |
|
| 122 | + //FIXME why do we sometimes get a path like 'files//username'? |
|
| 123 | + $path = str_replace('//', '/', $path); |
|
| 124 | + |
|
| 125 | + // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 126 | + if (!$path || $path === '.') { |
|
| 127 | + $path = ''; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + return $path; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 135 | + * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 136 | + * |
|
| 137 | + * @param string $path |
|
| 138 | + * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 139 | + * @return \OC\Files\ObjectStore\NoopScanner |
|
| 140 | + */ |
|
| 141 | + public function getScanner($path = '', $storage = null) { |
|
| 142 | + if (!$storage) { |
|
| 143 | + $storage = $this; |
|
| 144 | + } |
|
| 145 | + if (!isset($this->scanner)) { |
|
| 146 | + $this->scanner = new NoopScanner($storage); |
|
| 147 | + } |
|
| 148 | + return $this->scanner; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + public function getId() { |
|
| 152 | + return $this->id; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + public function rmdir($path) { |
|
| 156 | + $path = $this->normalizePath($path); |
|
| 157 | + |
|
| 158 | + if (!$this->is_dir($path)) { |
|
| 159 | + return false; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $this->rmObjects($path); |
|
| 163 | + |
|
| 164 | + $this->getCache()->remove($path); |
|
| 165 | + |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + private function rmObjects($path) { |
|
| 170 | + $children = $this->getCache()->getFolderContents($path); |
|
| 171 | + foreach ($children as $child) { |
|
| 172 | + if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 173 | + $this->rmObjects($child['path']); |
|
| 174 | + } else { |
|
| 175 | + $this->unlink($child['path']); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + public function unlink($path) { |
|
| 181 | + $path = $this->normalizePath($path); |
|
| 182 | + $stat = $this->stat($path); |
|
| 183 | + |
|
| 184 | + if ($stat && isset($stat['fileid'])) { |
|
| 185 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 186 | + return $this->rmdir($path); |
|
| 187 | + } |
|
| 188 | + try { |
|
| 189 | + $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 190 | + } catch (\Exception $ex) { |
|
| 191 | + if ($ex->getCode() !== 404) { |
|
| 192 | + $this->logger->logException($ex, [ |
|
| 193 | + 'app' => 'objectstore', |
|
| 194 | + 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 195 | + ]); |
|
| 196 | + return false; |
|
| 197 | + } else { |
|
| 198 | + //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + $this->getCache()->remove($path); |
|
| 202 | + return true; |
|
| 203 | + } |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + public function stat($path) { |
|
| 208 | + $path = $this->normalizePath($path); |
|
| 209 | + $cacheEntry = $this->getCache()->get($path); |
|
| 210 | + if ($cacheEntry instanceof CacheEntry) { |
|
| 211 | + return $cacheEntry->getData(); |
|
| 212 | + } else { |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 219 | + * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 220 | + * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 221 | + * |
|
| 222 | + * @param int $fileId the fileid |
|
| 223 | + * @return null|string the unified resource name used to identify the object |
|
| 224 | + */ |
|
| 225 | + protected function getURN($fileId) { |
|
| 226 | + if (is_numeric($fileId)) { |
|
| 227 | + return $this->objectPrefix . $fileId; |
|
| 228 | + } |
|
| 229 | + return null; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + public function opendir($path) { |
|
| 233 | + $path = $this->normalizePath($path); |
|
| 234 | + |
|
| 235 | + try { |
|
| 236 | + $files = array(); |
|
| 237 | + $folderContents = $this->getCache()->getFolderContents($path); |
|
| 238 | + foreach ($folderContents as $file) { |
|
| 239 | + $files[] = $file['name']; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + return IteratorDirectory::wrap($files); |
|
| 243 | + } catch (\Exception $e) { |
|
| 244 | + $this->logger->logException($e); |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + public function filetype($path) { |
|
| 250 | + $path = $this->normalizePath($path); |
|
| 251 | + $stat = $this->stat($path); |
|
| 252 | + if ($stat) { |
|
| 253 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 254 | + return 'dir'; |
|
| 255 | + } |
|
| 256 | + return 'file'; |
|
| 257 | + } else { |
|
| 258 | + return false; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + public function fopen($path, $mode) { |
|
| 263 | + $path = $this->normalizePath($path); |
|
| 264 | + |
|
| 265 | + switch ($mode) { |
|
| 266 | + case 'r': |
|
| 267 | + case 'rb': |
|
| 268 | + $stat = $this->stat($path); |
|
| 269 | + if (is_array($stat)) { |
|
| 270 | + try { |
|
| 271 | + return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 272 | + } catch (\Exception $ex) { |
|
| 273 | + $this->logger->logException($ex, [ |
|
| 274 | + 'app' => 'objectstore', |
|
| 275 | + 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 276 | + ]); |
|
| 277 | + return false; |
|
| 278 | + } |
|
| 279 | + } else { |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + case 'w': |
|
| 283 | + case 'wb': |
|
| 284 | + case 'a': |
|
| 285 | + case 'ab': |
|
| 286 | + case 'r+': |
|
| 287 | + case 'w+': |
|
| 288 | + case 'wb+': |
|
| 289 | + case 'a+': |
|
| 290 | + case 'x': |
|
| 291 | + case 'x+': |
|
| 292 | + case 'c': |
|
| 293 | + case 'c+': |
|
| 294 | + if (strrpos($path, '.') !== false) { |
|
| 295 | + $ext = substr($path, strrpos($path, '.')); |
|
| 296 | + } else { |
|
| 297 | + $ext = ''; |
|
| 298 | + } |
|
| 299 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 300 | + if ($this->file_exists($path)) { |
|
| 301 | + $source = $this->fopen($path, 'r'); |
|
| 302 | + file_put_contents($tmpFile, $source); |
|
| 303 | + } |
|
| 304 | + $handle = fopen($tmpFile, $mode); |
|
| 305 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 306 | + $this->writeBack($tmpFile, $path); |
|
| 307 | + }); |
|
| 308 | + } |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + public function file_exists($path) { |
|
| 313 | + $path = $this->normalizePath($path); |
|
| 314 | + return (bool)$this->stat($path); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + public function rename($source, $target) { |
|
| 318 | + $source = $this->normalizePath($source); |
|
| 319 | + $target = $this->normalizePath($target); |
|
| 320 | + $this->remove($target); |
|
| 321 | + $this->getCache()->move($source, $target); |
|
| 322 | + $this->touch(dirname($target)); |
|
| 323 | + return true; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + public function getMimeType($path) { |
|
| 327 | + $path = $this->normalizePath($path); |
|
| 328 | + $stat = $this->stat($path); |
|
| 329 | + if (is_array($stat)) { |
|
| 330 | + return $stat['mimetype']; |
|
| 331 | + } else { |
|
| 332 | + return false; |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + public function touch($path, $mtime = null) { |
|
| 337 | + if (is_null($mtime)) { |
|
| 338 | + $mtime = time(); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + $path = $this->normalizePath($path); |
|
| 342 | + $dirName = dirname($path); |
|
| 343 | + $parentExists = $this->is_dir($dirName); |
|
| 344 | + if (!$parentExists) { |
|
| 345 | + return false; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $stat = $this->stat($path); |
|
| 349 | + if (is_array($stat)) { |
|
| 350 | + // update existing mtime in db |
|
| 351 | + $stat['mtime'] = $mtime; |
|
| 352 | + $this->getCache()->update($stat['fileid'], $stat); |
|
| 353 | + } else { |
|
| 354 | + try { |
|
| 355 | + //create a empty file, need to have at least on char to make it |
|
| 356 | + // work with all object storage implementations |
|
| 357 | + $this->file_put_contents($path, ' '); |
|
| 358 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 359 | + $stat = array( |
|
| 360 | + 'etag' => $this->getETag($path), |
|
| 361 | + 'mimetype' => $mimeType, |
|
| 362 | + 'size' => 0, |
|
| 363 | + 'mtime' => $mtime, |
|
| 364 | + 'storage_mtime' => $mtime, |
|
| 365 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 366 | + ); |
|
| 367 | + $this->getCache()->put($path, $stat); |
|
| 368 | + } catch (\Exception $ex) { |
|
| 369 | + $this->logger->logException($ex, [ |
|
| 370 | + 'app' => 'objectstore', |
|
| 371 | + 'message' => 'Could not create object for ' . $path, |
|
| 372 | + ]); |
|
| 373 | + return false; |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + return true; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + public function writeBack($tmpFile, $path) { |
|
| 380 | + $stat = $this->stat($path); |
|
| 381 | + if (empty($stat)) { |
|
| 382 | + // create new file |
|
| 383 | + $stat = array( |
|
| 384 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 385 | + ); |
|
| 386 | + } |
|
| 387 | + // update stat with new data |
|
| 388 | + $mTime = time(); |
|
| 389 | + $stat['size'] = filesize($tmpFile); |
|
| 390 | + $stat['mtime'] = $mTime; |
|
| 391 | + $stat['storage_mtime'] = $mTime; |
|
| 392 | + |
|
| 393 | + // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 394 | + $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 395 | + $mimetype = $mimetypeDetector->detectPath($path); |
|
| 396 | + if ($mimetype === 'application/octet-stream') { |
|
| 397 | + $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + $stat['mimetype'] = $mimetype; |
|
| 401 | + $stat['etag'] = $this->getETag($path); |
|
| 402 | + |
|
| 403 | + $fileId = $this->getCache()->put($path, $stat); |
|
| 404 | + try { |
|
| 405 | + //upload to object storage |
|
| 406 | + $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 407 | + } catch (\Exception $ex) { |
|
| 408 | + $this->getCache()->remove($path); |
|
| 409 | + $this->logger->logException($ex, [ |
|
| 410 | + 'app' => 'objectstore', |
|
| 411 | + 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 412 | + ]); |
|
| 413 | + throw $ex; // make this bubble up |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * external changes are not supported, exclusive access to the object storage is assumed |
|
| 419 | + * |
|
| 420 | + * @param string $path |
|
| 421 | + * @param int $time |
|
| 422 | + * @return false |
|
| 423 | + */ |
|
| 424 | + public function hasUpdated($path, $time) { |
|
| 425 | + return false; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + public function needsPartFile() { |
|
| 429 | + return false; |
|
| 430 | + } |
|
| 431 | 431 | } |
@@ -102,7 +102,7 @@ |
||
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | 104 | * @param \Icewind\SMB\Change $change |
| 105 | - * @return IChange|null |
|
| 105 | + * @return null|Change |
|
| 106 | 106 | */ |
| 107 | 107 | private function mapChange(\Icewind\SMB\Change $change) { |
| 108 | 108 | $path = $this->relativePath($change->getPath()); |
@@ -29,122 +29,122 @@ |
||
| 29 | 29 | use OCP\Files\Notify\INotifyHandler; |
| 30 | 30 | |
| 31 | 31 | class SMBNotifyHandler implements INotifyHandler { |
| 32 | - /** |
|
| 33 | - * @var \Icewind\SMB\INotifyHandler |
|
| 34 | - */ |
|
| 35 | - private $shareNotifyHandler; |
|
| 32 | + /** |
|
| 33 | + * @var \Icewind\SMB\INotifyHandler |
|
| 34 | + */ |
|
| 35 | + private $shareNotifyHandler; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - private $root; |
|
| 37 | + /** |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + private $root; |
|
| 41 | 41 | |
| 42 | - private $oldRenamePath = null; |
|
| 42 | + private $oldRenamePath = null; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * SMBNotifyHandler constructor. |
|
| 46 | - * |
|
| 47 | - * @param \Icewind\SMB\INotifyHandler $shareNotifyHandler |
|
| 48 | - * @param string $root |
|
| 49 | - */ |
|
| 50 | - public function __construct(\Icewind\SMB\INotifyHandler $shareNotifyHandler, $root) { |
|
| 51 | - $this->shareNotifyHandler = $shareNotifyHandler; |
|
| 52 | - $this->root = $root; |
|
| 53 | - } |
|
| 44 | + /** |
|
| 45 | + * SMBNotifyHandler constructor. |
|
| 46 | + * |
|
| 47 | + * @param \Icewind\SMB\INotifyHandler $shareNotifyHandler |
|
| 48 | + * @param string $root |
|
| 49 | + */ |
|
| 50 | + public function __construct(\Icewind\SMB\INotifyHandler $shareNotifyHandler, $root) { |
|
| 51 | + $this->shareNotifyHandler = $shareNotifyHandler; |
|
| 52 | + $this->root = $root; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - private function relativePath($fullPath) { |
|
| 56 | - if ($fullPath === $this->root) { |
|
| 57 | - return ''; |
|
| 58 | - } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 59 | - return substr($fullPath, strlen($this->root)); |
|
| 60 | - } else { |
|
| 61 | - return null; |
|
| 62 | - } |
|
| 63 | - } |
|
| 55 | + private function relativePath($fullPath) { |
|
| 56 | + if ($fullPath === $this->root) { |
|
| 57 | + return ''; |
|
| 58 | + } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 59 | + return substr($fullPath, strlen($this->root)); |
|
| 60 | + } else { |
|
| 61 | + return null; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public function listen(callable $callback) { |
|
| 66 | - $oldRenamePath = null; |
|
| 67 | - $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | - $change = $this->mapChange($shareChange); |
|
| 69 | - if (!is_null($change)) { |
|
| 70 | - return $callback($change); |
|
| 71 | - } else { |
|
| 72 | - return true; |
|
| 73 | - } |
|
| 74 | - }); |
|
| 75 | - } |
|
| 65 | + public function listen(callable $callback) { |
|
| 66 | + $oldRenamePath = null; |
|
| 67 | + $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | + $change = $this->mapChange($shareChange); |
|
| 69 | + if (!is_null($change)) { |
|
| 70 | + return $callback($change); |
|
| 71 | + } else { |
|
| 72 | + return true; |
|
| 73 | + } |
|
| 74 | + }); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Get all changes detected since the start of the notify process or the last call to getChanges |
|
| 79 | - * |
|
| 80 | - * @return IChange[] |
|
| 81 | - */ |
|
| 82 | - public function getChanges() { |
|
| 83 | - $shareChanges = $this->shareNotifyHandler->getChanges(); |
|
| 84 | - $changes = []; |
|
| 85 | - foreach ($shareChanges as $shareChange) { |
|
| 86 | - $change = $this->mapChange($shareChange); |
|
| 87 | - if ($change) { |
|
| 88 | - $changes[] = $change; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - return $changes; |
|
| 92 | - } |
|
| 77 | + /** |
|
| 78 | + * Get all changes detected since the start of the notify process or the last call to getChanges |
|
| 79 | + * |
|
| 80 | + * @return IChange[] |
|
| 81 | + */ |
|
| 82 | + public function getChanges() { |
|
| 83 | + $shareChanges = $this->shareNotifyHandler->getChanges(); |
|
| 84 | + $changes = []; |
|
| 85 | + foreach ($shareChanges as $shareChange) { |
|
| 86 | + $change = $this->mapChange($shareChange); |
|
| 87 | + if ($change) { |
|
| 88 | + $changes[] = $change; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + return $changes; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Stop listening for changes |
|
| 96 | - * |
|
| 97 | - * Note that any pending changes will be discarded |
|
| 98 | - */ |
|
| 99 | - public function stop() { |
|
| 100 | - $this->shareNotifyHandler->stop(); |
|
| 101 | - } |
|
| 94 | + /** |
|
| 95 | + * Stop listening for changes |
|
| 96 | + * |
|
| 97 | + * Note that any pending changes will be discarded |
|
| 98 | + */ |
|
| 99 | + public function stop() { |
|
| 100 | + $this->shareNotifyHandler->stop(); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @param \Icewind\SMB\Change $change |
|
| 105 | - * @return IChange|null |
|
| 106 | - */ |
|
| 107 | - private function mapChange(\Icewind\SMB\Change $change) { |
|
| 108 | - $path = $this->relativePath($change->getPath()); |
|
| 109 | - if (is_null($path)) { |
|
| 110 | - return null; |
|
| 111 | - } |
|
| 112 | - if ($change->getCode() === \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_OLD) { |
|
| 113 | - $this->oldRenamePath = $path; |
|
| 114 | - return null; |
|
| 115 | - } |
|
| 116 | - $type = $this->mapNotifyType($change->getCode()); |
|
| 117 | - if (is_null($type)) { |
|
| 118 | - return null; |
|
| 119 | - } |
|
| 120 | - if ($type === IChange::RENAMED) { |
|
| 121 | - if (!is_null($this->oldRenamePath)) { |
|
| 122 | - $result = new RenameChange($type, $this->oldRenamePath, $path); |
|
| 123 | - $this->oldRenamePath = null; |
|
| 124 | - } else { |
|
| 125 | - $result = null; |
|
| 126 | - } |
|
| 127 | - } else { |
|
| 128 | - $result = new Change($type, $path); |
|
| 129 | - } |
|
| 130 | - return $result; |
|
| 131 | - } |
|
| 103 | + /** |
|
| 104 | + * @param \Icewind\SMB\Change $change |
|
| 105 | + * @return IChange|null |
|
| 106 | + */ |
|
| 107 | + private function mapChange(\Icewind\SMB\Change $change) { |
|
| 108 | + $path = $this->relativePath($change->getPath()); |
|
| 109 | + if (is_null($path)) { |
|
| 110 | + return null; |
|
| 111 | + } |
|
| 112 | + if ($change->getCode() === \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_OLD) { |
|
| 113 | + $this->oldRenamePath = $path; |
|
| 114 | + return null; |
|
| 115 | + } |
|
| 116 | + $type = $this->mapNotifyType($change->getCode()); |
|
| 117 | + if (is_null($type)) { |
|
| 118 | + return null; |
|
| 119 | + } |
|
| 120 | + if ($type === IChange::RENAMED) { |
|
| 121 | + if (!is_null($this->oldRenamePath)) { |
|
| 122 | + $result = new RenameChange($type, $this->oldRenamePath, $path); |
|
| 123 | + $this->oldRenamePath = null; |
|
| 124 | + } else { |
|
| 125 | + $result = null; |
|
| 126 | + } |
|
| 127 | + } else { |
|
| 128 | + $result = new Change($type, $path); |
|
| 129 | + } |
|
| 130 | + return $result; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - private function mapNotifyType($smbType) { |
|
| 134 | - switch ($smbType) { |
|
| 135 | - case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED: |
|
| 136 | - return IChange::ADDED; |
|
| 137 | - case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED: |
|
| 138 | - return IChange::REMOVED; |
|
| 139 | - case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED: |
|
| 140 | - case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED_STREAM: |
|
| 141 | - case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED_STREAM: |
|
| 142 | - case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED_STREAM: |
|
| 143 | - return IChange::MODIFIED; |
|
| 144 | - case \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_NEW: |
|
| 145 | - return IChange::RENAMED; |
|
| 146 | - default: |
|
| 147 | - return null; |
|
| 148 | - } |
|
| 149 | - } |
|
| 133 | + private function mapNotifyType($smbType) { |
|
| 134 | + switch ($smbType) { |
|
| 135 | + case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED: |
|
| 136 | + return IChange::ADDED; |
|
| 137 | + case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED: |
|
| 138 | + return IChange::REMOVED; |
|
| 139 | + case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED: |
|
| 140 | + case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED_STREAM: |
|
| 141 | + case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED_STREAM: |
|
| 142 | + case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED_STREAM: |
|
| 143 | + return IChange::MODIFIED; |
|
| 144 | + case \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_NEW: |
|
| 145 | + return IChange::RENAMED; |
|
| 146 | + default: |
|
| 147 | + return null; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | 150 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | |
| 65 | 65 | public function listen(callable $callback) { |
| 66 | 66 | $oldRenamePath = null; |
| 67 | - $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 67 | + $this->shareNotifyHandler->listen(function(\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | 68 | $change = $this->mapChange($shareChange); |
| 69 | 69 | if (!is_null($change)) { |
| 70 | 70 | return $callback($change); |
@@ -31,7 +31,7 @@ |
||
| 31 | 31 | * Creates a Folder that represents a non-existing path |
| 32 | 32 | * |
| 33 | 33 | * @param string $path path |
| 34 | - * @return string non-existing node class |
|
| 34 | + * @return NonExistingFile non-existing node class |
|
| 35 | 35 | */ |
| 36 | 36 | protected function createNonExistingNode($path) { |
| 37 | 37 | return new NonExistingFile($this->root, $this->view, $path); |
@@ -29,113 +29,113 @@ |
||
| 29 | 29 | use OCP\Files\NotPermittedException; |
| 30 | 30 | |
| 31 | 31 | class File extends Node implements \OCP\Files\File { |
| 32 | - /** |
|
| 33 | - * Creates a Folder that represents a non-existing path |
|
| 34 | - * |
|
| 35 | - * @param string $path path |
|
| 36 | - * @return string non-existing node class |
|
| 37 | - */ |
|
| 38 | - protected function createNonExistingNode($path) { |
|
| 39 | - return new NonExistingFile($this->root, $this->view, $path); |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * Creates a Folder that represents a non-existing path |
|
| 34 | + * |
|
| 35 | + * @param string $path path |
|
| 36 | + * @return string non-existing node class |
|
| 37 | + */ |
|
| 38 | + protected function createNonExistingNode($path) { |
|
| 39 | + return new NonExistingFile($this->root, $this->view, $path); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return string |
|
| 44 | - * @throws \OCP\Files\NotPermittedException |
|
| 45 | - */ |
|
| 46 | - public function getContent() { |
|
| 47 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
|
| 48 | - /** |
|
| 49 | - * @var \OC\Files\Storage\Storage $storage; |
|
| 50 | - */ |
|
| 51 | - return $this->view->file_get_contents($this->path); |
|
| 52 | - } else { |
|
| 53 | - throw new NotPermittedException(); |
|
| 54 | - } |
|
| 55 | - } |
|
| 42 | + /** |
|
| 43 | + * @return string |
|
| 44 | + * @throws \OCP\Files\NotPermittedException |
|
| 45 | + */ |
|
| 46 | + public function getContent() { |
|
| 47 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) { |
|
| 48 | + /** |
|
| 49 | + * @var \OC\Files\Storage\Storage $storage; |
|
| 50 | + */ |
|
| 51 | + return $this->view->file_get_contents($this->path); |
|
| 52 | + } else { |
|
| 53 | + throw new NotPermittedException(); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @param string $data |
|
| 59 | - * @throws \OCP\Files\NotPermittedException |
|
| 60 | - */ |
|
| 61 | - public function putContent($data) { |
|
| 62 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
| 63 | - $this->sendHooks(array('preWrite')); |
|
| 64 | - $this->view->file_put_contents($this->path, $data); |
|
| 65 | - $this->fileInfo = null; |
|
| 66 | - $this->sendHooks(array('postWrite')); |
|
| 67 | - } else { |
|
| 68 | - throw new NotPermittedException(); |
|
| 69 | - } |
|
| 70 | - } |
|
| 57 | + /** |
|
| 58 | + * @param string $data |
|
| 59 | + * @throws \OCP\Files\NotPermittedException |
|
| 60 | + */ |
|
| 61 | + public function putContent($data) { |
|
| 62 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { |
|
| 63 | + $this->sendHooks(array('preWrite')); |
|
| 64 | + $this->view->file_put_contents($this->path, $data); |
|
| 65 | + $this->fileInfo = null; |
|
| 66 | + $this->sendHooks(array('postWrite')); |
|
| 67 | + } else { |
|
| 68 | + throw new NotPermittedException(); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @param string $mode |
|
| 74 | - * @return resource |
|
| 75 | - * @throws \OCP\Files\NotPermittedException |
|
| 76 | - */ |
|
| 77 | - public function fopen($mode) { |
|
| 78 | - $preHooks = array(); |
|
| 79 | - $postHooks = array(); |
|
| 80 | - $requiredPermissions = \OCP\Constants::PERMISSION_READ; |
|
| 81 | - switch ($mode) { |
|
| 82 | - case 'r+': |
|
| 83 | - case 'rb+': |
|
| 84 | - case 'w+': |
|
| 85 | - case 'wb+': |
|
| 86 | - case 'x+': |
|
| 87 | - case 'xb+': |
|
| 88 | - case 'a+': |
|
| 89 | - case 'ab+': |
|
| 90 | - case 'w': |
|
| 91 | - case 'wb': |
|
| 92 | - case 'x': |
|
| 93 | - case 'xb': |
|
| 94 | - case 'a': |
|
| 95 | - case 'ab': |
|
| 96 | - $preHooks[] = 'preWrite'; |
|
| 97 | - $postHooks[] = 'postWrite'; |
|
| 98 | - $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
| 99 | - break; |
|
| 100 | - } |
|
| 72 | + /** |
|
| 73 | + * @param string $mode |
|
| 74 | + * @return resource |
|
| 75 | + * @throws \OCP\Files\NotPermittedException |
|
| 76 | + */ |
|
| 77 | + public function fopen($mode) { |
|
| 78 | + $preHooks = array(); |
|
| 79 | + $postHooks = array(); |
|
| 80 | + $requiredPermissions = \OCP\Constants::PERMISSION_READ; |
|
| 81 | + switch ($mode) { |
|
| 82 | + case 'r+': |
|
| 83 | + case 'rb+': |
|
| 84 | + case 'w+': |
|
| 85 | + case 'wb+': |
|
| 86 | + case 'x+': |
|
| 87 | + case 'xb+': |
|
| 88 | + case 'a+': |
|
| 89 | + case 'ab+': |
|
| 90 | + case 'w': |
|
| 91 | + case 'wb': |
|
| 92 | + case 'x': |
|
| 93 | + case 'xb': |
|
| 94 | + case 'a': |
|
| 95 | + case 'ab': |
|
| 96 | + $preHooks[] = 'preWrite'; |
|
| 97 | + $postHooks[] = 'postWrite'; |
|
| 98 | + $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE; |
|
| 99 | + break; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - if ($this->checkPermissions($requiredPermissions)) { |
|
| 103 | - $this->sendHooks($preHooks); |
|
| 104 | - $result = $this->view->fopen($this->path, $mode); |
|
| 105 | - $this->sendHooks($postHooks); |
|
| 106 | - return $result; |
|
| 107 | - } else { |
|
| 108 | - throw new NotPermittedException(); |
|
| 109 | - } |
|
| 110 | - } |
|
| 102 | + if ($this->checkPermissions($requiredPermissions)) { |
|
| 103 | + $this->sendHooks($preHooks); |
|
| 104 | + $result = $this->view->fopen($this->path, $mode); |
|
| 105 | + $this->sendHooks($postHooks); |
|
| 106 | + return $result; |
|
| 107 | + } else { |
|
| 108 | + throw new NotPermittedException(); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - public function delete() { |
|
| 113 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 114 | - $this->sendHooks(array('preDelete')); |
|
| 115 | - $fileInfo = $this->getFileInfo(); |
|
| 116 | - $this->view->unlink($this->path); |
|
| 117 | - $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); |
|
| 118 | - $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 119 | - $this->exists = false; |
|
| 120 | - $this->fileInfo = null; |
|
| 121 | - } else { |
|
| 122 | - throw new NotPermittedException(); |
|
| 123 | - } |
|
| 124 | - } |
|
| 112 | + public function delete() { |
|
| 113 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 114 | + $this->sendHooks(array('preDelete')); |
|
| 115 | + $fileInfo = $this->getFileInfo(); |
|
| 116 | + $this->view->unlink($this->path); |
|
| 117 | + $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); |
|
| 118 | + $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 119 | + $this->exists = false; |
|
| 120 | + $this->fileInfo = null; |
|
| 121 | + } else { |
|
| 122 | + throw new NotPermittedException(); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * @param string $type |
|
| 128 | - * @param bool $raw |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - public function hash($type, $raw = false) { |
|
| 132 | - return $this->view->hash($type, $this->path, $raw); |
|
| 133 | - } |
|
| 126 | + /** |
|
| 127 | + * @param string $type |
|
| 128 | + * @param bool $raw |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + public function hash($type, $raw = false) { |
|
| 132 | + return $this->view->hash($type, $this->path, $raw); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * @inheritdoc |
|
| 137 | - */ |
|
| 138 | - public function getChecksum() { |
|
| 139 | - return $this->getFileInfo()->getChecksum(); |
|
| 140 | - } |
|
| 135 | + /** |
|
| 136 | + * @inheritdoc |
|
| 137 | + */ |
|
| 138 | + public function getChecksum() { |
|
| 139 | + return $this->getFileInfo()->getChecksum(); |
|
| 140 | + } |
|
| 141 | 141 | } |
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | * Creates a Folder that represents a non-existing path |
| 38 | 38 | * |
| 39 | 39 | * @param string $path path |
| 40 | - * @return string non-existing node class |
|
| 40 | + * @return NonExistingFolder non-existing node class |
|
| 41 | 41 | */ |
| 42 | 42 | protected function createNonExistingNode($path) { |
| 43 | 43 | return new NonExistingFolder($this->root, $this->view, $path); |
@@ -36,398 +36,398 @@ |
||
| 36 | 36 | use OCP\Files\Search\ISearchOperator; |
| 37 | 37 | |
| 38 | 38 | class Folder extends Node implements \OCP\Files\Folder { |
| 39 | - /** |
|
| 40 | - * Creates a Folder that represents a non-existing path |
|
| 41 | - * |
|
| 42 | - * @param string $path path |
|
| 43 | - * @return string non-existing node class |
|
| 44 | - */ |
|
| 45 | - protected function createNonExistingNode($path) { |
|
| 46 | - return new NonExistingFolder($this->root, $this->view, $path); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param string $path path relative to the folder |
|
| 51 | - * @return string |
|
| 52 | - * @throws \OCP\Files\NotPermittedException |
|
| 53 | - */ |
|
| 54 | - public function getFullPath($path) { |
|
| 55 | - if (!$this->isValidPath($path)) { |
|
| 56 | - throw new NotPermittedException('Invalid path'); |
|
| 57 | - } |
|
| 58 | - return $this->path . $this->normalizePath($path); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param string $path |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public function getRelativePath($path) { |
|
| 66 | - if ($this->path === '' or $this->path === '/') { |
|
| 67 | - return $this->normalizePath($path); |
|
| 68 | - } |
|
| 69 | - if ($path === $this->path) { |
|
| 70 | - return '/'; |
|
| 71 | - } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 72 | - return null; |
|
| 73 | - } else { |
|
| 74 | - $path = substr($path, strlen($this->path)); |
|
| 75 | - return $this->normalizePath($path); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * check if a node is a (grand-)child of the folder |
|
| 81 | - * |
|
| 82 | - * @param \OC\Files\Node\Node $node |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function isSubNode($node) { |
|
| 86 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * get the content of this directory |
|
| 91 | - * |
|
| 92 | - * @throws \OCP\Files\NotFoundException |
|
| 93 | - * @return Node[] |
|
| 94 | - */ |
|
| 95 | - public function getDirectoryListing() { |
|
| 96 | - $folderContent = $this->view->getDirectoryContent($this->path); |
|
| 97 | - |
|
| 98 | - return array_map(function (FileInfo $info) { |
|
| 99 | - if ($info->getMimetype() === 'httpd/unix-directory') { |
|
| 100 | - return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 101 | - } else { |
|
| 102 | - return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 103 | - } |
|
| 104 | - }, $folderContent); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param string $path |
|
| 109 | - * @param FileInfo $info |
|
| 110 | - * @return File|Folder |
|
| 111 | - */ |
|
| 112 | - protected function createNode($path, FileInfo $info = null) { |
|
| 113 | - if (is_null($info)) { |
|
| 114 | - $isDir = $this->view->is_dir($path); |
|
| 115 | - } else { |
|
| 116 | - $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 117 | - } |
|
| 118 | - if ($isDir) { |
|
| 119 | - return new Folder($this->root, $this->view, $path, $info); |
|
| 120 | - } else { |
|
| 121 | - return new File($this->root, $this->view, $path, $info); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Get the node at $path |
|
| 127 | - * |
|
| 128 | - * @param string $path |
|
| 129 | - * @return \OC\Files\Node\Node |
|
| 130 | - * @throws \OCP\Files\NotFoundException |
|
| 131 | - */ |
|
| 132 | - public function get($path) { |
|
| 133 | - return $this->root->get($this->getFullPath($path)); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @param string $path |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - public function nodeExists($path) { |
|
| 141 | - try { |
|
| 142 | - $this->get($path); |
|
| 143 | - return true; |
|
| 144 | - } catch (NotFoundException $e) { |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @param string $path |
|
| 151 | - * @return \OC\Files\Node\Folder |
|
| 152 | - * @throws \OCP\Files\NotPermittedException |
|
| 153 | - */ |
|
| 154 | - public function newFolder($path) { |
|
| 155 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 156 | - $fullPath = $this->getFullPath($path); |
|
| 157 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 158 | - $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 159 | - $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 160 | - $this->view->mkdir($fullPath); |
|
| 161 | - $node = new Folder($this->root, $this->view, $fullPath); |
|
| 162 | - $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 163 | - $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 164 | - return $node; |
|
| 165 | - } else { |
|
| 166 | - throw new NotPermittedException('No create permission for folder'); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param string $path |
|
| 172 | - * @return \OC\Files\Node\File |
|
| 173 | - * @throws \OCP\Files\NotPermittedException |
|
| 174 | - */ |
|
| 175 | - public function newFile($path) { |
|
| 176 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 177 | - $fullPath = $this->getFullPath($path); |
|
| 178 | - $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 179 | - $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 180 | - $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 181 | - $this->view->touch($fullPath); |
|
| 182 | - $node = new File($this->root, $this->view, $fullPath); |
|
| 183 | - $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 184 | - $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 185 | - return $node; |
|
| 186 | - } else { |
|
| 187 | - throw new NotPermittedException('No create permission for path'); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * search for files with the name matching $query |
|
| 193 | - * |
|
| 194 | - * @param string|ISearchOperator $query |
|
| 195 | - * @return \OC\Files\Node\Node[] |
|
| 196 | - */ |
|
| 197 | - public function search($query) { |
|
| 198 | - if (is_string($query)) { |
|
| 199 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 200 | - } else { |
|
| 201 | - return $this->searchCommon('searchQuery', array($query)); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * search for files by mimetype |
|
| 207 | - * |
|
| 208 | - * @param string $mimetype |
|
| 209 | - * @return Node[] |
|
| 210 | - */ |
|
| 211 | - public function searchByMime($mimetype) { |
|
| 212 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * search for files by tag |
|
| 217 | - * |
|
| 218 | - * @param string|int $tag name or tag id |
|
| 219 | - * @param string $userId owner of the tags |
|
| 220 | - * @return Node[] |
|
| 221 | - */ |
|
| 222 | - public function searchByTag($tag, $userId) { |
|
| 223 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param string $method cache method |
|
| 228 | - * @param array $args call args |
|
| 229 | - * @return \OC\Files\Node\Node[] |
|
| 230 | - */ |
|
| 231 | - private function searchCommon($method, $args) { |
|
| 232 | - $files = array(); |
|
| 233 | - $rootLength = strlen($this->path); |
|
| 234 | - $mount = $this->root->getMount($this->path); |
|
| 235 | - $storage = $mount->getStorage(); |
|
| 236 | - $internalPath = $mount->getInternalPath($this->path); |
|
| 237 | - $internalPath = rtrim($internalPath, '/'); |
|
| 238 | - if ($internalPath !== '') { |
|
| 239 | - $internalPath = $internalPath . '/'; |
|
| 240 | - } |
|
| 241 | - $internalRootLength = strlen($internalPath); |
|
| 242 | - |
|
| 243 | - $cache = $storage->getCache(''); |
|
| 244 | - |
|
| 245 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 246 | - foreach ($results as $result) { |
|
| 247 | - if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { |
|
| 248 | - $result['internalPath'] = $result['path']; |
|
| 249 | - $result['path'] = substr($result['path'], $internalRootLength); |
|
| 250 | - $result['storage'] = $storage; |
|
| 251 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 256 | - foreach ($mounts as $mount) { |
|
| 257 | - $storage = $mount->getStorage(); |
|
| 258 | - if ($storage) { |
|
| 259 | - $cache = $storage->getCache(''); |
|
| 260 | - |
|
| 261 | - $relativeMountPoint = substr($mount->getMountPoint(), $rootLength); |
|
| 262 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 263 | - foreach ($results as $result) { |
|
| 264 | - $result['internalPath'] = $result['path']; |
|
| 265 | - $result['path'] = $relativeMountPoint . $result['path']; |
|
| 266 | - $result['storage'] = $storage; |
|
| 267 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return array_map(function (FileInfo $file) { |
|
| 273 | - return $this->createNode($file->getPath(), $file); |
|
| 274 | - }, $files); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @param int $id |
|
| 279 | - * @return \OC\Files\Node\Node[] |
|
| 280 | - */ |
|
| 281 | - public function getById($id) { |
|
| 282 | - $mountCache = $this->root->getUserMountCache(); |
|
| 283 | - if (strpos($this->getPath(), '/', 1) > 0) { |
|
| 284 | - list(, $user) = explode('/', $this->getPath()); |
|
| 285 | - } else { |
|
| 286 | - $user = null; |
|
| 287 | - } |
|
| 288 | - $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 289 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 290 | - $mounts[] = $this->root->getMount($this->path); |
|
| 291 | - /** @var IMountPoint[] $folderMounts */ |
|
| 292 | - $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 293 | - return $mountPoint->getMountPoint(); |
|
| 294 | - }, $mounts), $mounts); |
|
| 295 | - |
|
| 296 | - /** @var ICachedMountInfo[] $mountsContainingFile */ |
|
| 297 | - $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 298 | - return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
|
| 299 | - })); |
|
| 300 | - |
|
| 301 | - if (count($mountsContainingFile) === 0) { |
|
| 302 | - return []; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) { |
|
| 306 | - $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
|
| 307 | - $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 308 | - if (!$cacheEntry) { |
|
| 309 | - return null; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - // cache jails will hide the "true" internal path |
|
| 313 | - $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 314 | - $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
|
| 315 | - $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 316 | - $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount; |
|
| 317 | - return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
|
| 318 | - $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
| 319 | - \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
| 320 | - )); |
|
| 321 | - }, $mountsContainingFile); |
|
| 322 | - |
|
| 323 | - $nodes = array_filter($nodes); |
|
| 324 | - |
|
| 325 | - return array_filter($nodes, function (Node $node) { |
|
| 326 | - return $this->getRelativePath($node->getPath()); |
|
| 327 | - }); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - public function getFreeSpace() { |
|
| 331 | - return $this->view->free_space($this->path); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - public function delete() { |
|
| 335 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 336 | - $this->sendHooks(array('preDelete')); |
|
| 337 | - $fileInfo = $this->getFileInfo(); |
|
| 338 | - $this->view->rmdir($this->path); |
|
| 339 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 340 | - $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 341 | - $this->exists = false; |
|
| 342 | - } else { |
|
| 343 | - throw new NotPermittedException('No delete permission for path'); |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Add a suffix to the name in case the file exists |
|
| 349 | - * |
|
| 350 | - * @param string $name |
|
| 351 | - * @return string |
|
| 352 | - * @throws NotPermittedException |
|
| 353 | - */ |
|
| 354 | - public function getNonExistingName($name) { |
|
| 355 | - $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 356 | - return trim($this->getRelativePath($uniqueName), '/'); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @param int $limit |
|
| 361 | - * @param int $offset |
|
| 362 | - * @return \OCP\Files\Node[] |
|
| 363 | - */ |
|
| 364 | - public function getRecent($limit, $offset = 0) { |
|
| 365 | - $mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 366 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 367 | - $mounts[] = $this->getMountPoint(); |
|
| 368 | - |
|
| 369 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 370 | - return $mount->getStorage(); |
|
| 371 | - }); |
|
| 372 | - $storageIds = array_map(function (IMountPoint $mount) { |
|
| 373 | - return $mount->getStorage()->getCache()->getNumericStorageId(); |
|
| 374 | - }, $mounts); |
|
| 375 | - /** @var IMountPoint[] $mountMap */ |
|
| 376 | - $mountMap = array_combine($storageIds, $mounts); |
|
| 377 | - $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER); |
|
| 378 | - |
|
| 379 | - //todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc) |
|
| 380 | - |
|
| 381 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 382 | - $query = $builder |
|
| 383 | - ->select('f.*') |
|
| 384 | - ->from('filecache', 'f') |
|
| 385 | - ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 386 | - ->andWhere($builder->expr()->orX( |
|
| 387 | - // handle non empty folders separate |
|
| 388 | - $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), |
|
| 389 | - $builder->expr()->eq('f.size', new Literal(0)) |
|
| 390 | - )) |
|
| 391 | - ->orderBy('f.mtime', 'DESC') |
|
| 392 | - ->setMaxResults($limit) |
|
| 393 | - ->setFirstResult($offset); |
|
| 394 | - |
|
| 395 | - $result = $query->execute()->fetchAll(); |
|
| 396 | - |
|
| 397 | - $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 398 | - $mount = $mountMap[$entry['storage']]; |
|
| 399 | - $entry['internalPath'] = $entry['path']; |
|
| 400 | - $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
|
| 401 | - $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']); |
|
| 402 | - $path = $this->getAbsolutePath($mount, $entry['path']); |
|
| 403 | - if (is_null($path)) { |
|
| 404 | - return null; |
|
| 405 | - } |
|
| 406 | - $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount); |
|
| 407 | - return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
|
| 408 | - }, $result)); |
|
| 409 | - |
|
| 410 | - return array_values(array_filter($files, function (Node $node) { |
|
| 411 | - $relative = $this->getRelativePath($node->getPath()); |
|
| 412 | - return $relative !== null && $relative !== '/'; |
|
| 413 | - })); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - private function getAbsolutePath(IMountPoint $mount, $path) { |
|
| 417 | - $storage = $mount->getStorage(); |
|
| 418 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) { |
|
| 419 | - /** @var \OC\Files\Storage\Wrapper\Jail $storage */ |
|
| 420 | - $jailRoot = $storage->getUnjailedPath(''); |
|
| 421 | - $rootLength = strlen($jailRoot) + 1; |
|
| 422 | - if ($path === $jailRoot) { |
|
| 423 | - return $mount->getMountPoint(); |
|
| 424 | - } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 425 | - return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 426 | - } else { |
|
| 427 | - return null; |
|
| 428 | - } |
|
| 429 | - } else { |
|
| 430 | - return $mount->getMountPoint() . $path; |
|
| 431 | - } |
|
| 432 | - } |
|
| 39 | + /** |
|
| 40 | + * Creates a Folder that represents a non-existing path |
|
| 41 | + * |
|
| 42 | + * @param string $path path |
|
| 43 | + * @return string non-existing node class |
|
| 44 | + */ |
|
| 45 | + protected function createNonExistingNode($path) { |
|
| 46 | + return new NonExistingFolder($this->root, $this->view, $path); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param string $path path relative to the folder |
|
| 51 | + * @return string |
|
| 52 | + * @throws \OCP\Files\NotPermittedException |
|
| 53 | + */ |
|
| 54 | + public function getFullPath($path) { |
|
| 55 | + if (!$this->isValidPath($path)) { |
|
| 56 | + throw new NotPermittedException('Invalid path'); |
|
| 57 | + } |
|
| 58 | + return $this->path . $this->normalizePath($path); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param string $path |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public function getRelativePath($path) { |
|
| 66 | + if ($this->path === '' or $this->path === '/') { |
|
| 67 | + return $this->normalizePath($path); |
|
| 68 | + } |
|
| 69 | + if ($path === $this->path) { |
|
| 70 | + return '/'; |
|
| 71 | + } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 72 | + return null; |
|
| 73 | + } else { |
|
| 74 | + $path = substr($path, strlen($this->path)); |
|
| 75 | + return $this->normalizePath($path); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * check if a node is a (grand-)child of the folder |
|
| 81 | + * |
|
| 82 | + * @param \OC\Files\Node\Node $node |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function isSubNode($node) { |
|
| 86 | + return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * get the content of this directory |
|
| 91 | + * |
|
| 92 | + * @throws \OCP\Files\NotFoundException |
|
| 93 | + * @return Node[] |
|
| 94 | + */ |
|
| 95 | + public function getDirectoryListing() { |
|
| 96 | + $folderContent = $this->view->getDirectoryContent($this->path); |
|
| 97 | + |
|
| 98 | + return array_map(function (FileInfo $info) { |
|
| 99 | + if ($info->getMimetype() === 'httpd/unix-directory') { |
|
| 100 | + return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 101 | + } else { |
|
| 102 | + return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 103 | + } |
|
| 104 | + }, $folderContent); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param string $path |
|
| 109 | + * @param FileInfo $info |
|
| 110 | + * @return File|Folder |
|
| 111 | + */ |
|
| 112 | + protected function createNode($path, FileInfo $info = null) { |
|
| 113 | + if (is_null($info)) { |
|
| 114 | + $isDir = $this->view->is_dir($path); |
|
| 115 | + } else { |
|
| 116 | + $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 117 | + } |
|
| 118 | + if ($isDir) { |
|
| 119 | + return new Folder($this->root, $this->view, $path, $info); |
|
| 120 | + } else { |
|
| 121 | + return new File($this->root, $this->view, $path, $info); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Get the node at $path |
|
| 127 | + * |
|
| 128 | + * @param string $path |
|
| 129 | + * @return \OC\Files\Node\Node |
|
| 130 | + * @throws \OCP\Files\NotFoundException |
|
| 131 | + */ |
|
| 132 | + public function get($path) { |
|
| 133 | + return $this->root->get($this->getFullPath($path)); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @param string $path |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + public function nodeExists($path) { |
|
| 141 | + try { |
|
| 142 | + $this->get($path); |
|
| 143 | + return true; |
|
| 144 | + } catch (NotFoundException $e) { |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @param string $path |
|
| 151 | + * @return \OC\Files\Node\Folder |
|
| 152 | + * @throws \OCP\Files\NotPermittedException |
|
| 153 | + */ |
|
| 154 | + public function newFolder($path) { |
|
| 155 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 156 | + $fullPath = $this->getFullPath($path); |
|
| 157 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 158 | + $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 159 | + $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 160 | + $this->view->mkdir($fullPath); |
|
| 161 | + $node = new Folder($this->root, $this->view, $fullPath); |
|
| 162 | + $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 163 | + $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 164 | + return $node; |
|
| 165 | + } else { |
|
| 166 | + throw new NotPermittedException('No create permission for folder'); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param string $path |
|
| 172 | + * @return \OC\Files\Node\File |
|
| 173 | + * @throws \OCP\Files\NotPermittedException |
|
| 174 | + */ |
|
| 175 | + public function newFile($path) { |
|
| 176 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 177 | + $fullPath = $this->getFullPath($path); |
|
| 178 | + $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 179 | + $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 180 | + $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 181 | + $this->view->touch($fullPath); |
|
| 182 | + $node = new File($this->root, $this->view, $fullPath); |
|
| 183 | + $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 184 | + $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 185 | + return $node; |
|
| 186 | + } else { |
|
| 187 | + throw new NotPermittedException('No create permission for path'); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * search for files with the name matching $query |
|
| 193 | + * |
|
| 194 | + * @param string|ISearchOperator $query |
|
| 195 | + * @return \OC\Files\Node\Node[] |
|
| 196 | + */ |
|
| 197 | + public function search($query) { |
|
| 198 | + if (is_string($query)) { |
|
| 199 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 200 | + } else { |
|
| 201 | + return $this->searchCommon('searchQuery', array($query)); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * search for files by mimetype |
|
| 207 | + * |
|
| 208 | + * @param string $mimetype |
|
| 209 | + * @return Node[] |
|
| 210 | + */ |
|
| 211 | + public function searchByMime($mimetype) { |
|
| 212 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * search for files by tag |
|
| 217 | + * |
|
| 218 | + * @param string|int $tag name or tag id |
|
| 219 | + * @param string $userId owner of the tags |
|
| 220 | + * @return Node[] |
|
| 221 | + */ |
|
| 222 | + public function searchByTag($tag, $userId) { |
|
| 223 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param string $method cache method |
|
| 228 | + * @param array $args call args |
|
| 229 | + * @return \OC\Files\Node\Node[] |
|
| 230 | + */ |
|
| 231 | + private function searchCommon($method, $args) { |
|
| 232 | + $files = array(); |
|
| 233 | + $rootLength = strlen($this->path); |
|
| 234 | + $mount = $this->root->getMount($this->path); |
|
| 235 | + $storage = $mount->getStorage(); |
|
| 236 | + $internalPath = $mount->getInternalPath($this->path); |
|
| 237 | + $internalPath = rtrim($internalPath, '/'); |
|
| 238 | + if ($internalPath !== '') { |
|
| 239 | + $internalPath = $internalPath . '/'; |
|
| 240 | + } |
|
| 241 | + $internalRootLength = strlen($internalPath); |
|
| 242 | + |
|
| 243 | + $cache = $storage->getCache(''); |
|
| 244 | + |
|
| 245 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 246 | + foreach ($results as $result) { |
|
| 247 | + if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { |
|
| 248 | + $result['internalPath'] = $result['path']; |
|
| 249 | + $result['path'] = substr($result['path'], $internalRootLength); |
|
| 250 | + $result['storage'] = $storage; |
|
| 251 | + $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 256 | + foreach ($mounts as $mount) { |
|
| 257 | + $storage = $mount->getStorage(); |
|
| 258 | + if ($storage) { |
|
| 259 | + $cache = $storage->getCache(''); |
|
| 260 | + |
|
| 261 | + $relativeMountPoint = substr($mount->getMountPoint(), $rootLength); |
|
| 262 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 263 | + foreach ($results as $result) { |
|
| 264 | + $result['internalPath'] = $result['path']; |
|
| 265 | + $result['path'] = $relativeMountPoint . $result['path']; |
|
| 266 | + $result['storage'] = $storage; |
|
| 267 | + $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return array_map(function (FileInfo $file) { |
|
| 273 | + return $this->createNode($file->getPath(), $file); |
|
| 274 | + }, $files); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @param int $id |
|
| 279 | + * @return \OC\Files\Node\Node[] |
|
| 280 | + */ |
|
| 281 | + public function getById($id) { |
|
| 282 | + $mountCache = $this->root->getUserMountCache(); |
|
| 283 | + if (strpos($this->getPath(), '/', 1) > 0) { |
|
| 284 | + list(, $user) = explode('/', $this->getPath()); |
|
| 285 | + } else { |
|
| 286 | + $user = null; |
|
| 287 | + } |
|
| 288 | + $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 289 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 290 | + $mounts[] = $this->root->getMount($this->path); |
|
| 291 | + /** @var IMountPoint[] $folderMounts */ |
|
| 292 | + $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 293 | + return $mountPoint->getMountPoint(); |
|
| 294 | + }, $mounts), $mounts); |
|
| 295 | + |
|
| 296 | + /** @var ICachedMountInfo[] $mountsContainingFile */ |
|
| 297 | + $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 298 | + return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
|
| 299 | + })); |
|
| 300 | + |
|
| 301 | + if (count($mountsContainingFile) === 0) { |
|
| 302 | + return []; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) { |
|
| 306 | + $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
|
| 307 | + $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 308 | + if (!$cacheEntry) { |
|
| 309 | + return null; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + // cache jails will hide the "true" internal path |
|
| 313 | + $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 314 | + $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
|
| 315 | + $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 316 | + $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount; |
|
| 317 | + return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
|
| 318 | + $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
| 319 | + \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
| 320 | + )); |
|
| 321 | + }, $mountsContainingFile); |
|
| 322 | + |
|
| 323 | + $nodes = array_filter($nodes); |
|
| 324 | + |
|
| 325 | + return array_filter($nodes, function (Node $node) { |
|
| 326 | + return $this->getRelativePath($node->getPath()); |
|
| 327 | + }); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + public function getFreeSpace() { |
|
| 331 | + return $this->view->free_space($this->path); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + public function delete() { |
|
| 335 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 336 | + $this->sendHooks(array('preDelete')); |
|
| 337 | + $fileInfo = $this->getFileInfo(); |
|
| 338 | + $this->view->rmdir($this->path); |
|
| 339 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 340 | + $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 341 | + $this->exists = false; |
|
| 342 | + } else { |
|
| 343 | + throw new NotPermittedException('No delete permission for path'); |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Add a suffix to the name in case the file exists |
|
| 349 | + * |
|
| 350 | + * @param string $name |
|
| 351 | + * @return string |
|
| 352 | + * @throws NotPermittedException |
|
| 353 | + */ |
|
| 354 | + public function getNonExistingName($name) { |
|
| 355 | + $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 356 | + return trim($this->getRelativePath($uniqueName), '/'); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @param int $limit |
|
| 361 | + * @param int $offset |
|
| 362 | + * @return \OCP\Files\Node[] |
|
| 363 | + */ |
|
| 364 | + public function getRecent($limit, $offset = 0) { |
|
| 365 | + $mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 366 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 367 | + $mounts[] = $this->getMountPoint(); |
|
| 368 | + |
|
| 369 | + $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 370 | + return $mount->getStorage(); |
|
| 371 | + }); |
|
| 372 | + $storageIds = array_map(function (IMountPoint $mount) { |
|
| 373 | + return $mount->getStorage()->getCache()->getNumericStorageId(); |
|
| 374 | + }, $mounts); |
|
| 375 | + /** @var IMountPoint[] $mountMap */ |
|
| 376 | + $mountMap = array_combine($storageIds, $mounts); |
|
| 377 | + $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER); |
|
| 378 | + |
|
| 379 | + //todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc) |
|
| 380 | + |
|
| 381 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 382 | + $query = $builder |
|
| 383 | + ->select('f.*') |
|
| 384 | + ->from('filecache', 'f') |
|
| 385 | + ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 386 | + ->andWhere($builder->expr()->orX( |
|
| 387 | + // handle non empty folders separate |
|
| 388 | + $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), |
|
| 389 | + $builder->expr()->eq('f.size', new Literal(0)) |
|
| 390 | + )) |
|
| 391 | + ->orderBy('f.mtime', 'DESC') |
|
| 392 | + ->setMaxResults($limit) |
|
| 393 | + ->setFirstResult($offset); |
|
| 394 | + |
|
| 395 | + $result = $query->execute()->fetchAll(); |
|
| 396 | + |
|
| 397 | + $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 398 | + $mount = $mountMap[$entry['storage']]; |
|
| 399 | + $entry['internalPath'] = $entry['path']; |
|
| 400 | + $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
|
| 401 | + $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']); |
|
| 402 | + $path = $this->getAbsolutePath($mount, $entry['path']); |
|
| 403 | + if (is_null($path)) { |
|
| 404 | + return null; |
|
| 405 | + } |
|
| 406 | + $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount); |
|
| 407 | + return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
|
| 408 | + }, $result)); |
|
| 409 | + |
|
| 410 | + return array_values(array_filter($files, function (Node $node) { |
|
| 411 | + $relative = $this->getRelativePath($node->getPath()); |
|
| 412 | + return $relative !== null && $relative !== '/'; |
|
| 413 | + })); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + private function getAbsolutePath(IMountPoint $mount, $path) { |
|
| 417 | + $storage = $mount->getStorage(); |
|
| 418 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) { |
|
| 419 | + /** @var \OC\Files\Storage\Wrapper\Jail $storage */ |
|
| 420 | + $jailRoot = $storage->getUnjailedPath(''); |
|
| 421 | + $rootLength = strlen($jailRoot) + 1; |
|
| 422 | + if ($path === $jailRoot) { |
|
| 423 | + return $mount->getMountPoint(); |
|
| 424 | + } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 425 | + return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 426 | + } else { |
|
| 427 | + return null; |
|
| 428 | + } |
|
| 429 | + } else { |
|
| 430 | + return $mount->getMountPoint() . $path; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | 433 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | if (!$this->isValidPath($path)) { |
| 56 | 56 | throw new NotPermittedException('Invalid path'); |
| 57 | 57 | } |
| 58 | - return $this->path . $this->normalizePath($path); |
|
| 58 | + return $this->path.$this->normalizePath($path); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | if ($path === $this->path) { |
| 70 | 70 | return '/'; |
| 71 | - } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 71 | + } else if (strpos($path, $this->path.'/') !== 0) { |
|
| 72 | 72 | return null; |
| 73 | 73 | } else { |
| 74 | 74 | $path = substr($path, strlen($this->path)); |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * @return bool |
| 84 | 84 | */ |
| 85 | 85 | public function isSubNode($node) { |
| 86 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 86 | + return strpos($node->getPath(), $this->path.'/') === 0; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | public function getDirectoryListing() { |
| 96 | 96 | $folderContent = $this->view->getDirectoryContent($this->path); |
| 97 | 97 | |
| 98 | - return array_map(function (FileInfo $info) { |
|
| 98 | + return array_map(function(FileInfo $info) { |
|
| 99 | 99 | if ($info->getMimetype() === 'httpd/unix-directory') { |
| 100 | 100 | return new Folder($this->root, $this->view, $info->getPath(), $info); |
| 101 | 101 | } else { |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | public function search($query) { |
| 198 | 198 | if (is_string($query)) { |
| 199 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 199 | + return $this->searchCommon('search', array('%'.$query.'%')); |
|
| 200 | 200 | } else { |
| 201 | 201 | return $this->searchCommon('searchQuery', array($query)); |
| 202 | 202 | } |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | $internalPath = $mount->getInternalPath($this->path); |
| 237 | 237 | $internalPath = rtrim($internalPath, '/'); |
| 238 | 238 | if ($internalPath !== '') { |
| 239 | - $internalPath = $internalPath . '/'; |
|
| 239 | + $internalPath = $internalPath.'/'; |
|
| 240 | 240 | } |
| 241 | 241 | $internalRootLength = strlen($internalPath); |
| 242 | 242 | |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | $result['internalPath'] = $result['path']; |
| 249 | 249 | $result['path'] = substr($result['path'], $internalRootLength); |
| 250 | 250 | $result['storage'] = $storage; |
| 251 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 251 | + $files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
@@ -262,14 +262,14 @@ discard block |
||
| 262 | 262 | $results = call_user_func_array(array($cache, $method), $args); |
| 263 | 263 | foreach ($results as $result) { |
| 264 | 264 | $result['internalPath'] = $result['path']; |
| 265 | - $result['path'] = $relativeMountPoint . $result['path']; |
|
| 265 | + $result['path'] = $relativeMountPoint.$result['path']; |
|
| 266 | 266 | $result['storage'] = $storage; |
| 267 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 267 | + $files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 268 | 268 | } |
| 269 | 269 | } |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - return array_map(function (FileInfo $file) { |
|
| 272 | + return array_map(function(FileInfo $file) { |
|
| 273 | 273 | return $this->createNode($file->getPath(), $file); |
| 274 | 274 | }, $files); |
| 275 | 275 | } |
@@ -285,16 +285,16 @@ discard block |
||
| 285 | 285 | } else { |
| 286 | 286 | $user = null; |
| 287 | 287 | } |
| 288 | - $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 288 | + $mountsContainingFile = $mountCache->getMountsForFileId((int) $id, $user); |
|
| 289 | 289 | $mounts = $this->root->getMountsIn($this->path); |
| 290 | 290 | $mounts[] = $this->root->getMount($this->path); |
| 291 | 291 | /** @var IMountPoint[] $folderMounts */ |
| 292 | - $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 292 | + $folderMounts = array_combine(array_map(function(IMountPoint $mountPoint) { |
|
| 293 | 293 | return $mountPoint->getMountPoint(); |
| 294 | 294 | }, $mounts), $mounts); |
| 295 | 295 | |
| 296 | 296 | /** @var ICachedMountInfo[] $mountsContainingFile */ |
| 297 | - $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 297 | + $mountsContainingFile = array_values(array_filter($mountsContainingFile, function(ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 298 | 298 | return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
| 299 | 299 | })); |
| 300 | 300 | |
@@ -302,18 +302,18 @@ discard block |
||
| 302 | 302 | return []; |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) { |
|
| 305 | + $nodes = array_map(function(ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) { |
|
| 306 | 306 | $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
| 307 | - $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 307 | + $cacheEntry = $mount->getStorage()->getCache()->get((int) $id); |
|
| 308 | 308 | if (!$cacheEntry) { |
| 309 | 309 | return null; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | // cache jails will hide the "true" internal path |
| 313 | - $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 313 | + $internalPath = ltrim($cachedMountInfo->getRootInternalPath().'/'.$cacheEntry->getPath(), '/'); |
|
| 314 | 314 | $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
| 315 | 315 | $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
| 316 | - $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount; |
|
| 316 | + $absolutePath = $cachedMountInfo->getMountPoint().$pathRelativeToMount; |
|
| 317 | 317 | return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
| 318 | 318 | $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
| 319 | 319 | \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | |
| 323 | 323 | $nodes = array_filter($nodes); |
| 324 | 324 | |
| 325 | - return array_filter($nodes, function (Node $node) { |
|
| 325 | + return array_filter($nodes, function(Node $node) { |
|
| 326 | 326 | return $this->getRelativePath($node->getPath()); |
| 327 | 327 | }); |
| 328 | 328 | } |
@@ -366,10 +366,10 @@ discard block |
||
| 366 | 366 | $mounts = $this->root->getMountsIn($this->path); |
| 367 | 367 | $mounts[] = $this->getMountPoint(); |
| 368 | 368 | |
| 369 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 369 | + $mounts = array_filter($mounts, function(IMountPoint $mount) { |
|
| 370 | 370 | return $mount->getStorage(); |
| 371 | 371 | }); |
| 372 | - $storageIds = array_map(function (IMountPoint $mount) { |
|
| 372 | + $storageIds = array_map(function(IMountPoint $mount) { |
|
| 373 | 373 | return $mount->getStorage()->getCache()->getNumericStorageId(); |
| 374 | 374 | }, $mounts); |
| 375 | 375 | /** @var IMountPoint[] $mountMap */ |
@@ -394,7 +394,7 @@ discard block |
||
| 394 | 394 | |
| 395 | 395 | $result = $query->execute()->fetchAll(); |
| 396 | 396 | |
| 397 | - $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 397 | + $files = array_filter(array_map(function(array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 398 | 398 | $mount = $mountMap[$entry['storage']]; |
| 399 | 399 | $entry['internalPath'] = $entry['path']; |
| 400 | 400 | $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
| 408 | 408 | }, $result)); |
| 409 | 409 | |
| 410 | - return array_values(array_filter($files, function (Node $node) { |
|
| 410 | + return array_values(array_filter($files, function(Node $node) { |
|
| 411 | 411 | $relative = $this->getRelativePath($node->getPath()); |
| 412 | 412 | return $relative !== null && $relative !== '/'; |
| 413 | 413 | })); |
@@ -421,13 +421,13 @@ discard block |
||
| 421 | 421 | $rootLength = strlen($jailRoot) + 1; |
| 422 | 422 | if ($path === $jailRoot) { |
| 423 | 423 | return $mount->getMountPoint(); |
| 424 | - } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 425 | - return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 424 | + } else if (substr($path, 0, $rootLength) === $jailRoot.'/') { |
|
| 425 | + return $mount->getMountPoint().substr($path, $rootLength); |
|
| 426 | 426 | } else { |
| 427 | 427 | return null; |
| 428 | 428 | } |
| 429 | 429 | } else { |
| 430 | - return $mount->getMountPoint() . $path; |
|
| 430 | + return $mount->getMountPoint().$path; |
|
| 431 | 431 | } |
| 432 | 432 | } |
| 433 | 433 | } |
@@ -33,6 +33,7 @@ |
||
| 33 | 33 | * |
| 34 | 34 | * @returns string |
| 35 | 35 | * @since 12 |
| 36 | + * @return string |
|
| 36 | 37 | */ |
| 37 | 38 | public function getIcon(); |
| 38 | 39 | } |
@@ -27,12 +27,12 @@ |
||
| 27 | 27 | * @since 12 |
| 28 | 28 | */ |
| 29 | 29 | interface IIconSection extends ISection { |
| 30 | - /** |
|
| 31 | - * returns the relative path to an 16*16 icon describing the section. |
|
| 32 | - * e.g. '/core/img/places/files.svg' |
|
| 33 | - * |
|
| 34 | - * @returns string |
|
| 35 | - * @since 12 |
|
| 36 | - */ |
|
| 37 | - public function getIcon(); |
|
| 30 | + /** |
|
| 31 | + * returns the relative path to an 16*16 icon describing the section. |
|
| 32 | + * e.g. '/core/img/places/files.svg' |
|
| 33 | + * |
|
| 34 | + * @returns string |
|
| 35 | + * @since 12 |
|
| 36 | + */ |
|
| 37 | + public function getIcon(); |
|
| 38 | 38 | } |
@@ -139,6 +139,9 @@ |
||
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | + /** |
|
| 143 | + * @param string $path |
|
| 144 | + */ |
|
| 142 | 145 | public function writeBack($tmpFile, $path) { |
| 143 | 146 | $this->uploadFile($tmpFile, $path); |
| 144 | 147 | unlink($tmpFile); |
@@ -93,8 +93,7 @@ |
||
| 93 | 93 | public function unlink($path) { |
| 94 | 94 | if ($this->is_dir($path)) { |
| 95 | 95 | return $this->rmdir($path); |
| 96 | - } |
|
| 97 | - else { |
|
| 96 | + } else { |
|
| 98 | 97 | $url = $this->constructUrl($path); |
| 99 | 98 | $result = unlink($url); |
| 100 | 99 | clearstatcache(true, $url); |
@@ -37,122 +37,122 @@ |
||
| 37 | 37 | use Icewind\Streams\RetryWrapper; |
| 38 | 38 | |
| 39 | 39 | class FTP extends StreamWrapper{ |
| 40 | - private $password; |
|
| 41 | - private $user; |
|
| 42 | - private $host; |
|
| 43 | - private $secure; |
|
| 44 | - private $root; |
|
| 40 | + private $password; |
|
| 41 | + private $user; |
|
| 42 | + private $host; |
|
| 43 | + private $secure; |
|
| 44 | + private $root; |
|
| 45 | 45 | |
| 46 | - private static $tempFiles=array(); |
|
| 46 | + private static $tempFiles=array(); |
|
| 47 | 47 | |
| 48 | - public function __construct($params) { |
|
| 49 | - if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 50 | - $this->host=$params['host']; |
|
| 51 | - $this->user=$params['user']; |
|
| 52 | - $this->password=$params['password']; |
|
| 53 | - if (isset($params['secure'])) { |
|
| 54 | - $this->secure = $params['secure']; |
|
| 55 | - } else { |
|
| 56 | - $this->secure = false; |
|
| 57 | - } |
|
| 58 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | - if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | - $this->root='/'.$this->root; |
|
| 61 | - } |
|
| 62 | - if (substr($this->root, -1) !== '/') { |
|
| 63 | - $this->root .= '/'; |
|
| 64 | - } |
|
| 65 | - } else { |
|
| 66 | - throw new \Exception('Creating FTP storage failed'); |
|
| 67 | - } |
|
| 48 | + public function __construct($params) { |
|
| 49 | + if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 50 | + $this->host=$params['host']; |
|
| 51 | + $this->user=$params['user']; |
|
| 52 | + $this->password=$params['password']; |
|
| 53 | + if (isset($params['secure'])) { |
|
| 54 | + $this->secure = $params['secure']; |
|
| 55 | + } else { |
|
| 56 | + $this->secure = false; |
|
| 57 | + } |
|
| 58 | + $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | + if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | + $this->root='/'.$this->root; |
|
| 61 | + } |
|
| 62 | + if (substr($this->root, -1) !== '/') { |
|
| 63 | + $this->root .= '/'; |
|
| 64 | + } |
|
| 65 | + } else { |
|
| 66 | + throw new \Exception('Creating FTP storage failed'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public function getId(){ |
|
| 72 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | - } |
|
| 71 | + public function getId(){ |
|
| 72 | + return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * construct the ftp url |
|
| 77 | - * @param string $path |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public function constructUrl($path) { |
|
| 81 | - $url='ftp'; |
|
| 82 | - if ($this->secure) { |
|
| 83 | - $url.='s'; |
|
| 84 | - } |
|
| 85 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | - return $url; |
|
| 87 | - } |
|
| 75 | + /** |
|
| 76 | + * construct the ftp url |
|
| 77 | + * @param string $path |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public function constructUrl($path) { |
|
| 81 | + $url='ftp'; |
|
| 82 | + if ($this->secure) { |
|
| 83 | + $url.='s'; |
|
| 84 | + } |
|
| 85 | + $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | + return $url; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Unlinks file or directory |
|
| 91 | - * @param string $path |
|
| 92 | - */ |
|
| 93 | - public function unlink($path) { |
|
| 94 | - if ($this->is_dir($path)) { |
|
| 95 | - return $this->rmdir($path); |
|
| 96 | - } |
|
| 97 | - else { |
|
| 98 | - $url = $this->constructUrl($path); |
|
| 99 | - $result = unlink($url); |
|
| 100 | - clearstatcache(true, $url); |
|
| 101 | - return $result; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - public function fopen($path,$mode) { |
|
| 105 | - switch($mode) { |
|
| 106 | - case 'r': |
|
| 107 | - case 'rb': |
|
| 108 | - case 'w': |
|
| 109 | - case 'wb': |
|
| 110 | - case 'a': |
|
| 111 | - case 'ab': |
|
| 112 | - //these are supported by the wrapper |
|
| 113 | - $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | - return RetryWrapper::wrap($handle); |
|
| 116 | - case 'r+': |
|
| 117 | - case 'w+': |
|
| 118 | - case 'wb+': |
|
| 119 | - case 'a+': |
|
| 120 | - case 'x': |
|
| 121 | - case 'x+': |
|
| 122 | - case 'c': |
|
| 123 | - case 'c+': |
|
| 124 | - //emulate these |
|
| 125 | - if (strrpos($path, '.')!==false) { |
|
| 126 | - $ext=substr($path, strrpos($path, '.')); |
|
| 127 | - } else { |
|
| 128 | - $ext=''; |
|
| 129 | - } |
|
| 130 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | - if ($this->file_exists($path)) { |
|
| 132 | - $this->getFile($path, $tmpFile); |
|
| 133 | - } |
|
| 134 | - $handle = fopen($tmpFile, $mode); |
|
| 135 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | - $this->writeBack($tmpFile, $path); |
|
| 137 | - }); |
|
| 138 | - } |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 89 | + /** |
|
| 90 | + * Unlinks file or directory |
|
| 91 | + * @param string $path |
|
| 92 | + */ |
|
| 93 | + public function unlink($path) { |
|
| 94 | + if ($this->is_dir($path)) { |
|
| 95 | + return $this->rmdir($path); |
|
| 96 | + } |
|
| 97 | + else { |
|
| 98 | + $url = $this->constructUrl($path); |
|
| 99 | + $result = unlink($url); |
|
| 100 | + clearstatcache(true, $url); |
|
| 101 | + return $result; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + public function fopen($path,$mode) { |
|
| 105 | + switch($mode) { |
|
| 106 | + case 'r': |
|
| 107 | + case 'rb': |
|
| 108 | + case 'w': |
|
| 109 | + case 'wb': |
|
| 110 | + case 'a': |
|
| 111 | + case 'ab': |
|
| 112 | + //these are supported by the wrapper |
|
| 113 | + $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | + return RetryWrapper::wrap($handle); |
|
| 116 | + case 'r+': |
|
| 117 | + case 'w+': |
|
| 118 | + case 'wb+': |
|
| 119 | + case 'a+': |
|
| 120 | + case 'x': |
|
| 121 | + case 'x+': |
|
| 122 | + case 'c': |
|
| 123 | + case 'c+': |
|
| 124 | + //emulate these |
|
| 125 | + if (strrpos($path, '.')!==false) { |
|
| 126 | + $ext=substr($path, strrpos($path, '.')); |
|
| 127 | + } else { |
|
| 128 | + $ext=''; |
|
| 129 | + } |
|
| 130 | + $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | + if ($this->file_exists($path)) { |
|
| 132 | + $this->getFile($path, $tmpFile); |
|
| 133 | + } |
|
| 134 | + $handle = fopen($tmpFile, $mode); |
|
| 135 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | + $this->writeBack($tmpFile, $path); |
|
| 137 | + }); |
|
| 138 | + } |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - public function writeBack($tmpFile, $path) { |
|
| 143 | - $this->uploadFile($tmpFile, $path); |
|
| 144 | - unlink($tmpFile); |
|
| 145 | - } |
|
| 142 | + public function writeBack($tmpFile, $path) { |
|
| 143 | + $this->uploadFile($tmpFile, $path); |
|
| 144 | + unlink($tmpFile); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * check if php-ftp is installed |
|
| 149 | - */ |
|
| 150 | - public static function checkDependencies() { |
|
| 151 | - if (function_exists('ftp_login')) { |
|
| 152 | - return(true); |
|
| 153 | - } else { |
|
| 154 | - return array('ftp'); |
|
| 155 | - } |
|
| 156 | - } |
|
| 147 | + /** |
|
| 148 | + * check if php-ftp is installed |
|
| 149 | + */ |
|
| 150 | + public static function checkDependencies() { |
|
| 151 | + if (function_exists('ftp_login')) { |
|
| 152 | + return(true); |
|
| 153 | + } else { |
|
| 154 | + return array('ftp'); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | } |
@@ -36,28 +36,28 @@ discard block |
||
| 36 | 36 | use Icewind\Streams\CallbackWrapper; |
| 37 | 37 | use Icewind\Streams\RetryWrapper; |
| 38 | 38 | |
| 39 | -class FTP extends StreamWrapper{ |
|
| 39 | +class FTP extends StreamWrapper { |
|
| 40 | 40 | private $password; |
| 41 | 41 | private $user; |
| 42 | 42 | private $host; |
| 43 | 43 | private $secure; |
| 44 | 44 | private $root; |
| 45 | 45 | |
| 46 | - private static $tempFiles=array(); |
|
| 46 | + private static $tempFiles = array(); |
|
| 47 | 47 | |
| 48 | 48 | public function __construct($params) { |
| 49 | 49 | if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
| 50 | - $this->host=$params['host']; |
|
| 51 | - $this->user=$params['user']; |
|
| 52 | - $this->password=$params['password']; |
|
| 50 | + $this->host = $params['host']; |
|
| 51 | + $this->user = $params['user']; |
|
| 52 | + $this->password = $params['password']; |
|
| 53 | 53 | if (isset($params['secure'])) { |
| 54 | 54 | $this->secure = $params['secure']; |
| 55 | 55 | } else { |
| 56 | 56 | $this->secure = false; |
| 57 | 57 | } |
| 58 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | - if ( ! $this->root || $this->root[0]!=='/') { |
|
| 60 | - $this->root='/'.$this->root; |
|
| 58 | + $this->root = isset($params['root']) ? $params['root'] : '/'; |
|
| 59 | + if (!$this->root || $this->root[0] !== '/') { |
|
| 60 | + $this->root = '/'.$this->root; |
|
| 61 | 61 | } |
| 62 | 62 | if (substr($this->root, -1) !== '/') { |
| 63 | 63 | $this->root .= '/'; |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function getId(){ |
|
| 72 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 71 | + public function getId() { |
|
| 72 | + return 'ftp::'.$this->user.'@'.$this->host.'/'.$this->root; |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -78,11 +78,11 @@ discard block |
||
| 78 | 78 | * @return string |
| 79 | 79 | */ |
| 80 | 80 | public function constructUrl($path) { |
| 81 | - $url='ftp'; |
|
| 81 | + $url = 'ftp'; |
|
| 82 | 82 | if ($this->secure) { |
| 83 | - $url.='s'; |
|
| 83 | + $url .= 's'; |
|
| 84 | 84 | } |
| 85 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 85 | + $url .= '://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | 86 | return $url; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | return $result; |
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | - public function fopen($path,$mode) { |
|
| 105 | - switch($mode) { |
|
| 104 | + public function fopen($path, $mode) { |
|
| 105 | + switch ($mode) { |
|
| 106 | 106 | case 'r': |
| 107 | 107 | case 'rb': |
| 108 | 108 | case 'w': |
@@ -122,17 +122,17 @@ discard block |
||
| 122 | 122 | case 'c': |
| 123 | 123 | case 'c+': |
| 124 | 124 | //emulate these |
| 125 | - if (strrpos($path, '.')!==false) { |
|
| 126 | - $ext=substr($path, strrpos($path, '.')); |
|
| 125 | + if (strrpos($path, '.') !== false) { |
|
| 126 | + $ext = substr($path, strrpos($path, '.')); |
|
| 127 | 127 | } else { |
| 128 | - $ext=''; |
|
| 128 | + $ext = ''; |
|
| 129 | 129 | } |
| 130 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 130 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 131 | 131 | if ($this->file_exists($path)) { |
| 132 | 132 | $this->getFile($path, $tmpFile); |
| 133 | 133 | } |
| 134 | 134 | $handle = fopen($tmpFile, $mode); |
| 135 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 135 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 136 | 136 | $this->writeBack($tmpFile, $path); |
| 137 | 137 | }); |
| 138 | 138 | } |