Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OC_Util often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OC_Util, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | class OC_Util { |
||
| 63 | public static $scripts = array(); |
||
| 64 | public static $styles = array(); |
||
| 65 | public static $headers = array(); |
||
| 66 | private static $rootMounted = false; |
||
| 67 | private static $fsSetup = false; |
||
| 68 | |||
| 69 | 2 | protected static function getAppManager() { |
|
| 70 | 2 | return \OC::$server->getAppManager(); |
|
| 71 | } |
||
| 72 | |||
| 73 | 1075 | private static function initLocalStorageRootFS() { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * mounting an object storage as the root fs will in essence remove the |
||
| 86 | * necessity of a data folder being present. |
||
| 87 | * TODO make home storage aware of this and use the object storage instead of local disk access |
||
| 88 | * |
||
| 89 | * @param array $config containing 'class' and optional 'arguments' |
||
| 90 | */ |
||
| 91 | private static function initObjectStoreRootFS($config) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Can be set up |
||
| 115 | * |
||
| 116 | * @param string $user |
||
| 117 | * @return boolean |
||
| 118 | * @description configure the initial filesystem based on the configuration |
||
| 119 | */ |
||
| 120 | 1101 | public static function setupFS($user = '') { |
|
| 212 | |||
| 213 | /** |
||
| 214 | * check if a password is required for each public link |
||
| 215 | * |
||
| 216 | * @return boolean |
||
| 217 | */ |
||
| 218 | 50 | public static function isPublicLinkPasswordRequired() { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * check if sharing is disabled for the current user |
||
| 226 | * @param IConfig $config |
||
| 227 | * @param IGroupManager $groupManager |
||
| 228 | * @param IUser|null $user |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | 905 | public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) { |
|
| 252 | |||
| 253 | /** |
||
| 254 | * check if share API enforces a default expire date |
||
| 255 | * |
||
| 256 | * @return boolean |
||
| 257 | */ |
||
| 258 | 10 | public static function isDefaultExpireDateEnforced() { |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Get the quota of a user |
||
| 271 | * |
||
| 272 | * @param string $user |
||
| 273 | * @return int Quota bytes |
||
| 274 | */ |
||
| 275 | 958 | public static function getUserQuota($user) { |
|
| 287 | |||
| 288 | /** |
||
| 289 | * copies the skeleton to the users /files |
||
| 290 | * |
||
| 291 | * @param String $userId |
||
| 292 | * @param \OCP\Files\Folder $userDirectory |
||
| 293 | */ |
||
| 294 | 493 | public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) { |
|
| 309 | |||
| 310 | /** |
||
| 311 | * copies a directory recursively by using streams |
||
| 312 | * |
||
| 313 | * @param string $source |
||
| 314 | * @param \OCP\Files\Folder $target |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | 493 | public static function copyr($source, \OCP\Files\Folder $target) { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | 1114 | public static function tearDownFS() { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * get the current installed version of ownCloud |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | 60 | public static function getVersion() { |
|
| 351 | |||
| 352 | /** |
||
| 353 | * get the current installed version string of ownCloud |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 1 | public static function getVersionString() { |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @description get the current installed edition of ownCloud. There is the community |
||
| 364 | * edition that just returns an empty string and the enterprise edition |
||
| 365 | * that returns "Enterprise". |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 41 | public static function getEditionString() { |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @description get the update channel of the current installed of ownCloud. |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | 9 | public static function getChannel() { |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @description get the build number of the current installed of ownCloud. |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | 5 | public static function getBuild() { |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @description load the version.php into the session as cache |
||
| 397 | */ |
||
| 398 | 65 | private static function loadVersion() { |
|
| 429 | |||
| 430 | /** |
||
| 431 | * generates a path for JS/CSS files. If no application is provided it will create the path for core. |
||
| 432 | * |
||
| 433 | * @param string $application application to get the files from |
||
| 434 | * @param string $directory directory withing this application (css, js, vendor, etc) |
||
| 435 | * @param string $file the file inside of the above folder |
||
| 436 | * @return string the path |
||
| 437 | */ |
||
| 438 | 5 | private static function generatePath($application, $directory, $file) { |
|
| 449 | |||
| 450 | /** |
||
| 451 | * add a javascript file |
||
| 452 | * |
||
| 453 | * @param string $application application id |
||
| 454 | * @param string|null $file filename |
||
| 455 | * @param bool $prepend prepend the Script to the beginning of the list |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | 5 | public static function addScript($application, $file = null, $prepend = false) { |
|
| 474 | |||
| 475 | /** |
||
| 476 | * add a javascript file from the vendor sub folder |
||
| 477 | * |
||
| 478 | * @param string $application application id |
||
| 479 | * @param string|null $file filename |
||
| 480 | * @param bool $prepend prepend the Script to the beginning of the list |
||
| 481 | * @return void |
||
| 482 | */ |
||
| 483 | 5 | View Code Duplication | public static function addVendorScript($application, $file = null, $prepend = false) { |
| 494 | |||
| 495 | /** |
||
| 496 | * add a translation JS file |
||
| 497 | * |
||
| 498 | * @param string $application application id |
||
| 499 | * @param string $languageCode language code, defaults to the current language |
||
| 500 | * @param bool $prepend prepend the Script to the beginning of the list |
||
| 501 | */ |
||
| 502 | 2 | public static function addTranslations($application, $languageCode = null, $prepend = false) { |
|
| 520 | |||
| 521 | /** |
||
| 522 | * add a css file |
||
| 523 | * |
||
| 524 | * @param string $application application id |
||
| 525 | * @param string|null $file filename |
||
| 526 | * @param bool $prepend prepend the Style to the beginning of the list |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 5 | View Code Duplication | public static function addStyle($application, $file = null, $prepend = false) { |
| 540 | |||
| 541 | /** |
||
| 542 | * add a css file from the vendor sub folder |
||
| 543 | * |
||
| 544 | * @param string $application application id |
||
| 545 | * @param string|null $file filename |
||
| 546 | * @param bool $prepend prepend the Style to the beginning of the list |
||
| 547 | * @return void |
||
| 548 | */ |
||
| 549 | 1 | View Code Duplication | public static function addVendorStyle($application, $file = null, $prepend = false) { |
| 560 | |||
| 561 | /** |
||
| 562 | * Add a custom element to the header |
||
| 563 | * If $text is null then the element will be written as empty element. |
||
| 564 | * So use "" to get a closing tag. |
||
| 565 | * @param string $tag tag name of the element |
||
| 566 | * @param array $attributes array of attributes for the element |
||
| 567 | * @param string $text the text content for the element |
||
| 568 | */ |
||
| 569 | View Code Duplication | public static function addHeader($tag, $attributes, $text=null) { |
|
| 576 | |||
| 577 | /** |
||
| 578 | * formats a timestamp in the "right" way |
||
| 579 | * |
||
| 580 | * @param int $timestamp |
||
| 581 | * @param bool $dateOnly option to omit time from the result |
||
| 582 | * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to |
||
| 583 | * @return string timestamp |
||
| 584 | * |
||
| 585 | * @deprecated Use \OC::$server->query('DateTimeFormatter') instead |
||
| 586 | */ |
||
| 587 | 9 | public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) { |
|
| 599 | |||
| 600 | /** |
||
| 601 | * check if the current server configuration is suitable for ownCloud |
||
| 602 | * |
||
| 603 | * @param \OCP\IConfig $config |
||
| 604 | * @return array arrays with error messages and hints |
||
| 605 | */ |
||
| 606 | 7 | public static function checkServer(\OCP\IConfig $config) { |
|
| 607 | 7 | $l = \OC::$server->getL10N('lib'); |
|
| 608 | 7 | $errors = array(); |
|
| 609 | 7 | $CONFIG_DATADIRECTORY = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data'); |
|
| 610 | |||
| 611 | 7 | if (!self::needUpgrade($config) && $config->getSystemValue('installed', false)) { |
|
| 612 | // this check needs to be done every time |
||
| 613 | 3 | $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY); |
|
| 614 | 3 | } |
|
| 615 | |||
| 616 | // Assume that if checkServer() succeeded before in this session, then all is fine. |
||
| 617 | 7 | if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) { |
|
| 618 | return $errors; |
||
| 619 | } |
||
| 620 | |||
| 621 | 7 | $webServerRestart = false; |
|
| 622 | 7 | $setup = new \OC\Setup($config, \OC::$server->getIniWrapper(), \OC::$server->getL10N('lib'), |
|
| 623 | 7 | new \OC_Defaults(), \OC::$server->getLogger(), \OC::$server->getSecureRandom()); |
|
| 624 | |||
| 625 | 7 | $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 626 | |||
| 627 | 7 | $availableDatabases = $setup->getSupportedDatabases(); |
|
| 628 | 7 | if (empty($availableDatabases)) { |
|
| 629 | $errors[] = array( |
||
| 630 | 'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'), |
||
| 631 | 'hint' => '' //TODO: sane hint |
||
| 632 | ); |
||
| 633 | $webServerRestart = true; |
||
| 634 | } |
||
| 635 | |||
| 636 | // Check if server running on Windows platform |
||
| 637 | 7 | if(OC_Util::runningOnWindows()) { |
|
| 638 | $errors[] = [ |
||
| 639 | 'error' => $l->t('Microsoft Windows Platform is not supported'), |
||
| 640 | 'hint' => $l->t('Running ownCloud Server on the Microsoft Windows platform is not supported. We suggest you ' . |
||
| 641 | 'use a Linux server in a virtual machine if you have no option for migrating the server itself. ' . |
||
| 642 | 'Find Linux packages as well as easy to deploy virtual machine images on <a href="%s">%s</a>. ' . |
||
| 643 | 'For migrating existing installations to Linux you can find some tips and a migration script ' . |
||
| 644 | 'in <a href="%s">our documentation</a>.', |
||
| 645 | ['https://owncloud.org/install/', 'owncloud.org/install/', 'https://owncloud.org/?p=8045']) |
||
| 646 | ]; |
||
| 647 | } |
||
| 648 | |||
| 649 | // Check if config folder is writable. |
||
| 650 | 7 | if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) { |
|
| 651 | $errors[] = array( |
||
| 652 | 'error' => $l->t('Cannot write into "config" directory'), |
||
| 653 | 'hint' => $l->t('This can usually be fixed by ' |
||
| 654 | . '%sgiving the webserver write access to the config directory%s.', |
||
| 655 | array('<a href="' . $urlGenerator->linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>')) |
||
| 656 | ); |
||
| 657 | } |
||
| 658 | |||
| 659 | // Check if there is a writable install folder. |
||
| 660 | 7 | if ($config->getSystemValue('appstoreenabled', true)) { |
|
| 661 | if (OC_App::getInstallPath() === null |
||
| 662 | || !is_writable(OC_App::getInstallPath()) |
||
| 663 | || !is_readable(OC_App::getInstallPath()) |
||
| 664 | ) { |
||
| 665 | $errors[] = array( |
||
| 666 | 'error' => $l->t('Cannot write into "apps" directory'), |
||
| 667 | 'hint' => $l->t('This can usually be fixed by ' |
||
| 668 | . '%sgiving the webserver write access to the apps directory%s' |
||
| 669 | . ' or disabling the appstore in the config file.', |
||
| 670 | array('<a href="' . $urlGenerator->linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>')) |
||
| 671 | ); |
||
| 672 | } |
||
| 673 | } |
||
| 674 | // Create root dir. |
||
| 675 | 7 | if ($config->getSystemValue('installed', false)) { |
|
| 676 | 5 | if (!is_dir($CONFIG_DATADIRECTORY)) { |
|
| 677 | $success = @mkdir($CONFIG_DATADIRECTORY); |
||
| 678 | if ($success) { |
||
| 679 | $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); |
||
| 680 | } else { |
||
| 681 | $errors[] = array( |
||
| 682 | 'error' => $l->t('Cannot create "data" directory (%s)', array($CONFIG_DATADIRECTORY)), |
||
| 683 | 'hint' => $l->t('This can usually be fixed by ' |
||
| 684 | . '<a href="%s" target="_blank">giving the webserver write access to the root directory</a>.', |
||
| 685 | array($urlGenerator->linkToDocs('admin-dir_permissions'))) |
||
| 686 | ); |
||
| 687 | } |
||
| 688 | 5 | } else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) { |
|
| 689 | //common hint for all file permissions error messages |
||
| 690 | 1 | $permissionsHint = $l->t('Permissions can usually be fixed by ' |
|
| 691 | 1 | . '%sgiving the webserver write access to the root directory%s.', |
|
| 692 | 1 | array('<a href="' . $urlGenerator->linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>')); |
|
| 693 | 1 | $errors[] = array( |
|
| 694 | 1 | 'error' => 'Data directory (' . $CONFIG_DATADIRECTORY . ') not writable by ownCloud', |
|
| 695 | 'hint' => $permissionsHint |
||
| 696 | 1 | ); |
|
| 697 | 1 | } else { |
|
| 698 | 4 | $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); |
|
| 699 | } |
||
| 700 | 5 | } |
|
| 701 | |||
| 702 | 7 | if (!OC_Util::isSetLocaleWorking()) { |
|
| 703 | $errors[] = array( |
||
| 704 | 'error' => $l->t('Setting locale to %s failed', |
||
| 705 | array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/' |
||
| 706 | . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')), |
||
| 707 | 'hint' => $l->t('Please install one of these locales on your system and restart your webserver.') |
||
| 708 | ); |
||
| 709 | } |
||
| 710 | |||
| 711 | // Contains the dependencies that should be checked against |
||
| 712 | // classes = class_exists |
||
| 713 | // functions = function_exists |
||
| 714 | // defined = defined |
||
| 715 | // ini = ini_get |
||
| 716 | // If the dependency is not found the missing module name is shown to the EndUser |
||
| 717 | // When adding new checks always verify that they pass on Travis as well |
||
| 718 | // for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini |
||
| 719 | $dependencies = array( |
||
| 720 | 'classes' => array( |
||
| 721 | 7 | 'ZipArchive' => 'zip', |
|
| 722 | 7 | 'DOMDocument' => 'dom', |
|
| 723 | 'XMLWriter' => 'XMLWriter' |
||
| 724 | 7 | ), |
|
| 725 | 'functions' => [ |
||
| 726 | 7 | 'xml_parser_create' => 'libxml', |
|
| 727 | 7 | 'mb_detect_encoding' => 'mb multibyte', |
|
| 728 | 7 | 'ctype_digit' => 'ctype', |
|
| 729 | 7 | 'json_encode' => 'JSON', |
|
| 730 | 7 | 'gd_info' => 'GD', |
|
| 731 | 7 | 'gzencode' => 'zlib', |
|
| 732 | 7 | 'iconv' => 'iconv', |
|
| 733 | 7 | 'simplexml_load_string' => 'SimpleXML', |
|
| 734 | 7 | 'hash' => 'HASH Message Digest Framework', |
|
| 735 | 7 | 'curl_init' => 'cURL', |
|
| 736 | 7 | ], |
|
| 737 | 'defined' => array( |
||
| 738 | 'PDO::ATTR_DRIVER_NAME' => 'PDO' |
||
| 739 | 7 | ), |
|
| 740 | 'ini' => [ |
||
| 741 | 7 | 'default_charset' => 'UTF-8', |
|
| 742 | 7 | ], |
|
| 743 | 7 | ); |
|
| 744 | 7 | $missingDependencies = array(); |
|
| 745 | 7 | $invalidIniSettings = []; |
|
| 746 | 7 | $moduleHint = $l->t('Please ask your server administrator to install the module.'); |
|
| 747 | |||
| 748 | /** |
||
| 749 | * FIXME: The dependency check does not work properly on HHVM on the moment |
||
| 750 | * and prevents installation. Once HHVM is more compatible with our |
||
| 751 | * approach to check for these values we should re-enable those |
||
| 752 | * checks. |
||
| 753 | */ |
||
| 754 | 7 | $iniWrapper = \OC::$server->getIniWrapper(); |
|
| 755 | 7 | if (!self::runningOnHhvm()) { |
|
| 756 | 7 | foreach ($dependencies['classes'] as $class => $module) { |
|
| 757 | 7 | if (!class_exists($class)) { |
|
| 758 | $missingDependencies[] = $module; |
||
| 759 | } |
||
| 760 | 7 | } |
|
| 761 | 7 | foreach ($dependencies['functions'] as $function => $module) { |
|
| 762 | 7 | if (!function_exists($function)) { |
|
| 763 | $missingDependencies[] = $module; |
||
| 764 | } |
||
| 765 | 7 | } |
|
| 766 | 7 | foreach ($dependencies['defined'] as $defined => $module) { |
|
| 767 | 7 | if (!defined($defined)) { |
|
| 768 | $missingDependencies[] = $module; |
||
| 769 | } |
||
| 770 | 7 | } |
|
| 771 | 7 | foreach ($dependencies['ini'] as $setting => $expected) { |
|
| 772 | 7 | if (is_bool($expected)) { |
|
| 773 | if ($iniWrapper->getBool($setting) !== $expected) { |
||
| 774 | $invalidIniSettings[] = [$setting, $expected]; |
||
| 775 | } |
||
| 776 | } |
||
| 777 | 7 | if (is_int($expected)) { |
|
| 778 | if ($iniWrapper->getNumeric($setting) !== $expected) { |
||
| 779 | $invalidIniSettings[] = [$setting, $expected]; |
||
| 780 | } |
||
| 781 | } |
||
| 782 | 7 | if (is_string($expected)) { |
|
| 783 | 7 | if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) { |
|
| 784 | $invalidIniSettings[] = [$setting, $expected]; |
||
| 785 | } |
||
| 786 | 7 | } |
|
| 787 | 7 | } |
|
| 788 | 7 | } |
|
| 789 | |||
| 790 | 7 | foreach($missingDependencies as $missingDependency) { |
|
| 791 | $errors[] = array( |
||
| 792 | 'error' => $l->t('PHP module %s not installed.', array($missingDependency)), |
||
| 793 | 'hint' => $moduleHint |
||
| 794 | ); |
||
| 795 | $webServerRestart = true; |
||
| 796 | 7 | } |
|
| 797 | 7 | foreach($invalidIniSettings as $setting) { |
|
| 798 | if(is_bool($setting[1])) { |
||
| 799 | $setting[1] = ($setting[1]) ? 'on' : 'off'; |
||
| 800 | } |
||
| 801 | $errors[] = [ |
||
| 802 | 'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]), |
||
| 803 | 'hint' => $l->t('Adjusting this setting in php.ini will make ownCloud run again') |
||
| 804 | ]; |
||
| 805 | $webServerRestart = true; |
||
| 806 | 7 | } |
|
| 807 | |||
| 808 | /** |
||
| 809 | * The mbstring.func_overload check can only be performed if the mbstring |
||
| 810 | * module is installed as it will return null if the checking setting is |
||
| 811 | * not available and thus a check on the boolean value fails. |
||
| 812 | * |
||
| 813 | * TODO: Should probably be implemented in the above generic dependency |
||
| 814 | * check somehow in the long-term. |
||
| 815 | */ |
||
| 816 | 7 | if($iniWrapper->getBool('mbstring.func_overload') !== null && |
|
| 817 | 7 | $iniWrapper->getBool('mbstring.func_overload') === true) { |
|
| 818 | $errors[] = array( |
||
| 819 | 'error' => $l->t('mbstring.func_overload is set to "%s" instead of the expected value "0"', [$iniWrapper->getString('mbstring.func_overload')]), |
||
| 820 | 'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini') |
||
| 821 | ); |
||
| 822 | } |
||
| 823 | |||
| 824 | 7 | if (!self::isAnnotationsWorking()) { |
|
| 825 | $errors[] = array( |
||
| 826 | 'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'), |
||
| 827 | 'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.') |
||
| 828 | ); |
||
| 829 | } |
||
| 830 | |||
| 831 | 7 | if (!\OC::$CLI && $webServerRestart) { |
|
| 832 | $errors[] = array( |
||
| 833 | 'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'), |
||
| 834 | 'hint' => $l->t('Please ask your server administrator to restart the web server.') |
||
| 835 | ); |
||
| 836 | } |
||
| 837 | |||
| 838 | 7 | $errors = array_merge($errors, self::checkDatabaseVersion()); |
|
| 839 | |||
| 840 | // Cache the result of this function |
||
| 841 | 7 | \OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0); |
|
| 842 | |||
| 843 | 7 | return $errors; |
|
| 844 | } |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Check the database version |
||
| 848 | * |
||
| 849 | * @return array errors array |
||
| 850 | */ |
||
| 851 | 7 | public static function checkDatabaseVersion() { |
|
| 852 | 7 | $l = \OC::$server->getL10N('lib'); |
|
| 853 | 7 | $errors = array(); |
|
| 854 | 7 | $dbType = \OC_Config::getValue('dbtype', 'sqlite'); |
|
| 855 | 7 | if ($dbType === 'pgsql') { |
|
| 856 | // check PostgreSQL version |
||
| 857 | try { |
||
| 858 | $result = \OC_DB::executeAudited('SHOW SERVER_VERSION'); |
||
| 859 | $data = $result->fetchRow(); |
||
| 860 | if (isset($data['server_version'])) { |
||
| 861 | $version = $data['server_version']; |
||
| 862 | if (version_compare($version, '9.0.0', '<')) { |
||
| 863 | $errors[] = array( |
||
| 864 | 'error' => $l->t('PostgreSQL >= 9 required'), |
||
| 865 | 'hint' => $l->t('Please upgrade your database version') |
||
| 866 | ); |
||
| 867 | } |
||
| 868 | } |
||
| 869 | } catch (\Doctrine\DBAL\DBALException $e) { |
||
| 870 | $logger = \OC::$server->getLogger(); |
||
| 871 | $logger->warning('Error occurred while checking PostgreSQL version, assuming >= 9'); |
||
| 872 | $logger->logException($e); |
||
| 873 | } |
||
| 874 | } |
||
| 875 | 7 | return $errors; |
|
| 876 | } |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Check for correct file permissions of data directory |
||
| 880 | * |
||
| 881 | * @param string $dataDirectory |
||
| 882 | * @return array arrays with error messages and hints |
||
| 883 | */ |
||
| 884 | 4 | public static function checkDataDirectoryPermissions($dataDirectory) { |
|
| 907 | |||
| 908 | /** |
||
| 909 | * Check that the data directory exists and is valid by |
||
| 910 | * checking the existence of the ".ocdata" file. |
||
| 911 | * |
||
| 912 | * @param string $dataDirectory data directory path |
||
| 913 | * @return array errors found |
||
| 914 | */ |
||
| 915 | 5 | public static function checkDataDirectoryValidity($dataDirectory) { |
|
| 933 | |||
| 934 | /** |
||
| 935 | * @param array $errors |
||
| 936 | * @param string[] $messages |
||
| 937 | */ |
||
| 938 | public static function displayLoginPage($errors = array(), $messages = []) { |
||
| 960 | |||
| 961 | |||
| 962 | /** |
||
| 963 | * Check if the app is enabled, redirects to home if not |
||
| 964 | * |
||
| 965 | * @param string $app |
||
| 966 | * @return void |
||
| 967 | */ |
||
| 968 | public static function checkAppEnabled($app) { |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Check if the user is logged in, redirects to home if not. With |
||
| 977 | * redirect URL parameter to the request URI. |
||
| 978 | * |
||
| 979 | * @return void |
||
| 980 | */ |
||
| 981 | public static function checkLoggedIn() { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Check if the user is a admin, redirects to home if not |
||
| 996 | * |
||
| 997 | * @return void |
||
| 998 | */ |
||
| 999 | public static function checkAdminUser() { |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Check if it is allowed to remember login. |
||
| 1009 | * |
||
| 1010 | * @note Every app can set 'rememberlogin' to 'false' to disable the remember login feature |
||
| 1011 | * |
||
| 1012 | * @return bool |
||
| 1013 | */ |
||
| 1014 | public static function rememberLoginAllowed() { |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Check if the user is a subadmin, redirects to home if not |
||
| 1030 | * |
||
| 1031 | * @return null|boolean $groups where the current user is subadmin |
||
| 1032 | */ |
||
| 1033 | public static function checkSubAdminUser() { |
||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * Returns the URL of the default page |
||
| 1050 | * based on the system configuration and |
||
| 1051 | * the apps visible for the current user |
||
| 1052 | * |
||
| 1053 | * @return string URL |
||
| 1054 | */ |
||
| 1055 | 7 | public static function getDefaultPageUrl() { |
|
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Redirect to the user default page |
||
| 1089 | * |
||
| 1090 | * @return void |
||
| 1091 | */ |
||
| 1092 | public static function redirectToDefaultPage() { |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * get an id unique for this instance |
||
| 1100 | * |
||
| 1101 | * @return string |
||
| 1102 | */ |
||
| 1103 | 11 | public static function getInstanceId() { |
|
| 1112 | |||
| 1113 | protected static $obfuscatedToken; |
||
| 1114 | /** |
||
| 1115 | * Register an get/post call. Important to prevent CSRF attacks. |
||
| 1116 | * |
||
| 1117 | * @return string The encrypted CSRF token, the shared secret is appended after the `:`. |
||
| 1118 | * |
||
| 1119 | * @description |
||
| 1120 | * Creates a 'request token' (random) and stores it inside the session. |
||
| 1121 | * Ever subsequent (ajax) request must use such a valid token to succeed, |
||
| 1122 | * otherwise the request will be denied as a protection against CSRF. |
||
| 1123 | * @see OC_Util::isCallRegistered() |
||
| 1124 | */ |
||
| 1125 | 20 | public static function callRegister() { |
|
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Check an ajax get/post call if the request token is valid. |
||
| 1152 | * |
||
| 1153 | * @return boolean False if request token is not set or is invalid. |
||
| 1154 | * @see OC_Util::callRegister() |
||
| 1155 | */ |
||
| 1156 | public static function isCallRegistered() { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Check an ajax get/post call if the request token is valid. Exit if not. |
||
| 1162 | * |
||
| 1163 | * @return void |
||
| 1164 | */ |
||
| 1165 | public static function callCheck() { |
||
| 1170 | |||
| 1171 | /** |
||
| 1172 | * Public function to sanitize HTML |
||
| 1173 | * |
||
| 1174 | * This function is used to sanitize HTML and should be applied on any |
||
| 1175 | * string or array of strings before displaying it on a web page. |
||
| 1176 | * |
||
| 1177 | * @param string|array &$value |
||
| 1178 | * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
||
| 1179 | */ |
||
| 1180 | 8 | public static function sanitizeHTML(&$value) { |
|
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Public function to encode url parameters |
||
| 1192 | * |
||
| 1193 | * This function is used to encode path to file before output. |
||
| 1194 | * Encoding is done according to RFC 3986 with one exception: |
||
| 1195 | * Character '/' is preserved as is. |
||
| 1196 | * |
||
| 1197 | * @param string $component part of URI to encode |
||
| 1198 | * @return string |
||
| 1199 | */ |
||
| 1200 | 4 | public static function encodePath($component) { |
|
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Check if the .htaccess file is working |
||
| 1208 | * @param \OCP\IConfig $config |
||
| 1209 | * @return bool |
||
| 1210 | * @throws Exception |
||
| 1211 | * @throws \OC\HintException If the test file can't get written. |
||
| 1212 | */ |
||
| 1213 | public function isHtaccessWorking(\OCP\IConfig $config) { |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Check if the setlocal call does not work. This can happen if the right |
||
| 1259 | * local packages are not available on the server. |
||
| 1260 | * |
||
| 1261 | * @return bool |
||
| 1262 | */ |
||
| 1263 | 7 | public static function isSetLocaleWorking() { |
|
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Check if it's possible to get the inline annotations |
||
| 1278 | * |
||
| 1279 | * @return bool |
||
| 1280 | */ |
||
| 1281 | 7 | public static function isAnnotationsWorking() { |
|
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Check if the PHP module fileinfo is loaded. |
||
| 1290 | * |
||
| 1291 | * @return bool |
||
| 1292 | */ |
||
| 1293 | 305 | public static function fileInfoLoaded() { |
|
| 1296 | |||
| 1297 | /** |
||
| 1298 | * clear all levels of output buffering |
||
| 1299 | * |
||
| 1300 | * @return void |
||
| 1301 | */ |
||
| 1302 | public static function obEnd() { |
||
| 1307 | |||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * Generates a cryptographic secure pseudo-random string |
||
| 1311 | * |
||
| 1312 | * @param int $length of the random string |
||
| 1313 | * @return string |
||
| 1314 | * @deprecated Use \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length); instead |
||
| 1315 | */ |
||
| 1316 | 2 | public static function generateRandomBytes($length = 30) { |
|
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Get URL content |
||
| 1322 | * @param string $url Url to get content |
||
| 1323 | * @throws Exception If the URL does not start with http:// or https:// |
||
| 1324 | * @return string of the response or false on error |
||
| 1325 | * This function get the content of a page via curl, if curl is enabled. |
||
| 1326 | * If not, file_get_contents is used. |
||
| 1327 | * @deprecated Use \OC::$server->getHTTPClientService()->newClient()->get($url); |
||
| 1328 | */ |
||
| 1329 | public static function getUrlContent($url) { |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * Checks whether the server is running on Windows |
||
| 1339 | * |
||
| 1340 | * @return bool true if running on Windows, false otherwise |
||
| 1341 | */ |
||
| 1342 | 230 | public static function runningOnWindows() { |
|
| 1345 | |||
| 1346 | /** |
||
| 1347 | * Checks whether the server is running on Mac OS X |
||
| 1348 | * |
||
| 1349 | * @return bool true if running on Mac OS X, false otherwise |
||
| 1350 | */ |
||
| 1351 | public static function runningOnMac() { |
||
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Checks whether server is running on HHVM |
||
| 1357 | * |
||
| 1358 | * @return bool True if running on HHVM, false otherwise |
||
| 1359 | */ |
||
| 1360 | 7 | public static function runningOnHhvm() { |
|
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Handles the case that there may not be a theme, then check if a "default" |
||
| 1366 | * theme exists and take that one |
||
| 1367 | * |
||
| 1368 | * @return string the theme |
||
| 1369 | */ |
||
| 1370 | 50 | public static function getTheme() { |
|
| 1381 | |||
| 1382 | /** |
||
| 1383 | * Clear a single file from the opcode cache |
||
| 1384 | * This is useful for writing to the config file |
||
| 1385 | * in case the opcode cache does not re-validate files |
||
| 1386 | * Returns true if successful, false if unsuccessful: |
||
| 1387 | * caller should fall back on clearing the entire cache |
||
| 1388 | * with clearOpcodeCache() if unsuccessful |
||
| 1389 | * |
||
| 1390 | * @param string $path the path of the file to clear from the cache |
||
| 1391 | * @return bool true if underlying function returns true, otherwise false |
||
| 1392 | */ |
||
| 1393 | 59 | public static function deleteFromOpcodeCache($path) { |
|
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Clear the opcode cache if one exists |
||
| 1410 | * This is necessary for writing to the config file |
||
| 1411 | * in case the opcode cache does not re-validate files |
||
| 1412 | * |
||
| 1413 | * @return void |
||
| 1414 | */ |
||
| 1415 | 59 | public static function clearOpcodeCache() { |
|
| 1437 | |||
| 1438 | /** |
||
| 1439 | * Normalize a unicode string |
||
| 1440 | * |
||
| 1441 | * @param string $value a not normalized string |
||
| 1442 | * @return bool|string |
||
| 1443 | */ |
||
| 1444 | 1251 | public static function normalizeUnicode($value) { |
|
| 1457 | |||
| 1458 | /** |
||
| 1459 | * @param boolean|string $file |
||
| 1460 | * @return string |
||
| 1461 | */ |
||
| 1462 | 987 | public static function basename($file) { |
|
| 1467 | |||
| 1468 | /** |
||
| 1469 | * A human readable string is generated based on version, channel and build number |
||
| 1470 | * |
||
| 1471 | * @return string |
||
| 1472 | */ |
||
| 1473 | public static function getHumanVersion() { |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * Returns whether the given file name is valid |
||
| 1484 | * |
||
| 1485 | * @param string $file file name to check |
||
| 1486 | * @return bool true if the file name is valid, false otherwise |
||
| 1487 | * @deprecated use \OC\Files\View::verifyPath() |
||
| 1488 | */ |
||
| 1489 | 31 | public static function isValidFileName($file) { |
|
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Check whether the instance needs to perform an upgrade, |
||
| 1507 | * either when the core version is higher or any app requires |
||
| 1508 | * an upgrade. |
||
| 1509 | * |
||
| 1510 | * @param \OCP\IConfig $config |
||
| 1511 | * @return bool whether the core or any app needs an upgrade |
||
| 1512 | */ |
||
| 1513 | 8 | public static function needUpgrade(\OCP\IConfig $config) { |
|
| 1539 | |||
| 1540 | } |
||
| 1541 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):