123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179 |
- <?php
- use bantu\IniGetWrapper\IniGetWrapper;
- use OC\Files\SetupManager;
- use OCP\Files\Template\ITemplateManager;
- use OCP\IConfig;
- use OCP\IGroupManager;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\Share\IManager;
- use Psr\Log\LoggerInterface;
- class OC_Util {
- public static $scripts = [];
- public static $styles = [];
- public static $headers = [];
-
- private static $versionCache = null;
- protected static function getAppManager() {
- return \OC::$server->getAppManager();
- }
-
- public static function setupFS(?string $user = '') {
-
- if ($user === '') {
- $userObject = \OC::$server->get(\OCP\IUserSession::class)->getUser();
- } else {
- $userObject = \OC::$server->get(\OCP\IUserManager::class)->get($user);
- }
-
- $setupManager = \OC::$server->get(SetupManager::class);
- if ($userObject) {
- $setupManager->setupForUser($userObject);
- } else {
- $setupManager->setupRoot();
- }
- return true;
- }
-
- public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) {
-
- $shareManager = \OC::$server->get(IManager::class);
- return $shareManager->shareApiLinkEnforcePassword($checkGroupMembership);
- }
-
- public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) {
-
- $shareManager = \OC::$server->get(IManager::class);
- $userId = $user ? $user->getUID() : null;
- return $shareManager->sharingDisabledForUser($userId);
- }
-
- public static function isDefaultExpireDateEnforced() {
-
- $shareManager = \OC::$server->get(IManager::class);
- return $shareManager->shareApiLinkDefaultExpireDateEnforced();
- }
-
- public static function getUserQuota(?IUser $user) {
- if (is_null($user)) {
- return \OCP\Files\FileInfo::SPACE_UNLIMITED;
- }
- $userQuota = $user->getQuota();
- if ($userQuota === 'none') {
- return \OCP\Files\FileInfo::SPACE_UNLIMITED;
- }
- return OC_Helper::computerFileSize($userQuota);
- }
-
- public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) {
-
- $logger = \OC::$server->get(LoggerInterface::class);
- $plainSkeletonDirectory = \OC::$server->getConfig()->getSystemValueString('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton');
- $userLang = \OC::$server->getL10NFactory()->findLanguage();
- $skeletonDirectory = str_replace('{lang}', $userLang, $plainSkeletonDirectory);
- if (!file_exists($skeletonDirectory)) {
- $dialectStart = strpos($userLang, '_');
- if ($dialectStart !== false) {
- $skeletonDirectory = str_replace('{lang}', substr($userLang, 0, $dialectStart), $plainSkeletonDirectory);
- }
- if ($dialectStart === false || !file_exists($skeletonDirectory)) {
- $skeletonDirectory = str_replace('{lang}', 'default', $plainSkeletonDirectory);
- }
- if (!file_exists($skeletonDirectory)) {
- $skeletonDirectory = '';
- }
- }
- $instanceId = \OC::$server->getConfig()->getSystemValue('instanceid', '');
- if ($instanceId === null) {
- throw new \RuntimeException('no instance id!');
- }
- $appdata = 'appdata_' . $instanceId;
- if ($userId === $appdata) {
- throw new \RuntimeException('username is reserved name: ' . $appdata);
- }
- if (!empty($skeletonDirectory)) {
- $logger->debug('copying skeleton for '.$userId.' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'), ['app' => 'files_skeleton']);
- self::copyr($skeletonDirectory, $userDirectory);
-
- $userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
-
- $templateManager = \OC::$server->get(ITemplateManager::class);
- $templateManager->initializeTemplateDirectory(null, $userId);
- }
- }
-
- public static function copyr($source, \OCP\Files\Folder $target) {
- $logger = \OC::$server->getLogger();
-
- $dir = opendir($source);
- if ($dir === false) {
- $logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']);
- return;
- }
-
- while (false !== ($file = readdir($dir))) {
- if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
- if (is_dir($source . '/' . $file)) {
- $child = $target->newFolder($file);
- self::copyr($source . '/' . $file, $child);
- } else {
- $child = $target->newFile($file);
- $sourceStream = fopen($source . '/' . $file, 'r');
- if ($sourceStream === false) {
- $logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']);
- closedir($dir);
- return;
- }
- $child->putContent($sourceStream);
- }
- }
- }
- closedir($dir);
- }
-
- public static function tearDownFS() {
-
- $setupManager = \OC::$server->get(SetupManager::class);
- $setupManager->tearDown();
- }
-
- public static function getVersion() {
- OC_Util::loadVersion();
- return self::$versionCache['OC_Version'];
- }
-
- public static function getVersionString() {
- OC_Util::loadVersion();
- return self::$versionCache['OC_VersionString'];
- }
-
- public static function getEditionString() {
- return '';
- }
-
- public static function getChannel() {
- OC_Util::loadVersion();
- return \OC::$server->getConfig()->getSystemValueString('updater.release.channel', self::$versionCache['OC_Channel']);
- }
-
- public static function getBuild() {
- OC_Util::loadVersion();
- return self::$versionCache['OC_Build'];
- }
-
- private static function loadVersion() {
- if (self::$versionCache !== null) {
- return;
- }
- $timestamp = filemtime(OC::$SERVERROOT . '/version.php');
- require OC::$SERVERROOT . '/version.php';
-
- self::$versionCache['OC_Version_Timestamp'] = $timestamp;
-
- self::$versionCache['OC_Version'] = $OC_Version;
-
- self::$versionCache['OC_VersionString'] = $OC_VersionString;
-
- self::$versionCache['OC_Build'] = $OC_Build;
-
- self::$versionCache['OC_Channel'] = $OC_Channel;
- }
-
- private static function generatePath($application, $directory, $file) {
- if (is_null($file)) {
- $file = $application;
- $application = "";
- }
- if (!empty($application)) {
- return "$application/$directory/$file";
- } else {
- return "$directory/$file";
- }
- }
-
- public static function addScript($application, $file = null, $prepend = false) {
- $path = OC_Util::generatePath($application, 'js', $file);
-
- if ($application !== 'core' && $file !== null) {
- self::addTranslations($application);
- }
- self::addExternalResource($application, $prepend, $path, "script");
- }
-
- public static function addVendorScript($application, $file = null, $prepend = false) {
- $path = OC_Util::generatePath($application, 'vendor', $file);
- self::addExternalResource($application, $prepend, $path, "script");
- }
-
- public static function addTranslations($application, $languageCode = null, $prepend = false) {
- if (is_null($languageCode)) {
- $languageCode = \OC::$server->getL10NFactory()->findLanguage($application);
- }
- if (!empty($application)) {
- $path = "$application/l10n/$languageCode";
- } else {
- $path = "l10n/$languageCode";
- }
- self::addExternalResource($application, $prepend, $path, "script");
- }
-
- public static function addStyle($application, $file = null, $prepend = false) {
- $path = OC_Util::generatePath($application, 'css', $file);
- self::addExternalResource($application, $prepend, $path, "style");
- }
-
- public static function addVendorStyle($application, $file = null, $prepend = false) {
- $path = OC_Util::generatePath($application, 'vendor', $file);
- self::addExternalResource($application, $prepend, $path, "style");
- }
-
- private static function addExternalResource($application, $prepend, $path, $type = "script") {
- if ($type === "style") {
- if (!in_array($path, self::$styles)) {
- if ($prepend === true) {
- array_unshift(self::$styles, $path);
- } else {
- self::$styles[] = $path;
- }
- }
- } elseif ($type === "script") {
- if (!in_array($path, self::$scripts)) {
- if ($prepend === true) {
- array_unshift(self::$scripts, $path);
- } else {
- self::$scripts [] = $path;
- }
- }
- }
- }
-
- public static function addHeader($tag, $attributes, $text = null, $prepend = false) {
- $header = [
- 'tag' => $tag,
- 'attributes' => $attributes,
- 'text' => $text
- ];
- if ($prepend === true) {
- array_unshift(self::$headers, $header);
- } else {
- self::$headers[] = $header;
- }
- }
-
- public static function checkServer(\OC\SystemConfig $config) {
- $l = \OC::$server->getL10N('lib');
- $errors = [];
- $CONFIG_DATADIRECTORY = $config->getValue('datadirectory', OC::$SERVERROOT . '/data');
- if (!self::needUpgrade($config) && $config->getValue('installed', false)) {
-
- $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY);
- }
-
- if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) {
- return $errors;
- }
- $webServerRestart = false;
- $setup = new \OC\Setup(
- $config,
- \OC::$server->get(IniGetWrapper::class),
- \OC::$server->getL10N('lib'),
- \OC::$server->get(\OCP\Defaults::class),
- \OC::$server->get(LoggerInterface::class),
- \OC::$server->getSecureRandom(),
- \OC::$server->get(\OC\Installer::class)
- );
- $urlGenerator = \OC::$server->getURLGenerator();
- $availableDatabases = $setup->getSupportedDatabases();
- if (empty($availableDatabases)) {
- $errors[] = [
- 'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'),
- 'hint' => ''
- ];
- $webServerRestart = true;
- }
-
- if (!OC_Helper::isReadOnlyConfigEnabled()) {
- if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) {
- $errors[] = [
- 'error' => $l->t('Cannot write into "config" directory.'),
- 'hint' => $l->t('This can usually be fixed by giving the web server write access to the config directory. See %s',
- [ $urlGenerator->linkToDocs('admin-dir_permissions') ]) . '. '
- . $l->t('Or, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it. See %s',
- [ $urlGenerator->linkToDocs('admin-config') ])
- ];
- }
- }
-
- if ($config->getValue('appstoreenabled', true)) {
- if (OC_App::getInstallPath() === null
- || !is_writable(OC_App::getInstallPath())
- || !is_readable(OC_App::getInstallPath())
- ) {
- $errors[] = [
- 'error' => $l->t('Cannot write into "apps" directory.'),
- 'hint' => $l->t('This can usually be fixed by giving the web server write access to the apps directory'
- . ' or disabling the App Store in the config file.')
- ];
- }
- }
-
- if ($config->getValue('installed', false)) {
- if (!is_dir($CONFIG_DATADIRECTORY)) {
- $success = @mkdir($CONFIG_DATADIRECTORY);
- if ($success) {
- $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
- } else {
- $errors[] = [
- 'error' => $l->t('Cannot create "data" directory.'),
- 'hint' => $l->t('This can usually be fixed by giving the web server write access to the root directory. See %s',
- [$urlGenerator->linkToDocs('admin-dir_permissions')])
- ];
- }
- } elseif (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) {
-
- $testFile = sprintf('%s/%s.tmp', $CONFIG_DATADIRECTORY, uniqid('data_dir_writability_test_'));
- $handle = fopen($testFile, 'w');
- if (!$handle || fwrite($handle, 'Test write operation') === false) {
- $permissionsHint = $l->t('Permissions can usually be fixed by giving the web server write access to the root directory. See %s.',
- [$urlGenerator->linkToDocs('admin-dir_permissions')]);
- $errors[] = [
- 'error' => $l->t('Your data directory is not writable.'),
- 'hint' => $permissionsHint
- ];
- } else {
- fclose($handle);
- unlink($testFile);
- }
- } else {
- $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
- }
- }
- if (!OC_Util::isSetLocaleWorking()) {
- $errors[] = [
- 'error' => $l->t('Setting locale to %s failed.',
- ['en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/'
- . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8']),
- 'hint' => $l->t('Please install one of these locales on your system and restart your web server.')
- ];
- }
-
-
-
-
-
-
-
-
- $dependencies = [
- 'classes' => [
- 'ZipArchive' => 'zip',
- 'DOMDocument' => 'dom',
- 'XMLWriter' => 'XMLWriter',
- 'XMLReader' => 'XMLReader',
- ],
- 'functions' => [
- 'xml_parser_create' => 'libxml',
- 'mb_strcut' => 'mbstring',
- 'ctype_digit' => 'ctype',
- 'json_encode' => 'JSON',
- 'gd_info' => 'GD',
- 'gzencode' => 'zlib',
- 'simplexml_load_string' => 'SimpleXML',
- 'hash' => 'HASH Message Digest Framework',
- 'curl_init' => 'cURL',
- 'openssl_verify' => 'OpenSSL',
- ],
- 'defined' => [
- 'PDO::ATTR_DRIVER_NAME' => 'PDO'
- ],
- 'ini' => [
- 'default_charset' => 'UTF-8',
- ],
- ];
- $missingDependencies = [];
- $invalidIniSettings = [];
- $iniWrapper = \OC::$server->get(IniGetWrapper::class);
- foreach ($dependencies['classes'] as $class => $module) {
- if (!class_exists($class)) {
- $missingDependencies[] = $module;
- }
- }
- foreach ($dependencies['functions'] as $function => $module) {
- if (!function_exists($function)) {
- $missingDependencies[] = $module;
- }
- }
- foreach ($dependencies['defined'] as $defined => $module) {
- if (!defined($defined)) {
- $missingDependencies[] = $module;
- }
- }
- foreach ($dependencies['ini'] as $setting => $expected) {
- if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) {
- $invalidIniSettings[] = [$setting, $expected];
- }
- }
- foreach ($missingDependencies as $missingDependency) {
- $errors[] = [
- 'error' => $l->t('PHP module %s not installed.', [$missingDependency]),
- 'hint' => $l->t('Please ask your server administrator to install the module.'),
- ];
- $webServerRestart = true;
- }
- foreach ($invalidIniSettings as $setting) {
- $errors[] = [
- 'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]),
- 'hint' => $l->t('Adjusting this setting in php.ini will make Nextcloud run again')
- ];
- $webServerRestart = true;
- }
-
- if ($iniWrapper->getBool('mbstring.func_overload') !== null &&
- $iniWrapper->getBool('mbstring.func_overload') === true) {
- $errors[] = [
- 'error' => $l->t('<code>mbstring.func_overload</code> is set to <code>%s</code> instead of the expected value <code>0</code>.', [$iniWrapper->getString('mbstring.func_overload')]),
- 'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini.')
- ];
- }
- if (!self::isAnnotationsWorking()) {
- $errors[] = [
- 'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'),
- 'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.')
- ];
- }
- if (!\OC::$CLI && $webServerRestart) {
- $errors[] = [
- 'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'),
- 'hint' => $l->t('Please ask your server administrator to restart the web server.')
- ];
- }
- foreach (['secret', 'instanceid', 'passwordsalt'] as $requiredConfig) {
- if ($config->getValue($requiredConfig, '') === '' && !\OC::$CLI && $config->getValue('installed', false)) {
- $errors[] = [
- 'error' => $l->t('The required %s config variable is not configured in the config.php file.', [$requiredConfig]),
- 'hint' => $l->t('Please ask your server administrator to check the Nextcloud configuration.')
- ];
- }
- }
-
- \OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0);
- return $errors;
- }
-
- public static function checkDataDirectoryPermissions($dataDirectory) {
- if (!\OC::$server->getConfig()->getSystemValueBool('check_data_directory_permissions', true)) {
- return [];
- }
- $perms = substr(decoct(@fileperms($dataDirectory)), -3);
- if (substr($perms, -1) !== '0') {
- chmod($dataDirectory, 0770);
- clearstatcache();
- $perms = substr(decoct(@fileperms($dataDirectory)), -3);
- if ($perms[2] !== '0') {
- $l = \OC::$server->getL10N('lib');
- return [[
- 'error' => $l->t('Your data directory is readable by other users.'),
- 'hint' => $l->t('Please change the permissions to 0770 so that the directory cannot be listed by other users.'),
- ]];
- }
- }
- return [];
- }
-
- public static function checkDataDirectoryValidity($dataDirectory) {
- $l = \OC::$server->getL10N('lib');
- $errors = [];
- if ($dataDirectory[0] !== '/') {
- $errors[] = [
- 'error' => $l->t('Your data directory must be an absolute path.'),
- 'hint' => $l->t('Check the value of "datadirectory" in your configuration.')
- ];
- }
- if (!file_exists($dataDirectory . '/.ocdata')) {
- $errors[] = [
- 'error' => $l->t('Your data directory is invalid.'),
- 'hint' => $l->t('Ensure there is a file called ".ocdata"' .
- ' in the root of the data directory.')
- ];
- }
- return $errors;
- }
-
- public static function checkLoggedIn() {
-
- if (!\OC::$server->getUserSession()->isLoggedIn()) {
- header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute(
- 'core.login.showLoginForm',
- [
- 'redirect_url' => \OC::$server->getRequest()->getRequestUri(),
- ]
- )
- );
- exit();
- }
-
- if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
- header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
- exit();
- }
- }
-
- public static function checkAdminUser() {
- OC_Util::checkLoggedIn();
- if (!OC_User::isAdminUser(OC_User::getUser())) {
- header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php'));
- exit();
- }
- }
-
- public static function getDefaultPageUrl() {
-
- $urlGenerator = \OC::$server->get(IURLGenerator::class);
- return $urlGenerator->linkToDefaultPageUrl();
- }
-
- public static function redirectToDefaultPage() {
- $location = self::getDefaultPageUrl();
- header('Location: ' . $location);
- exit();
- }
-
- public static function getInstanceId() {
- $id = \OC::$server->getSystemConfig()->getValue('instanceid', null);
- if (is_null($id)) {
-
- $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
- \OC::$server->getSystemConfig()->setValue('instanceid', $id);
- }
- return $id;
- }
-
- public static function sanitizeHTML($value) {
- if (is_array($value)) {
-
- $value = array_map(function ($value) {
- return self::sanitizeHTML($value);
- }, $value);
- } else {
-
- $value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
- }
- return $value;
- }
-
- public static function encodePath($component) {
- $encoded = rawurlencode($component);
- $encoded = str_replace('%2F', '/', $encoded);
- return $encoded;
- }
- public function createHtaccessTestFile(\OCP\IConfig $config) {
-
- if (php_sapi_name() === 'cli-server') {
- return false;
- }
-
- $fileName = '/htaccesstest.txt';
- $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.';
-
- $testFile = $config->getSystemValueString('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName;
- if (file_exists($testFile)) {
- return false;
- }
- $fp = @fopen($testFile, 'w');
- if (!$fp) {
- throw new \OCP\HintException('Can\'t create test file to check for working .htaccess file.',
- 'Make sure it is possible for the web server to write to ' . $testFile);
- }
- fwrite($fp, $testContent);
- fclose($fp);
- return $testContent;
- }
-
- public function isHtaccessWorking(\OCP\IConfig $config) {
- if (\OC::$CLI || !$config->getSystemValueBool('check_for_working_htaccess', true)) {
- return true;
- }
- $testContent = $this->createHtaccessTestFile($config);
- if ($testContent === false) {
- return false;
- }
- $fileName = '/htaccesstest.txt';
- $testFile = $config->getSystemValueString('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName;
-
- $url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName);
- try {
- $content = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody();
- } catch (\Exception $e) {
- $content = false;
- }
- if (str_starts_with($url, 'https:')) {
- $url = 'http:' . substr($url, 6);
- } else {
- $url = 'https:' . substr($url, 5);
- }
- try {
- $fallbackContent = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody();
- } catch (\Exception $e) {
- $fallbackContent = false;
- }
-
- @unlink($testFile);
-
- return $content !== $testContent && $fallbackContent !== $testContent;
- }
-
- private static function isNonUTF8Locale() {
- if (function_exists('escapeshellcmd')) {
- return '' === escapeshellcmd('§');
- } elseif (function_exists('escapeshellarg')) {
- return '\'\'' === escapeshellarg('§');
- } else {
- return 0 === preg_match('/utf-?8/i', setlocale(LC_CTYPE, 0));
- }
- }
-
- public static function isSetLocaleWorking() {
- if (self::isNonUTF8Locale()) {
-
- setlocale(LC_ALL, 'C.UTF-8', 'C');
- setlocale(LC_CTYPE, 'en_US.UTF-8', 'fr_FR.UTF-8', 'es_ES.UTF-8', 'de_DE.UTF-8', 'ru_RU.UTF-8', 'pt_BR.UTF-8', 'it_IT.UTF-8', 'ja_JP.UTF-8', 'zh_CN.UTF-8', '0');
-
- if (self::isNonUTF8Locale()) {
- return false;
- }
- }
- return true;
- }
-
- public static function isAnnotationsWorking() {
- $reflection = new \ReflectionMethod(__METHOD__);
- $docs = $reflection->getDocComment();
- return (is_string($docs) && strlen($docs) > 50);
- }
-
- public static function fileInfoLoaded() {
- return function_exists('finfo_open');
- }
-
- public static function obEnd() {
- while (ob_get_level()) {
- ob_end_clean();
- }
- }
-
- public static function runningOnMac() {
- return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN');
- }
-
- public static function getTheme() {
- $theme = \OC::$server->getSystemConfig()->getValue("theme", '');
- if ($theme === '') {
- if (is_dir(OC::$SERVERROOT . '/themes/default')) {
- $theme = 'default';
- }
- }
- return $theme;
- }
-
- public static function normalizeUnicode($value) {
- if (Normalizer::isNormalized($value)) {
- return $value;
- }
- $normalizedValue = Normalizer::normalize($value);
- if ($normalizedValue === null || $normalizedValue === false) {
- \OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']);
- return $value;
- }
- return $normalizedValue;
- }
-
- public static function getHumanVersion() {
- $version = OC_Util::getVersionString();
- $build = OC_Util::getBuild();
- if (!empty($build) and OC_Util::getChannel() === 'daily') {
- $version .= ' Build:' . $build;
- }
- return $version;
- }
-
- public static function isValidFileName($file) {
- $trimmed = trim($file);
- if ($trimmed === '') {
- return false;
- }
- if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) {
- return false;
- }
-
- if (preg_match('/' . \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX . '/', $trimmed) !== 0) {
- return false;
- }
- foreach (str_split($trimmed) as $char) {
- if (str_contains(\OCP\Constants::FILENAME_INVALID_CHARS, $char)) {
- return false;
- }
- }
- return true;
- }
-
- public static function needUpgrade(\OC\SystemConfig $config) {
- if ($config->getValue('installed', false)) {
- $installedVersion = $config->getValue('version', '0.0.0');
- $currentVersion = implode('.', \OCP\Util::getVersion());
- $versionDiff = version_compare($currentVersion, $installedVersion);
- if ($versionDiff > 0) {
- return true;
- } elseif ($config->getValue('debug', false) && $versionDiff < 0) {
-
- $installedMajor = explode('.', $installedVersion);
- $installedMajor = $installedMajor[0] . '.' . $installedMajor[1];
- $currentMajor = explode('.', $currentVersion);
- $currentMajor = $currentMajor[0] . '.' . $currentMajor[1];
- if ($installedMajor === $currentMajor) {
-
- return true;
- } else {
-
- throw new \OCP\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')');
- }
- } elseif ($versionDiff < 0) {
-
- throw new \OCP\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')');
- }
-
- $apps = \OC_App::getEnabledApps(false, true);
- $shouldUpgrade = false;
- foreach ($apps as $app) {
- if (\OC_App::shouldUpgrade($app)) {
- $shouldUpgrade = true;
- break;
- }
- }
- return $shouldUpgrade;
- } else {
- return false;
- }
- }
- }
|