@@ -25,48 +25,48 @@ |
||
| 25 | 25 | * @since 8.2.0 |
| 26 | 26 | */ |
| 27 | 27 | interface IFactory { |
| 28 | - /** |
|
| 29 | - * Get a language instance |
|
| 30 | - * |
|
| 31 | - * @param string $app |
|
| 32 | - * @param string|null $lang |
|
| 33 | - * @return \OCP\IL10N |
|
| 34 | - * @since 8.2.0 |
|
| 35 | - */ |
|
| 36 | - public function get($app, $lang = null); |
|
| 28 | + /** |
|
| 29 | + * Get a language instance |
|
| 30 | + * |
|
| 31 | + * @param string $app |
|
| 32 | + * @param string|null $lang |
|
| 33 | + * @return \OCP\IL10N |
|
| 34 | + * @since 8.2.0 |
|
| 35 | + */ |
|
| 36 | + public function get($app, $lang = null); |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Find the best language |
|
| 40 | - * |
|
| 41 | - * @param string|null $app App id or null for core |
|
| 42 | - * @return string language If nothing works it returns 'en' |
|
| 43 | - * @since 9.0.0 |
|
| 44 | - */ |
|
| 45 | - public function findLanguage($app = null); |
|
| 38 | + /** |
|
| 39 | + * Find the best language |
|
| 40 | + * |
|
| 41 | + * @param string|null $app App id or null for core |
|
| 42 | + * @return string language If nothing works it returns 'en' |
|
| 43 | + * @since 9.0.0 |
|
| 44 | + */ |
|
| 45 | + public function findLanguage($app = null); |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Find all available languages for an app |
|
| 49 | - * |
|
| 50 | - * @param string|null $app App id or null for core |
|
| 51 | - * @return string[] an array of available languages |
|
| 52 | - * @since 9.0.0 |
|
| 53 | - */ |
|
| 54 | - public function findAvailableLanguages($app = null); |
|
| 47 | + /** |
|
| 48 | + * Find all available languages for an app |
|
| 49 | + * |
|
| 50 | + * @param string|null $app App id or null for core |
|
| 51 | + * @return string[] an array of available languages |
|
| 52 | + * @since 9.0.0 |
|
| 53 | + */ |
|
| 54 | + public function findAvailableLanguages($app = null); |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @param string|null $app App id or null for core |
|
| 58 | - * @param string $lang |
|
| 59 | - * @return bool |
|
| 60 | - * @since 9.0.0 |
|
| 61 | - */ |
|
| 62 | - public function languageExists($app, $lang); |
|
| 56 | + /** |
|
| 57 | + * @param string|null $app App id or null for core |
|
| 58 | + * @param string $lang |
|
| 59 | + * @return bool |
|
| 60 | + * @since 9.0.0 |
|
| 61 | + */ |
|
| 62 | + public function languageExists($app, $lang); |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Creates a function from the plural string |
|
| 66 | - * |
|
| 67 | - * @param string $string |
|
| 68 | - * @return string Unique function name |
|
| 69 | - * @since 9.0.0 |
|
| 70 | - */ |
|
| 71 | - public function createPluralFunction($string); |
|
| 64 | + /** |
|
| 65 | + * Creates a function from the plural string |
|
| 66 | + * |
|
| 67 | + * @param string $string |
|
| 68 | + * @return string Unique function name |
|
| 69 | + * @since 9.0.0 |
|
| 70 | + */ |
|
| 71 | + public function createPluralFunction($string); |
|
| 72 | 72 | } |
@@ -40,373 +40,373 @@ |
||
| 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 | - if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 103 | - $lang = $this->findLanguage($app); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if (!isset($this->instances[$lang][$app])) { |
|
| 107 | - $this->instances[$lang][$app] = new L10N( |
|
| 108 | - $this, $app, $lang, |
|
| 109 | - $this->getL10nFilesForApp($app, $lang) |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $this->instances[$lang][$app]; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Find the best language |
|
| 118 | - * |
|
| 119 | - * @param string|null $app App id or null for core |
|
| 120 | - * @return string language If nothing works it returns 'en' |
|
| 121 | - */ |
|
| 122 | - public function findLanguage($app = null) { |
|
| 123 | - if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 124 | - return $this->requestLanguage; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * At this point ownCloud might not yet be installed and thus the lookup |
|
| 129 | - * in the preferences table might fail. For this reason we need to check |
|
| 130 | - * whether the instance has already been installed |
|
| 131 | - * |
|
| 132 | - * @link https://github.com/owncloud/core/issues/21955 |
|
| 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)) { |
|
| 137 | - $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 138 | - } else { |
|
| 139 | - $userLang = null; |
|
| 140 | - } |
|
| 141 | - } else { |
|
| 142 | - $userId = null; |
|
| 143 | - $userLang = null; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - if ($userLang) { |
|
| 147 | - $this->requestLanguage = $userLang; |
|
| 148 | - if ($this->languageExists($app, $userLang)) { |
|
| 149 | - return $userLang; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - try { |
|
| 154 | - // Try to get the language from the Request |
|
| 155 | - $lang = $this->getLanguageFromRequest($app); |
|
| 156 | - if ($userId !== null && $app === null && !$userLang) { |
|
| 157 | - $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 158 | - } |
|
| 159 | - return $lang; |
|
| 160 | - } catch (LanguageNotFoundException $e) { |
|
| 161 | - // Finding language from request failed fall back to default language |
|
| 162 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 163 | - if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 164 | - return $defaultLanguage; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // We could not find any language so fall back to english |
|
| 169 | - return 'en'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Find all available languages for an app |
|
| 174 | - * |
|
| 175 | - * @param string|null $app App id or null for core |
|
| 176 | - * @return array an array of available languages |
|
| 177 | - */ |
|
| 178 | - public function findAvailableLanguages($app = null) { |
|
| 179 | - $key = $app; |
|
| 180 | - if ($key === null) { |
|
| 181 | - $key = 'null'; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // also works with null as key |
|
| 185 | - if (!empty($this->availableLanguages[$key])) { |
|
| 186 | - return $this->availableLanguages[$key]; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $available = ['en']; //english is always available |
|
| 190 | - $dir = $this->findL10nDir($app); |
|
| 191 | - if (is_dir($dir)) { |
|
| 192 | - $files = scandir($dir); |
|
| 193 | - if ($files !== false) { |
|
| 194 | - foreach ($files as $file) { |
|
| 195 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 196 | - $available[] = substr($file, 0, -5); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // merge with translations from theme |
|
| 203 | - $theme = $this->config->getSystemValue('theme'); |
|
| 204 | - if (!empty($theme)) { |
|
| 205 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 206 | - |
|
| 207 | - if (is_dir($themeDir)) { |
|
| 208 | - $files = scandir($themeDir); |
|
| 209 | - if ($files !== false) { |
|
| 210 | - foreach ($files as $file) { |
|
| 211 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 212 | - $available[] = substr($file, 0, -5); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $this->availableLanguages[$key] = $available; |
|
| 220 | - return $available; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * @param string|null $app App id or null for core |
|
| 225 | - * @param string $lang |
|
| 226 | - * @return bool |
|
| 227 | - */ |
|
| 228 | - public function languageExists($app, $lang) { |
|
| 229 | - if ($lang === 'en') {//english is always available |
|
| 230 | - return true; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $languages = $this->findAvailableLanguages($app); |
|
| 234 | - return array_search($lang, $languages) !== false; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @param string|null $app |
|
| 239 | - * @return string |
|
| 240 | - * @throws LanguageNotFoundException |
|
| 241 | - */ |
|
| 242 | - private function getLanguageFromRequest($app) { |
|
| 243 | - $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 244 | - if ($header) { |
|
| 245 | - $available = $this->findAvailableLanguages($app); |
|
| 246 | - |
|
| 247 | - // E.g. make sure that 'de' is before 'de_DE'. |
|
| 248 | - sort($available); |
|
| 249 | - |
|
| 250 | - $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 251 | - foreach ($preferences as $preference) { |
|
| 252 | - list($preferred_language) = explode(';', $preference); |
|
| 253 | - $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 254 | - |
|
| 255 | - foreach ($available as $available_language) { |
|
| 256 | - if ($preferred_language === strtolower($available_language)) { |
|
| 257 | - return $available_language; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - // Fallback from de_De to de |
|
| 262 | - foreach ($available as $available_language) { |
|
| 263 | - if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 264 | - return $available_language; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - throw new LanguageNotFoundException(); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Checks if $sub is a subdirectory of $parent |
|
| 275 | - * |
|
| 276 | - * @param string $sub |
|
| 277 | - * @param string $parent |
|
| 278 | - * @return bool |
|
| 279 | - */ |
|
| 280 | - private function isSubDirectory($sub, $parent) { |
|
| 281 | - // Check whether $sub contains no ".." |
|
| 282 | - if(strpos($sub, '..') !== false) { |
|
| 283 | - return false; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - // Check whether $sub is a subdirectory of $parent |
|
| 287 | - if (strpos($sub, $parent) === 0) { |
|
| 288 | - return true; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - return false; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Get a list of language files that should be loaded |
|
| 296 | - * |
|
| 297 | - * @param string $app |
|
| 298 | - * @param string $lang |
|
| 299 | - * @return string[] |
|
| 300 | - */ |
|
| 301 | - // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 302 | - // FIXME This is also the reason, why it is not in the public interface |
|
| 303 | - public function getL10nFilesForApp($app, $lang) { |
|
| 304 | - $languageFiles = []; |
|
| 305 | - |
|
| 306 | - $i18nDir = $this->findL10nDir($app); |
|
| 307 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 308 | - |
|
| 309 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 310 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 311 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 312 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 313 | - ) |
|
| 314 | - && file_exists($transFile)) { |
|
| 315 | - // load the translations file |
|
| 316 | - $languageFiles[] = $transFile; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // merge with translations from theme |
|
| 320 | - $theme = $this->config->getSystemValue('theme'); |
|
| 321 | - if (!empty($theme)) { |
|
| 322 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 323 | - if (file_exists($transFile)) { |
|
| 324 | - $languageFiles[] = $transFile; |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return $languageFiles; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * find the l10n directory |
|
| 333 | - * |
|
| 334 | - * @param string $app App id or empty string for core |
|
| 335 | - * @return string directory |
|
| 336 | - */ |
|
| 337 | - protected function findL10nDir($app = null) { |
|
| 338 | - if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 339 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 340 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 341 | - } |
|
| 342 | - } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 343 | - // Check if the app is in the app folder |
|
| 344 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 345 | - } |
|
| 346 | - return $this->serverRoot . '/core/l10n/'; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Creates a function from the plural string |
|
| 352 | - * |
|
| 353 | - * Parts of the code is copied from Habari: |
|
| 354 | - * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 355 | - * @param string $string |
|
| 356 | - * @return string |
|
| 357 | - */ |
|
| 358 | - public function createPluralFunction($string) { |
|
| 359 | - if (isset($this->pluralFunctions[$string])) { |
|
| 360 | - return $this->pluralFunctions[$string]; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 364 | - // sanitize |
|
| 365 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 366 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 367 | - |
|
| 368 | - $body = str_replace( |
|
| 369 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 370 | - array( '$plural', '$n', '$nplurals', ), |
|
| 371 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 372 | - ); |
|
| 373 | - |
|
| 374 | - // add parents |
|
| 375 | - // important since PHP's ternary evaluates from left to right |
|
| 376 | - $body .= ';'; |
|
| 377 | - $res = ''; |
|
| 378 | - $p = 0; |
|
| 379 | - for($i = 0; $i < strlen($body); $i++) { |
|
| 380 | - $ch = $body[$i]; |
|
| 381 | - switch ( $ch ) { |
|
| 382 | - case '?': |
|
| 383 | - $res .= ' ? ('; |
|
| 384 | - $p++; |
|
| 385 | - break; |
|
| 386 | - case ':': |
|
| 387 | - $res .= ') : ('; |
|
| 388 | - break; |
|
| 389 | - case ';': |
|
| 390 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 391 | - $p = 0; |
|
| 392 | - break; |
|
| 393 | - default: |
|
| 394 | - $res .= $ch; |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 399 | - $function = create_function('$n', $body); |
|
| 400 | - $this->pluralFunctions[$string] = $function; |
|
| 401 | - return $function; |
|
| 402 | - } else { |
|
| 403 | - // default: one plural form for all cases but n==1 (english) |
|
| 404 | - $function = create_function( |
|
| 405 | - '$n', |
|
| 406 | - '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 407 | - ); |
|
| 408 | - $this->pluralFunctions[$string] = $function; |
|
| 409 | - return $function; |
|
| 410 | - } |
|
| 411 | - } |
|
| 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 | + if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 103 | + $lang = $this->findLanguage($app); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if (!isset($this->instances[$lang][$app])) { |
|
| 107 | + $this->instances[$lang][$app] = new L10N( |
|
| 108 | + $this, $app, $lang, |
|
| 109 | + $this->getL10nFilesForApp($app, $lang) |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $this->instances[$lang][$app]; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Find the best language |
|
| 118 | + * |
|
| 119 | + * @param string|null $app App id or null for core |
|
| 120 | + * @return string language If nothing works it returns 'en' |
|
| 121 | + */ |
|
| 122 | + public function findLanguage($app = null) { |
|
| 123 | + if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 124 | + return $this->requestLanguage; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * At this point ownCloud might not yet be installed and thus the lookup |
|
| 129 | + * in the preferences table might fail. For this reason we need to check |
|
| 130 | + * whether the instance has already been installed |
|
| 131 | + * |
|
| 132 | + * @link https://github.com/owncloud/core/issues/21955 |
|
| 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)) { |
|
| 137 | + $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 138 | + } else { |
|
| 139 | + $userLang = null; |
|
| 140 | + } |
|
| 141 | + } else { |
|
| 142 | + $userId = null; |
|
| 143 | + $userLang = null; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + if ($userLang) { |
|
| 147 | + $this->requestLanguage = $userLang; |
|
| 148 | + if ($this->languageExists($app, $userLang)) { |
|
| 149 | + return $userLang; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + try { |
|
| 154 | + // Try to get the language from the Request |
|
| 155 | + $lang = $this->getLanguageFromRequest($app); |
|
| 156 | + if ($userId !== null && $app === null && !$userLang) { |
|
| 157 | + $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 158 | + } |
|
| 159 | + return $lang; |
|
| 160 | + } catch (LanguageNotFoundException $e) { |
|
| 161 | + // Finding language from request failed fall back to default language |
|
| 162 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 163 | + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 164 | + return $defaultLanguage; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // We could not find any language so fall back to english |
|
| 169 | + return 'en'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Find all available languages for an app |
|
| 174 | + * |
|
| 175 | + * @param string|null $app App id or null for core |
|
| 176 | + * @return array an array of available languages |
|
| 177 | + */ |
|
| 178 | + public function findAvailableLanguages($app = null) { |
|
| 179 | + $key = $app; |
|
| 180 | + if ($key === null) { |
|
| 181 | + $key = 'null'; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // also works with null as key |
|
| 185 | + if (!empty($this->availableLanguages[$key])) { |
|
| 186 | + return $this->availableLanguages[$key]; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $available = ['en']; //english is always available |
|
| 190 | + $dir = $this->findL10nDir($app); |
|
| 191 | + if (is_dir($dir)) { |
|
| 192 | + $files = scandir($dir); |
|
| 193 | + if ($files !== false) { |
|
| 194 | + foreach ($files as $file) { |
|
| 195 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 196 | + $available[] = substr($file, 0, -5); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // merge with translations from theme |
|
| 203 | + $theme = $this->config->getSystemValue('theme'); |
|
| 204 | + if (!empty($theme)) { |
|
| 205 | + $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 206 | + |
|
| 207 | + if (is_dir($themeDir)) { |
|
| 208 | + $files = scandir($themeDir); |
|
| 209 | + if ($files !== false) { |
|
| 210 | + foreach ($files as $file) { |
|
| 211 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 212 | + $available[] = substr($file, 0, -5); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $this->availableLanguages[$key] = $available; |
|
| 220 | + return $available; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * @param string|null $app App id or null for core |
|
| 225 | + * @param string $lang |
|
| 226 | + * @return bool |
|
| 227 | + */ |
|
| 228 | + public function languageExists($app, $lang) { |
|
| 229 | + if ($lang === 'en') {//english is always available |
|
| 230 | + return true; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $languages = $this->findAvailableLanguages($app); |
|
| 234 | + return array_search($lang, $languages) !== false; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @param string|null $app |
|
| 239 | + * @return string |
|
| 240 | + * @throws LanguageNotFoundException |
|
| 241 | + */ |
|
| 242 | + private function getLanguageFromRequest($app) { |
|
| 243 | + $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 244 | + if ($header) { |
|
| 245 | + $available = $this->findAvailableLanguages($app); |
|
| 246 | + |
|
| 247 | + // E.g. make sure that 'de' is before 'de_DE'. |
|
| 248 | + sort($available); |
|
| 249 | + |
|
| 250 | + $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 251 | + foreach ($preferences as $preference) { |
|
| 252 | + list($preferred_language) = explode(';', $preference); |
|
| 253 | + $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 254 | + |
|
| 255 | + foreach ($available as $available_language) { |
|
| 256 | + if ($preferred_language === strtolower($available_language)) { |
|
| 257 | + return $available_language; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + // Fallback from de_De to de |
|
| 262 | + foreach ($available as $available_language) { |
|
| 263 | + if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 264 | + return $available_language; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + throw new LanguageNotFoundException(); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Checks if $sub is a subdirectory of $parent |
|
| 275 | + * |
|
| 276 | + * @param string $sub |
|
| 277 | + * @param string $parent |
|
| 278 | + * @return bool |
|
| 279 | + */ |
|
| 280 | + private function isSubDirectory($sub, $parent) { |
|
| 281 | + // Check whether $sub contains no ".." |
|
| 282 | + if(strpos($sub, '..') !== false) { |
|
| 283 | + return false; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + // Check whether $sub is a subdirectory of $parent |
|
| 287 | + if (strpos($sub, $parent) === 0) { |
|
| 288 | + return true; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + return false; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Get a list of language files that should be loaded |
|
| 296 | + * |
|
| 297 | + * @param string $app |
|
| 298 | + * @param string $lang |
|
| 299 | + * @return string[] |
|
| 300 | + */ |
|
| 301 | + // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 302 | + // FIXME This is also the reason, why it is not in the public interface |
|
| 303 | + public function getL10nFilesForApp($app, $lang) { |
|
| 304 | + $languageFiles = []; |
|
| 305 | + |
|
| 306 | + $i18nDir = $this->findL10nDir($app); |
|
| 307 | + $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 308 | + |
|
| 309 | + if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 310 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 311 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 312 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 313 | + ) |
|
| 314 | + && file_exists($transFile)) { |
|
| 315 | + // load the translations file |
|
| 316 | + $languageFiles[] = $transFile; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // merge with translations from theme |
|
| 320 | + $theme = $this->config->getSystemValue('theme'); |
|
| 321 | + if (!empty($theme)) { |
|
| 322 | + $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 323 | + if (file_exists($transFile)) { |
|
| 324 | + $languageFiles[] = $transFile; |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return $languageFiles; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * find the l10n directory |
|
| 333 | + * |
|
| 334 | + * @param string $app App id or empty string for core |
|
| 335 | + * @return string directory |
|
| 336 | + */ |
|
| 337 | + protected function findL10nDir($app = null) { |
|
| 338 | + if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 339 | + if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 340 | + return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 341 | + } |
|
| 342 | + } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 343 | + // Check if the app is in the app folder |
|
| 344 | + return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 345 | + } |
|
| 346 | + return $this->serverRoot . '/core/l10n/'; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Creates a function from the plural string |
|
| 352 | + * |
|
| 353 | + * Parts of the code is copied from Habari: |
|
| 354 | + * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 355 | + * @param string $string |
|
| 356 | + * @return string |
|
| 357 | + */ |
|
| 358 | + public function createPluralFunction($string) { |
|
| 359 | + if (isset($this->pluralFunctions[$string])) { |
|
| 360 | + return $this->pluralFunctions[$string]; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 364 | + // sanitize |
|
| 365 | + $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 366 | + $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 367 | + |
|
| 368 | + $body = str_replace( |
|
| 369 | + array( 'plural', 'n', '$n$plurals', ), |
|
| 370 | + array( '$plural', '$n', '$nplurals', ), |
|
| 371 | + 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 372 | + ); |
|
| 373 | + |
|
| 374 | + // add parents |
|
| 375 | + // important since PHP's ternary evaluates from left to right |
|
| 376 | + $body .= ';'; |
|
| 377 | + $res = ''; |
|
| 378 | + $p = 0; |
|
| 379 | + for($i = 0; $i < strlen($body); $i++) { |
|
| 380 | + $ch = $body[$i]; |
|
| 381 | + switch ( $ch ) { |
|
| 382 | + case '?': |
|
| 383 | + $res .= ' ? ('; |
|
| 384 | + $p++; |
|
| 385 | + break; |
|
| 386 | + case ':': |
|
| 387 | + $res .= ') : ('; |
|
| 388 | + break; |
|
| 389 | + case ';': |
|
| 390 | + $res .= str_repeat( ')', $p ) . ';'; |
|
| 391 | + $p = 0; |
|
| 392 | + break; |
|
| 393 | + default: |
|
| 394 | + $res .= $ch; |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 399 | + $function = create_function('$n', $body); |
|
| 400 | + $this->pluralFunctions[$string] = $function; |
|
| 401 | + return $function; |
|
| 402 | + } else { |
|
| 403 | + // default: one plural form for all cases but n==1 (english) |
|
| 404 | + $function = create_function( |
|
| 405 | + '$n', |
|
| 406 | + '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 407 | + ); |
|
| 408 | + $this->pluralFunctions[$string] = $function; |
|
| 409 | + return $function; |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | 412 | } |
@@ -32,13 +32,13 @@ discard block |
||
| 32 | 32 | require_once __DIR__ . '/../lib/base.php'; |
| 33 | 33 | |
| 34 | 34 | if (\OCP\Util::needUpgrade() |
| 35 | - || \OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 36 | - // since the behavior of apps or remotes are unpredictable during |
|
| 37 | - // an upgrade, return a 503 directly |
|
| 38 | - OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); |
|
| 39 | - $response = new OC_OCS_Result(null, OC_Response::STATUS_SERVICE_UNAVAILABLE, 'Service unavailable'); |
|
| 40 | - OC_API::respond($response, OC_API::requestedFormat()); |
|
| 41 | - exit; |
|
| 35 | + || \OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 36 | + // since the behavior of apps or remotes are unpredictable during |
|
| 37 | + // an upgrade, return a 503 directly |
|
| 38 | + OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); |
|
| 39 | + $response = new OC_OCS_Result(null, OC_Response::STATUS_SERVICE_UNAVAILABLE, 'Service unavailable'); |
|
| 40 | + OC_API::respond($response, OC_API::requestedFormat()); |
|
| 41 | + exit; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
@@ -49,53 +49,53 @@ discard block |
||
| 49 | 49 | * We first try the old routes since the appframework triggers more login stuff. |
| 50 | 50 | */ |
| 51 | 51 | try { |
| 52 | - OC_App::loadApps(['session']); |
|
| 53 | - OC_App::loadApps(['authentication']); |
|
| 54 | - // load all apps to get all api routes properly setup |
|
| 55 | - OC_App::loadApps(); |
|
| 52 | + OC_App::loadApps(['session']); |
|
| 53 | + OC_App::loadApps(['authentication']); |
|
| 54 | + // load all apps to get all api routes properly setup |
|
| 55 | + OC_App::loadApps(); |
|
| 56 | 56 | |
| 57 | - OC::$server->getRouter()->match('/ocs'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 58 | - return; |
|
| 57 | + OC::$server->getRouter()->match('/ocs'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 58 | + return; |
|
| 59 | 59 | } catch (ResourceNotFoundException $e) { |
| 60 | - // Fall through the not found |
|
| 60 | + // Fall through the not found |
|
| 61 | 61 | } catch (MethodNotAllowedException $e) { |
| 62 | - OC_API::setContentType(); |
|
| 63 | - OC_Response::setStatus(405); |
|
| 64 | - exit(); |
|
| 62 | + OC_API::setContentType(); |
|
| 63 | + OC_Response::setStatus(405); |
|
| 64 | + exit(); |
|
| 65 | 65 | } catch (Exception $ex) { |
| 66 | - OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 67 | - exit(); |
|
| 66 | + OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 67 | + exit(); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /* |
| 71 | 71 | * Try the appframework routes |
| 72 | 72 | */ |
| 73 | 73 | try { |
| 74 | - if(!\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 75 | - OC::handleLogin(\OC::$server->getRequest()); |
|
| 76 | - } |
|
| 77 | - OC::$server->getRouter()->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 74 | + if(!\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 75 | + OC::handleLogin(\OC::$server->getRequest()); |
|
| 76 | + } |
|
| 77 | + OC::$server->getRouter()->match('/ocsapp'.\OC::$server->getRequest()->getRawPathInfo()); |
|
| 78 | 78 | } catch (ResourceNotFoundException $e) { |
| 79 | - OC_API::setContentType(); |
|
| 79 | + OC_API::setContentType(); |
|
| 80 | 80 | |
| 81 | - $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 82 | - $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 83 | - .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 84 | - OC_API::respond(new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 81 | + $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 82 | + $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 83 | + .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 84 | + OC_API::respond(new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 85 | 85 | } catch (MethodNotAllowedException $e) { |
| 86 | - OC_API::setContentType(); |
|
| 87 | - OC_Response::setStatus(405); |
|
| 86 | + OC_API::setContentType(); |
|
| 87 | + OC_Response::setStatus(405); |
|
| 88 | 88 | } catch (\OC\OCS\Exception $ex) { |
| 89 | - OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 89 | + OC_API::respond($ex->getResult(), OC_API::requestedFormat()); |
|
| 90 | 90 | } catch (\OC\User\LoginException $e) { |
| 91 | - OC_API::respond(new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'Unauthorised')); |
|
| 91 | + OC_API::respond(new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'Unauthorised')); |
|
| 92 | 92 | } catch (\Exception $e) { |
| 93 | - \OC::$server->getLogger()->logException($e); |
|
| 94 | - OC_API::setContentType(); |
|
| 93 | + \OC::$server->getLogger()->logException($e); |
|
| 94 | + OC_API::setContentType(); |
|
| 95 | 95 | |
| 96 | - $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 97 | - $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 98 | - .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 99 | - OC_API::respond(new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 96 | + $format = \OC::$server->getRequest()->getParam('format', 'xml'); |
|
| 97 | + $txt='Invalid query, please check the syntax. API specifications are here:' |
|
| 98 | + .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; |
|
| 99 | + OC_API::respond(new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format); |
|
| 100 | 100 | } |
| 101 | 101 | |
@@ -44,45 +44,45 @@ discard block |
||
| 44 | 44 | * @param Exception | Error $e |
| 45 | 45 | */ |
| 46 | 46 | function handleException($e) { |
| 47 | - $request = \OC::$server->getRequest(); |
|
| 48 | - // in case the request content type is text/xml - we assume it's a WebDAV request |
|
| 49 | - $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml'); |
|
| 50 | - if ($isXmlContentType === 0) { |
|
| 51 | - // fire up a simple server to properly process the exception |
|
| 52 | - $server = new Server(); |
|
| 53 | - if (!($e instanceof RemoteException)) { |
|
| 54 | - // we shall not log on RemoteException |
|
| 55 | - $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger())); |
|
| 56 | - } |
|
| 57 | - $server->on('beforeMethod', function () use ($e) { |
|
| 58 | - if ($e instanceof RemoteException) { |
|
| 59 | - switch ($e->getCode()) { |
|
| 60 | - case OC_Response::STATUS_SERVICE_UNAVAILABLE: |
|
| 61 | - throw new ServiceUnavailable($e->getMessage()); |
|
| 62 | - case OC_Response::STATUS_NOT_FOUND: |
|
| 63 | - throw new \Sabre\DAV\Exception\NotFound($e->getMessage()); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - $class = get_class($e); |
|
| 67 | - $msg = $e->getMessage(); |
|
| 68 | - throw new ServiceUnavailable("$class: $msg"); |
|
| 69 | - }); |
|
| 70 | - $server->exec(); |
|
| 71 | - } else { |
|
| 72 | - $statusCode = OC_Response::STATUS_INTERNAL_SERVER_ERROR; |
|
| 73 | - if ($e instanceof \OC\ServiceUnavailableException ) { |
|
| 74 | - $statusCode = OC_Response::STATUS_SERVICE_UNAVAILABLE; |
|
| 75 | - } |
|
| 76 | - if ($e instanceof RemoteException) { |
|
| 77 | - // we shall not log on RemoteException |
|
| 78 | - OC_Response::setStatus($e->getCode()); |
|
| 79 | - OC_Template::printErrorPage($e->getMessage()); |
|
| 80 | - } else { |
|
| 81 | - \OC::$server->getLogger()->logException($e, ['app' => 'remote']); |
|
| 82 | - OC_Response::setStatus($statusCode); |
|
| 83 | - OC_Template::printExceptionErrorPage($e); |
|
| 84 | - } |
|
| 85 | - } |
|
| 47 | + $request = \OC::$server->getRequest(); |
|
| 48 | + // in case the request content type is text/xml - we assume it's a WebDAV request |
|
| 49 | + $isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml'); |
|
| 50 | + if ($isXmlContentType === 0) { |
|
| 51 | + // fire up a simple server to properly process the exception |
|
| 52 | + $server = new Server(); |
|
| 53 | + if (!($e instanceof RemoteException)) { |
|
| 54 | + // we shall not log on RemoteException |
|
| 55 | + $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger())); |
|
| 56 | + } |
|
| 57 | + $server->on('beforeMethod', function () use ($e) { |
|
| 58 | + if ($e instanceof RemoteException) { |
|
| 59 | + switch ($e->getCode()) { |
|
| 60 | + case OC_Response::STATUS_SERVICE_UNAVAILABLE: |
|
| 61 | + throw new ServiceUnavailable($e->getMessage()); |
|
| 62 | + case OC_Response::STATUS_NOT_FOUND: |
|
| 63 | + throw new \Sabre\DAV\Exception\NotFound($e->getMessage()); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + $class = get_class($e); |
|
| 67 | + $msg = $e->getMessage(); |
|
| 68 | + throw new ServiceUnavailable("$class: $msg"); |
|
| 69 | + }); |
|
| 70 | + $server->exec(); |
|
| 71 | + } else { |
|
| 72 | + $statusCode = OC_Response::STATUS_INTERNAL_SERVER_ERROR; |
|
| 73 | + if ($e instanceof \OC\ServiceUnavailableException ) { |
|
| 74 | + $statusCode = OC_Response::STATUS_SERVICE_UNAVAILABLE; |
|
| 75 | + } |
|
| 76 | + if ($e instanceof RemoteException) { |
|
| 77 | + // we shall not log on RemoteException |
|
| 78 | + OC_Response::setStatus($e->getCode()); |
|
| 79 | + OC_Template::printErrorPage($e->getMessage()); |
|
| 80 | + } else { |
|
| 81 | + \OC::$server->getLogger()->logException($e, ['app' => 'remote']); |
|
| 82 | + OC_Response::setStatus($statusCode); |
|
| 83 | + OC_Template::printExceptionErrorPage($e); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -90,79 +90,79 @@ discard block |
||
| 90 | 90 | * @return string |
| 91 | 91 | */ |
| 92 | 92 | function resolveService($service) { |
| 93 | - $services = [ |
|
| 94 | - 'webdav' => 'dav/appinfo/v1/webdav.php', |
|
| 95 | - 'dav' => 'dav/appinfo/v2/remote.php', |
|
| 96 | - 'caldav' => 'dav/appinfo/v1/caldav.php', |
|
| 97 | - 'calendar' => 'dav/appinfo/v1/caldav.php', |
|
| 98 | - 'carddav' => 'dav/appinfo/v1/carddav.php', |
|
| 99 | - 'contacts' => 'dav/appinfo/v1/carddav.php', |
|
| 100 | - 'files' => 'dav/appinfo/v1/webdav.php', |
|
| 101 | - ]; |
|
| 102 | - if (isset($services[$service])) { |
|
| 103 | - return $services[$service]; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service); |
|
| 93 | + $services = [ |
|
| 94 | + 'webdav' => 'dav/appinfo/v1/webdav.php', |
|
| 95 | + 'dav' => 'dav/appinfo/v2/remote.php', |
|
| 96 | + 'caldav' => 'dav/appinfo/v1/caldav.php', |
|
| 97 | + 'calendar' => 'dav/appinfo/v1/caldav.php', |
|
| 98 | + 'carddav' => 'dav/appinfo/v1/carddav.php', |
|
| 99 | + 'contacts' => 'dav/appinfo/v1/carddav.php', |
|
| 100 | + 'files' => 'dav/appinfo/v1/webdav.php', |
|
| 101 | + ]; |
|
| 102 | + if (isset($services[$service])) { |
|
| 103 | + return $services[$service]; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | try { |
| 110 | - require_once __DIR__ . '/lib/base.php'; |
|
| 111 | - |
|
| 112 | - // All resources served via the DAV endpoint should have the strictest possible |
|
| 113 | - // policy. Exempted from this is the SabreDAV browser plugin which overwrites |
|
| 114 | - // this policy with a softer one if debug mode is enabled. |
|
| 115 | - header("Content-Security-Policy: default-src 'none';"); |
|
| 116 | - |
|
| 117 | - if (\OCP\Util::needUpgrade()) { |
|
| 118 | - // since the behavior of apps or remotes are unpredictable during |
|
| 119 | - // an upgrade, return a 503 directly |
|
| 120 | - throw new RemoteException('Service unavailable', OC_Response::STATUS_SERVICE_UNAVAILABLE); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $request = \OC::$server->getRequest(); |
|
| 124 | - $pathInfo = $request->getPathInfo(); |
|
| 125 | - if ($pathInfo === false || $pathInfo === '') { |
|
| 126 | - throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND); |
|
| 127 | - } |
|
| 128 | - if (!$pos = strpos($pathInfo, '/', 1)) { |
|
| 129 | - $pos = strlen($pathInfo); |
|
| 130 | - } |
|
| 131 | - $service=substr($pathInfo, 1, $pos-1); |
|
| 132 | - |
|
| 133 | - $file = resolveService($service); |
|
| 134 | - |
|
| 135 | - if(is_null($file)) { |
|
| 136 | - throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $file=ltrim($file, '/'); |
|
| 140 | - |
|
| 141 | - $parts=explode('/', $file, 2); |
|
| 142 | - $app=$parts[0]; |
|
| 143 | - |
|
| 144 | - // Load all required applications |
|
| 145 | - \OC::$REQUESTEDAPP = $app; |
|
| 146 | - OC_App::loadApps(array('authentication')); |
|
| 147 | - OC_App::loadApps(array('filesystem', 'logging')); |
|
| 148 | - |
|
| 149 | - switch ($app) { |
|
| 150 | - case 'core': |
|
| 151 | - $file = OC::$SERVERROOT .'/'. $file; |
|
| 152 | - break; |
|
| 153 | - default: |
|
| 154 | - if (!\OC::$server->getAppManager()->isInstalled($app)) { |
|
| 155 | - throw new RemoteException('App not installed: ' . $app); |
|
| 156 | - } |
|
| 157 | - OC_App::loadApp($app); |
|
| 158 | - $file = OC_App::getAppPath($app) .'/'. $parts[1]; |
|
| 159 | - break; |
|
| 160 | - } |
|
| 161 | - $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/'; |
|
| 162 | - require_once $file; |
|
| 110 | + require_once __DIR__ . '/lib/base.php'; |
|
| 111 | + |
|
| 112 | + // All resources served via the DAV endpoint should have the strictest possible |
|
| 113 | + // policy. Exempted from this is the SabreDAV browser plugin which overwrites |
|
| 114 | + // this policy with a softer one if debug mode is enabled. |
|
| 115 | + header("Content-Security-Policy: default-src 'none';"); |
|
| 116 | + |
|
| 117 | + if (\OCP\Util::needUpgrade()) { |
|
| 118 | + // since the behavior of apps or remotes are unpredictable during |
|
| 119 | + // an upgrade, return a 503 directly |
|
| 120 | + throw new RemoteException('Service unavailable', OC_Response::STATUS_SERVICE_UNAVAILABLE); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $request = \OC::$server->getRequest(); |
|
| 124 | + $pathInfo = $request->getPathInfo(); |
|
| 125 | + if ($pathInfo === false || $pathInfo === '') { |
|
| 126 | + throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND); |
|
| 127 | + } |
|
| 128 | + if (!$pos = strpos($pathInfo, '/', 1)) { |
|
| 129 | + $pos = strlen($pathInfo); |
|
| 130 | + } |
|
| 131 | + $service=substr($pathInfo, 1, $pos-1); |
|
| 132 | + |
|
| 133 | + $file = resolveService($service); |
|
| 134 | + |
|
| 135 | + if(is_null($file)) { |
|
| 136 | + throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $file=ltrim($file, '/'); |
|
| 140 | + |
|
| 141 | + $parts=explode('/', $file, 2); |
|
| 142 | + $app=$parts[0]; |
|
| 143 | + |
|
| 144 | + // Load all required applications |
|
| 145 | + \OC::$REQUESTEDAPP = $app; |
|
| 146 | + OC_App::loadApps(array('authentication')); |
|
| 147 | + OC_App::loadApps(array('filesystem', 'logging')); |
|
| 148 | + |
|
| 149 | + switch ($app) { |
|
| 150 | + case 'core': |
|
| 151 | + $file = OC::$SERVERROOT .'/'. $file; |
|
| 152 | + break; |
|
| 153 | + default: |
|
| 154 | + if (!\OC::$server->getAppManager()->isInstalled($app)) { |
|
| 155 | + throw new RemoteException('App not installed: ' . $app); |
|
| 156 | + } |
|
| 157 | + OC_App::loadApp($app); |
|
| 158 | + $file = OC_App::getAppPath($app) .'/'. $parts[1]; |
|
| 159 | + break; |
|
| 160 | + } |
|
| 161 | + $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/'; |
|
| 162 | + require_once $file; |
|
| 163 | 163 | |
| 164 | 164 | } catch (Exception $ex) { |
| 165 | - handleException($ex); |
|
| 165 | + handleException($ex); |
|
| 166 | 166 | } catch (Error $e) { |
| 167 | - handleException($e); |
|
| 167 | + handleException($e); |
|
| 168 | 168 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | // we shall not log on RemoteException |
| 55 | 55 | $server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger())); |
| 56 | 56 | } |
| 57 | - $server->on('beforeMethod', function () use ($e) { |
|
| 57 | + $server->on('beforeMethod', function() use ($e) { |
|
| 58 | 58 | if ($e instanceof RemoteException) { |
| 59 | 59 | switch ($e->getCode()) { |
| 60 | 60 | case OC_Response::STATUS_SERVICE_UNAVAILABLE: |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | $server->exec(); |
| 71 | 71 | } else { |
| 72 | 72 | $statusCode = OC_Response::STATUS_INTERNAL_SERVER_ERROR; |
| 73 | - if ($e instanceof \OC\ServiceUnavailableException ) { |
|
| 73 | + if ($e instanceof \OC\ServiceUnavailableException) { |
|
| 74 | 74 | $statusCode = OC_Response::STATUS_SERVICE_UNAVAILABLE; |
| 75 | 75 | } |
| 76 | 76 | if ($e instanceof RemoteException) { |
@@ -103,11 +103,11 @@ discard block |
||
| 103 | 103 | return $services[$service]; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service); |
|
| 106 | + return \OC::$server->getConfig()->getAppValue('core', 'remote_'.$service); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | try { |
| 110 | - require_once __DIR__ . '/lib/base.php'; |
|
| 110 | + require_once __DIR__.'/lib/base.php'; |
|
| 111 | 111 | |
| 112 | 112 | // All resources served via the DAV endpoint should have the strictest possible |
| 113 | 113 | // policy. Exempted from this is the SabreDAV browser plugin which overwrites |
@@ -128,18 +128,18 @@ discard block |
||
| 128 | 128 | if (!$pos = strpos($pathInfo, '/', 1)) { |
| 129 | 129 | $pos = strlen($pathInfo); |
| 130 | 130 | } |
| 131 | - $service=substr($pathInfo, 1, $pos-1); |
|
| 131 | + $service = substr($pathInfo, 1, $pos - 1); |
|
| 132 | 132 | |
| 133 | 133 | $file = resolveService($service); |
| 134 | 134 | |
| 135 | - if(is_null($file)) { |
|
| 135 | + if (is_null($file)) { |
|
| 136 | 136 | throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND); |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - $file=ltrim($file, '/'); |
|
| 139 | + $file = ltrim($file, '/'); |
|
| 140 | 140 | |
| 141 | - $parts=explode('/', $file, 2); |
|
| 142 | - $app=$parts[0]; |
|
| 141 | + $parts = explode('/', $file, 2); |
|
| 142 | + $app = $parts[0]; |
|
| 143 | 143 | |
| 144 | 144 | // Load all required applications |
| 145 | 145 | \OC::$REQUESTEDAPP = $app; |
@@ -148,17 +148,17 @@ discard block |
||
| 148 | 148 | |
| 149 | 149 | switch ($app) { |
| 150 | 150 | case 'core': |
| 151 | - $file = OC::$SERVERROOT .'/'. $file; |
|
| 151 | + $file = OC::$SERVERROOT.'/'.$file; |
|
| 152 | 152 | break; |
| 153 | 153 | default: |
| 154 | 154 | if (!\OC::$server->getAppManager()->isInstalled($app)) { |
| 155 | - throw new RemoteException('App not installed: ' . $app); |
|
| 155 | + throw new RemoteException('App not installed: '.$app); |
|
| 156 | 156 | } |
| 157 | 157 | OC_App::loadApp($app); |
| 158 | - $file = OC_App::getAppPath($app) .'/'. $parts[1]; |
|
| 158 | + $file = OC_App::getAppPath($app).'/'.$parts[1]; |
|
| 159 | 159 | break; |
| 160 | 160 | } |
| 161 | - $baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/'; |
|
| 161 | + $baseuri = OC::$WEBROOT.'/remote.php/'.$service.'/'; |
|
| 162 | 162 | require_once $file; |
| 163 | 163 | |
| 164 | 164 | } catch (Exception $ex) { |