@@ -49,577 +49,577 @@ |
||
49 | 49 | * Collection of useful functions |
50 | 50 | */ |
51 | 51 | class OC_Helper { |
52 | - private static $templateManager; |
|
53 | - |
|
54 | - /** |
|
55 | - * Make a human file size |
|
56 | - * @param int $bytes file size in bytes |
|
57 | - * @return string a human readable file size |
|
58 | - * |
|
59 | - * Makes 2048 to 2 kB. |
|
60 | - */ |
|
61 | - public static function humanFileSize($bytes) { |
|
62 | - if ($bytes < 0) { |
|
63 | - return "?"; |
|
64 | - } |
|
65 | - if ($bytes < 1024) { |
|
66 | - return "$bytes B"; |
|
67 | - } |
|
68 | - $bytes = round($bytes / 1024, 0); |
|
69 | - if ($bytes < 1024) { |
|
70 | - return "$bytes KB"; |
|
71 | - } |
|
72 | - $bytes = round($bytes / 1024, 1); |
|
73 | - if ($bytes < 1024) { |
|
74 | - return "$bytes MB"; |
|
75 | - } |
|
76 | - $bytes = round($bytes / 1024, 1); |
|
77 | - if ($bytes < 1024) { |
|
78 | - return "$bytes GB"; |
|
79 | - } |
|
80 | - $bytes = round($bytes / 1024, 1); |
|
81 | - if ($bytes < 1024) { |
|
82 | - return "$bytes TB"; |
|
83 | - } |
|
84 | - |
|
85 | - $bytes = round($bytes / 1024, 1); |
|
86 | - return "$bytes PB"; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Make a php file size |
|
91 | - * @param int $bytes file size in bytes |
|
92 | - * @return string a php parseable file size |
|
93 | - * |
|
94 | - * Makes 2048 to 2k and 2^41 to 2048G |
|
95 | - */ |
|
96 | - public static function phpFileSize($bytes) { |
|
97 | - if ($bytes < 0) { |
|
98 | - return "?"; |
|
99 | - } |
|
100 | - if ($bytes < 1024) { |
|
101 | - return $bytes . "B"; |
|
102 | - } |
|
103 | - $bytes = round($bytes / 1024, 1); |
|
104 | - if ($bytes < 1024) { |
|
105 | - return $bytes . "K"; |
|
106 | - } |
|
107 | - $bytes = round($bytes / 1024, 1); |
|
108 | - if ($bytes < 1024) { |
|
109 | - return $bytes . "M"; |
|
110 | - } |
|
111 | - $bytes = round($bytes / 1024, 1); |
|
112 | - return $bytes . "G"; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Make a computer file size |
|
117 | - * @param string $str file size in human readable format |
|
118 | - * @return float|bool a file size in bytes |
|
119 | - * |
|
120 | - * Makes 2kB to 2048. |
|
121 | - * |
|
122 | - * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
123 | - */ |
|
124 | - public static function computerFileSize($str) { |
|
125 | - $str = strtolower($str); |
|
126 | - if (is_numeric($str)) { |
|
127 | - return (float)$str; |
|
128 | - } |
|
129 | - |
|
130 | - $bytes_array = array( |
|
131 | - 'b' => 1, |
|
132 | - 'k' => 1024, |
|
133 | - 'kb' => 1024, |
|
134 | - 'mb' => 1024 * 1024, |
|
135 | - 'm' => 1024 * 1024, |
|
136 | - 'gb' => 1024 * 1024 * 1024, |
|
137 | - 'g' => 1024 * 1024 * 1024, |
|
138 | - 'tb' => 1024 * 1024 * 1024 * 1024, |
|
139 | - 't' => 1024 * 1024 * 1024 * 1024, |
|
140 | - 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
141 | - 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
142 | - ); |
|
143 | - |
|
144 | - $bytes = (float)$str; |
|
145 | - |
|
146 | - if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
147 | - $bytes *= $bytes_array[$matches[1]]; |
|
148 | - } else { |
|
149 | - return false; |
|
150 | - } |
|
151 | - |
|
152 | - $bytes = round($bytes); |
|
153 | - |
|
154 | - return $bytes; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Recursive copying of folders |
|
159 | - * @param string $src source folder |
|
160 | - * @param string $dest target folder |
|
161 | - * |
|
162 | - */ |
|
163 | - static function copyr($src, $dest) { |
|
164 | - if (is_dir($src)) { |
|
165 | - if (!is_dir($dest)) { |
|
166 | - mkdir($dest); |
|
167 | - } |
|
168 | - $files = scandir($src); |
|
169 | - foreach ($files as $file) { |
|
170 | - if ($file != "." && $file != "..") { |
|
171 | - self::copyr("$src/$file", "$dest/$file"); |
|
172 | - } |
|
173 | - } |
|
174 | - } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
175 | - copy($src, $dest); |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Recursive deletion of folders |
|
181 | - * @param string $dir path to the folder |
|
182 | - * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - static function rmdirr($dir, $deleteSelf = true) { |
|
186 | - if (is_dir($dir)) { |
|
187 | - $files = new RecursiveIteratorIterator( |
|
188 | - new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
189 | - RecursiveIteratorIterator::CHILD_FIRST |
|
190 | - ); |
|
191 | - |
|
192 | - foreach ($files as $fileInfo) { |
|
193 | - /** @var SplFileInfo $fileInfo */ |
|
194 | - if ($fileInfo->isLink()) { |
|
195 | - unlink($fileInfo->getPathname()); |
|
196 | - } else if ($fileInfo->isDir()) { |
|
197 | - rmdir($fileInfo->getRealPath()); |
|
198 | - } else { |
|
199 | - unlink($fileInfo->getRealPath()); |
|
200 | - } |
|
201 | - } |
|
202 | - if ($deleteSelf) { |
|
203 | - rmdir($dir); |
|
204 | - } |
|
205 | - } elseif (file_exists($dir)) { |
|
206 | - if ($deleteSelf) { |
|
207 | - unlink($dir); |
|
208 | - } |
|
209 | - } |
|
210 | - if (!$deleteSelf) { |
|
211 | - return true; |
|
212 | - } |
|
213 | - |
|
214 | - return !file_exists($dir); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * @return \OC\Files\Type\TemplateManager |
|
219 | - */ |
|
220 | - static public function getFileTemplateManager() { |
|
221 | - if (!self::$templateManager) { |
|
222 | - self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
223 | - } |
|
224 | - return self::$templateManager; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * detect if a given program is found in the search PATH |
|
229 | - * |
|
230 | - * @param string $name |
|
231 | - * @param bool $path |
|
232 | - * @internal param string $program name |
|
233 | - * @internal param string $optional search path, defaults to $PATH |
|
234 | - * @return bool true if executable program found in path |
|
235 | - */ |
|
236 | - public static function canExecute($name, $path = false) { |
|
237 | - // path defaults to PATH from environment if not set |
|
238 | - if ($path === false) { |
|
239 | - $path = getenv("PATH"); |
|
240 | - } |
|
241 | - // we look for an executable file of that name |
|
242 | - $exts = [""]; |
|
243 | - $check_fn = "is_executable"; |
|
244 | - // Default check will be done with $path directories : |
|
245 | - $dirs = explode(PATH_SEPARATOR, $path); |
|
246 | - // WARNING : We have to check if open_basedir is enabled : |
|
247 | - $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
248 | - if ($obd != "none") { |
|
249 | - $obd_values = explode(PATH_SEPARATOR, $obd); |
|
250 | - if (count($obd_values) > 0 and $obd_values[0]) { |
|
251 | - // open_basedir is in effect ! |
|
252 | - // We need to check if the program is in one of these dirs : |
|
253 | - $dirs = $obd_values; |
|
254 | - } |
|
255 | - } |
|
256 | - foreach ($dirs as $dir) { |
|
257 | - foreach ($exts as $ext) { |
|
258 | - if ($check_fn("$dir/$name" . $ext)) |
|
259 | - return true; |
|
260 | - } |
|
261 | - } |
|
262 | - return false; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * copy the contents of one stream to another |
|
267 | - * |
|
268 | - * @param resource $source |
|
269 | - * @param resource $target |
|
270 | - * @return array the number of bytes copied and result |
|
271 | - */ |
|
272 | - public static function streamCopy($source, $target) { |
|
273 | - if (!$source or !$target) { |
|
274 | - return array(0, false); |
|
275 | - } |
|
276 | - $bufSize = 8192; |
|
277 | - $result = true; |
|
278 | - $count = 0; |
|
279 | - while (!feof($source)) { |
|
280 | - $buf = fread($source, $bufSize); |
|
281 | - $bytesWritten = fwrite($target, $buf); |
|
282 | - if ($bytesWritten !== false) { |
|
283 | - $count += $bytesWritten; |
|
284 | - } |
|
285 | - // note: strlen is expensive so only use it when necessary, |
|
286 | - // on the last block |
|
287 | - if ($bytesWritten === false |
|
288 | - || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
289 | - ) { |
|
290 | - // write error, could be disk full ? |
|
291 | - $result = false; |
|
292 | - break; |
|
293 | - } |
|
294 | - } |
|
295 | - return array($count, $result); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Adds a suffix to the name in case the file exists |
|
300 | - * |
|
301 | - * @param string $path |
|
302 | - * @param string $filename |
|
303 | - * @return string |
|
304 | - */ |
|
305 | - public static function buildNotExistingFileName($path, $filename) { |
|
306 | - $view = \OC\Files\Filesystem::getView(); |
|
307 | - return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Adds a suffix to the name in case the file exists |
|
312 | - * |
|
313 | - * @param string $path |
|
314 | - * @param string $filename |
|
315 | - * @return string |
|
316 | - */ |
|
317 | - public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
318 | - if ($path === '/') { |
|
319 | - $path = ''; |
|
320 | - } |
|
321 | - if ($pos = strrpos($filename, '.')) { |
|
322 | - $name = substr($filename, 0, $pos); |
|
323 | - $ext = substr($filename, $pos); |
|
324 | - } else { |
|
325 | - $name = $filename; |
|
326 | - $ext = ''; |
|
327 | - } |
|
328 | - |
|
329 | - $newpath = $path . '/' . $filename; |
|
330 | - if ($view->file_exists($newpath)) { |
|
331 | - if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
332 | - //Replace the last "(number)" with "(number+1)" |
|
333 | - $last_match = count($matches[0]) - 1; |
|
334 | - $counter = $matches[1][$last_match][0] + 1; |
|
335 | - $offset = $matches[0][$last_match][1]; |
|
336 | - $match_length = strlen($matches[0][$last_match][0]); |
|
337 | - } else { |
|
338 | - $counter = 2; |
|
339 | - $match_length = 0; |
|
340 | - $offset = false; |
|
341 | - } |
|
342 | - do { |
|
343 | - if ($offset) { |
|
344 | - //Replace the last "(number)" with "(number+1)" |
|
345 | - $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
346 | - } else { |
|
347 | - $newname = $name . ' (' . $counter . ')'; |
|
348 | - } |
|
349 | - $newpath = $path . '/' . $newname . $ext; |
|
350 | - $counter++; |
|
351 | - } while ($view->file_exists($newpath)); |
|
352 | - } |
|
353 | - |
|
354 | - return $newpath; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
359 | - * |
|
360 | - * @param array $input The array to work on |
|
361 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
362 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
363 | - * @return array |
|
364 | - * |
|
365 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
366 | - * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
367 | - * |
|
368 | - */ |
|
369 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
370 | - $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
371 | - $ret = array(); |
|
372 | - foreach ($input as $k => $v) { |
|
373 | - $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
374 | - } |
|
375 | - return $ret; |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * performs a search in a nested array |
|
380 | - * @param array $haystack the array to be searched |
|
381 | - * @param string $needle the search string |
|
382 | - * @param mixed $index optional, only search this key name |
|
383 | - * @return mixed the key of the matching field, otherwise false |
|
384 | - * |
|
385 | - * performs a search in a nested array |
|
386 | - * |
|
387 | - * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
388 | - */ |
|
389 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
390 | - $aIt = new RecursiveArrayIterator($haystack); |
|
391 | - $it = new RecursiveIteratorIterator($aIt); |
|
392 | - |
|
393 | - while ($it->valid()) { |
|
394 | - if (((isset($index) AND ($it->key() == $index)) OR !isset($index)) AND ($it->current() == $needle)) { |
|
395 | - return $aIt->key(); |
|
396 | - } |
|
397 | - |
|
398 | - $it->next(); |
|
399 | - } |
|
400 | - |
|
401 | - return false; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
406 | - * |
|
407 | - * @param string $dir the current folder where the user currently operates |
|
408 | - * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
409 | - * @return int number of bytes representing |
|
410 | - */ |
|
411 | - public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
412 | - if (is_null($freeSpace) || $freeSpace < 0){ |
|
413 | - $freeSpace = self::freeSpace($dir); |
|
414 | - } |
|
415 | - return min($freeSpace, self::uploadLimit()); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Calculate free space left within user quota |
|
420 | - * |
|
421 | - * @param string $dir the current folder where the user currently operates |
|
422 | - * @return int number of bytes representing |
|
423 | - */ |
|
424 | - public static function freeSpace($dir) { |
|
425 | - $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
426 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
427 | - $freeSpace = max($freeSpace, 0); |
|
428 | - return $freeSpace; |
|
429 | - } else { |
|
430 | - return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
431 | - } |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Calculate PHP upload limit |
|
436 | - * |
|
437 | - * @return int PHP upload file size limit |
|
438 | - */ |
|
439 | - public static function uploadLimit() { |
|
440 | - $ini = \OC::$server->getIniWrapper(); |
|
441 | - $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
442 | - $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
443 | - if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
444 | - return INF; |
|
445 | - } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
446 | - return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
447 | - } else { |
|
448 | - return min($upload_max_filesize, $post_max_size); |
|
449 | - } |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Checks if a function is available |
|
454 | - * |
|
455 | - * @param string $function_name |
|
456 | - * @return bool |
|
457 | - */ |
|
458 | - public static function is_function_enabled($function_name) { |
|
459 | - if (!function_exists($function_name)) { |
|
460 | - return false; |
|
461 | - } |
|
462 | - $ini = \OC::$server->getIniWrapper(); |
|
463 | - $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
464 | - $disabled = array_map('trim', $disabled); |
|
465 | - if (in_array($function_name, $disabled)) { |
|
466 | - return false; |
|
467 | - } |
|
468 | - $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist') ?: ''); |
|
469 | - $disabled = array_map('trim', $disabled); |
|
470 | - if (in_array($function_name, $disabled)) { |
|
471 | - return false; |
|
472 | - } |
|
473 | - return true; |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Try to find a program |
|
478 | - * |
|
479 | - * @param string $program |
|
480 | - * @return null|string |
|
481 | - */ |
|
482 | - public static function findBinaryPath($program) { |
|
483 | - $memcache = \OC::$server->getMemCacheFactory()->createDistributed('findBinaryPath'); |
|
484 | - if ($memcache->hasKey($program)) { |
|
485 | - return $memcache->get($program); |
|
486 | - } |
|
487 | - $result = null; |
|
488 | - if (self::is_function_enabled('exec')) { |
|
489 | - $exeSniffer = new ExecutableFinder(); |
|
490 | - // Returns null if nothing is found |
|
491 | - $result = $exeSniffer->find($program, null, ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin', '/opt/bin']); |
|
492 | - } |
|
493 | - // store the value for 5 minutes |
|
494 | - $memcache->set($program, $result, 300); |
|
495 | - return $result; |
|
496 | - } |
|
497 | - |
|
498 | - /** |
|
499 | - * Calculate the disc space for the given path |
|
500 | - * |
|
501 | - * @param string $path |
|
502 | - * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
503 | - * @return array |
|
504 | - * @throws \OCP\Files\NotFoundException |
|
505 | - */ |
|
506 | - public static function getStorageInfo($path, $rootInfo = null) { |
|
507 | - // return storage info without adding mount points |
|
508 | - $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
509 | - |
|
510 | - if (!$rootInfo) { |
|
511 | - $rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false); |
|
512 | - } |
|
513 | - if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
514 | - throw new \OCP\Files\NotFoundException(); |
|
515 | - } |
|
516 | - $used = $rootInfo->getSize(); |
|
517 | - if ($used < 0) { |
|
518 | - $used = 0; |
|
519 | - } |
|
520 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
521 | - $storage = $rootInfo->getStorage(); |
|
522 | - $sourceStorage = $storage; |
|
523 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
524 | - $includeExtStorage = false; |
|
525 | - $sourceStorage = $storage->getSourceStorage(); |
|
526 | - } |
|
527 | - if ($includeExtStorage) { |
|
528 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
529 | - || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
530 | - ) { |
|
531 | - /** @var \OC\Files\Storage\Home $storage */ |
|
532 | - $userInstance = $storage->getUser(); |
|
533 | - $user = ($userInstance === null) ? null : $userInstance->getUID(); |
|
534 | - } else { |
|
535 | - $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
536 | - } |
|
537 | - if ($user) { |
|
538 | - $quota = OC_Util::getUserQuota($user); |
|
539 | - } else { |
|
540 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
541 | - } |
|
542 | - if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
543 | - // always get free space / total space from root + mount points |
|
544 | - return self::getGlobalStorageInfo(); |
|
545 | - } |
|
546 | - } |
|
547 | - |
|
548 | - // TODO: need a better way to get total space from storage |
|
549 | - if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
550 | - /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
551 | - $quota = $sourceStorage->getQuota(); |
|
552 | - } |
|
553 | - $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
554 | - if ($free >= 0) { |
|
555 | - $total = $free + $used; |
|
556 | - } else { |
|
557 | - $total = $free; //either unknown or unlimited |
|
558 | - } |
|
559 | - if ($total > 0) { |
|
560 | - if ($quota > 0 && $total > $quota) { |
|
561 | - $total = $quota; |
|
562 | - } |
|
563 | - // prevent division by zero or error codes (negative values) |
|
564 | - $relative = round(($used / $total) * 10000) / 100; |
|
565 | - } else { |
|
566 | - $relative = 0; |
|
567 | - } |
|
568 | - |
|
569 | - $ownerId = $storage->getOwner($path); |
|
570 | - $ownerDisplayName = ''; |
|
571 | - $owner = \OC::$server->getUserManager()->get($ownerId); |
|
572 | - if($owner) { |
|
573 | - $ownerDisplayName = $owner->getDisplayName(); |
|
574 | - } |
|
575 | - |
|
576 | - return [ |
|
577 | - 'free' => $free, |
|
578 | - 'used' => $used, |
|
579 | - 'quota' => $quota, |
|
580 | - 'total' => $total, |
|
581 | - 'relative' => $relative, |
|
582 | - 'owner' => $ownerId, |
|
583 | - 'ownerDisplayName' => $ownerDisplayName, |
|
584 | - ]; |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Get storage info including all mount points and quota |
|
589 | - * |
|
590 | - * @return array |
|
591 | - */ |
|
592 | - private static function getGlobalStorageInfo() { |
|
593 | - $quota = OC_Util::getUserQuota(\OCP\User::getUser()); |
|
594 | - |
|
595 | - $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
596 | - $used = $rootInfo['size']; |
|
597 | - if ($used < 0) { |
|
598 | - $used = 0; |
|
599 | - } |
|
600 | - |
|
601 | - $total = $quota; |
|
602 | - $free = $quota - $used; |
|
603 | - |
|
604 | - if ($total > 0) { |
|
605 | - if ($quota > 0 && $total > $quota) { |
|
606 | - $total = $quota; |
|
607 | - } |
|
608 | - // prevent division by zero or error codes (negative values) |
|
609 | - $relative = round(($used / $total) * 10000) / 100; |
|
610 | - } else { |
|
611 | - $relative = 0; |
|
612 | - } |
|
613 | - |
|
614 | - return array('free' => $free, 'used' => $used, 'total' => $total, 'relative' => $relative); |
|
615 | - |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * Returns whether the config file is set manually to read-only |
|
620 | - * @return bool |
|
621 | - */ |
|
622 | - public static function isReadOnlyConfigEnabled() { |
|
623 | - return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
624 | - } |
|
52 | + private static $templateManager; |
|
53 | + |
|
54 | + /** |
|
55 | + * Make a human file size |
|
56 | + * @param int $bytes file size in bytes |
|
57 | + * @return string a human readable file size |
|
58 | + * |
|
59 | + * Makes 2048 to 2 kB. |
|
60 | + */ |
|
61 | + public static function humanFileSize($bytes) { |
|
62 | + if ($bytes < 0) { |
|
63 | + return "?"; |
|
64 | + } |
|
65 | + if ($bytes < 1024) { |
|
66 | + return "$bytes B"; |
|
67 | + } |
|
68 | + $bytes = round($bytes / 1024, 0); |
|
69 | + if ($bytes < 1024) { |
|
70 | + return "$bytes KB"; |
|
71 | + } |
|
72 | + $bytes = round($bytes / 1024, 1); |
|
73 | + if ($bytes < 1024) { |
|
74 | + return "$bytes MB"; |
|
75 | + } |
|
76 | + $bytes = round($bytes / 1024, 1); |
|
77 | + if ($bytes < 1024) { |
|
78 | + return "$bytes GB"; |
|
79 | + } |
|
80 | + $bytes = round($bytes / 1024, 1); |
|
81 | + if ($bytes < 1024) { |
|
82 | + return "$bytes TB"; |
|
83 | + } |
|
84 | + |
|
85 | + $bytes = round($bytes / 1024, 1); |
|
86 | + return "$bytes PB"; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Make a php file size |
|
91 | + * @param int $bytes file size in bytes |
|
92 | + * @return string a php parseable file size |
|
93 | + * |
|
94 | + * Makes 2048 to 2k and 2^41 to 2048G |
|
95 | + */ |
|
96 | + public static function phpFileSize($bytes) { |
|
97 | + if ($bytes < 0) { |
|
98 | + return "?"; |
|
99 | + } |
|
100 | + if ($bytes < 1024) { |
|
101 | + return $bytes . "B"; |
|
102 | + } |
|
103 | + $bytes = round($bytes / 1024, 1); |
|
104 | + if ($bytes < 1024) { |
|
105 | + return $bytes . "K"; |
|
106 | + } |
|
107 | + $bytes = round($bytes / 1024, 1); |
|
108 | + if ($bytes < 1024) { |
|
109 | + return $bytes . "M"; |
|
110 | + } |
|
111 | + $bytes = round($bytes / 1024, 1); |
|
112 | + return $bytes . "G"; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Make a computer file size |
|
117 | + * @param string $str file size in human readable format |
|
118 | + * @return float|bool a file size in bytes |
|
119 | + * |
|
120 | + * Makes 2kB to 2048. |
|
121 | + * |
|
122 | + * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
123 | + */ |
|
124 | + public static function computerFileSize($str) { |
|
125 | + $str = strtolower($str); |
|
126 | + if (is_numeric($str)) { |
|
127 | + return (float)$str; |
|
128 | + } |
|
129 | + |
|
130 | + $bytes_array = array( |
|
131 | + 'b' => 1, |
|
132 | + 'k' => 1024, |
|
133 | + 'kb' => 1024, |
|
134 | + 'mb' => 1024 * 1024, |
|
135 | + 'm' => 1024 * 1024, |
|
136 | + 'gb' => 1024 * 1024 * 1024, |
|
137 | + 'g' => 1024 * 1024 * 1024, |
|
138 | + 'tb' => 1024 * 1024 * 1024 * 1024, |
|
139 | + 't' => 1024 * 1024 * 1024 * 1024, |
|
140 | + 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
141 | + 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
142 | + ); |
|
143 | + |
|
144 | + $bytes = (float)$str; |
|
145 | + |
|
146 | + if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
147 | + $bytes *= $bytes_array[$matches[1]]; |
|
148 | + } else { |
|
149 | + return false; |
|
150 | + } |
|
151 | + |
|
152 | + $bytes = round($bytes); |
|
153 | + |
|
154 | + return $bytes; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Recursive copying of folders |
|
159 | + * @param string $src source folder |
|
160 | + * @param string $dest target folder |
|
161 | + * |
|
162 | + */ |
|
163 | + static function copyr($src, $dest) { |
|
164 | + if (is_dir($src)) { |
|
165 | + if (!is_dir($dest)) { |
|
166 | + mkdir($dest); |
|
167 | + } |
|
168 | + $files = scandir($src); |
|
169 | + foreach ($files as $file) { |
|
170 | + if ($file != "." && $file != "..") { |
|
171 | + self::copyr("$src/$file", "$dest/$file"); |
|
172 | + } |
|
173 | + } |
|
174 | + } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
175 | + copy($src, $dest); |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Recursive deletion of folders |
|
181 | + * @param string $dir path to the folder |
|
182 | + * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + static function rmdirr($dir, $deleteSelf = true) { |
|
186 | + if (is_dir($dir)) { |
|
187 | + $files = new RecursiveIteratorIterator( |
|
188 | + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
189 | + RecursiveIteratorIterator::CHILD_FIRST |
|
190 | + ); |
|
191 | + |
|
192 | + foreach ($files as $fileInfo) { |
|
193 | + /** @var SplFileInfo $fileInfo */ |
|
194 | + if ($fileInfo->isLink()) { |
|
195 | + unlink($fileInfo->getPathname()); |
|
196 | + } else if ($fileInfo->isDir()) { |
|
197 | + rmdir($fileInfo->getRealPath()); |
|
198 | + } else { |
|
199 | + unlink($fileInfo->getRealPath()); |
|
200 | + } |
|
201 | + } |
|
202 | + if ($deleteSelf) { |
|
203 | + rmdir($dir); |
|
204 | + } |
|
205 | + } elseif (file_exists($dir)) { |
|
206 | + if ($deleteSelf) { |
|
207 | + unlink($dir); |
|
208 | + } |
|
209 | + } |
|
210 | + if (!$deleteSelf) { |
|
211 | + return true; |
|
212 | + } |
|
213 | + |
|
214 | + return !file_exists($dir); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * @return \OC\Files\Type\TemplateManager |
|
219 | + */ |
|
220 | + static public function getFileTemplateManager() { |
|
221 | + if (!self::$templateManager) { |
|
222 | + self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
223 | + } |
|
224 | + return self::$templateManager; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * detect if a given program is found in the search PATH |
|
229 | + * |
|
230 | + * @param string $name |
|
231 | + * @param bool $path |
|
232 | + * @internal param string $program name |
|
233 | + * @internal param string $optional search path, defaults to $PATH |
|
234 | + * @return bool true if executable program found in path |
|
235 | + */ |
|
236 | + public static function canExecute($name, $path = false) { |
|
237 | + // path defaults to PATH from environment if not set |
|
238 | + if ($path === false) { |
|
239 | + $path = getenv("PATH"); |
|
240 | + } |
|
241 | + // we look for an executable file of that name |
|
242 | + $exts = [""]; |
|
243 | + $check_fn = "is_executable"; |
|
244 | + // Default check will be done with $path directories : |
|
245 | + $dirs = explode(PATH_SEPARATOR, $path); |
|
246 | + // WARNING : We have to check if open_basedir is enabled : |
|
247 | + $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
248 | + if ($obd != "none") { |
|
249 | + $obd_values = explode(PATH_SEPARATOR, $obd); |
|
250 | + if (count($obd_values) > 0 and $obd_values[0]) { |
|
251 | + // open_basedir is in effect ! |
|
252 | + // We need to check if the program is in one of these dirs : |
|
253 | + $dirs = $obd_values; |
|
254 | + } |
|
255 | + } |
|
256 | + foreach ($dirs as $dir) { |
|
257 | + foreach ($exts as $ext) { |
|
258 | + if ($check_fn("$dir/$name" . $ext)) |
|
259 | + return true; |
|
260 | + } |
|
261 | + } |
|
262 | + return false; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * copy the contents of one stream to another |
|
267 | + * |
|
268 | + * @param resource $source |
|
269 | + * @param resource $target |
|
270 | + * @return array the number of bytes copied and result |
|
271 | + */ |
|
272 | + public static function streamCopy($source, $target) { |
|
273 | + if (!$source or !$target) { |
|
274 | + return array(0, false); |
|
275 | + } |
|
276 | + $bufSize = 8192; |
|
277 | + $result = true; |
|
278 | + $count = 0; |
|
279 | + while (!feof($source)) { |
|
280 | + $buf = fread($source, $bufSize); |
|
281 | + $bytesWritten = fwrite($target, $buf); |
|
282 | + if ($bytesWritten !== false) { |
|
283 | + $count += $bytesWritten; |
|
284 | + } |
|
285 | + // note: strlen is expensive so only use it when necessary, |
|
286 | + // on the last block |
|
287 | + if ($bytesWritten === false |
|
288 | + || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
289 | + ) { |
|
290 | + // write error, could be disk full ? |
|
291 | + $result = false; |
|
292 | + break; |
|
293 | + } |
|
294 | + } |
|
295 | + return array($count, $result); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Adds a suffix to the name in case the file exists |
|
300 | + * |
|
301 | + * @param string $path |
|
302 | + * @param string $filename |
|
303 | + * @return string |
|
304 | + */ |
|
305 | + public static function buildNotExistingFileName($path, $filename) { |
|
306 | + $view = \OC\Files\Filesystem::getView(); |
|
307 | + return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Adds a suffix to the name in case the file exists |
|
312 | + * |
|
313 | + * @param string $path |
|
314 | + * @param string $filename |
|
315 | + * @return string |
|
316 | + */ |
|
317 | + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
318 | + if ($path === '/') { |
|
319 | + $path = ''; |
|
320 | + } |
|
321 | + if ($pos = strrpos($filename, '.')) { |
|
322 | + $name = substr($filename, 0, $pos); |
|
323 | + $ext = substr($filename, $pos); |
|
324 | + } else { |
|
325 | + $name = $filename; |
|
326 | + $ext = ''; |
|
327 | + } |
|
328 | + |
|
329 | + $newpath = $path . '/' . $filename; |
|
330 | + if ($view->file_exists($newpath)) { |
|
331 | + if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
332 | + //Replace the last "(number)" with "(number+1)" |
|
333 | + $last_match = count($matches[0]) - 1; |
|
334 | + $counter = $matches[1][$last_match][0] + 1; |
|
335 | + $offset = $matches[0][$last_match][1]; |
|
336 | + $match_length = strlen($matches[0][$last_match][0]); |
|
337 | + } else { |
|
338 | + $counter = 2; |
|
339 | + $match_length = 0; |
|
340 | + $offset = false; |
|
341 | + } |
|
342 | + do { |
|
343 | + if ($offset) { |
|
344 | + //Replace the last "(number)" with "(number+1)" |
|
345 | + $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
346 | + } else { |
|
347 | + $newname = $name . ' (' . $counter . ')'; |
|
348 | + } |
|
349 | + $newpath = $path . '/' . $newname . $ext; |
|
350 | + $counter++; |
|
351 | + } while ($view->file_exists($newpath)); |
|
352 | + } |
|
353 | + |
|
354 | + return $newpath; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
359 | + * |
|
360 | + * @param array $input The array to work on |
|
361 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
362 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
363 | + * @return array |
|
364 | + * |
|
365 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
366 | + * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
367 | + * |
|
368 | + */ |
|
369 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
370 | + $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
371 | + $ret = array(); |
|
372 | + foreach ($input as $k => $v) { |
|
373 | + $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
374 | + } |
|
375 | + return $ret; |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * performs a search in a nested array |
|
380 | + * @param array $haystack the array to be searched |
|
381 | + * @param string $needle the search string |
|
382 | + * @param mixed $index optional, only search this key name |
|
383 | + * @return mixed the key of the matching field, otherwise false |
|
384 | + * |
|
385 | + * performs a search in a nested array |
|
386 | + * |
|
387 | + * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
388 | + */ |
|
389 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
390 | + $aIt = new RecursiveArrayIterator($haystack); |
|
391 | + $it = new RecursiveIteratorIterator($aIt); |
|
392 | + |
|
393 | + while ($it->valid()) { |
|
394 | + if (((isset($index) AND ($it->key() == $index)) OR !isset($index)) AND ($it->current() == $needle)) { |
|
395 | + return $aIt->key(); |
|
396 | + } |
|
397 | + |
|
398 | + $it->next(); |
|
399 | + } |
|
400 | + |
|
401 | + return false; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
406 | + * |
|
407 | + * @param string $dir the current folder where the user currently operates |
|
408 | + * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
409 | + * @return int number of bytes representing |
|
410 | + */ |
|
411 | + public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
412 | + if (is_null($freeSpace) || $freeSpace < 0){ |
|
413 | + $freeSpace = self::freeSpace($dir); |
|
414 | + } |
|
415 | + return min($freeSpace, self::uploadLimit()); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Calculate free space left within user quota |
|
420 | + * |
|
421 | + * @param string $dir the current folder where the user currently operates |
|
422 | + * @return int number of bytes representing |
|
423 | + */ |
|
424 | + public static function freeSpace($dir) { |
|
425 | + $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
426 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
427 | + $freeSpace = max($freeSpace, 0); |
|
428 | + return $freeSpace; |
|
429 | + } else { |
|
430 | + return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
431 | + } |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Calculate PHP upload limit |
|
436 | + * |
|
437 | + * @return int PHP upload file size limit |
|
438 | + */ |
|
439 | + public static function uploadLimit() { |
|
440 | + $ini = \OC::$server->getIniWrapper(); |
|
441 | + $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
442 | + $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
443 | + if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
444 | + return INF; |
|
445 | + } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
446 | + return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
447 | + } else { |
|
448 | + return min($upload_max_filesize, $post_max_size); |
|
449 | + } |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Checks if a function is available |
|
454 | + * |
|
455 | + * @param string $function_name |
|
456 | + * @return bool |
|
457 | + */ |
|
458 | + public static function is_function_enabled($function_name) { |
|
459 | + if (!function_exists($function_name)) { |
|
460 | + return false; |
|
461 | + } |
|
462 | + $ini = \OC::$server->getIniWrapper(); |
|
463 | + $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
464 | + $disabled = array_map('trim', $disabled); |
|
465 | + if (in_array($function_name, $disabled)) { |
|
466 | + return false; |
|
467 | + } |
|
468 | + $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist') ?: ''); |
|
469 | + $disabled = array_map('trim', $disabled); |
|
470 | + if (in_array($function_name, $disabled)) { |
|
471 | + return false; |
|
472 | + } |
|
473 | + return true; |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Try to find a program |
|
478 | + * |
|
479 | + * @param string $program |
|
480 | + * @return null|string |
|
481 | + */ |
|
482 | + public static function findBinaryPath($program) { |
|
483 | + $memcache = \OC::$server->getMemCacheFactory()->createDistributed('findBinaryPath'); |
|
484 | + if ($memcache->hasKey($program)) { |
|
485 | + return $memcache->get($program); |
|
486 | + } |
|
487 | + $result = null; |
|
488 | + if (self::is_function_enabled('exec')) { |
|
489 | + $exeSniffer = new ExecutableFinder(); |
|
490 | + // Returns null if nothing is found |
|
491 | + $result = $exeSniffer->find($program, null, ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin', '/opt/bin']); |
|
492 | + } |
|
493 | + // store the value for 5 minutes |
|
494 | + $memcache->set($program, $result, 300); |
|
495 | + return $result; |
|
496 | + } |
|
497 | + |
|
498 | + /** |
|
499 | + * Calculate the disc space for the given path |
|
500 | + * |
|
501 | + * @param string $path |
|
502 | + * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
503 | + * @return array |
|
504 | + * @throws \OCP\Files\NotFoundException |
|
505 | + */ |
|
506 | + public static function getStorageInfo($path, $rootInfo = null) { |
|
507 | + // return storage info without adding mount points |
|
508 | + $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
509 | + |
|
510 | + if (!$rootInfo) { |
|
511 | + $rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false); |
|
512 | + } |
|
513 | + if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
514 | + throw new \OCP\Files\NotFoundException(); |
|
515 | + } |
|
516 | + $used = $rootInfo->getSize(); |
|
517 | + if ($used < 0) { |
|
518 | + $used = 0; |
|
519 | + } |
|
520 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
521 | + $storage = $rootInfo->getStorage(); |
|
522 | + $sourceStorage = $storage; |
|
523 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
524 | + $includeExtStorage = false; |
|
525 | + $sourceStorage = $storage->getSourceStorage(); |
|
526 | + } |
|
527 | + if ($includeExtStorage) { |
|
528 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
529 | + || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
530 | + ) { |
|
531 | + /** @var \OC\Files\Storage\Home $storage */ |
|
532 | + $userInstance = $storage->getUser(); |
|
533 | + $user = ($userInstance === null) ? null : $userInstance->getUID(); |
|
534 | + } else { |
|
535 | + $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
536 | + } |
|
537 | + if ($user) { |
|
538 | + $quota = OC_Util::getUserQuota($user); |
|
539 | + } else { |
|
540 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
541 | + } |
|
542 | + if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
543 | + // always get free space / total space from root + mount points |
|
544 | + return self::getGlobalStorageInfo(); |
|
545 | + } |
|
546 | + } |
|
547 | + |
|
548 | + // TODO: need a better way to get total space from storage |
|
549 | + if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
550 | + /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
551 | + $quota = $sourceStorage->getQuota(); |
|
552 | + } |
|
553 | + $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
554 | + if ($free >= 0) { |
|
555 | + $total = $free + $used; |
|
556 | + } else { |
|
557 | + $total = $free; //either unknown or unlimited |
|
558 | + } |
|
559 | + if ($total > 0) { |
|
560 | + if ($quota > 0 && $total > $quota) { |
|
561 | + $total = $quota; |
|
562 | + } |
|
563 | + // prevent division by zero or error codes (negative values) |
|
564 | + $relative = round(($used / $total) * 10000) / 100; |
|
565 | + } else { |
|
566 | + $relative = 0; |
|
567 | + } |
|
568 | + |
|
569 | + $ownerId = $storage->getOwner($path); |
|
570 | + $ownerDisplayName = ''; |
|
571 | + $owner = \OC::$server->getUserManager()->get($ownerId); |
|
572 | + if($owner) { |
|
573 | + $ownerDisplayName = $owner->getDisplayName(); |
|
574 | + } |
|
575 | + |
|
576 | + return [ |
|
577 | + 'free' => $free, |
|
578 | + 'used' => $used, |
|
579 | + 'quota' => $quota, |
|
580 | + 'total' => $total, |
|
581 | + 'relative' => $relative, |
|
582 | + 'owner' => $ownerId, |
|
583 | + 'ownerDisplayName' => $ownerDisplayName, |
|
584 | + ]; |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Get storage info including all mount points and quota |
|
589 | + * |
|
590 | + * @return array |
|
591 | + */ |
|
592 | + private static function getGlobalStorageInfo() { |
|
593 | + $quota = OC_Util::getUserQuota(\OCP\User::getUser()); |
|
594 | + |
|
595 | + $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
596 | + $used = $rootInfo['size']; |
|
597 | + if ($used < 0) { |
|
598 | + $used = 0; |
|
599 | + } |
|
600 | + |
|
601 | + $total = $quota; |
|
602 | + $free = $quota - $used; |
|
603 | + |
|
604 | + if ($total > 0) { |
|
605 | + if ($quota > 0 && $total > $quota) { |
|
606 | + $total = $quota; |
|
607 | + } |
|
608 | + // prevent division by zero or error codes (negative values) |
|
609 | + $relative = round(($used / $total) * 10000) / 100; |
|
610 | + } else { |
|
611 | + $relative = 0; |
|
612 | + } |
|
613 | + |
|
614 | + return array('free' => $free, 'used' => $used, 'total' => $total, 'relative' => $relative); |
|
615 | + |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * Returns whether the config file is set manually to read-only |
|
620 | + * @return bool |
|
621 | + */ |
|
622 | + public static function isReadOnlyConfigEnabled() { |
|
623 | + return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
624 | + } |
|
625 | 625 | } |
@@ -57,476 +57,476 @@ |
||
57 | 57 | * @since 4.0.0 |
58 | 58 | */ |
59 | 59 | class Util { |
60 | - /** |
|
61 | - * @deprecated 14.0.0 use \OCP\ILogger::DEBUG |
|
62 | - */ |
|
63 | - const DEBUG=0; |
|
64 | - /** |
|
65 | - * @deprecated 14.0.0 use \OCP\ILogger::INFO |
|
66 | - */ |
|
67 | - const INFO=1; |
|
68 | - /** |
|
69 | - * @deprecated 14.0.0 use \OCP\ILogger::WARN |
|
70 | - */ |
|
71 | - const WARN=2; |
|
72 | - /** |
|
73 | - * @deprecated 14.0.0 use \OCP\ILogger::ERROR |
|
74 | - */ |
|
75 | - const ERROR=3; |
|
76 | - /** |
|
77 | - * @deprecated 14.0.0 use \OCP\ILogger::FATAL |
|
78 | - */ |
|
79 | - const FATAL=4; |
|
80 | - |
|
81 | - /** \OCP\Share\IManager */ |
|
82 | - private static $shareManager; |
|
83 | - |
|
84 | - /** |
|
85 | - * get the current installed version of Nextcloud |
|
86 | - * @return array |
|
87 | - * @since 4.0.0 |
|
88 | - */ |
|
89 | - public static function getVersion() { |
|
90 | - return \OC_Util::getVersion(); |
|
91 | - } |
|
60 | + /** |
|
61 | + * @deprecated 14.0.0 use \OCP\ILogger::DEBUG |
|
62 | + */ |
|
63 | + const DEBUG=0; |
|
64 | + /** |
|
65 | + * @deprecated 14.0.0 use \OCP\ILogger::INFO |
|
66 | + */ |
|
67 | + const INFO=1; |
|
68 | + /** |
|
69 | + * @deprecated 14.0.0 use \OCP\ILogger::WARN |
|
70 | + */ |
|
71 | + const WARN=2; |
|
72 | + /** |
|
73 | + * @deprecated 14.0.0 use \OCP\ILogger::ERROR |
|
74 | + */ |
|
75 | + const ERROR=3; |
|
76 | + /** |
|
77 | + * @deprecated 14.0.0 use \OCP\ILogger::FATAL |
|
78 | + */ |
|
79 | + const FATAL=4; |
|
80 | + |
|
81 | + /** \OCP\Share\IManager */ |
|
82 | + private static $shareManager; |
|
83 | + |
|
84 | + /** |
|
85 | + * get the current installed version of Nextcloud |
|
86 | + * @return array |
|
87 | + * @since 4.0.0 |
|
88 | + */ |
|
89 | + public static function getVersion() { |
|
90 | + return \OC_Util::getVersion(); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Set current update channel |
|
95 | - * @param string $channel |
|
96 | - * @since 8.1.0 |
|
97 | - */ |
|
98 | - public static function setChannel($channel) { |
|
99 | - \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
100 | - } |
|
93 | + /** |
|
94 | + * Set current update channel |
|
95 | + * @param string $channel |
|
96 | + * @since 8.1.0 |
|
97 | + */ |
|
98 | + public static function setChannel($channel) { |
|
99 | + \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Get current update channel |
|
104 | - * @return string |
|
105 | - * @since 8.1.0 |
|
106 | - */ |
|
107 | - public static function getChannel() { |
|
108 | - return \OC_Util::getChannel(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * write a message in the log |
|
113 | - * @param string $app |
|
114 | - * @param string $message |
|
115 | - * @param int $level |
|
116 | - * @since 4.0.0 |
|
117 | - * @deprecated 13.0.0 use log of \OCP\ILogger |
|
118 | - */ |
|
119 | - public static function writeLog( $app, $message, $level ) { |
|
120 | - $context = ['app' => $app]; |
|
121 | - \OC::$server->getLogger()->log($level, $message, $context); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * check if sharing is disabled for the current user |
|
126 | - * |
|
127 | - * @return boolean |
|
128 | - * @since 7.0.0 |
|
129 | - * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser |
|
130 | - */ |
|
131 | - public static function isSharingDisabledForUser() { |
|
132 | - if (self::$shareManager === null) { |
|
133 | - self::$shareManager = \OC::$server->getShareManager(); |
|
134 | - } |
|
135 | - |
|
136 | - $user = \OC::$server->getUserSession()->getUser(); |
|
137 | - if ($user !== null) { |
|
138 | - $user = $user->getUID(); |
|
139 | - } |
|
140 | - |
|
141 | - return self::$shareManager->sharingDisabledForUser($user); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * get l10n object |
|
146 | - * @param string $application |
|
147 | - * @param string|null $language |
|
148 | - * @return \OCP\IL10N |
|
149 | - * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
150 | - */ |
|
151 | - public static function getL10N($application, $language = null) { |
|
152 | - return \OC::$server->getL10N($application, $language); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * add a css file |
|
157 | - * @param string $application |
|
158 | - * @param string $file |
|
159 | - * @since 4.0.0 |
|
160 | - */ |
|
161 | - public static function addStyle( $application, $file = null ) { |
|
162 | - \OC_Util::addStyle( $application, $file ); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * add a javascript file |
|
167 | - * @param string $application |
|
168 | - * @param string $file |
|
169 | - * @since 4.0.0 |
|
170 | - */ |
|
171 | - public static function addScript( $application, $file = null ) { |
|
172 | - \OC_Util::addScript( $application, $file ); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Add a translation JS file |
|
177 | - * @param string $application application id |
|
178 | - * @param string $languageCode language code, defaults to the current locale |
|
179 | - * @since 8.0.0 |
|
180 | - */ |
|
181 | - public static function addTranslations($application, $languageCode = null) { |
|
182 | - \OC_Util::addTranslations($application, $languageCode); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Add a custom element to the header |
|
187 | - * If $text is null then the element will be written as empty element. |
|
188 | - * So use "" to get a closing tag. |
|
189 | - * @param string $tag tag name of the element |
|
190 | - * @param array $attributes array of attributes for the element |
|
191 | - * @param string $text the text content for the element |
|
192 | - * @since 4.0.0 |
|
193 | - */ |
|
194 | - public static function addHeader($tag, $attributes, $text=null) { |
|
195 | - \OC_Util::addHeader($tag, $attributes, $text); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Creates an absolute url to the given app and file. |
|
200 | - * @param string $app app |
|
201 | - * @param string $file file |
|
202 | - * @param array $args array with param=>value, will be appended to the returned url |
|
203 | - * The value of $args will be urlencoded |
|
204 | - * @return string the url |
|
205 | - * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
206 | - */ |
|
207 | - public static function linkToAbsolute( $app, $file, $args = array() ) { |
|
208 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
209 | - return $urlGenerator->getAbsoluteURL( |
|
210 | - $urlGenerator->linkTo($app, $file, $args) |
|
211 | - ); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Creates an absolute url for remote use. |
|
216 | - * @param string $service id |
|
217 | - * @return string the url |
|
218 | - * @since 4.0.0 |
|
219 | - */ |
|
220 | - public static function linkToRemote( $service ) { |
|
221 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
222 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
223 | - return $urlGenerator->getAbsoluteURL( |
|
224 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
225 | - ); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Creates an absolute url for public use |
|
230 | - * @param string $service id |
|
231 | - * @return string the url |
|
232 | - * @since 4.5.0 |
|
233 | - * @deprecated 15.0.0 - use OCP\IURLGenerator |
|
234 | - */ |
|
235 | - public static function linkToPublic($service) { |
|
236 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
237 | - if ($service === 'files') { |
|
238 | - return $urlGenerator->getAbsoluteURL('/s'); |
|
239 | - } |
|
240 | - return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Returns the server host name without an eventual port number |
|
245 | - * @return string the server hostname |
|
246 | - * @since 5.0.0 |
|
247 | - */ |
|
248 | - public static function getServerHostName() { |
|
249 | - $host_name = \OC::$server->getRequest()->getServerHost(); |
|
250 | - // strip away port number (if existing) |
|
251 | - $colon_pos = strpos($host_name, ':'); |
|
252 | - if ($colon_pos != FALSE) { |
|
253 | - $host_name = substr($host_name, 0, $colon_pos); |
|
254 | - } |
|
255 | - return $host_name; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Returns the default email address |
|
260 | - * @param string $user_part the user part of the address |
|
261 | - * @return string the default email address |
|
262 | - * |
|
263 | - * Assembles a default email address (using the server hostname |
|
264 | - * and the given user part, and returns it |
|
265 | - * Example: when given lostpassword-noreply as $user_part param, |
|
266 | - * and is currently accessed via http(s)://example.com/, |
|
267 | - * it would return '[email protected]' |
|
268 | - * |
|
269 | - * If the configuration value 'mail_from_address' is set in |
|
270 | - * config.php, this value will override the $user_part that |
|
271 | - * is passed to this function |
|
272 | - * @since 5.0.0 |
|
273 | - */ |
|
274 | - public static function getDefaultEmailAddress($user_part) { |
|
275 | - $config = \OC::$server->getConfig(); |
|
276 | - $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
277 | - $host_name = self::getServerHostName(); |
|
278 | - $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
279 | - $defaultEmailAddress = $user_part.'@'.$host_name; |
|
280 | - |
|
281 | - $mailer = \OC::$server->getMailer(); |
|
282 | - if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
283 | - return $defaultEmailAddress; |
|
284 | - } |
|
285 | - |
|
286 | - // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
287 | - return $user_part.'@localhost.localdomain'; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Make a human file size (2048 to 2 kB) |
|
292 | - * @param int $bytes file size in bytes |
|
293 | - * @return string a human readable file size |
|
294 | - * @since 4.0.0 |
|
295 | - */ |
|
296 | - public static function humanFileSize($bytes) { |
|
297 | - return \OC_Helper::humanFileSize($bytes); |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Make a computer file size (2 kB to 2048) |
|
302 | - * @param string $str file size in a fancy format |
|
303 | - * @return float a file size in bytes |
|
304 | - * |
|
305 | - * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
306 | - * @since 4.0.0 |
|
307 | - */ |
|
308 | - public static function computerFileSize($str) { |
|
309 | - return \OC_Helper::computerFileSize($str); |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * connects a function to a hook |
|
314 | - * |
|
315 | - * @param string $signalClass class name of emitter |
|
316 | - * @param string $signalName name of signal |
|
317 | - * @param string|object $slotClass class name of slot |
|
318 | - * @param string $slotName name of slot |
|
319 | - * @return bool |
|
320 | - * |
|
321 | - * This function makes it very easy to connect to use hooks. |
|
322 | - * |
|
323 | - * TODO: write example |
|
324 | - * @since 4.0.0 |
|
325 | - */ |
|
326 | - static public function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
327 | - return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Emits a signal. To get data from the slot use references! |
|
332 | - * @param string $signalclass class name of emitter |
|
333 | - * @param string $signalname name of signal |
|
334 | - * @param array $params default: array() array with additional data |
|
335 | - * @return bool true if slots exists or false if not |
|
336 | - * |
|
337 | - * TODO: write example |
|
338 | - * @since 4.0.0 |
|
339 | - */ |
|
340 | - static public function emitHook($signalclass, $signalname, $params = array()) { |
|
341 | - return \OC_Hook::emit($signalclass, $signalname, $params); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
346 | - * multiple OC_Template elements which invoke `callRegister`. If the value |
|
347 | - * would not be cached these unit-tests would fail. |
|
348 | - * @var string |
|
349 | - */ |
|
350 | - private static $token = ''; |
|
351 | - |
|
352 | - /** |
|
353 | - * Register an get/post call. This is important to prevent CSRF attacks |
|
354 | - * @since 4.5.0 |
|
355 | - */ |
|
356 | - public static function callRegister() { |
|
357 | - if(self::$token === '') { |
|
358 | - self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
359 | - } |
|
360 | - return self::$token; |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Check an ajax get/post call if the request token is valid. exit if not. |
|
365 | - * @since 4.5.0 |
|
366 | - * @deprecated 9.0.0 Use annotations based on the app framework. |
|
367 | - */ |
|
368 | - public static function callCheck() { |
|
369 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
370 | - header('Location: '.\OC::$WEBROOT); |
|
371 | - exit(); |
|
372 | - } |
|
373 | - |
|
374 | - if (!\OC::$server->getRequest()->passesCSRFCheck()) { |
|
375 | - exit(); |
|
376 | - } |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Used to sanitize HTML |
|
381 | - * |
|
382 | - * This function is used to sanitize HTML and should be applied on any |
|
383 | - * string or array of strings before displaying it on a web page. |
|
384 | - * |
|
385 | - * @param string|array $value |
|
386 | - * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
387 | - * @since 4.5.0 |
|
388 | - */ |
|
389 | - public static function sanitizeHTML($value) { |
|
390 | - return \OC_Util::sanitizeHTML($value); |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Public function to encode url parameters |
|
395 | - * |
|
396 | - * This function is used to encode path to file before output. |
|
397 | - * Encoding is done according to RFC 3986 with one exception: |
|
398 | - * Character '/' is preserved as is. |
|
399 | - * |
|
400 | - * @param string $component part of URI to encode |
|
401 | - * @return string |
|
402 | - * @since 6.0.0 |
|
403 | - */ |
|
404 | - public static function encodePath($component) { |
|
405 | - return \OC_Util::encodePath($component); |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
410 | - * |
|
411 | - * @param array $input The array to work on |
|
412 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
413 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
414 | - * @return array |
|
415 | - * @since 4.5.0 |
|
416 | - */ |
|
417 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
418 | - return \OC_Helper::mb_array_change_key_case($input, $case, $encoding); |
|
419 | - } |
|
420 | - |
|
421 | - /** |
|
422 | - * performs a search in a nested array |
|
423 | - * |
|
424 | - * @param array $haystack the array to be searched |
|
425 | - * @param string $needle the search string |
|
426 | - * @param mixed $index optional, only search this key name |
|
427 | - * @return mixed the key of the matching field, otherwise false |
|
428 | - * @since 4.5.0 |
|
429 | - */ |
|
430 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
431 | - return \OC_Helper::recursiveArraySearch($haystack, $needle, $index); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
436 | - * |
|
437 | - * @param string $dir the current folder where the user currently operates |
|
438 | - * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
439 | - * @return int number of bytes representing |
|
440 | - * @since 5.0.0 |
|
441 | - */ |
|
442 | - public static function maxUploadFilesize($dir, $free = null) { |
|
443 | - return \OC_Helper::maxUploadFilesize($dir, $free); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Calculate free space left within user quota |
|
448 | - * @param string $dir the current folder where the user currently operates |
|
449 | - * @return int number of bytes representing |
|
450 | - * @since 7.0.0 |
|
451 | - */ |
|
452 | - public static function freeSpace($dir) { |
|
453 | - return \OC_Helper::freeSpace($dir); |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * Calculate PHP upload limit |
|
458 | - * |
|
459 | - * @return int number of bytes representing |
|
460 | - * @since 7.0.0 |
|
461 | - */ |
|
462 | - public static function uploadLimit() { |
|
463 | - return \OC_Helper::uploadLimit(); |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * Returns whether the given file name is valid |
|
468 | - * @param string $file file name to check |
|
469 | - * @return bool true if the file name is valid, false otherwise |
|
470 | - * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
471 | - * @since 7.0.0 |
|
472 | - * @suppress PhanDeprecatedFunction |
|
473 | - */ |
|
474 | - public static function isValidFileName($file) { |
|
475 | - return \OC_Util::isValidFileName($file); |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Compare two strings to provide a natural sort |
|
480 | - * @param string $a first string to compare |
|
481 | - * @param string $b second string to compare |
|
482 | - * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
483 | - * or 0 if the strings are identical |
|
484 | - * @since 7.0.0 |
|
485 | - */ |
|
486 | - public static function naturalSortCompare($a, $b) { |
|
487 | - return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * check if a password is required for each public link |
|
492 | - * @return boolean |
|
493 | - * @since 7.0.0 |
|
494 | - */ |
|
495 | - public static function isPublicLinkPasswordRequired() { |
|
496 | - return \OC_Util::isPublicLinkPasswordRequired(); |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
500 | - * check if share API enforces a default expire date |
|
501 | - * @return boolean |
|
502 | - * @since 8.0.0 |
|
503 | - */ |
|
504 | - public static function isDefaultExpireDateEnforced() { |
|
505 | - return \OC_Util::isDefaultExpireDateEnforced(); |
|
506 | - } |
|
507 | - |
|
508 | - protected static $needUpgradeCache = null; |
|
509 | - |
|
510 | - /** |
|
511 | - * Checks whether the current version needs upgrade. |
|
512 | - * |
|
513 | - * @return bool true if upgrade is needed, false otherwise |
|
514 | - * @since 7.0.0 |
|
515 | - */ |
|
516 | - public static function needUpgrade() { |
|
517 | - if (!isset(self::$needUpgradeCache)) { |
|
518 | - self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getSystemConfig()); |
|
519 | - } |
|
520 | - return self::$needUpgradeCache; |
|
521 | - } |
|
522 | - |
|
523 | - /** |
|
524 | - * is this Internet explorer ? |
|
525 | - * |
|
526 | - * @return boolean |
|
527 | - * @since 14.0.0 |
|
528 | - */ |
|
529 | - public static function isIe() { |
|
530 | - return \OC_Util::isIe(); |
|
531 | - } |
|
102 | + /** |
|
103 | + * Get current update channel |
|
104 | + * @return string |
|
105 | + * @since 8.1.0 |
|
106 | + */ |
|
107 | + public static function getChannel() { |
|
108 | + return \OC_Util::getChannel(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * write a message in the log |
|
113 | + * @param string $app |
|
114 | + * @param string $message |
|
115 | + * @param int $level |
|
116 | + * @since 4.0.0 |
|
117 | + * @deprecated 13.0.0 use log of \OCP\ILogger |
|
118 | + */ |
|
119 | + public static function writeLog( $app, $message, $level ) { |
|
120 | + $context = ['app' => $app]; |
|
121 | + \OC::$server->getLogger()->log($level, $message, $context); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * check if sharing is disabled for the current user |
|
126 | + * |
|
127 | + * @return boolean |
|
128 | + * @since 7.0.0 |
|
129 | + * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser |
|
130 | + */ |
|
131 | + public static function isSharingDisabledForUser() { |
|
132 | + if (self::$shareManager === null) { |
|
133 | + self::$shareManager = \OC::$server->getShareManager(); |
|
134 | + } |
|
135 | + |
|
136 | + $user = \OC::$server->getUserSession()->getUser(); |
|
137 | + if ($user !== null) { |
|
138 | + $user = $user->getUID(); |
|
139 | + } |
|
140 | + |
|
141 | + return self::$shareManager->sharingDisabledForUser($user); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * get l10n object |
|
146 | + * @param string $application |
|
147 | + * @param string|null $language |
|
148 | + * @return \OCP\IL10N |
|
149 | + * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
150 | + */ |
|
151 | + public static function getL10N($application, $language = null) { |
|
152 | + return \OC::$server->getL10N($application, $language); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * add a css file |
|
157 | + * @param string $application |
|
158 | + * @param string $file |
|
159 | + * @since 4.0.0 |
|
160 | + */ |
|
161 | + public static function addStyle( $application, $file = null ) { |
|
162 | + \OC_Util::addStyle( $application, $file ); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * add a javascript file |
|
167 | + * @param string $application |
|
168 | + * @param string $file |
|
169 | + * @since 4.0.0 |
|
170 | + */ |
|
171 | + public static function addScript( $application, $file = null ) { |
|
172 | + \OC_Util::addScript( $application, $file ); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Add a translation JS file |
|
177 | + * @param string $application application id |
|
178 | + * @param string $languageCode language code, defaults to the current locale |
|
179 | + * @since 8.0.0 |
|
180 | + */ |
|
181 | + public static function addTranslations($application, $languageCode = null) { |
|
182 | + \OC_Util::addTranslations($application, $languageCode); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Add a custom element to the header |
|
187 | + * If $text is null then the element will be written as empty element. |
|
188 | + * So use "" to get a closing tag. |
|
189 | + * @param string $tag tag name of the element |
|
190 | + * @param array $attributes array of attributes for the element |
|
191 | + * @param string $text the text content for the element |
|
192 | + * @since 4.0.0 |
|
193 | + */ |
|
194 | + public static function addHeader($tag, $attributes, $text=null) { |
|
195 | + \OC_Util::addHeader($tag, $attributes, $text); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Creates an absolute url to the given app and file. |
|
200 | + * @param string $app app |
|
201 | + * @param string $file file |
|
202 | + * @param array $args array with param=>value, will be appended to the returned url |
|
203 | + * The value of $args will be urlencoded |
|
204 | + * @return string the url |
|
205 | + * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
206 | + */ |
|
207 | + public static function linkToAbsolute( $app, $file, $args = array() ) { |
|
208 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
209 | + return $urlGenerator->getAbsoluteURL( |
|
210 | + $urlGenerator->linkTo($app, $file, $args) |
|
211 | + ); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Creates an absolute url for remote use. |
|
216 | + * @param string $service id |
|
217 | + * @return string the url |
|
218 | + * @since 4.0.0 |
|
219 | + */ |
|
220 | + public static function linkToRemote( $service ) { |
|
221 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
222 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
223 | + return $urlGenerator->getAbsoluteURL( |
|
224 | + $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
225 | + ); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Creates an absolute url for public use |
|
230 | + * @param string $service id |
|
231 | + * @return string the url |
|
232 | + * @since 4.5.0 |
|
233 | + * @deprecated 15.0.0 - use OCP\IURLGenerator |
|
234 | + */ |
|
235 | + public static function linkToPublic($service) { |
|
236 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
237 | + if ($service === 'files') { |
|
238 | + return $urlGenerator->getAbsoluteURL('/s'); |
|
239 | + } |
|
240 | + return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Returns the server host name without an eventual port number |
|
245 | + * @return string the server hostname |
|
246 | + * @since 5.0.0 |
|
247 | + */ |
|
248 | + public static function getServerHostName() { |
|
249 | + $host_name = \OC::$server->getRequest()->getServerHost(); |
|
250 | + // strip away port number (if existing) |
|
251 | + $colon_pos = strpos($host_name, ':'); |
|
252 | + if ($colon_pos != FALSE) { |
|
253 | + $host_name = substr($host_name, 0, $colon_pos); |
|
254 | + } |
|
255 | + return $host_name; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Returns the default email address |
|
260 | + * @param string $user_part the user part of the address |
|
261 | + * @return string the default email address |
|
262 | + * |
|
263 | + * Assembles a default email address (using the server hostname |
|
264 | + * and the given user part, and returns it |
|
265 | + * Example: when given lostpassword-noreply as $user_part param, |
|
266 | + * and is currently accessed via http(s)://example.com/, |
|
267 | + * it would return '[email protected]' |
|
268 | + * |
|
269 | + * If the configuration value 'mail_from_address' is set in |
|
270 | + * config.php, this value will override the $user_part that |
|
271 | + * is passed to this function |
|
272 | + * @since 5.0.0 |
|
273 | + */ |
|
274 | + public static function getDefaultEmailAddress($user_part) { |
|
275 | + $config = \OC::$server->getConfig(); |
|
276 | + $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
277 | + $host_name = self::getServerHostName(); |
|
278 | + $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
279 | + $defaultEmailAddress = $user_part.'@'.$host_name; |
|
280 | + |
|
281 | + $mailer = \OC::$server->getMailer(); |
|
282 | + if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
283 | + return $defaultEmailAddress; |
|
284 | + } |
|
285 | + |
|
286 | + // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
287 | + return $user_part.'@localhost.localdomain'; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Make a human file size (2048 to 2 kB) |
|
292 | + * @param int $bytes file size in bytes |
|
293 | + * @return string a human readable file size |
|
294 | + * @since 4.0.0 |
|
295 | + */ |
|
296 | + public static function humanFileSize($bytes) { |
|
297 | + return \OC_Helper::humanFileSize($bytes); |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Make a computer file size (2 kB to 2048) |
|
302 | + * @param string $str file size in a fancy format |
|
303 | + * @return float a file size in bytes |
|
304 | + * |
|
305 | + * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
306 | + * @since 4.0.0 |
|
307 | + */ |
|
308 | + public static function computerFileSize($str) { |
|
309 | + return \OC_Helper::computerFileSize($str); |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * connects a function to a hook |
|
314 | + * |
|
315 | + * @param string $signalClass class name of emitter |
|
316 | + * @param string $signalName name of signal |
|
317 | + * @param string|object $slotClass class name of slot |
|
318 | + * @param string $slotName name of slot |
|
319 | + * @return bool |
|
320 | + * |
|
321 | + * This function makes it very easy to connect to use hooks. |
|
322 | + * |
|
323 | + * TODO: write example |
|
324 | + * @since 4.0.0 |
|
325 | + */ |
|
326 | + static public function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
327 | + return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Emits a signal. To get data from the slot use references! |
|
332 | + * @param string $signalclass class name of emitter |
|
333 | + * @param string $signalname name of signal |
|
334 | + * @param array $params default: array() array with additional data |
|
335 | + * @return bool true if slots exists or false if not |
|
336 | + * |
|
337 | + * TODO: write example |
|
338 | + * @since 4.0.0 |
|
339 | + */ |
|
340 | + static public function emitHook($signalclass, $signalname, $params = array()) { |
|
341 | + return \OC_Hook::emit($signalclass, $signalname, $params); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
346 | + * multiple OC_Template elements which invoke `callRegister`. If the value |
|
347 | + * would not be cached these unit-tests would fail. |
|
348 | + * @var string |
|
349 | + */ |
|
350 | + private static $token = ''; |
|
351 | + |
|
352 | + /** |
|
353 | + * Register an get/post call. This is important to prevent CSRF attacks |
|
354 | + * @since 4.5.0 |
|
355 | + */ |
|
356 | + public static function callRegister() { |
|
357 | + if(self::$token === '') { |
|
358 | + self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
359 | + } |
|
360 | + return self::$token; |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Check an ajax get/post call if the request token is valid. exit if not. |
|
365 | + * @since 4.5.0 |
|
366 | + * @deprecated 9.0.0 Use annotations based on the app framework. |
|
367 | + */ |
|
368 | + public static function callCheck() { |
|
369 | + if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
370 | + header('Location: '.\OC::$WEBROOT); |
|
371 | + exit(); |
|
372 | + } |
|
373 | + |
|
374 | + if (!\OC::$server->getRequest()->passesCSRFCheck()) { |
|
375 | + exit(); |
|
376 | + } |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Used to sanitize HTML |
|
381 | + * |
|
382 | + * This function is used to sanitize HTML and should be applied on any |
|
383 | + * string or array of strings before displaying it on a web page. |
|
384 | + * |
|
385 | + * @param string|array $value |
|
386 | + * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
387 | + * @since 4.5.0 |
|
388 | + */ |
|
389 | + public static function sanitizeHTML($value) { |
|
390 | + return \OC_Util::sanitizeHTML($value); |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Public function to encode url parameters |
|
395 | + * |
|
396 | + * This function is used to encode path to file before output. |
|
397 | + * Encoding is done according to RFC 3986 with one exception: |
|
398 | + * Character '/' is preserved as is. |
|
399 | + * |
|
400 | + * @param string $component part of URI to encode |
|
401 | + * @return string |
|
402 | + * @since 6.0.0 |
|
403 | + */ |
|
404 | + public static function encodePath($component) { |
|
405 | + return \OC_Util::encodePath($component); |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
410 | + * |
|
411 | + * @param array $input The array to work on |
|
412 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
413 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
414 | + * @return array |
|
415 | + * @since 4.5.0 |
|
416 | + */ |
|
417 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
418 | + return \OC_Helper::mb_array_change_key_case($input, $case, $encoding); |
|
419 | + } |
|
420 | + |
|
421 | + /** |
|
422 | + * performs a search in a nested array |
|
423 | + * |
|
424 | + * @param array $haystack the array to be searched |
|
425 | + * @param string $needle the search string |
|
426 | + * @param mixed $index optional, only search this key name |
|
427 | + * @return mixed the key of the matching field, otherwise false |
|
428 | + * @since 4.5.0 |
|
429 | + */ |
|
430 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
431 | + return \OC_Helper::recursiveArraySearch($haystack, $needle, $index); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
436 | + * |
|
437 | + * @param string $dir the current folder where the user currently operates |
|
438 | + * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
439 | + * @return int number of bytes representing |
|
440 | + * @since 5.0.0 |
|
441 | + */ |
|
442 | + public static function maxUploadFilesize($dir, $free = null) { |
|
443 | + return \OC_Helper::maxUploadFilesize($dir, $free); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Calculate free space left within user quota |
|
448 | + * @param string $dir the current folder where the user currently operates |
|
449 | + * @return int number of bytes representing |
|
450 | + * @since 7.0.0 |
|
451 | + */ |
|
452 | + public static function freeSpace($dir) { |
|
453 | + return \OC_Helper::freeSpace($dir); |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * Calculate PHP upload limit |
|
458 | + * |
|
459 | + * @return int number of bytes representing |
|
460 | + * @since 7.0.0 |
|
461 | + */ |
|
462 | + public static function uploadLimit() { |
|
463 | + return \OC_Helper::uploadLimit(); |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * Returns whether the given file name is valid |
|
468 | + * @param string $file file name to check |
|
469 | + * @return bool true if the file name is valid, false otherwise |
|
470 | + * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
471 | + * @since 7.0.0 |
|
472 | + * @suppress PhanDeprecatedFunction |
|
473 | + */ |
|
474 | + public static function isValidFileName($file) { |
|
475 | + return \OC_Util::isValidFileName($file); |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Compare two strings to provide a natural sort |
|
480 | + * @param string $a first string to compare |
|
481 | + * @param string $b second string to compare |
|
482 | + * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
483 | + * or 0 if the strings are identical |
|
484 | + * @since 7.0.0 |
|
485 | + */ |
|
486 | + public static function naturalSortCompare($a, $b) { |
|
487 | + return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * check if a password is required for each public link |
|
492 | + * @return boolean |
|
493 | + * @since 7.0.0 |
|
494 | + */ |
|
495 | + public static function isPublicLinkPasswordRequired() { |
|
496 | + return \OC_Util::isPublicLinkPasswordRequired(); |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | + * check if share API enforces a default expire date |
|
501 | + * @return boolean |
|
502 | + * @since 8.0.0 |
|
503 | + */ |
|
504 | + public static function isDefaultExpireDateEnforced() { |
|
505 | + return \OC_Util::isDefaultExpireDateEnforced(); |
|
506 | + } |
|
507 | + |
|
508 | + protected static $needUpgradeCache = null; |
|
509 | + |
|
510 | + /** |
|
511 | + * Checks whether the current version needs upgrade. |
|
512 | + * |
|
513 | + * @return bool true if upgrade is needed, false otherwise |
|
514 | + * @since 7.0.0 |
|
515 | + */ |
|
516 | + public static function needUpgrade() { |
|
517 | + if (!isset(self::$needUpgradeCache)) { |
|
518 | + self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getSystemConfig()); |
|
519 | + } |
|
520 | + return self::$needUpgradeCache; |
|
521 | + } |
|
522 | + |
|
523 | + /** |
|
524 | + * is this Internet explorer ? |
|
525 | + * |
|
526 | + * @return boolean |
|
527 | + * @since 14.0.0 |
|
528 | + */ |
|
529 | + public static function isIe() { |
|
530 | + return \OC_Util::isIe(); |
|
531 | + } |
|
532 | 532 | } |