@@ -25,641 +25,641 @@ |
||
| 25 | 25 | * @since 4.0.0 |
| 26 | 26 | */ |
| 27 | 27 | class Util { |
| 28 | - private static ?IManager $shareManager = null; |
|
| 29 | - |
|
| 30 | - private static array $scriptsInit = []; |
|
| 31 | - private static array $scripts = []; |
|
| 32 | - private static array $scriptDeps = []; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * get the current installed version of Nextcloud |
|
| 36 | - * @return array |
|
| 37 | - * @since 4.0.0 |
|
| 38 | - * @deprecated 31.0.0 Use \OCP\ServerVersion::getVersion |
|
| 39 | - */ |
|
| 40 | - public static function getVersion() { |
|
| 41 | - return Server::get(ServerVersion::class)->getVersion(); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @since 17.0.0 |
|
| 46 | - */ |
|
| 47 | - public static function hasExtendedSupport(): bool { |
|
| 48 | - try { |
|
| 49 | - /** @var \OCP\Support\Subscription\IRegistry */ |
|
| 50 | - $subscriptionRegistry = Server::get(\OCP\Support\Subscription\IRegistry::class); |
|
| 51 | - return $subscriptionRegistry->delegateHasExtendedSupport(); |
|
| 52 | - } catch (ContainerExceptionInterface $e) { |
|
| 53 | - } |
|
| 54 | - return \OCP\Server::get(IConfig::class)->getSystemValueBool('extendedSupport', false); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Set current update channel |
|
| 59 | - * @param string $channel |
|
| 60 | - * @since 8.1.0 |
|
| 61 | - */ |
|
| 62 | - public static function setChannel($channel) { |
|
| 63 | - \OCP\Server::get(IConfig::class)->setSystemValue('updater.release.channel', $channel); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get current update channel |
|
| 68 | - * @return string |
|
| 69 | - * @since 8.1.0 |
|
| 70 | - * @deprecated 31.0.0 Use \OCP\ServerVersion::getChannel |
|
| 71 | - */ |
|
| 72 | - public static function getChannel() { |
|
| 73 | - return \OCP\Server::get(ServerVersion::class)->getChannel(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * check if sharing is disabled for the current user |
|
| 78 | - * |
|
| 79 | - * @return boolean |
|
| 80 | - * @since 7.0.0 |
|
| 81 | - * @deprecated 9.1.0 Use Server::get(\OCP\Share\IManager::class)->sharingDisabledForUser |
|
| 82 | - */ |
|
| 83 | - public static function isSharingDisabledForUser() { |
|
| 84 | - if (self::$shareManager === null) { |
|
| 85 | - self::$shareManager = Server::get(IManager::class); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $user = Server::get(\OCP\IUserSession::class)->getUser(); |
|
| 89 | - |
|
| 90 | - return self::$shareManager->sharingDisabledForUser($user?->getUID()); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * get l10n object |
|
| 95 | - * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
| 96 | - */ |
|
| 97 | - public static function getL10N(string $application, ?string $language = null): IL10N { |
|
| 98 | - return Server::get(\OCP\L10N\IFactory::class)->get($application, $language); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Add a css file |
|
| 103 | - * |
|
| 104 | - * @param string $application application id |
|
| 105 | - * @param ?string $file filename |
|
| 106 | - * @param bool $prepend prepend the style to the beginning of the list |
|
| 107 | - * @since 4.0.0 |
|
| 108 | - */ |
|
| 109 | - public static function addStyle(string $application, ?string $file = null, bool $prepend = false): void { |
|
| 110 | - \OC_Util::addStyle($application, $file, $prepend); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Add a standalone init js file that is loaded for initialization |
|
| 115 | - * |
|
| 116 | - * Be careful loading scripts using this method as they are loaded early |
|
| 117 | - * and block the initial page rendering. They should not have dependencies |
|
| 118 | - * on any other scripts than core-common and core-main. |
|
| 119 | - * |
|
| 120 | - * @since 28.0.0 |
|
| 121 | - */ |
|
| 122 | - public static function addInitScript(string $application, string $file): void { |
|
| 123 | - if (!empty($application)) { |
|
| 124 | - $path = "$application/js/$file"; |
|
| 125 | - } else { |
|
| 126 | - $path = "js/$file"; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // We need to handle the translation BEFORE the init script |
|
| 130 | - // is loaded, as the init script might use translations |
|
| 131 | - if ($application !== 'core' && !str_contains($file, 'l10n')) { |
|
| 132 | - self::addTranslations($application, null, true); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - self::$scriptsInit[] = $path; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * add a javascript file |
|
| 140 | - * |
|
| 141 | - * @param string $application |
|
| 142 | - * @param string|null $file |
|
| 143 | - * @param string $afterAppId |
|
| 144 | - * @param bool $prepend |
|
| 145 | - * @since 4.0.0 |
|
| 146 | - */ |
|
| 147 | - public static function addScript(string $application, ?string $file = null, string $afterAppId = 'core', bool $prepend = false): void { |
|
| 148 | - if (!empty($application)) { |
|
| 149 | - $path = "$application/js/$file"; |
|
| 150 | - } else { |
|
| 151 | - $path = "js/$file"; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // Inject js translations if we load a script for |
|
| 155 | - // a specific app that is not core, as those js files |
|
| 156 | - // need separate handling |
|
| 157 | - if ($application !== 'core' |
|
| 158 | - && $file !== null |
|
| 159 | - && !str_contains($file, 'l10n')) { |
|
| 160 | - self::addTranslations($application); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // store app in dependency list |
|
| 164 | - if (!array_key_exists($application, self::$scriptDeps)) { |
|
| 165 | - self::$scriptDeps[$application] = new AppScriptDependency($application, [$afterAppId]); |
|
| 166 | - } else { |
|
| 167 | - self::$scriptDeps[$application]->addDep($afterAppId); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if ($prepend) { |
|
| 171 | - array_unshift(self::$scripts[$application], $path); |
|
| 172 | - } else { |
|
| 173 | - self::$scripts[$application][] = $path; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Return the list of scripts injected to the page |
|
| 179 | - * |
|
| 180 | - * @return array |
|
| 181 | - * @since 24.0.0 |
|
| 182 | - */ |
|
| 183 | - public static function getScripts(): array { |
|
| 184 | - // Sort scriptDeps into sortedScriptDeps |
|
| 185 | - $scriptSort = \OCP\Server::get(AppScriptSort::class); |
|
| 186 | - $sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps); |
|
| 187 | - |
|
| 188 | - // Flatten array and remove duplicates |
|
| 189 | - $sortedScripts = array_merge([self::$scriptsInit], $sortedScripts); |
|
| 190 | - $sortedScripts = array_merge(...array_values($sortedScripts)); |
|
| 191 | - |
|
| 192 | - // Override core-common and core-main order |
|
| 193 | - if (in_array('core/js/main', $sortedScripts)) { |
|
| 194 | - array_unshift($sortedScripts, 'core/js/main'); |
|
| 195 | - } |
|
| 196 | - if (in_array('core/js/common', $sortedScripts)) { |
|
| 197 | - array_unshift($sortedScripts, 'core/js/common'); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - return array_unique($sortedScripts); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Add a translation JS file |
|
| 205 | - * @param string $application application id |
|
| 206 | - * @param string $languageCode language code, defaults to the current locale |
|
| 207 | - * @param bool $init whether the translations should be loaded early or not |
|
| 208 | - * @since 8.0.0 |
|
| 209 | - */ |
|
| 210 | - public static function addTranslations($application, $languageCode = null, $init = false) { |
|
| 211 | - if (is_null($languageCode)) { |
|
| 212 | - $languageCode = \OCP\Server::get(IFactory::class)->findLanguage($application); |
|
| 213 | - } |
|
| 214 | - if (!empty($application)) { |
|
| 215 | - $path = "$application/l10n/$languageCode"; |
|
| 216 | - } else { |
|
| 217 | - $path = "l10n/$languageCode"; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - if ($init) { |
|
| 221 | - self::$scriptsInit[] = $path; |
|
| 222 | - } else { |
|
| 223 | - self::$scripts[$application][] = $path; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Add a custom element to the header |
|
| 229 | - * If $text is null then the element will be written as empty element. |
|
| 230 | - * So use "" to get a closing tag. |
|
| 231 | - * @param string $tag tag name of the element |
|
| 232 | - * @param array $attributes array of attributes for the element |
|
| 233 | - * @param string $text the text content for the element |
|
| 234 | - * @since 4.0.0 |
|
| 235 | - */ |
|
| 236 | - public static function addHeader($tag, $attributes, $text = null) { |
|
| 237 | - \OC_Util::addHeader($tag, $attributes, $text); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Creates an absolute url to the given app and file. |
|
| 242 | - * @param string $app app |
|
| 243 | - * @param string $file file |
|
| 244 | - * @param array $args array with param=>value, will be appended to the returned url |
|
| 245 | - * The value of $args will be urlencoded |
|
| 246 | - * @return string the url |
|
| 247 | - * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
| 248 | - */ |
|
| 249 | - public static function linkToAbsolute($app, $file, $args = []) { |
|
| 250 | - $urlGenerator = \OCP\Server::get(IURLGenerator::class); |
|
| 251 | - return $urlGenerator->getAbsoluteURL( |
|
| 252 | - $urlGenerator->linkTo($app, $file, $args) |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Creates an absolute url for remote use. |
|
| 258 | - * @param string $service id |
|
| 259 | - * @return string the url |
|
| 260 | - * @since 4.0.0 |
|
| 261 | - */ |
|
| 262 | - public static function linkToRemote($service) { |
|
| 263 | - $urlGenerator = \OCP\Server::get(IURLGenerator::class); |
|
| 264 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
| 265 | - return $urlGenerator->getAbsoluteURL( |
|
| 266 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
| 267 | - ); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Returns the server host name without an eventual port number |
|
| 272 | - * @return string the server hostname |
|
| 273 | - * @since 5.0.0 |
|
| 274 | - */ |
|
| 275 | - public static function getServerHostName() { |
|
| 276 | - $host_name = \OCP\Server::get(IRequest::class)->getServerHost(); |
|
| 277 | - // strip away port number (if existing) |
|
| 278 | - $colon_pos = strpos($host_name, ':'); |
|
| 279 | - if ($colon_pos != false) { |
|
| 280 | - $host_name = substr($host_name, 0, $colon_pos); |
|
| 281 | - } |
|
| 282 | - return $host_name; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Returns the default email address |
|
| 287 | - * @param string $user_part the user part of the address |
|
| 288 | - * @return string the default email address |
|
| 289 | - * |
|
| 290 | - * Assembles a default email address (using the server hostname |
|
| 291 | - * and the given user part, and returns it |
|
| 292 | - * Example: when given lostpassword-noreply as $user_part param, |
|
| 293 | - * and is currently accessed via http(s)://example.com/, |
|
| 294 | - * it would return '[email protected]' |
|
| 295 | - * |
|
| 296 | - * If the configuration value 'mail_from_address' is set in |
|
| 297 | - * config.php, this value will override the $user_part that |
|
| 298 | - * is passed to this function |
|
| 299 | - * @since 5.0.0 |
|
| 300 | - */ |
|
| 301 | - public static function getDefaultEmailAddress(string $user_part): string { |
|
| 302 | - $config = \OCP\Server::get(IConfig::class); |
|
| 303 | - $user_part = $config->getSystemValueString('mail_from_address', $user_part); |
|
| 304 | - $host_name = self::getServerHostName(); |
|
| 305 | - $host_name = $config->getSystemValueString('mail_domain', $host_name); |
|
| 306 | - $defaultEmailAddress = $user_part . '@' . $host_name; |
|
| 307 | - |
|
| 308 | - $mailer = \OCP\Server::get(IMailer::class); |
|
| 309 | - if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
| 310 | - return $defaultEmailAddress; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
| 314 | - return $user_part . '@localhost.localdomain'; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Converts string to int of float depending if it fits an int |
|
| 319 | - * @param numeric-string|float|int $number numeric string |
|
| 320 | - * @return int|float int if it fits, float if it is too big |
|
| 321 | - * @since 26.0.0 |
|
| 322 | - */ |
|
| 323 | - public static function numericToNumber(string|float|int $number): int|float { |
|
| 324 | - /* This is a hack to cast to (int|float) */ |
|
| 325 | - return 0 + (string)$number; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Make a human file size (2048 to 2 kB) |
|
| 330 | - * @param int|float $bytes file size in bytes |
|
| 331 | - * @return string a human readable file size |
|
| 332 | - * @since 4.0.0 |
|
| 333 | - */ |
|
| 334 | - public static function humanFileSize(int|float $bytes): string { |
|
| 335 | - if ($bytes < 0) { |
|
| 336 | - return '?'; |
|
| 337 | - } |
|
| 338 | - if ($bytes < 1024) { |
|
| 339 | - return "$bytes B"; |
|
| 340 | - } |
|
| 341 | - $bytes = round($bytes / 1024, 0); |
|
| 342 | - if ($bytes < 1024) { |
|
| 343 | - return "$bytes KB"; |
|
| 344 | - } |
|
| 345 | - $bytes = round($bytes / 1024, 1); |
|
| 346 | - if ($bytes < 1024) { |
|
| 347 | - return "$bytes MB"; |
|
| 348 | - } |
|
| 349 | - $bytes = round($bytes / 1024, 1); |
|
| 350 | - if ($bytes < 1024) { |
|
| 351 | - return "$bytes GB"; |
|
| 352 | - } |
|
| 353 | - $bytes = round($bytes / 1024, 1); |
|
| 354 | - if ($bytes < 1024) { |
|
| 355 | - return "$bytes TB"; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - $bytes = round($bytes / 1024, 1); |
|
| 359 | - return "$bytes PB"; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Make a computer file size (2 kB to 2048) |
|
| 364 | - * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
|
| 365 | - * |
|
| 366 | - * @param string $str file size in a fancy format |
|
| 367 | - * @return false|int|float a file size in bytes |
|
| 368 | - * @since 4.0.0 |
|
| 369 | - */ |
|
| 370 | - public static function computerFileSize(string $str): false|int|float { |
|
| 371 | - $str = strtolower($str); |
|
| 372 | - if (is_numeric($str)) { |
|
| 373 | - return Util::numericToNumber($str); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - $bytes_array = [ |
|
| 377 | - 'b' => 1, |
|
| 378 | - 'k' => 1024, |
|
| 379 | - 'kb' => 1024, |
|
| 380 | - 'mb' => 1024 * 1024, |
|
| 381 | - 'm' => 1024 * 1024, |
|
| 382 | - 'gb' => 1024 * 1024 * 1024, |
|
| 383 | - 'g' => 1024 * 1024 * 1024, |
|
| 384 | - 'tb' => 1024 * 1024 * 1024 * 1024, |
|
| 385 | - 't' => 1024 * 1024 * 1024 * 1024, |
|
| 386 | - 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
| 387 | - 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
| 388 | - ]; |
|
| 389 | - |
|
| 390 | - $bytes = (float)$str; |
|
| 391 | - |
|
| 392 | - if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && isset($bytes_array[$matches[1]])) { |
|
| 393 | - $bytes *= $bytes_array[$matches[1]]; |
|
| 394 | - } else { |
|
| 395 | - return false; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - return Util::numericToNumber(round($bytes)); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * connects a function to a hook |
|
| 403 | - * |
|
| 404 | - * @param string $signalClass class name of emitter |
|
| 405 | - * @param string $signalName name of signal |
|
| 406 | - * @param string|object $slotClass class name of slot |
|
| 407 | - * @param string $slotName name of slot |
|
| 408 | - * @return bool |
|
| 409 | - * |
|
| 410 | - * This function makes it very easy to connect to use hooks. |
|
| 411 | - * |
|
| 412 | - * TODO: write example |
|
| 413 | - * @since 4.0.0 |
|
| 414 | - * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener |
|
| 415 | - */ |
|
| 416 | - public static function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
| 417 | - return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Emits a signal. To get data from the slot use references! |
|
| 422 | - * @param string $signalclass class name of emitter |
|
| 423 | - * @param string $signalname name of signal |
|
| 424 | - * @param array $params default: array() array with additional data |
|
| 425 | - * @return bool true if slots exists or false if not |
|
| 426 | - * |
|
| 427 | - * TODO: write example |
|
| 428 | - * @since 4.0.0 |
|
| 429 | - * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent |
|
| 430 | - */ |
|
| 431 | - public static function emitHook($signalclass, $signalname, $params = []) { |
|
| 432 | - return \OC_Hook::emit($signalclass, $signalname, $params); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
| 437 | - * multiple Template elements which invoke `callRegister`. If the value |
|
| 438 | - * would not be cached these unit-tests would fail. |
|
| 439 | - * @var string |
|
| 440 | - */ |
|
| 441 | - private static $token = ''; |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Register an get/post call. This is important to prevent CSRF attacks |
|
| 445 | - * @since 4.5.0 |
|
| 446 | - * @deprecated 32.0.0 directly use CsrfTokenManager instead |
|
| 447 | - */ |
|
| 448 | - public static function callRegister() { |
|
| 449 | - if (self::$token === '') { |
|
| 450 | - self::$token = \OCP\Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue(); |
|
| 451 | - } |
|
| 452 | - return self::$token; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Used to sanitize HTML |
|
| 457 | - * |
|
| 458 | - * This function is used to sanitize HTML and should be applied on any |
|
| 459 | - * string or array of strings before displaying it on a web page. |
|
| 460 | - * |
|
| 461 | - * @param string|string[] $value |
|
| 462 | - * @return ($value is array ? string[] : string) an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
| 463 | - * @since 4.5.0 |
|
| 464 | - */ |
|
| 465 | - public static function sanitizeHTML($value) { |
|
| 466 | - return \OC_Util::sanitizeHTML($value); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * Public function to encode url parameters |
|
| 471 | - * |
|
| 472 | - * This function is used to encode path to file before output. |
|
| 473 | - * Encoding is done according to RFC 3986 with one exception: |
|
| 474 | - * Character '/' is preserved as is. |
|
| 475 | - * |
|
| 476 | - * @param string $component part of URI to encode |
|
| 477 | - * @return string |
|
| 478 | - * @since 6.0.0 |
|
| 479 | - */ |
|
| 480 | - public static function encodePath($component) { |
|
| 481 | - return \OC_Util::encodePath($component); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
| 486 | - * |
|
| 487 | - * @param array $input The array to work on |
|
| 488 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
| 489 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
| 490 | - * @return array |
|
| 491 | - * @since 4.5.0 |
|
| 492 | - */ |
|
| 493 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
| 494 | - $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
| 495 | - $ret = []; |
|
| 496 | - foreach ($input as $k => $v) { |
|
| 497 | - $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
| 498 | - } |
|
| 499 | - return $ret; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * performs a search in a nested array |
|
| 504 | - * |
|
| 505 | - * @param array $haystack the array to be searched |
|
| 506 | - * @param string $needle the search string |
|
| 507 | - * @param mixed $index optional, only search this key name |
|
| 508 | - * @return mixed the key of the matching field, otherwise false |
|
| 509 | - * @since 4.5.0 |
|
| 510 | - * @deprecated 15.0.0 |
|
| 511 | - */ |
|
| 512 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
| 513 | - $aIt = new \RecursiveArrayIterator($haystack); |
|
| 514 | - $it = new \RecursiveIteratorIterator($aIt); |
|
| 515 | - |
|
| 516 | - while ($it->valid()) { |
|
| 517 | - if (((isset($index) and ($it->key() == $index)) or !isset($index)) and ($it->current() == $needle)) { |
|
| 518 | - return $aIt->key(); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - $it->next(); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - return false; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
| 529 | - * |
|
| 530 | - * @param string $dir the current folder where the user currently operates |
|
| 531 | - * @param int|float|null $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
| 532 | - * @return int|float number of bytes representing |
|
| 533 | - * @since 5.0.0 |
|
| 534 | - */ |
|
| 535 | - public static function maxUploadFilesize(string $dir, int|float|null $free = null): int|float { |
|
| 536 | - if (is_null($free) || $free < 0) { |
|
| 537 | - $free = self::freeSpace($dir); |
|
| 538 | - } |
|
| 539 | - return min($free, self::uploadLimit()); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * Calculate free space left within user quota |
|
| 544 | - * @param string $dir the current folder where the user currently operates |
|
| 545 | - * @return int|float number of bytes representing |
|
| 546 | - * @since 7.0.0 |
|
| 547 | - */ |
|
| 548 | - public static function freeSpace(string $dir): int|float { |
|
| 549 | - $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
| 550 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 551 | - $freeSpace = max($freeSpace, 0); |
|
| 552 | - return $freeSpace; |
|
| 553 | - } else { |
|
| 554 | - return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * Calculate PHP upload limit |
|
| 560 | - * |
|
| 561 | - * @return int|float number of bytes representing |
|
| 562 | - * @since 7.0.0 |
|
| 563 | - */ |
|
| 564 | - public static function uploadLimit(): int|float { |
|
| 565 | - $ini = Server::get(IniGetWrapper::class); |
|
| 566 | - $upload_max_filesize = self::computerFileSize($ini->get('upload_max_filesize')) ?: 0; |
|
| 567 | - $post_max_size = self::computerFileSize($ini->get('post_max_size')) ?: 0; |
|
| 568 | - if ($upload_max_filesize === 0 && $post_max_size === 0) { |
|
| 569 | - return INF; |
|
| 570 | - } elseif ($upload_max_filesize === 0 || $post_max_size === 0) { |
|
| 571 | - return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
| 572 | - } else { |
|
| 573 | - return min($upload_max_filesize, $post_max_size); |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Compare two strings to provide a natural sort |
|
| 579 | - * @param string $a first string to compare |
|
| 580 | - * @param string $b second string to compare |
|
| 581 | - * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
| 582 | - * or 0 if the strings are identical |
|
| 583 | - * @since 7.0.0 |
|
| 584 | - */ |
|
| 585 | - public static function naturalSortCompare($a, $b) { |
|
| 586 | - return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - /** |
|
| 590 | - * Check if a password is required for each public link |
|
| 591 | - * |
|
| 592 | - * @param bool $checkGroupMembership Check group membership exclusion |
|
| 593 | - * @return boolean |
|
| 594 | - * @since 7.0.0 |
|
| 595 | - */ |
|
| 596 | - public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) { |
|
| 597 | - return \OC_Util::isPublicLinkPasswordRequired($checkGroupMembership); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * check if share API enforces a default expire date |
|
| 602 | - * @return boolean |
|
| 603 | - * @since 8.0.0 |
|
| 604 | - */ |
|
| 605 | - public static function isDefaultExpireDateEnforced() { |
|
| 606 | - return \OC_Util::isDefaultExpireDateEnforced(); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - protected static $needUpgradeCache = null; |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Checks whether the current version needs upgrade. |
|
| 613 | - * |
|
| 614 | - * @return bool true if upgrade is needed, false otherwise |
|
| 615 | - * @since 7.0.0 |
|
| 616 | - */ |
|
| 617 | - public static function needUpgrade() { |
|
| 618 | - if (!isset(self::$needUpgradeCache)) { |
|
| 619 | - self::$needUpgradeCache = \OC_Util::needUpgrade(\OCP\Server::get(\OC\SystemConfig::class)); |
|
| 620 | - } |
|
| 621 | - return self::$needUpgradeCache; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * Sometimes a string has to be shortened to fit within a certain maximum |
|
| 626 | - * data length in bytes. substr() you may break multibyte characters, |
|
| 627 | - * because it operates on single byte level. mb_substr() operates on |
|
| 628 | - * characters, so does not ensure that the shortened string satisfies the |
|
| 629 | - * max length in bytes. |
|
| 630 | - * |
|
| 631 | - * For example, json_encode is messing with multibyte characters a lot, |
|
| 632 | - * replacing them with something along "\u1234". |
|
| 633 | - * |
|
| 634 | - * This function shortens the string with by $accuracy (-5) from |
|
| 635 | - * $dataLength characters, until it fits within $dataLength bytes. |
|
| 636 | - * |
|
| 637 | - * @since 23.0.0 |
|
| 638 | - */ |
|
| 639 | - public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string { |
|
| 640 | - $temp = mb_substr($subject, 0, $dataLength); |
|
| 641 | - // json encodes encapsulates the string in double quotes, they need to be substracted |
|
| 642 | - while ((strlen(json_encode($temp)) - 2) > $dataLength) { |
|
| 643 | - $temp = mb_substr($temp, 0, -$accuracy); |
|
| 644 | - } |
|
| 645 | - return $temp; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Check if a function is enabled in the php configuration |
|
| 650 | - * |
|
| 651 | - * @since 25.0.0 |
|
| 652 | - */ |
|
| 653 | - public static function isFunctionEnabled(string $functionName): bool { |
|
| 654 | - if (!function_exists($functionName)) { |
|
| 655 | - return false; |
|
| 656 | - } |
|
| 657 | - $ini = Server::get(IniGetWrapper::class); |
|
| 658 | - $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
| 659 | - $disabled = array_map('trim', $disabled); |
|
| 660 | - if (in_array($functionName, $disabled)) { |
|
| 661 | - return false; |
|
| 662 | - } |
|
| 663 | - return true; |
|
| 664 | - } |
|
| 28 | + private static ?IManager $shareManager = null; |
|
| 29 | + |
|
| 30 | + private static array $scriptsInit = []; |
|
| 31 | + private static array $scripts = []; |
|
| 32 | + private static array $scriptDeps = []; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * get the current installed version of Nextcloud |
|
| 36 | + * @return array |
|
| 37 | + * @since 4.0.0 |
|
| 38 | + * @deprecated 31.0.0 Use \OCP\ServerVersion::getVersion |
|
| 39 | + */ |
|
| 40 | + public static function getVersion() { |
|
| 41 | + return Server::get(ServerVersion::class)->getVersion(); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @since 17.0.0 |
|
| 46 | + */ |
|
| 47 | + public static function hasExtendedSupport(): bool { |
|
| 48 | + try { |
|
| 49 | + /** @var \OCP\Support\Subscription\IRegistry */ |
|
| 50 | + $subscriptionRegistry = Server::get(\OCP\Support\Subscription\IRegistry::class); |
|
| 51 | + return $subscriptionRegistry->delegateHasExtendedSupport(); |
|
| 52 | + } catch (ContainerExceptionInterface $e) { |
|
| 53 | + } |
|
| 54 | + return \OCP\Server::get(IConfig::class)->getSystemValueBool('extendedSupport', false); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Set current update channel |
|
| 59 | + * @param string $channel |
|
| 60 | + * @since 8.1.0 |
|
| 61 | + */ |
|
| 62 | + public static function setChannel($channel) { |
|
| 63 | + \OCP\Server::get(IConfig::class)->setSystemValue('updater.release.channel', $channel); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get current update channel |
|
| 68 | + * @return string |
|
| 69 | + * @since 8.1.0 |
|
| 70 | + * @deprecated 31.0.0 Use \OCP\ServerVersion::getChannel |
|
| 71 | + */ |
|
| 72 | + public static function getChannel() { |
|
| 73 | + return \OCP\Server::get(ServerVersion::class)->getChannel(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * check if sharing is disabled for the current user |
|
| 78 | + * |
|
| 79 | + * @return boolean |
|
| 80 | + * @since 7.0.0 |
|
| 81 | + * @deprecated 9.1.0 Use Server::get(\OCP\Share\IManager::class)->sharingDisabledForUser |
|
| 82 | + */ |
|
| 83 | + public static function isSharingDisabledForUser() { |
|
| 84 | + if (self::$shareManager === null) { |
|
| 85 | + self::$shareManager = Server::get(IManager::class); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $user = Server::get(\OCP\IUserSession::class)->getUser(); |
|
| 89 | + |
|
| 90 | + return self::$shareManager->sharingDisabledForUser($user?->getUID()); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * get l10n object |
|
| 95 | + * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
| 96 | + */ |
|
| 97 | + public static function getL10N(string $application, ?string $language = null): IL10N { |
|
| 98 | + return Server::get(\OCP\L10N\IFactory::class)->get($application, $language); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Add a css file |
|
| 103 | + * |
|
| 104 | + * @param string $application application id |
|
| 105 | + * @param ?string $file filename |
|
| 106 | + * @param bool $prepend prepend the style to the beginning of the list |
|
| 107 | + * @since 4.0.0 |
|
| 108 | + */ |
|
| 109 | + public static function addStyle(string $application, ?string $file = null, bool $prepend = false): void { |
|
| 110 | + \OC_Util::addStyle($application, $file, $prepend); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Add a standalone init js file that is loaded for initialization |
|
| 115 | + * |
|
| 116 | + * Be careful loading scripts using this method as they are loaded early |
|
| 117 | + * and block the initial page rendering. They should not have dependencies |
|
| 118 | + * on any other scripts than core-common and core-main. |
|
| 119 | + * |
|
| 120 | + * @since 28.0.0 |
|
| 121 | + */ |
|
| 122 | + public static function addInitScript(string $application, string $file): void { |
|
| 123 | + if (!empty($application)) { |
|
| 124 | + $path = "$application/js/$file"; |
|
| 125 | + } else { |
|
| 126 | + $path = "js/$file"; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // We need to handle the translation BEFORE the init script |
|
| 130 | + // is loaded, as the init script might use translations |
|
| 131 | + if ($application !== 'core' && !str_contains($file, 'l10n')) { |
|
| 132 | + self::addTranslations($application, null, true); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + self::$scriptsInit[] = $path; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * add a javascript file |
|
| 140 | + * |
|
| 141 | + * @param string $application |
|
| 142 | + * @param string|null $file |
|
| 143 | + * @param string $afterAppId |
|
| 144 | + * @param bool $prepend |
|
| 145 | + * @since 4.0.0 |
|
| 146 | + */ |
|
| 147 | + public static function addScript(string $application, ?string $file = null, string $afterAppId = 'core', bool $prepend = false): void { |
|
| 148 | + if (!empty($application)) { |
|
| 149 | + $path = "$application/js/$file"; |
|
| 150 | + } else { |
|
| 151 | + $path = "js/$file"; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // Inject js translations if we load a script for |
|
| 155 | + // a specific app that is not core, as those js files |
|
| 156 | + // need separate handling |
|
| 157 | + if ($application !== 'core' |
|
| 158 | + && $file !== null |
|
| 159 | + && !str_contains($file, 'l10n')) { |
|
| 160 | + self::addTranslations($application); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // store app in dependency list |
|
| 164 | + if (!array_key_exists($application, self::$scriptDeps)) { |
|
| 165 | + self::$scriptDeps[$application] = new AppScriptDependency($application, [$afterAppId]); |
|
| 166 | + } else { |
|
| 167 | + self::$scriptDeps[$application]->addDep($afterAppId); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if ($prepend) { |
|
| 171 | + array_unshift(self::$scripts[$application], $path); |
|
| 172 | + } else { |
|
| 173 | + self::$scripts[$application][] = $path; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Return the list of scripts injected to the page |
|
| 179 | + * |
|
| 180 | + * @return array |
|
| 181 | + * @since 24.0.0 |
|
| 182 | + */ |
|
| 183 | + public static function getScripts(): array { |
|
| 184 | + // Sort scriptDeps into sortedScriptDeps |
|
| 185 | + $scriptSort = \OCP\Server::get(AppScriptSort::class); |
|
| 186 | + $sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps); |
|
| 187 | + |
|
| 188 | + // Flatten array and remove duplicates |
|
| 189 | + $sortedScripts = array_merge([self::$scriptsInit], $sortedScripts); |
|
| 190 | + $sortedScripts = array_merge(...array_values($sortedScripts)); |
|
| 191 | + |
|
| 192 | + // Override core-common and core-main order |
|
| 193 | + if (in_array('core/js/main', $sortedScripts)) { |
|
| 194 | + array_unshift($sortedScripts, 'core/js/main'); |
|
| 195 | + } |
|
| 196 | + if (in_array('core/js/common', $sortedScripts)) { |
|
| 197 | + array_unshift($sortedScripts, 'core/js/common'); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + return array_unique($sortedScripts); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Add a translation JS file |
|
| 205 | + * @param string $application application id |
|
| 206 | + * @param string $languageCode language code, defaults to the current locale |
|
| 207 | + * @param bool $init whether the translations should be loaded early or not |
|
| 208 | + * @since 8.0.0 |
|
| 209 | + */ |
|
| 210 | + public static function addTranslations($application, $languageCode = null, $init = false) { |
|
| 211 | + if (is_null($languageCode)) { |
|
| 212 | + $languageCode = \OCP\Server::get(IFactory::class)->findLanguage($application); |
|
| 213 | + } |
|
| 214 | + if (!empty($application)) { |
|
| 215 | + $path = "$application/l10n/$languageCode"; |
|
| 216 | + } else { |
|
| 217 | + $path = "l10n/$languageCode"; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + if ($init) { |
|
| 221 | + self::$scriptsInit[] = $path; |
|
| 222 | + } else { |
|
| 223 | + self::$scripts[$application][] = $path; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Add a custom element to the header |
|
| 229 | + * If $text is null then the element will be written as empty element. |
|
| 230 | + * So use "" to get a closing tag. |
|
| 231 | + * @param string $tag tag name of the element |
|
| 232 | + * @param array $attributes array of attributes for the element |
|
| 233 | + * @param string $text the text content for the element |
|
| 234 | + * @since 4.0.0 |
|
| 235 | + */ |
|
| 236 | + public static function addHeader($tag, $attributes, $text = null) { |
|
| 237 | + \OC_Util::addHeader($tag, $attributes, $text); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Creates an absolute url to the given app and file. |
|
| 242 | + * @param string $app app |
|
| 243 | + * @param string $file file |
|
| 244 | + * @param array $args array with param=>value, will be appended to the returned url |
|
| 245 | + * The value of $args will be urlencoded |
|
| 246 | + * @return string the url |
|
| 247 | + * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
| 248 | + */ |
|
| 249 | + public static function linkToAbsolute($app, $file, $args = []) { |
|
| 250 | + $urlGenerator = \OCP\Server::get(IURLGenerator::class); |
|
| 251 | + return $urlGenerator->getAbsoluteURL( |
|
| 252 | + $urlGenerator->linkTo($app, $file, $args) |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Creates an absolute url for remote use. |
|
| 258 | + * @param string $service id |
|
| 259 | + * @return string the url |
|
| 260 | + * @since 4.0.0 |
|
| 261 | + */ |
|
| 262 | + public static function linkToRemote($service) { |
|
| 263 | + $urlGenerator = \OCP\Server::get(IURLGenerator::class); |
|
| 264 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
| 265 | + return $urlGenerator->getAbsoluteURL( |
|
| 266 | + $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
| 267 | + ); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Returns the server host name without an eventual port number |
|
| 272 | + * @return string the server hostname |
|
| 273 | + * @since 5.0.0 |
|
| 274 | + */ |
|
| 275 | + public static function getServerHostName() { |
|
| 276 | + $host_name = \OCP\Server::get(IRequest::class)->getServerHost(); |
|
| 277 | + // strip away port number (if existing) |
|
| 278 | + $colon_pos = strpos($host_name, ':'); |
|
| 279 | + if ($colon_pos != false) { |
|
| 280 | + $host_name = substr($host_name, 0, $colon_pos); |
|
| 281 | + } |
|
| 282 | + return $host_name; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Returns the default email address |
|
| 287 | + * @param string $user_part the user part of the address |
|
| 288 | + * @return string the default email address |
|
| 289 | + * |
|
| 290 | + * Assembles a default email address (using the server hostname |
|
| 291 | + * and the given user part, and returns it |
|
| 292 | + * Example: when given lostpassword-noreply as $user_part param, |
|
| 293 | + * and is currently accessed via http(s)://example.com/, |
|
| 294 | + * it would return '[email protected]' |
|
| 295 | + * |
|
| 296 | + * If the configuration value 'mail_from_address' is set in |
|
| 297 | + * config.php, this value will override the $user_part that |
|
| 298 | + * is passed to this function |
|
| 299 | + * @since 5.0.0 |
|
| 300 | + */ |
|
| 301 | + public static function getDefaultEmailAddress(string $user_part): string { |
|
| 302 | + $config = \OCP\Server::get(IConfig::class); |
|
| 303 | + $user_part = $config->getSystemValueString('mail_from_address', $user_part); |
|
| 304 | + $host_name = self::getServerHostName(); |
|
| 305 | + $host_name = $config->getSystemValueString('mail_domain', $host_name); |
|
| 306 | + $defaultEmailAddress = $user_part . '@' . $host_name; |
|
| 307 | + |
|
| 308 | + $mailer = \OCP\Server::get(IMailer::class); |
|
| 309 | + if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
| 310 | + return $defaultEmailAddress; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
| 314 | + return $user_part . '@localhost.localdomain'; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Converts string to int of float depending if it fits an int |
|
| 319 | + * @param numeric-string|float|int $number numeric string |
|
| 320 | + * @return int|float int if it fits, float if it is too big |
|
| 321 | + * @since 26.0.0 |
|
| 322 | + */ |
|
| 323 | + public static function numericToNumber(string|float|int $number): int|float { |
|
| 324 | + /* This is a hack to cast to (int|float) */ |
|
| 325 | + return 0 + (string)$number; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Make a human file size (2048 to 2 kB) |
|
| 330 | + * @param int|float $bytes file size in bytes |
|
| 331 | + * @return string a human readable file size |
|
| 332 | + * @since 4.0.0 |
|
| 333 | + */ |
|
| 334 | + public static function humanFileSize(int|float $bytes): string { |
|
| 335 | + if ($bytes < 0) { |
|
| 336 | + return '?'; |
|
| 337 | + } |
|
| 338 | + if ($bytes < 1024) { |
|
| 339 | + return "$bytes B"; |
|
| 340 | + } |
|
| 341 | + $bytes = round($bytes / 1024, 0); |
|
| 342 | + if ($bytes < 1024) { |
|
| 343 | + return "$bytes KB"; |
|
| 344 | + } |
|
| 345 | + $bytes = round($bytes / 1024, 1); |
|
| 346 | + if ($bytes < 1024) { |
|
| 347 | + return "$bytes MB"; |
|
| 348 | + } |
|
| 349 | + $bytes = round($bytes / 1024, 1); |
|
| 350 | + if ($bytes < 1024) { |
|
| 351 | + return "$bytes GB"; |
|
| 352 | + } |
|
| 353 | + $bytes = round($bytes / 1024, 1); |
|
| 354 | + if ($bytes < 1024) { |
|
| 355 | + return "$bytes TB"; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + $bytes = round($bytes / 1024, 1); |
|
| 359 | + return "$bytes PB"; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Make a computer file size (2 kB to 2048) |
|
| 364 | + * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
|
| 365 | + * |
|
| 366 | + * @param string $str file size in a fancy format |
|
| 367 | + * @return false|int|float a file size in bytes |
|
| 368 | + * @since 4.0.0 |
|
| 369 | + */ |
|
| 370 | + public static function computerFileSize(string $str): false|int|float { |
|
| 371 | + $str = strtolower($str); |
|
| 372 | + if (is_numeric($str)) { |
|
| 373 | + return Util::numericToNumber($str); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + $bytes_array = [ |
|
| 377 | + 'b' => 1, |
|
| 378 | + 'k' => 1024, |
|
| 379 | + 'kb' => 1024, |
|
| 380 | + 'mb' => 1024 * 1024, |
|
| 381 | + 'm' => 1024 * 1024, |
|
| 382 | + 'gb' => 1024 * 1024 * 1024, |
|
| 383 | + 'g' => 1024 * 1024 * 1024, |
|
| 384 | + 'tb' => 1024 * 1024 * 1024 * 1024, |
|
| 385 | + 't' => 1024 * 1024 * 1024 * 1024, |
|
| 386 | + 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
| 387 | + 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
| 388 | + ]; |
|
| 389 | + |
|
| 390 | + $bytes = (float)$str; |
|
| 391 | + |
|
| 392 | + if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && isset($bytes_array[$matches[1]])) { |
|
| 393 | + $bytes *= $bytes_array[$matches[1]]; |
|
| 394 | + } else { |
|
| 395 | + return false; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + return Util::numericToNumber(round($bytes)); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * connects a function to a hook |
|
| 403 | + * |
|
| 404 | + * @param string $signalClass class name of emitter |
|
| 405 | + * @param string $signalName name of signal |
|
| 406 | + * @param string|object $slotClass class name of slot |
|
| 407 | + * @param string $slotName name of slot |
|
| 408 | + * @return bool |
|
| 409 | + * |
|
| 410 | + * This function makes it very easy to connect to use hooks. |
|
| 411 | + * |
|
| 412 | + * TODO: write example |
|
| 413 | + * @since 4.0.0 |
|
| 414 | + * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener |
|
| 415 | + */ |
|
| 416 | + public static function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
| 417 | + return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Emits a signal. To get data from the slot use references! |
|
| 422 | + * @param string $signalclass class name of emitter |
|
| 423 | + * @param string $signalname name of signal |
|
| 424 | + * @param array $params default: array() array with additional data |
|
| 425 | + * @return bool true if slots exists or false if not |
|
| 426 | + * |
|
| 427 | + * TODO: write example |
|
| 428 | + * @since 4.0.0 |
|
| 429 | + * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent |
|
| 430 | + */ |
|
| 431 | + public static function emitHook($signalclass, $signalname, $params = []) { |
|
| 432 | + return \OC_Hook::emit($signalclass, $signalname, $params); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
| 437 | + * multiple Template elements which invoke `callRegister`. If the value |
|
| 438 | + * would not be cached these unit-tests would fail. |
|
| 439 | + * @var string |
|
| 440 | + */ |
|
| 441 | + private static $token = ''; |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Register an get/post call. This is important to prevent CSRF attacks |
|
| 445 | + * @since 4.5.0 |
|
| 446 | + * @deprecated 32.0.0 directly use CsrfTokenManager instead |
|
| 447 | + */ |
|
| 448 | + public static function callRegister() { |
|
| 449 | + if (self::$token === '') { |
|
| 450 | + self::$token = \OCP\Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue(); |
|
| 451 | + } |
|
| 452 | + return self::$token; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Used to sanitize HTML |
|
| 457 | + * |
|
| 458 | + * This function is used to sanitize HTML and should be applied on any |
|
| 459 | + * string or array of strings before displaying it on a web page. |
|
| 460 | + * |
|
| 461 | + * @param string|string[] $value |
|
| 462 | + * @return ($value is array ? string[] : string) an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
| 463 | + * @since 4.5.0 |
|
| 464 | + */ |
|
| 465 | + public static function sanitizeHTML($value) { |
|
| 466 | + return \OC_Util::sanitizeHTML($value); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * Public function to encode url parameters |
|
| 471 | + * |
|
| 472 | + * This function is used to encode path to file before output. |
|
| 473 | + * Encoding is done according to RFC 3986 with one exception: |
|
| 474 | + * Character '/' is preserved as is. |
|
| 475 | + * |
|
| 476 | + * @param string $component part of URI to encode |
|
| 477 | + * @return string |
|
| 478 | + * @since 6.0.0 |
|
| 479 | + */ |
|
| 480 | + public static function encodePath($component) { |
|
| 481 | + return \OC_Util::encodePath($component); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
| 486 | + * |
|
| 487 | + * @param array $input The array to work on |
|
| 488 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
| 489 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
| 490 | + * @return array |
|
| 491 | + * @since 4.5.0 |
|
| 492 | + */ |
|
| 493 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
| 494 | + $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
| 495 | + $ret = []; |
|
| 496 | + foreach ($input as $k => $v) { |
|
| 497 | + $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
| 498 | + } |
|
| 499 | + return $ret; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * performs a search in a nested array |
|
| 504 | + * |
|
| 505 | + * @param array $haystack the array to be searched |
|
| 506 | + * @param string $needle the search string |
|
| 507 | + * @param mixed $index optional, only search this key name |
|
| 508 | + * @return mixed the key of the matching field, otherwise false |
|
| 509 | + * @since 4.5.0 |
|
| 510 | + * @deprecated 15.0.0 |
|
| 511 | + */ |
|
| 512 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
| 513 | + $aIt = new \RecursiveArrayIterator($haystack); |
|
| 514 | + $it = new \RecursiveIteratorIterator($aIt); |
|
| 515 | + |
|
| 516 | + while ($it->valid()) { |
|
| 517 | + if (((isset($index) and ($it->key() == $index)) or !isset($index)) and ($it->current() == $needle)) { |
|
| 518 | + return $aIt->key(); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + $it->next(); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + return false; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
| 529 | + * |
|
| 530 | + * @param string $dir the current folder where the user currently operates |
|
| 531 | + * @param int|float|null $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
| 532 | + * @return int|float number of bytes representing |
|
| 533 | + * @since 5.0.0 |
|
| 534 | + */ |
|
| 535 | + public static function maxUploadFilesize(string $dir, int|float|null $free = null): int|float { |
|
| 536 | + if (is_null($free) || $free < 0) { |
|
| 537 | + $free = self::freeSpace($dir); |
|
| 538 | + } |
|
| 539 | + return min($free, self::uploadLimit()); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * Calculate free space left within user quota |
|
| 544 | + * @param string $dir the current folder where the user currently operates |
|
| 545 | + * @return int|float number of bytes representing |
|
| 546 | + * @since 7.0.0 |
|
| 547 | + */ |
|
| 548 | + public static function freeSpace(string $dir): int|float { |
|
| 549 | + $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
| 550 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 551 | + $freeSpace = max($freeSpace, 0); |
|
| 552 | + return $freeSpace; |
|
| 553 | + } else { |
|
| 554 | + return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * Calculate PHP upload limit |
|
| 560 | + * |
|
| 561 | + * @return int|float number of bytes representing |
|
| 562 | + * @since 7.0.0 |
|
| 563 | + */ |
|
| 564 | + public static function uploadLimit(): int|float { |
|
| 565 | + $ini = Server::get(IniGetWrapper::class); |
|
| 566 | + $upload_max_filesize = self::computerFileSize($ini->get('upload_max_filesize')) ?: 0; |
|
| 567 | + $post_max_size = self::computerFileSize($ini->get('post_max_size')) ?: 0; |
|
| 568 | + if ($upload_max_filesize === 0 && $post_max_size === 0) { |
|
| 569 | + return INF; |
|
| 570 | + } elseif ($upload_max_filesize === 0 || $post_max_size === 0) { |
|
| 571 | + return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
| 572 | + } else { |
|
| 573 | + return min($upload_max_filesize, $post_max_size); |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Compare two strings to provide a natural sort |
|
| 579 | + * @param string $a first string to compare |
|
| 580 | + * @param string $b second string to compare |
|
| 581 | + * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
| 582 | + * or 0 if the strings are identical |
|
| 583 | + * @since 7.0.0 |
|
| 584 | + */ |
|
| 585 | + public static function naturalSortCompare($a, $b) { |
|
| 586 | + return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + /** |
|
| 590 | + * Check if a password is required for each public link |
|
| 591 | + * |
|
| 592 | + * @param bool $checkGroupMembership Check group membership exclusion |
|
| 593 | + * @return boolean |
|
| 594 | + * @since 7.0.0 |
|
| 595 | + */ |
|
| 596 | + public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) { |
|
| 597 | + return \OC_Util::isPublicLinkPasswordRequired($checkGroupMembership); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * check if share API enforces a default expire date |
|
| 602 | + * @return boolean |
|
| 603 | + * @since 8.0.0 |
|
| 604 | + */ |
|
| 605 | + public static function isDefaultExpireDateEnforced() { |
|
| 606 | + return \OC_Util::isDefaultExpireDateEnforced(); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + protected static $needUpgradeCache = null; |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Checks whether the current version needs upgrade. |
|
| 613 | + * |
|
| 614 | + * @return bool true if upgrade is needed, false otherwise |
|
| 615 | + * @since 7.0.0 |
|
| 616 | + */ |
|
| 617 | + public static function needUpgrade() { |
|
| 618 | + if (!isset(self::$needUpgradeCache)) { |
|
| 619 | + self::$needUpgradeCache = \OC_Util::needUpgrade(\OCP\Server::get(\OC\SystemConfig::class)); |
|
| 620 | + } |
|
| 621 | + return self::$needUpgradeCache; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * Sometimes a string has to be shortened to fit within a certain maximum |
|
| 626 | + * data length in bytes. substr() you may break multibyte characters, |
|
| 627 | + * because it operates on single byte level. mb_substr() operates on |
|
| 628 | + * characters, so does not ensure that the shortened string satisfies the |
|
| 629 | + * max length in bytes. |
|
| 630 | + * |
|
| 631 | + * For example, json_encode is messing with multibyte characters a lot, |
|
| 632 | + * replacing them with something along "\u1234". |
|
| 633 | + * |
|
| 634 | + * This function shortens the string with by $accuracy (-5) from |
|
| 635 | + * $dataLength characters, until it fits within $dataLength bytes. |
|
| 636 | + * |
|
| 637 | + * @since 23.0.0 |
|
| 638 | + */ |
|
| 639 | + public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string { |
|
| 640 | + $temp = mb_substr($subject, 0, $dataLength); |
|
| 641 | + // json encodes encapsulates the string in double quotes, they need to be substracted |
|
| 642 | + while ((strlen(json_encode($temp)) - 2) > $dataLength) { |
|
| 643 | + $temp = mb_substr($temp, 0, -$accuracy); |
|
| 644 | + } |
|
| 645 | + return $temp; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Check if a function is enabled in the php configuration |
|
| 650 | + * |
|
| 651 | + * @since 25.0.0 |
|
| 652 | + */ |
|
| 653 | + public static function isFunctionEnabled(string $functionName): bool { |
|
| 654 | + if (!function_exists($functionName)) { |
|
| 655 | + return false; |
|
| 656 | + } |
|
| 657 | + $ini = Server::get(IniGetWrapper::class); |
|
| 658 | + $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
| 659 | + $disabled = array_map('trim', $disabled); |
|
| 660 | + if (in_array($functionName, $disabled)) { |
|
| 661 | + return false; |
|
| 662 | + } |
|
| 663 | + return true; |
|
| 664 | + } |
|
| 665 | 665 | } |
@@ -261,9 +261,9 @@ discard block |
||
| 261 | 261 | */ |
| 262 | 262 | public static function linkToRemote($service) { |
| 263 | 263 | $urlGenerator = \OCP\Server::get(IURLGenerator::class); |
| 264 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
| 264 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php').'/'.$service; |
|
| 265 | 265 | return $urlGenerator->getAbsoluteURL( |
| 266 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
| 266 | + $remoteBase.(($service[strlen($service) - 1] != '/') ? '/' : '') |
|
| 267 | 267 | ); |
| 268 | 268 | } |
| 269 | 269 | |
@@ -303,7 +303,7 @@ discard block |
||
| 303 | 303 | $user_part = $config->getSystemValueString('mail_from_address', $user_part); |
| 304 | 304 | $host_name = self::getServerHostName(); |
| 305 | 305 | $host_name = $config->getSystemValueString('mail_domain', $host_name); |
| 306 | - $defaultEmailAddress = $user_part . '@' . $host_name; |
|
| 306 | + $defaultEmailAddress = $user_part.'@'.$host_name; |
|
| 307 | 307 | |
| 308 | 308 | $mailer = \OCP\Server::get(IMailer::class); |
| 309 | 309 | if ($mailer->validateMailAddress($defaultEmailAddress)) { |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
| 314 | - return $user_part . '@localhost.localdomain'; |
|
| 314 | + return $user_part.'@localhost.localdomain'; |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | /** |
@@ -320,9 +320,9 @@ discard block |
||
| 320 | 320 | * @return int|float int if it fits, float if it is too big |
| 321 | 321 | * @since 26.0.0 |
| 322 | 322 | */ |
| 323 | - public static function numericToNumber(string|float|int $number): int|float { |
|
| 323 | + public static function numericToNumber(string | float | int $number): int | float { |
|
| 324 | 324 | /* This is a hack to cast to (int|float) */ |
| 325 | - return 0 + (string)$number; |
|
| 325 | + return 0 + (string) $number; |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
@@ -331,7 +331,7 @@ discard block |
||
| 331 | 331 | * @return string a human readable file size |
| 332 | 332 | * @since 4.0.0 |
| 333 | 333 | */ |
| 334 | - public static function humanFileSize(int|float $bytes): string { |
|
| 334 | + public static function humanFileSize(int | float $bytes): string { |
|
| 335 | 335 | if ($bytes < 0) { |
| 336 | 336 | return '?'; |
| 337 | 337 | } |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | * @return false|int|float a file size in bytes |
| 368 | 368 | * @since 4.0.0 |
| 369 | 369 | */ |
| 370 | - public static function computerFileSize(string $str): false|int|float { |
|
| 370 | + public static function computerFileSize(string $str): false | int | float { |
|
| 371 | 371 | $str = strtolower($str); |
| 372 | 372 | if (is_numeric($str)) { |
| 373 | 373 | return Util::numericToNumber($str); |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
| 388 | 388 | ]; |
| 389 | 389 | |
| 390 | - $bytes = (float)$str; |
|
| 390 | + $bytes = (float) $str; |
|
| 391 | 391 | |
| 392 | 392 | if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && isset($bytes_array[$matches[1]])) { |
| 393 | 393 | $bytes *= $bytes_array[$matches[1]]; |
@@ -532,7 +532,7 @@ discard block |
||
| 532 | 532 | * @return int|float number of bytes representing |
| 533 | 533 | * @since 5.0.0 |
| 534 | 534 | */ |
| 535 | - public static function maxUploadFilesize(string $dir, int|float|null $free = null): int|float { |
|
| 535 | + public static function maxUploadFilesize(string $dir, int | float | null $free = null): int | float { |
|
| 536 | 536 | if (is_null($free) || $free < 0) { |
| 537 | 537 | $free = self::freeSpace($dir); |
| 538 | 538 | } |
@@ -545,13 +545,13 @@ discard block |
||
| 545 | 545 | * @return int|float number of bytes representing |
| 546 | 546 | * @since 7.0.0 |
| 547 | 547 | */ |
| 548 | - public static function freeSpace(string $dir): int|float { |
|
| 548 | + public static function freeSpace(string $dir): int | float { |
|
| 549 | 549 | $freeSpace = \OC\Files\Filesystem::free_space($dir); |
| 550 | 550 | if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
| 551 | 551 | $freeSpace = max($freeSpace, 0); |
| 552 | 552 | return $freeSpace; |
| 553 | 553 | } else { |
| 554 | - return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 554 | + return (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 555 | 555 | } |
| 556 | 556 | } |
| 557 | 557 | |
@@ -561,7 +561,7 @@ discard block |
||
| 561 | 561 | * @return int|float number of bytes representing |
| 562 | 562 | * @since 7.0.0 |
| 563 | 563 | */ |
| 564 | - public static function uploadLimit(): int|float { |
|
| 564 | + public static function uploadLimit(): int | float { |
|
| 565 | 565 | $ini = Server::get(IniGetWrapper::class); |
| 566 | 566 | $upload_max_filesize = self::computerFileSize($ini->get('upload_max_filesize')) ?: 0; |
| 567 | 567 | $post_max_size = self::computerFileSize($ini->get('post_max_size')) ?: 0; |
@@ -31,509 +31,509 @@ |
||
| 31 | 31 | * } |
| 32 | 32 | */ |
| 33 | 33 | class OC_Helper { |
| 34 | - private static $templateManager; |
|
| 35 | - private static ?ICacheFactory $cacheFactory = null; |
|
| 36 | - private static ?bool $quotaIncludeExternalStorage = null; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Make a human file size |
|
| 40 | - * @param int|float $bytes file size in bytes |
|
| 41 | - * @return string a human readable file size |
|
| 42 | - * @deprecated 4.0.0 replaced with \OCP\Util::humanFileSize |
|
| 43 | - * |
|
| 44 | - * Makes 2048 to 2 kB. |
|
| 45 | - */ |
|
| 46 | - public static function humanFileSize(int|float $bytes): string { |
|
| 47 | - return \OCP\Util::humanFileSize($bytes); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Make a computer file size |
|
| 52 | - * @param string $str file size in human readable format |
|
| 53 | - * @return false|int|float a file size in bytes |
|
| 54 | - * @deprecated 4.0.0 Use \OCP\Util::computerFileSize |
|
| 55 | - * |
|
| 56 | - * Makes 2kB to 2048. |
|
| 57 | - * |
|
| 58 | - * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
|
| 59 | - */ |
|
| 60 | - public static function computerFileSize(string $str): false|int|float { |
|
| 61 | - return \OCP\Util::computerFileSize($str); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Recursive copying of folders |
|
| 66 | - * @param string $src source folder |
|
| 67 | - * @param string $dest target folder |
|
| 68 | - * @return void |
|
| 69 | - */ |
|
| 70 | - public static function copyr($src, $dest) { |
|
| 71 | - if (!file_exists($src)) { |
|
| 72 | - return; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - if (is_dir($src)) { |
|
| 76 | - if (!is_dir($dest)) { |
|
| 77 | - mkdir($dest); |
|
| 78 | - } |
|
| 79 | - $files = scandir($src); |
|
| 80 | - foreach ($files as $file) { |
|
| 81 | - if ($file != '.' && $file != '..') { |
|
| 82 | - self::copyr("$src/$file", "$dest/$file"); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - } else { |
|
| 86 | - $validator = \OCP\Server::get(FilenameValidator::class); |
|
| 87 | - if (!$validator->isForbidden($src)) { |
|
| 88 | - copy($src, $dest); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Recursive deletion of folders |
|
| 95 | - * @param string $dir path to the folder |
|
| 96 | - * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
| 97 | - * @return bool |
|
| 98 | - * @deprecated 5.0.0 use \OCP\Files::rmdirr instead |
|
| 99 | - */ |
|
| 100 | - public static function rmdirr($dir, $deleteSelf = true) { |
|
| 101 | - return \OCP\Files::rmdirr($dir, $deleteSelf); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @deprecated 18.0.0 |
|
| 106 | - * @return \OC\Files\Type\TemplateManager |
|
| 107 | - */ |
|
| 108 | - public static function getFileTemplateManager() { |
|
| 109 | - if (!self::$templateManager) { |
|
| 110 | - self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
| 111 | - } |
|
| 112 | - return self::$templateManager; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * detect if a given program is found in the search PATH |
|
| 117 | - * |
|
| 118 | - * @param string $name |
|
| 119 | - * @param bool $path |
|
| 120 | - * @internal param string $program name |
|
| 121 | - * @internal param string $optional search path, defaults to $PATH |
|
| 122 | - * @return bool true if executable program found in path |
|
| 123 | - * @deprecated 32.0.0 use the \OCP\IBinaryFinder |
|
| 124 | - */ |
|
| 125 | - public static function canExecute($name, $path = false) { |
|
| 126 | - // path defaults to PATH from environment if not set |
|
| 127 | - if ($path === false) { |
|
| 128 | - $path = getenv('PATH'); |
|
| 129 | - } |
|
| 130 | - // we look for an executable file of that name |
|
| 131 | - $exts = ['']; |
|
| 132 | - $check_fn = 'is_executable'; |
|
| 133 | - // Default check will be done with $path directories : |
|
| 134 | - $dirs = explode(PATH_SEPARATOR, $path); |
|
| 135 | - // WARNING : We have to check if open_basedir is enabled : |
|
| 136 | - $obd = OC::$server->get(IniGetWrapper::class)->getString('open_basedir'); |
|
| 137 | - if ($obd != 'none') { |
|
| 138 | - $obd_values = explode(PATH_SEPARATOR, $obd); |
|
| 139 | - if (count($obd_values) > 0 and $obd_values[0]) { |
|
| 140 | - // open_basedir is in effect ! |
|
| 141 | - // We need to check if the program is in one of these dirs : |
|
| 142 | - $dirs = $obd_values; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - foreach ($dirs as $dir) { |
|
| 146 | - foreach ($exts as $ext) { |
|
| 147 | - if ($check_fn("$dir/$name" . $ext)) { |
|
| 148 | - return true; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - return false; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * copy the contents of one stream to another |
|
| 157 | - * |
|
| 158 | - * @param resource $source |
|
| 159 | - * @param resource $target |
|
| 160 | - * @return array the number of bytes copied and result |
|
| 161 | - */ |
|
| 162 | - public static function streamCopy($source, $target) { |
|
| 163 | - if (!$source or !$target) { |
|
| 164 | - return [0, false]; |
|
| 165 | - } |
|
| 166 | - $bufSize = 8192; |
|
| 167 | - $result = true; |
|
| 168 | - $count = 0; |
|
| 169 | - while (!feof($source)) { |
|
| 170 | - $buf = fread($source, $bufSize); |
|
| 171 | - $bytesWritten = fwrite($target, $buf); |
|
| 172 | - if ($bytesWritten !== false) { |
|
| 173 | - $count += $bytesWritten; |
|
| 174 | - } |
|
| 175 | - // note: strlen is expensive so only use it when necessary, |
|
| 176 | - // on the last block |
|
| 177 | - if ($bytesWritten === false |
|
| 178 | - || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
| 179 | - ) { |
|
| 180 | - // write error, could be disk full ? |
|
| 181 | - $result = false; |
|
| 182 | - break; |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - return [$count, $result]; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Adds a suffix to the name in case the file exists |
|
| 190 | - * |
|
| 191 | - * @param string $path |
|
| 192 | - * @param string $filename |
|
| 193 | - * @return string |
|
| 194 | - */ |
|
| 195 | - public static function buildNotExistingFileName($path, $filename) { |
|
| 196 | - $view = \OC\Files\Filesystem::getView(); |
|
| 197 | - return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Adds a suffix to the name in case the file exists |
|
| 202 | - * |
|
| 203 | - * @param string $path |
|
| 204 | - * @param string $filename |
|
| 205 | - * @return string |
|
| 206 | - */ |
|
| 207 | - public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
| 208 | - if ($path === '/') { |
|
| 209 | - $path = ''; |
|
| 210 | - } |
|
| 211 | - if ($pos = strrpos($filename, '.')) { |
|
| 212 | - $name = substr($filename, 0, $pos); |
|
| 213 | - $ext = substr($filename, $pos); |
|
| 214 | - } else { |
|
| 215 | - $name = $filename; |
|
| 216 | - $ext = ''; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $newpath = $path . '/' . $filename; |
|
| 220 | - if ($view->file_exists($newpath)) { |
|
| 221 | - if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
| 222 | - //Replace the last "(number)" with "(number+1)" |
|
| 223 | - $last_match = count($matches[0]) - 1; |
|
| 224 | - $counter = $matches[1][$last_match][0] + 1; |
|
| 225 | - $offset = $matches[0][$last_match][1]; |
|
| 226 | - $match_length = strlen($matches[0][$last_match][0]); |
|
| 227 | - } else { |
|
| 228 | - $counter = 2; |
|
| 229 | - $match_length = 0; |
|
| 230 | - $offset = false; |
|
| 231 | - } |
|
| 232 | - do { |
|
| 233 | - if ($offset) { |
|
| 234 | - //Replace the last "(number)" with "(number+1)" |
|
| 235 | - $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
| 236 | - } else { |
|
| 237 | - $newname = $name . ' (' . $counter . ')'; |
|
| 238 | - } |
|
| 239 | - $newpath = $path . '/' . $newname . $ext; |
|
| 240 | - $counter++; |
|
| 241 | - } while ($view->file_exists($newpath)); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - return $newpath; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
| 249 | - * Based on https://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
| 250 | - * |
|
| 251 | - * @param array $input The array to work on |
|
| 252 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
| 253 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
| 254 | - * @return array |
|
| 255 | - * @deprecated 4.5.0 use \OCP\Util::mb_array_change_key_case instead |
|
| 256 | - */ |
|
| 257 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
| 258 | - return \OCP\Util::mb_array_change_key_case($input, $case, $encoding); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Performs a search in a nested array. |
|
| 263 | - * Taken from https://www.php.net/manual/en/function.array-search.php#97645 |
|
| 264 | - * |
|
| 265 | - * @param array $haystack the array to be searched |
|
| 266 | - * @param string $needle the search string |
|
| 267 | - * @param mixed $index optional, only search this key name |
|
| 268 | - * @return mixed the key of the matching field, otherwise false |
|
| 269 | - * @deprecated 4.5.0 - use \OCP\Util::recursiveArraySearch |
|
| 270 | - */ |
|
| 271 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
| 272 | - return \OCP\Util::recursiveArraySearch($haystack, $needle, $index); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
| 277 | - * |
|
| 278 | - * @param string $dir the current folder where the user currently operates |
|
| 279 | - * @param int|float $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
| 280 | - * @return int|float number of bytes representing |
|
| 281 | - * @deprecated 5.0.0 - use \OCP\Util::maxUploadFilesize |
|
| 282 | - */ |
|
| 283 | - public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
| 284 | - return \OCP\Util::maxUploadFilesize($dir, $freeSpace); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Calculate free space left within user quota |
|
| 289 | - * |
|
| 290 | - * @param string $dir the current folder where the user currently operates |
|
| 291 | - * @return int|float number of bytes representing |
|
| 292 | - * @deprecated 7.0.0 - use \OCP\Util::freeSpace |
|
| 293 | - */ |
|
| 294 | - public static function freeSpace($dir) { |
|
| 295 | - return \OCP\Util::freeSpace($dir); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Calculate PHP upload limit |
|
| 300 | - * |
|
| 301 | - * @return int|float PHP upload file size limit |
|
| 302 | - * @deprecated 7.0.0 - use \OCP\Util::uploadLimit |
|
| 303 | - */ |
|
| 304 | - public static function uploadLimit() { |
|
| 305 | - return \OCP\Util::uploadLimit(); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Checks if a function is available |
|
| 310 | - * |
|
| 311 | - * @deprecated 25.0.0 use \OCP\Util::isFunctionEnabled instead |
|
| 312 | - */ |
|
| 313 | - public static function is_function_enabled(string $function_name): bool { |
|
| 314 | - return \OCP\Util::isFunctionEnabled($function_name); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Try to find a program |
|
| 319 | - * @deprecated 25.0.0 Use \OC\BinaryFinder directly |
|
| 320 | - */ |
|
| 321 | - public static function findBinaryPath(string $program): ?string { |
|
| 322 | - $result = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath($program); |
|
| 323 | - return $result !== false ? $result : null; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Calculate the disc space for the given path |
|
| 328 | - * |
|
| 329 | - * BEWARE: this requires that Util::setupFS() was called |
|
| 330 | - * already ! |
|
| 331 | - * |
|
| 332 | - * @param string $path |
|
| 333 | - * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
| 334 | - * @param bool $includeMountPoints whether to include mount points in the size calculation |
|
| 335 | - * @param bool $useCache whether to use the cached quota values |
|
| 336 | - * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct |
|
| 337 | - * @return StorageInfo |
|
| 338 | - * @throws \OCP\Files\NotFoundException |
|
| 339 | - */ |
|
| 340 | - public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) { |
|
| 341 | - if (!self::$cacheFactory) { |
|
| 342 | - self::$cacheFactory = \OC::$server->get(ICacheFactory::class); |
|
| 343 | - } |
|
| 344 | - $memcache = self::$cacheFactory->createLocal('storage_info'); |
|
| 345 | - |
|
| 346 | - // return storage info without adding mount points |
|
| 347 | - if (self::$quotaIncludeExternalStorage === null) { |
|
| 348 | - self::$quotaIncludeExternalStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $view = Filesystem::getView(); |
|
| 352 | - if (!$view) { |
|
| 353 | - throw new \OCP\Files\NotFoundException(); |
|
| 354 | - } |
|
| 355 | - $fullPath = Filesystem::normalizePath($view->getAbsolutePath($path)); |
|
| 356 | - |
|
| 357 | - $cacheKey = $fullPath . '::' . ($includeMountPoints ? 'include' : 'exclude'); |
|
| 358 | - if ($useCache) { |
|
| 359 | - $cached = $memcache->get($cacheKey); |
|
| 360 | - if ($cached) { |
|
| 361 | - return $cached; |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - if (!$rootInfo) { |
|
| 366 | - $rootInfo = \OC\Files\Filesystem::getFileInfo($path, self::$quotaIncludeExternalStorage ? 'ext' : false); |
|
| 367 | - } |
|
| 368 | - if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
| 369 | - throw new \OCP\Files\NotFoundException('The root directory of the user\'s files is missing'); |
|
| 370 | - } |
|
| 371 | - $used = $rootInfo->getSize($includeMountPoints); |
|
| 372 | - if ($used < 0) { |
|
| 373 | - $used = 0.0; |
|
| 374 | - } |
|
| 375 | - /** @var int|float $quota */ |
|
| 376 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
| 377 | - $mount = $rootInfo->getMountPoint(); |
|
| 378 | - $storage = $mount->getStorage(); |
|
| 379 | - $sourceStorage = $storage; |
|
| 380 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
| 381 | - self::$quotaIncludeExternalStorage = false; |
|
| 382 | - } |
|
| 383 | - if (self::$quotaIncludeExternalStorage) { |
|
| 384 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
| 385 | - || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
| 386 | - ) { |
|
| 387 | - /** @var \OC\Files\Storage\Home $storage */ |
|
| 388 | - $user = $storage->getUser(); |
|
| 389 | - } else { |
|
| 390 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 391 | - } |
|
| 392 | - $quota = OC_Util::getUserQuota($user); |
|
| 393 | - if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 394 | - // always get free space / total space from root + mount points |
|
| 395 | - return self::getGlobalStorageInfo($quota, $user, $mount); |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // TODO: need a better way to get total space from storage |
|
| 400 | - if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
| 401 | - /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
| 402 | - $quota = $sourceStorage->getQuota(); |
|
| 403 | - } |
|
| 404 | - try { |
|
| 405 | - $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
| 406 | - if (is_bool($free)) { |
|
| 407 | - $free = 0.0; |
|
| 408 | - } |
|
| 409 | - } catch (\Exception $e) { |
|
| 410 | - if ($path === '') { |
|
| 411 | - throw $e; |
|
| 412 | - } |
|
| 413 | - /** @var LoggerInterface $logger */ |
|
| 414 | - $logger = \OC::$server->get(LoggerInterface::class); |
|
| 415 | - $logger->warning('Error while getting quota info, using root quota', ['exception' => $e]); |
|
| 416 | - $rootInfo = self::getStorageInfo(''); |
|
| 417 | - $memcache->set($cacheKey, $rootInfo, 5 * 60); |
|
| 418 | - return $rootInfo; |
|
| 419 | - } |
|
| 420 | - if ($free >= 0) { |
|
| 421 | - $total = $free + $used; |
|
| 422 | - } else { |
|
| 423 | - $total = $free; //either unknown or unlimited |
|
| 424 | - } |
|
| 425 | - if ($total > 0) { |
|
| 426 | - if ($quota > 0 && $total > $quota) { |
|
| 427 | - $total = $quota; |
|
| 428 | - } |
|
| 429 | - // prevent division by zero or error codes (negative values) |
|
| 430 | - $relative = round(($used / $total) * 10000) / 100; |
|
| 431 | - } else { |
|
| 432 | - $relative = 0; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /* |
|
| 34 | + private static $templateManager; |
|
| 35 | + private static ?ICacheFactory $cacheFactory = null; |
|
| 36 | + private static ?bool $quotaIncludeExternalStorage = null; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Make a human file size |
|
| 40 | + * @param int|float $bytes file size in bytes |
|
| 41 | + * @return string a human readable file size |
|
| 42 | + * @deprecated 4.0.0 replaced with \OCP\Util::humanFileSize |
|
| 43 | + * |
|
| 44 | + * Makes 2048 to 2 kB. |
|
| 45 | + */ |
|
| 46 | + public static function humanFileSize(int|float $bytes): string { |
|
| 47 | + return \OCP\Util::humanFileSize($bytes); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Make a computer file size |
|
| 52 | + * @param string $str file size in human readable format |
|
| 53 | + * @return false|int|float a file size in bytes |
|
| 54 | + * @deprecated 4.0.0 Use \OCP\Util::computerFileSize |
|
| 55 | + * |
|
| 56 | + * Makes 2kB to 2048. |
|
| 57 | + * |
|
| 58 | + * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
|
| 59 | + */ |
|
| 60 | + public static function computerFileSize(string $str): false|int|float { |
|
| 61 | + return \OCP\Util::computerFileSize($str); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Recursive copying of folders |
|
| 66 | + * @param string $src source folder |
|
| 67 | + * @param string $dest target folder |
|
| 68 | + * @return void |
|
| 69 | + */ |
|
| 70 | + public static function copyr($src, $dest) { |
|
| 71 | + if (!file_exists($src)) { |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + if (is_dir($src)) { |
|
| 76 | + if (!is_dir($dest)) { |
|
| 77 | + mkdir($dest); |
|
| 78 | + } |
|
| 79 | + $files = scandir($src); |
|
| 80 | + foreach ($files as $file) { |
|
| 81 | + if ($file != '.' && $file != '..') { |
|
| 82 | + self::copyr("$src/$file", "$dest/$file"); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + } else { |
|
| 86 | + $validator = \OCP\Server::get(FilenameValidator::class); |
|
| 87 | + if (!$validator->isForbidden($src)) { |
|
| 88 | + copy($src, $dest); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Recursive deletion of folders |
|
| 95 | + * @param string $dir path to the folder |
|
| 96 | + * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
| 97 | + * @return bool |
|
| 98 | + * @deprecated 5.0.0 use \OCP\Files::rmdirr instead |
|
| 99 | + */ |
|
| 100 | + public static function rmdirr($dir, $deleteSelf = true) { |
|
| 101 | + return \OCP\Files::rmdirr($dir, $deleteSelf); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @deprecated 18.0.0 |
|
| 106 | + * @return \OC\Files\Type\TemplateManager |
|
| 107 | + */ |
|
| 108 | + public static function getFileTemplateManager() { |
|
| 109 | + if (!self::$templateManager) { |
|
| 110 | + self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
| 111 | + } |
|
| 112 | + return self::$templateManager; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * detect if a given program is found in the search PATH |
|
| 117 | + * |
|
| 118 | + * @param string $name |
|
| 119 | + * @param bool $path |
|
| 120 | + * @internal param string $program name |
|
| 121 | + * @internal param string $optional search path, defaults to $PATH |
|
| 122 | + * @return bool true if executable program found in path |
|
| 123 | + * @deprecated 32.0.0 use the \OCP\IBinaryFinder |
|
| 124 | + */ |
|
| 125 | + public static function canExecute($name, $path = false) { |
|
| 126 | + // path defaults to PATH from environment if not set |
|
| 127 | + if ($path === false) { |
|
| 128 | + $path = getenv('PATH'); |
|
| 129 | + } |
|
| 130 | + // we look for an executable file of that name |
|
| 131 | + $exts = ['']; |
|
| 132 | + $check_fn = 'is_executable'; |
|
| 133 | + // Default check will be done with $path directories : |
|
| 134 | + $dirs = explode(PATH_SEPARATOR, $path); |
|
| 135 | + // WARNING : We have to check if open_basedir is enabled : |
|
| 136 | + $obd = OC::$server->get(IniGetWrapper::class)->getString('open_basedir'); |
|
| 137 | + if ($obd != 'none') { |
|
| 138 | + $obd_values = explode(PATH_SEPARATOR, $obd); |
|
| 139 | + if (count($obd_values) > 0 and $obd_values[0]) { |
|
| 140 | + // open_basedir is in effect ! |
|
| 141 | + // We need to check if the program is in one of these dirs : |
|
| 142 | + $dirs = $obd_values; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + foreach ($dirs as $dir) { |
|
| 146 | + foreach ($exts as $ext) { |
|
| 147 | + if ($check_fn("$dir/$name" . $ext)) { |
|
| 148 | + return true; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + return false; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * copy the contents of one stream to another |
|
| 157 | + * |
|
| 158 | + * @param resource $source |
|
| 159 | + * @param resource $target |
|
| 160 | + * @return array the number of bytes copied and result |
|
| 161 | + */ |
|
| 162 | + public static function streamCopy($source, $target) { |
|
| 163 | + if (!$source or !$target) { |
|
| 164 | + return [0, false]; |
|
| 165 | + } |
|
| 166 | + $bufSize = 8192; |
|
| 167 | + $result = true; |
|
| 168 | + $count = 0; |
|
| 169 | + while (!feof($source)) { |
|
| 170 | + $buf = fread($source, $bufSize); |
|
| 171 | + $bytesWritten = fwrite($target, $buf); |
|
| 172 | + if ($bytesWritten !== false) { |
|
| 173 | + $count += $bytesWritten; |
|
| 174 | + } |
|
| 175 | + // note: strlen is expensive so only use it when necessary, |
|
| 176 | + // on the last block |
|
| 177 | + if ($bytesWritten === false |
|
| 178 | + || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
| 179 | + ) { |
|
| 180 | + // write error, could be disk full ? |
|
| 181 | + $result = false; |
|
| 182 | + break; |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + return [$count, $result]; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Adds a suffix to the name in case the file exists |
|
| 190 | + * |
|
| 191 | + * @param string $path |
|
| 192 | + * @param string $filename |
|
| 193 | + * @return string |
|
| 194 | + */ |
|
| 195 | + public static function buildNotExistingFileName($path, $filename) { |
|
| 196 | + $view = \OC\Files\Filesystem::getView(); |
|
| 197 | + return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Adds a suffix to the name in case the file exists |
|
| 202 | + * |
|
| 203 | + * @param string $path |
|
| 204 | + * @param string $filename |
|
| 205 | + * @return string |
|
| 206 | + */ |
|
| 207 | + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
| 208 | + if ($path === '/') { |
|
| 209 | + $path = ''; |
|
| 210 | + } |
|
| 211 | + if ($pos = strrpos($filename, '.')) { |
|
| 212 | + $name = substr($filename, 0, $pos); |
|
| 213 | + $ext = substr($filename, $pos); |
|
| 214 | + } else { |
|
| 215 | + $name = $filename; |
|
| 216 | + $ext = ''; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $newpath = $path . '/' . $filename; |
|
| 220 | + if ($view->file_exists($newpath)) { |
|
| 221 | + if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
| 222 | + //Replace the last "(number)" with "(number+1)" |
|
| 223 | + $last_match = count($matches[0]) - 1; |
|
| 224 | + $counter = $matches[1][$last_match][0] + 1; |
|
| 225 | + $offset = $matches[0][$last_match][1]; |
|
| 226 | + $match_length = strlen($matches[0][$last_match][0]); |
|
| 227 | + } else { |
|
| 228 | + $counter = 2; |
|
| 229 | + $match_length = 0; |
|
| 230 | + $offset = false; |
|
| 231 | + } |
|
| 232 | + do { |
|
| 233 | + if ($offset) { |
|
| 234 | + //Replace the last "(number)" with "(number+1)" |
|
| 235 | + $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
| 236 | + } else { |
|
| 237 | + $newname = $name . ' (' . $counter . ')'; |
|
| 238 | + } |
|
| 239 | + $newpath = $path . '/' . $newname . $ext; |
|
| 240 | + $counter++; |
|
| 241 | + } while ($view->file_exists($newpath)); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + return $newpath; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
| 249 | + * Based on https://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
| 250 | + * |
|
| 251 | + * @param array $input The array to work on |
|
| 252 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
| 253 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
| 254 | + * @return array |
|
| 255 | + * @deprecated 4.5.0 use \OCP\Util::mb_array_change_key_case instead |
|
| 256 | + */ |
|
| 257 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
| 258 | + return \OCP\Util::mb_array_change_key_case($input, $case, $encoding); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Performs a search in a nested array. |
|
| 263 | + * Taken from https://www.php.net/manual/en/function.array-search.php#97645 |
|
| 264 | + * |
|
| 265 | + * @param array $haystack the array to be searched |
|
| 266 | + * @param string $needle the search string |
|
| 267 | + * @param mixed $index optional, only search this key name |
|
| 268 | + * @return mixed the key of the matching field, otherwise false |
|
| 269 | + * @deprecated 4.5.0 - use \OCP\Util::recursiveArraySearch |
|
| 270 | + */ |
|
| 271 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
| 272 | + return \OCP\Util::recursiveArraySearch($haystack, $needle, $index); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
| 277 | + * |
|
| 278 | + * @param string $dir the current folder where the user currently operates |
|
| 279 | + * @param int|float $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
| 280 | + * @return int|float number of bytes representing |
|
| 281 | + * @deprecated 5.0.0 - use \OCP\Util::maxUploadFilesize |
|
| 282 | + */ |
|
| 283 | + public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
| 284 | + return \OCP\Util::maxUploadFilesize($dir, $freeSpace); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Calculate free space left within user quota |
|
| 289 | + * |
|
| 290 | + * @param string $dir the current folder where the user currently operates |
|
| 291 | + * @return int|float number of bytes representing |
|
| 292 | + * @deprecated 7.0.0 - use \OCP\Util::freeSpace |
|
| 293 | + */ |
|
| 294 | + public static function freeSpace($dir) { |
|
| 295 | + return \OCP\Util::freeSpace($dir); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Calculate PHP upload limit |
|
| 300 | + * |
|
| 301 | + * @return int|float PHP upload file size limit |
|
| 302 | + * @deprecated 7.0.0 - use \OCP\Util::uploadLimit |
|
| 303 | + */ |
|
| 304 | + public static function uploadLimit() { |
|
| 305 | + return \OCP\Util::uploadLimit(); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Checks if a function is available |
|
| 310 | + * |
|
| 311 | + * @deprecated 25.0.0 use \OCP\Util::isFunctionEnabled instead |
|
| 312 | + */ |
|
| 313 | + public static function is_function_enabled(string $function_name): bool { |
|
| 314 | + return \OCP\Util::isFunctionEnabled($function_name); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Try to find a program |
|
| 319 | + * @deprecated 25.0.0 Use \OC\BinaryFinder directly |
|
| 320 | + */ |
|
| 321 | + public static function findBinaryPath(string $program): ?string { |
|
| 322 | + $result = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath($program); |
|
| 323 | + return $result !== false ? $result : null; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Calculate the disc space for the given path |
|
| 328 | + * |
|
| 329 | + * BEWARE: this requires that Util::setupFS() was called |
|
| 330 | + * already ! |
|
| 331 | + * |
|
| 332 | + * @param string $path |
|
| 333 | + * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
| 334 | + * @param bool $includeMountPoints whether to include mount points in the size calculation |
|
| 335 | + * @param bool $useCache whether to use the cached quota values |
|
| 336 | + * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct |
|
| 337 | + * @return StorageInfo |
|
| 338 | + * @throws \OCP\Files\NotFoundException |
|
| 339 | + */ |
|
| 340 | + public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) { |
|
| 341 | + if (!self::$cacheFactory) { |
|
| 342 | + self::$cacheFactory = \OC::$server->get(ICacheFactory::class); |
|
| 343 | + } |
|
| 344 | + $memcache = self::$cacheFactory->createLocal('storage_info'); |
|
| 345 | + |
|
| 346 | + // return storage info without adding mount points |
|
| 347 | + if (self::$quotaIncludeExternalStorage === null) { |
|
| 348 | + self::$quotaIncludeExternalStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $view = Filesystem::getView(); |
|
| 352 | + if (!$view) { |
|
| 353 | + throw new \OCP\Files\NotFoundException(); |
|
| 354 | + } |
|
| 355 | + $fullPath = Filesystem::normalizePath($view->getAbsolutePath($path)); |
|
| 356 | + |
|
| 357 | + $cacheKey = $fullPath . '::' . ($includeMountPoints ? 'include' : 'exclude'); |
|
| 358 | + if ($useCache) { |
|
| 359 | + $cached = $memcache->get($cacheKey); |
|
| 360 | + if ($cached) { |
|
| 361 | + return $cached; |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + if (!$rootInfo) { |
|
| 366 | + $rootInfo = \OC\Files\Filesystem::getFileInfo($path, self::$quotaIncludeExternalStorage ? 'ext' : false); |
|
| 367 | + } |
|
| 368 | + if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
| 369 | + throw new \OCP\Files\NotFoundException('The root directory of the user\'s files is missing'); |
|
| 370 | + } |
|
| 371 | + $used = $rootInfo->getSize($includeMountPoints); |
|
| 372 | + if ($used < 0) { |
|
| 373 | + $used = 0.0; |
|
| 374 | + } |
|
| 375 | + /** @var int|float $quota */ |
|
| 376 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
| 377 | + $mount = $rootInfo->getMountPoint(); |
|
| 378 | + $storage = $mount->getStorage(); |
|
| 379 | + $sourceStorage = $storage; |
|
| 380 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
| 381 | + self::$quotaIncludeExternalStorage = false; |
|
| 382 | + } |
|
| 383 | + if (self::$quotaIncludeExternalStorage) { |
|
| 384 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
| 385 | + || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
| 386 | + ) { |
|
| 387 | + /** @var \OC\Files\Storage\Home $storage */ |
|
| 388 | + $user = $storage->getUser(); |
|
| 389 | + } else { |
|
| 390 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 391 | + } |
|
| 392 | + $quota = OC_Util::getUserQuota($user); |
|
| 393 | + if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 394 | + // always get free space / total space from root + mount points |
|
| 395 | + return self::getGlobalStorageInfo($quota, $user, $mount); |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // TODO: need a better way to get total space from storage |
|
| 400 | + if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
| 401 | + /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
| 402 | + $quota = $sourceStorage->getQuota(); |
|
| 403 | + } |
|
| 404 | + try { |
|
| 405 | + $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
| 406 | + if (is_bool($free)) { |
|
| 407 | + $free = 0.0; |
|
| 408 | + } |
|
| 409 | + } catch (\Exception $e) { |
|
| 410 | + if ($path === '') { |
|
| 411 | + throw $e; |
|
| 412 | + } |
|
| 413 | + /** @var LoggerInterface $logger */ |
|
| 414 | + $logger = \OC::$server->get(LoggerInterface::class); |
|
| 415 | + $logger->warning('Error while getting quota info, using root quota', ['exception' => $e]); |
|
| 416 | + $rootInfo = self::getStorageInfo(''); |
|
| 417 | + $memcache->set($cacheKey, $rootInfo, 5 * 60); |
|
| 418 | + return $rootInfo; |
|
| 419 | + } |
|
| 420 | + if ($free >= 0) { |
|
| 421 | + $total = $free + $used; |
|
| 422 | + } else { |
|
| 423 | + $total = $free; //either unknown or unlimited |
|
| 424 | + } |
|
| 425 | + if ($total > 0) { |
|
| 426 | + if ($quota > 0 && $total > $quota) { |
|
| 427 | + $total = $quota; |
|
| 428 | + } |
|
| 429 | + // prevent division by zero or error codes (negative values) |
|
| 430 | + $relative = round(($used / $total) * 10000) / 100; |
|
| 431 | + } else { |
|
| 432 | + $relative = 0; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /* |
|
| 436 | 436 | * \OCA\Files_Sharing\External\Storage returns the cloud ID as the owner for the storage. |
| 437 | 437 | * It is unnecessary to query the user manager for the display name, as it won't have this information. |
| 438 | 438 | */ |
| 439 | - $isRemoteShare = $storage->instanceOfStorage(\OCA\Files_Sharing\External\Storage::class); |
|
| 440 | - |
|
| 441 | - $ownerId = $storage->getOwner($path); |
|
| 442 | - $ownerDisplayName = ''; |
|
| 443 | - |
|
| 444 | - if ($isRemoteShare === false && $ownerId !== false) { |
|
| 445 | - $ownerDisplayName = \OC::$server->getUserManager()->getDisplayName($ownerId) ?? ''; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - if (substr_count($mount->getMountPoint(), '/') < 3) { |
|
| 449 | - $mountPoint = ''; |
|
| 450 | - } else { |
|
| 451 | - [,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - $info = [ |
|
| 455 | - 'free' => $free, |
|
| 456 | - 'used' => $used, |
|
| 457 | - 'quota' => $quota, |
|
| 458 | - 'total' => $total, |
|
| 459 | - 'relative' => $relative, |
|
| 460 | - 'owner' => $ownerId, |
|
| 461 | - 'ownerDisplayName' => $ownerDisplayName, |
|
| 462 | - 'mountType' => $mount->getMountType(), |
|
| 463 | - 'mountPoint' => trim($mountPoint, '/'), |
|
| 464 | - ]; |
|
| 465 | - |
|
| 466 | - if ($isRemoteShare === false && $ownerId !== false && $path === '/') { |
|
| 467 | - // If path is root, store this as last known quota usage for this user |
|
| 468 | - \OCP\Server::get(\OCP\IConfig::class)->setUserValue($ownerId, 'files', 'lastSeenQuotaUsage', (string)$relative); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - $memcache->set($cacheKey, $info, 5 * 60); |
|
| 472 | - |
|
| 473 | - return $info; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Get storage info including all mount points and quota |
|
| 478 | - * |
|
| 479 | - * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct |
|
| 480 | - * @return StorageInfo |
|
| 481 | - */ |
|
| 482 | - private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array { |
|
| 483 | - $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
| 484 | - /** @var int|float $used */ |
|
| 485 | - $used = $rootInfo['size']; |
|
| 486 | - if ($used < 0) { |
|
| 487 | - $used = 0.0; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - $total = $quota; |
|
| 491 | - /** @var int|float $free */ |
|
| 492 | - $free = $quota - $used; |
|
| 493 | - |
|
| 494 | - if ($total > 0) { |
|
| 495 | - if ($quota > 0 && $total > $quota) { |
|
| 496 | - $total = $quota; |
|
| 497 | - } |
|
| 498 | - // prevent division by zero or error codes (negative values) |
|
| 499 | - $relative = round(($used / $total) * 10000) / 100; |
|
| 500 | - } else { |
|
| 501 | - $relative = 0.0; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - if (substr_count($mount->getMountPoint(), '/') < 3) { |
|
| 505 | - $mountPoint = ''; |
|
| 506 | - } else { |
|
| 507 | - [,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - return [ |
|
| 511 | - 'free' => $free, |
|
| 512 | - 'used' => $used, |
|
| 513 | - 'total' => $total, |
|
| 514 | - 'relative' => $relative, |
|
| 515 | - 'quota' => $quota, |
|
| 516 | - 'owner' => $user->getUID(), |
|
| 517 | - 'ownerDisplayName' => $user->getDisplayName(), |
|
| 518 | - 'mountType' => $mount->getMountType(), |
|
| 519 | - 'mountPoint' => trim($mountPoint, '/'), |
|
| 520 | - ]; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - public static function clearStorageInfo(string $absolutePath): void { |
|
| 524 | - /** @var ICacheFactory $cacheFactory */ |
|
| 525 | - $cacheFactory = \OC::$server->get(ICacheFactory::class); |
|
| 526 | - $memcache = $cacheFactory->createLocal('storage_info'); |
|
| 527 | - $cacheKeyPrefix = Filesystem::normalizePath($absolutePath) . '::'; |
|
| 528 | - $memcache->remove($cacheKeyPrefix . 'include'); |
|
| 529 | - $memcache->remove($cacheKeyPrefix . 'exclude'); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * Returns whether the config file is set manually to read-only |
|
| 534 | - * @return bool |
|
| 535 | - */ |
|
| 536 | - public static function isReadOnlyConfigEnabled() { |
|
| 537 | - return \OC::$server->getConfig()->getSystemValueBool('config_is_read_only', false); |
|
| 538 | - } |
|
| 439 | + $isRemoteShare = $storage->instanceOfStorage(\OCA\Files_Sharing\External\Storage::class); |
|
| 440 | + |
|
| 441 | + $ownerId = $storage->getOwner($path); |
|
| 442 | + $ownerDisplayName = ''; |
|
| 443 | + |
|
| 444 | + if ($isRemoteShare === false && $ownerId !== false) { |
|
| 445 | + $ownerDisplayName = \OC::$server->getUserManager()->getDisplayName($ownerId) ?? ''; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + if (substr_count($mount->getMountPoint(), '/') < 3) { |
|
| 449 | + $mountPoint = ''; |
|
| 450 | + } else { |
|
| 451 | + [,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + $info = [ |
|
| 455 | + 'free' => $free, |
|
| 456 | + 'used' => $used, |
|
| 457 | + 'quota' => $quota, |
|
| 458 | + 'total' => $total, |
|
| 459 | + 'relative' => $relative, |
|
| 460 | + 'owner' => $ownerId, |
|
| 461 | + 'ownerDisplayName' => $ownerDisplayName, |
|
| 462 | + 'mountType' => $mount->getMountType(), |
|
| 463 | + 'mountPoint' => trim($mountPoint, '/'), |
|
| 464 | + ]; |
|
| 465 | + |
|
| 466 | + if ($isRemoteShare === false && $ownerId !== false && $path === '/') { |
|
| 467 | + // If path is root, store this as last known quota usage for this user |
|
| 468 | + \OCP\Server::get(\OCP\IConfig::class)->setUserValue($ownerId, 'files', 'lastSeenQuotaUsage', (string)$relative); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + $memcache->set($cacheKey, $info, 5 * 60); |
|
| 472 | + |
|
| 473 | + return $info; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Get storage info including all mount points and quota |
|
| 478 | + * |
|
| 479 | + * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct |
|
| 480 | + * @return StorageInfo |
|
| 481 | + */ |
|
| 482 | + private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array { |
|
| 483 | + $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
| 484 | + /** @var int|float $used */ |
|
| 485 | + $used = $rootInfo['size']; |
|
| 486 | + if ($used < 0) { |
|
| 487 | + $used = 0.0; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + $total = $quota; |
|
| 491 | + /** @var int|float $free */ |
|
| 492 | + $free = $quota - $used; |
|
| 493 | + |
|
| 494 | + if ($total > 0) { |
|
| 495 | + if ($quota > 0 && $total > $quota) { |
|
| 496 | + $total = $quota; |
|
| 497 | + } |
|
| 498 | + // prevent division by zero or error codes (negative values) |
|
| 499 | + $relative = round(($used / $total) * 10000) / 100; |
|
| 500 | + } else { |
|
| 501 | + $relative = 0.0; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + if (substr_count($mount->getMountPoint(), '/') < 3) { |
|
| 505 | + $mountPoint = ''; |
|
| 506 | + } else { |
|
| 507 | + [,,,$mountPoint] = explode('/', $mount->getMountPoint(), 4); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + return [ |
|
| 511 | + 'free' => $free, |
|
| 512 | + 'used' => $used, |
|
| 513 | + 'total' => $total, |
|
| 514 | + 'relative' => $relative, |
|
| 515 | + 'quota' => $quota, |
|
| 516 | + 'owner' => $user->getUID(), |
|
| 517 | + 'ownerDisplayName' => $user->getDisplayName(), |
|
| 518 | + 'mountType' => $mount->getMountType(), |
|
| 519 | + 'mountPoint' => trim($mountPoint, '/'), |
|
| 520 | + ]; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + public static function clearStorageInfo(string $absolutePath): void { |
|
| 524 | + /** @var ICacheFactory $cacheFactory */ |
|
| 525 | + $cacheFactory = \OC::$server->get(ICacheFactory::class); |
|
| 526 | + $memcache = $cacheFactory->createLocal('storage_info'); |
|
| 527 | + $cacheKeyPrefix = Filesystem::normalizePath($absolutePath) . '::'; |
|
| 528 | + $memcache->remove($cacheKeyPrefix . 'include'); |
|
| 529 | + $memcache->remove($cacheKeyPrefix . 'exclude'); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * Returns whether the config file is set manually to read-only |
|
| 534 | + * @return bool |
|
| 535 | + */ |
|
| 536 | + public static function isReadOnlyConfigEnabled() { |
|
| 537 | + return \OC::$server->getConfig()->getSystemValueBool('config_is_read_only', false); |
|
| 538 | + } |
|
| 539 | 539 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | * |
| 44 | 44 | * Makes 2048 to 2 kB. |
| 45 | 45 | */ |
| 46 | - public static function humanFileSize(int|float $bytes): string { |
|
| 46 | + public static function humanFileSize(int | float $bytes): string { |
|
| 47 | 47 | return \OCP\Util::humanFileSize($bytes); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
| 59 | 59 | */ |
| 60 | - public static function computerFileSize(string $str): false|int|float { |
|
| 60 | + public static function computerFileSize(string $str): false | int | float { |
|
| 61 | 61 | return \OCP\Util::computerFileSize($str); |
| 62 | 62 | } |
| 63 | 63 | |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | } |
| 145 | 145 | foreach ($dirs as $dir) { |
| 146 | 146 | foreach ($exts as $ext) { |
| 147 | - if ($check_fn("$dir/$name" . $ext)) { |
|
| 147 | + if ($check_fn("$dir/$name".$ext)) { |
|
| 148 | 148 | return true; |
| 149 | 149 | } |
| 150 | 150 | } |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | $ext = ''; |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | - $newpath = $path . '/' . $filename; |
|
| 219 | + $newpath = $path.'/'.$filename; |
|
| 220 | 220 | if ($view->file_exists($newpath)) { |
| 221 | 221 | if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
| 222 | 222 | //Replace the last "(number)" with "(number+1)" |
@@ -232,11 +232,11 @@ discard block |
||
| 232 | 232 | do { |
| 233 | 233 | if ($offset) { |
| 234 | 234 | //Replace the last "(number)" with "(number+1)" |
| 235 | - $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
| 235 | + $newname = substr_replace($name, '('.$counter.')', $offset, $match_length); |
|
| 236 | 236 | } else { |
| 237 | - $newname = $name . ' (' . $counter . ')'; |
|
| 237 | + $newname = $name.' ('.$counter.')'; |
|
| 238 | 238 | } |
| 239 | - $newpath = $path . '/' . $newname . $ext; |
|
| 239 | + $newpath = $path.'/'.$newname.$ext; |
|
| 240 | 240 | $counter++; |
| 241 | 241 | } while ($view->file_exists($newpath)); |
| 242 | 242 | } |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | } |
| 355 | 355 | $fullPath = Filesystem::normalizePath($view->getAbsolutePath($path)); |
| 356 | 356 | |
| 357 | - $cacheKey = $fullPath . '::' . ($includeMountPoints ? 'include' : 'exclude'); |
|
| 357 | + $cacheKey = $fullPath.'::'.($includeMountPoints ? 'include' : 'exclude'); |
|
| 358 | 358 | if ($useCache) { |
| 359 | 359 | $cached = $memcache->get($cacheKey); |
| 360 | 360 | if ($cached) { |
@@ -465,7 +465,7 @@ discard block |
||
| 465 | 465 | |
| 466 | 466 | if ($isRemoteShare === false && $ownerId !== false && $path === '/') { |
| 467 | 467 | // If path is root, store this as last known quota usage for this user |
| 468 | - \OCP\Server::get(\OCP\IConfig::class)->setUserValue($ownerId, 'files', 'lastSeenQuotaUsage', (string)$relative); |
|
| 468 | + \OCP\Server::get(\OCP\IConfig::class)->setUserValue($ownerId, 'files', 'lastSeenQuotaUsage', (string) $relative); |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | $memcache->set($cacheKey, $info, 5 * 60); |
@@ -479,7 +479,7 @@ discard block |
||
| 479 | 479 | * @psalm-suppress LessSpecificReturnStatement Legacy code outputs weird types - manually validated that they are correct |
| 480 | 480 | * @return StorageInfo |
| 481 | 481 | */ |
| 482 | - private static function getGlobalStorageInfo(int|float $quota, IUser $user, IMountPoint $mount): array { |
|
| 482 | + private static function getGlobalStorageInfo(int | float $quota, IUser $user, IMountPoint $mount): array { |
|
| 483 | 483 | $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
| 484 | 484 | /** @var int|float $used */ |
| 485 | 485 | $used = $rootInfo['size']; |
@@ -524,9 +524,9 @@ discard block |
||
| 524 | 524 | /** @var ICacheFactory $cacheFactory */ |
| 525 | 525 | $cacheFactory = \OC::$server->get(ICacheFactory::class); |
| 526 | 526 | $memcache = $cacheFactory->createLocal('storage_info'); |
| 527 | - $cacheKeyPrefix = Filesystem::normalizePath($absolutePath) . '::'; |
|
| 528 | - $memcache->remove($cacheKeyPrefix . 'include'); |
|
| 529 | - $memcache->remove($cacheKeyPrefix . 'exclude'); |
|
| 527 | + $cacheKeyPrefix = Filesystem::normalizePath($absolutePath).'::'; |
|
| 528 | + $memcache->remove($cacheKeyPrefix.'include'); |
|
| 529 | + $memcache->remove($cacheKeyPrefix.'exclude'); |
|
| 530 | 530 | } |
| 531 | 531 | |
| 532 | 532 | /** |