@@ -34,118 +34,118 @@ |
||
| 34 | 34 | |
| 35 | 35 | class CSSResourceLocator extends ResourceLocator { |
| 36 | 36 | |
| 37 | - /** @var SCSSCacher */ |
|
| 38 | - protected $scssCacher; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param ILogger $logger |
|
| 42 | - * @param string $theme |
|
| 43 | - * @param array $core_map |
|
| 44 | - * @param array $party_map |
|
| 45 | - * @param SCSSCacher $scssCacher |
|
| 46 | - */ |
|
| 47 | - public function __construct(ILogger $logger, $theme, $core_map, $party_map, $scssCacher) { |
|
| 48 | - $this->scssCacher = $scssCacher; |
|
| 49 | - |
|
| 50 | - parent::__construct($logger, $theme, $core_map, $party_map); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param string $style |
|
| 55 | - */ |
|
| 56 | - public function doFind($style) { |
|
| 57 | - $app = substr($style, 0, strpos($style, '/')); |
|
| 58 | - if (strpos($style, '3rdparty') === 0 |
|
| 59 | - && $this->appendIfExist($this->thirdpartyroot, $style.'.css') |
|
| 60 | - || $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss', $app) |
|
| 61 | - || $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss') |
|
| 62 | - || $this->appendIfExist($this->serverroot, $style.'.css') |
|
| 63 | - || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') |
|
| 64 | - ) { |
|
| 65 | - return; |
|
| 66 | - } |
|
| 67 | - $style = substr($style, strpos($style, '/')+1); |
|
| 68 | - $app_path = \OC_App::getAppPath($app); |
|
| 69 | - $app_url = \OC_App::getAppWebPath($app); |
|
| 70 | - |
|
| 71 | - if ($app_path === false && $app_url === false) { |
|
| 72 | - $this->logger->error('Could not find resource {resource} to load', [ |
|
| 73 | - 'resource' => $app . '/' . $style . '.css', |
|
| 74 | - 'app' => 'cssresourceloader', |
|
| 75 | - ]); |
|
| 76 | - return; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - // Account for the possibility of having symlinks in app path. Doing |
|
| 80 | - // this here instead of above as an empty argument to realpath gets |
|
| 81 | - // turned into cwd. |
|
| 82 | - $app_path = realpath($app_path); |
|
| 83 | - |
|
| 84 | - if(!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) { |
|
| 85 | - $this->append($app_path, $style.'.css', $app_url); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param string $style |
|
| 91 | - */ |
|
| 92 | - public function doFindTheme($style) { |
|
| 93 | - $theme_dir = 'themes/'.$this->theme.'/'; |
|
| 94 | - $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') |
|
| 95 | - || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') |
|
| 96 | - || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * cache and append the scss $file if exist at $root |
|
| 101 | - * |
|
| 102 | - * @param string $root path to check |
|
| 103 | - * @param string $file the filename |
|
| 104 | - * @return bool True if the resource was found and cached, false otherwise |
|
| 105 | - */ |
|
| 106 | - protected function cacheAndAppendScssIfExist($root, $file, $app = 'core') { |
|
| 107 | - if (is_file($root.'/'.$file)) { |
|
| 108 | - if($this->scssCacher !== null) { |
|
| 109 | - if($this->scssCacher->process($root, $file, $app)) { |
|
| 110 | - |
|
| 111 | - $this->append($root, $this->scssCacher->getCachedSCSS($app, $file), \OC::$WEBROOT, true, true); |
|
| 112 | - return true; |
|
| 113 | - } else { |
|
| 114 | - $this->logger->warning('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']); |
|
| 115 | - return false; |
|
| 116 | - } |
|
| 117 | - } else { |
|
| 118 | - $this->logger->debug('Scss is disabled for '.$root.'/'.$file.', ignoring', ['app' => 'core']); |
|
| 119 | - return true; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function append($root, $file, $webRoot = null, $throw = true, $scss = false) { |
|
| 126 | - if (!$scss) { |
|
| 127 | - parent::append($root, $file, $webRoot, $throw); |
|
| 128 | - } else { |
|
| 129 | - if (!$webRoot) { |
|
| 130 | - $webRoot = $this->findWebRoot($root); |
|
| 131 | - |
|
| 132 | - if ($webRoot === null) { |
|
| 133 | - $webRoot = ''; |
|
| 134 | - $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
| 135 | - 'app' => 'lib', |
|
| 136 | - 'root' => $root, |
|
| 137 | - 'file' => $file, |
|
| 138 | - 'webRoot' => $webRoot, |
|
| 139 | - 'throw' => $throw ? 'true' : 'false' |
|
| 140 | - ]); |
|
| 141 | - |
|
| 142 | - if ($throw && $root === '/') { |
|
| 143 | - throw new ResourceNotFoundException($file, $webRoot); |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $this->resources[] = array($webRoot? : \OC::$WEBROOT, $webRoot, $file); |
|
| 149 | - } |
|
| 150 | - } |
|
| 37 | + /** @var SCSSCacher */ |
|
| 38 | + protected $scssCacher; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param ILogger $logger |
|
| 42 | + * @param string $theme |
|
| 43 | + * @param array $core_map |
|
| 44 | + * @param array $party_map |
|
| 45 | + * @param SCSSCacher $scssCacher |
|
| 46 | + */ |
|
| 47 | + public function __construct(ILogger $logger, $theme, $core_map, $party_map, $scssCacher) { |
|
| 48 | + $this->scssCacher = $scssCacher; |
|
| 49 | + |
|
| 50 | + parent::__construct($logger, $theme, $core_map, $party_map); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param string $style |
|
| 55 | + */ |
|
| 56 | + public function doFind($style) { |
|
| 57 | + $app = substr($style, 0, strpos($style, '/')); |
|
| 58 | + if (strpos($style, '3rdparty') === 0 |
|
| 59 | + && $this->appendIfExist($this->thirdpartyroot, $style.'.css') |
|
| 60 | + || $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss', $app) |
|
| 61 | + || $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss') |
|
| 62 | + || $this->appendIfExist($this->serverroot, $style.'.css') |
|
| 63 | + || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') |
|
| 64 | + ) { |
|
| 65 | + return; |
|
| 66 | + } |
|
| 67 | + $style = substr($style, strpos($style, '/')+1); |
|
| 68 | + $app_path = \OC_App::getAppPath($app); |
|
| 69 | + $app_url = \OC_App::getAppWebPath($app); |
|
| 70 | + |
|
| 71 | + if ($app_path === false && $app_url === false) { |
|
| 72 | + $this->logger->error('Could not find resource {resource} to load', [ |
|
| 73 | + 'resource' => $app . '/' . $style . '.css', |
|
| 74 | + 'app' => 'cssresourceloader', |
|
| 75 | + ]); |
|
| 76 | + return; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + // Account for the possibility of having symlinks in app path. Doing |
|
| 80 | + // this here instead of above as an empty argument to realpath gets |
|
| 81 | + // turned into cwd. |
|
| 82 | + $app_path = realpath($app_path); |
|
| 83 | + |
|
| 84 | + if(!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) { |
|
| 85 | + $this->append($app_path, $style.'.css', $app_url); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param string $style |
|
| 91 | + */ |
|
| 92 | + public function doFindTheme($style) { |
|
| 93 | + $theme_dir = 'themes/'.$this->theme.'/'; |
|
| 94 | + $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') |
|
| 95 | + || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') |
|
| 96 | + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * cache and append the scss $file if exist at $root |
|
| 101 | + * |
|
| 102 | + * @param string $root path to check |
|
| 103 | + * @param string $file the filename |
|
| 104 | + * @return bool True if the resource was found and cached, false otherwise |
|
| 105 | + */ |
|
| 106 | + protected function cacheAndAppendScssIfExist($root, $file, $app = 'core') { |
|
| 107 | + if (is_file($root.'/'.$file)) { |
|
| 108 | + if($this->scssCacher !== null) { |
|
| 109 | + if($this->scssCacher->process($root, $file, $app)) { |
|
| 110 | + |
|
| 111 | + $this->append($root, $this->scssCacher->getCachedSCSS($app, $file), \OC::$WEBROOT, true, true); |
|
| 112 | + return true; |
|
| 113 | + } else { |
|
| 114 | + $this->logger->warning('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']); |
|
| 115 | + return false; |
|
| 116 | + } |
|
| 117 | + } else { |
|
| 118 | + $this->logger->debug('Scss is disabled for '.$root.'/'.$file.', ignoring', ['app' => 'core']); |
|
| 119 | + return true; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function append($root, $file, $webRoot = null, $throw = true, $scss = false) { |
|
| 126 | + if (!$scss) { |
|
| 127 | + parent::append($root, $file, $webRoot, $throw); |
|
| 128 | + } else { |
|
| 129 | + if (!$webRoot) { |
|
| 130 | + $webRoot = $this->findWebRoot($root); |
|
| 131 | + |
|
| 132 | + if ($webRoot === null) { |
|
| 133 | + $webRoot = ''; |
|
| 134 | + $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
| 135 | + 'app' => 'lib', |
|
| 136 | + 'root' => $root, |
|
| 137 | + 'file' => $file, |
|
| 138 | + 'webRoot' => $webRoot, |
|
| 139 | + 'throw' => $throw ? 'true' : 'false' |
|
| 140 | + ]); |
|
| 141 | + |
|
| 142 | + if ($throw && $root === '/') { |
|
| 143 | + throw new ResourceNotFoundException($file, $webRoot); |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $this->resources[] = array($webRoot? : \OC::$WEBROOT, $webRoot, $file); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -64,13 +64,13 @@ discard block |
||
| 64 | 64 | ) { |
| 65 | 65 | return; |
| 66 | 66 | } |
| 67 | - $style = substr($style, strpos($style, '/')+1); |
|
| 67 | + $style = substr($style, strpos($style, '/') + 1); |
|
| 68 | 68 | $app_path = \OC_App::getAppPath($app); |
| 69 | 69 | $app_url = \OC_App::getAppWebPath($app); |
| 70 | 70 | |
| 71 | 71 | if ($app_path === false && $app_url === false) { |
| 72 | 72 | $this->logger->error('Could not find resource {resource} to load', [ |
| 73 | - 'resource' => $app . '/' . $style . '.css', |
|
| 73 | + 'resource' => $app.'/'.$style.'.css', |
|
| 74 | 74 | 'app' => 'cssresourceloader', |
| 75 | 75 | ]); |
| 76 | 76 | return; |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | // turned into cwd. |
| 82 | 82 | $app_path = realpath($app_path); |
| 83 | 83 | |
| 84 | - if(!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) { |
|
| 84 | + if (!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) { |
|
| 85 | 85 | $this->append($app_path, $style.'.css', $app_url); |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -105,8 +105,8 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | protected function cacheAndAppendScssIfExist($root, $file, $app = 'core') { |
| 107 | 107 | if (is_file($root.'/'.$file)) { |
| 108 | - if($this->scssCacher !== null) { |
|
| 109 | - if($this->scssCacher->process($root, $file, $app)) { |
|
| 108 | + if ($this->scssCacher !== null) { |
|
| 109 | + if ($this->scssCacher->process($root, $file, $app)) { |
|
| 110 | 110 | |
| 111 | 111 | $this->append($root, $this->scssCacher->getCachedSCSS($app, $file), \OC::$WEBROOT, true, true); |
| 112 | 112 | return true; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | } |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - $this->resources[] = array($webRoot? : \OC::$WEBROOT, $webRoot, $file); |
|
| 148 | + $this->resources[] = array($webRoot ?: \OC::$WEBROOT, $webRoot, $file); |
|
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | } |
@@ -63,1184 +63,1184 @@ |
||
| 63 | 63 | * upgrading and removing apps. |
| 64 | 64 | */ |
| 65 | 65 | class OC_App { |
| 66 | - static private $appVersion = []; |
|
| 67 | - static private $adminForms = array(); |
|
| 68 | - static private $personalForms = array(); |
|
| 69 | - static private $appInfo = array(); |
|
| 70 | - static private $appTypes = array(); |
|
| 71 | - static private $loadedApps = array(); |
|
| 72 | - static private $altLogin = array(); |
|
| 73 | - static private $alreadyRegistered = []; |
|
| 74 | - const officialApp = 200; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * clean the appId |
|
| 78 | - * |
|
| 79 | - * @param string|boolean $app AppId that needs to be cleaned |
|
| 80 | - * @return string |
|
| 81 | - */ |
|
| 82 | - public static function cleanAppId($app) { |
|
| 83 | - return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Check if an app is loaded |
|
| 88 | - * |
|
| 89 | - * @param string $app |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public static function isAppLoaded($app) { |
|
| 93 | - return in_array($app, self::$loadedApps, true); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * loads all apps |
|
| 98 | - * |
|
| 99 | - * @param string[] | string | null $types |
|
| 100 | - * @return bool |
|
| 101 | - * |
|
| 102 | - * This function walks through the ownCloud directory and loads all apps |
|
| 103 | - * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 104 | - * exists. |
|
| 105 | - * |
|
| 106 | - * if $types is set, only apps of those types will be loaded |
|
| 107 | - */ |
|
| 108 | - public static function loadApps($types = null) { |
|
| 109 | - if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - // Load the enabled apps here |
|
| 113 | - $apps = self::getEnabledApps(); |
|
| 114 | - |
|
| 115 | - // Add each apps' folder as allowed class path |
|
| 116 | - foreach($apps as $app) { |
|
| 117 | - $path = self::getAppPath($app); |
|
| 118 | - if($path !== false) { |
|
| 119 | - self::registerAutoloading($app, $path); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // prevent app.php from printing output |
|
| 124 | - ob_start(); |
|
| 125 | - foreach ($apps as $app) { |
|
| 126 | - if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 127 | - self::loadApp($app); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - ob_end_clean(); |
|
| 131 | - |
|
| 132 | - return true; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * load a single app |
|
| 137 | - * |
|
| 138 | - * @param string $app |
|
| 139 | - */ |
|
| 140 | - public static function loadApp($app) { |
|
| 141 | - self::$loadedApps[] = $app; |
|
| 142 | - $appPath = self::getAppPath($app); |
|
| 143 | - if($appPath === false) { |
|
| 144 | - return; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // in case someone calls loadApp() directly |
|
| 148 | - self::registerAutoloading($app, $appPath); |
|
| 149 | - |
|
| 150 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 151 | - \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 152 | - self::requireAppFile($app); |
|
| 153 | - if (self::isType($app, array('authentication'))) { |
|
| 154 | - // since authentication apps affect the "is app enabled for group" check, |
|
| 155 | - // the enabled apps cache needs to be cleared to make sure that the |
|
| 156 | - // next time getEnableApps() is called it will also include apps that were |
|
| 157 | - // enabled for groups |
|
| 158 | - self::$enabledAppsCache = array(); |
|
| 159 | - } |
|
| 160 | - \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $info = self::getAppInfo($app); |
|
| 164 | - if (!empty($info['activity']['filters'])) { |
|
| 165 | - foreach ($info['activity']['filters'] as $filter) { |
|
| 166 | - \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - if (!empty($info['activity']['settings'])) { |
|
| 170 | - foreach ($info['activity']['settings'] as $setting) { |
|
| 171 | - \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - if (!empty($info['activity']['providers'])) { |
|
| 175 | - foreach ($info['activity']['providers'] as $provider) { |
|
| 176 | - \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - if (!empty($info['collaboration']['plugins'])) { |
|
| 180 | - // deal with one or many plugin entries |
|
| 181 | - $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ? |
|
| 182 | - [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin']; |
|
| 183 | - foreach ($plugins as $plugin) { |
|
| 184 | - if($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 185 | - $pluginInfo = [ |
|
| 186 | - 'shareType' => $plugin['@attributes']['share-type'], |
|
| 187 | - 'class' => $plugin['@value'], |
|
| 188 | - ]; |
|
| 189 | - \OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo); |
|
| 190 | - } else if ($plugin['@attributes']['type'] === 'autocomplete-sort') { |
|
| 191 | - \OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @internal |
|
| 199 | - * @param string $app |
|
| 200 | - * @param string $path |
|
| 201 | - */ |
|
| 202 | - public static function registerAutoloading($app, $path) { |
|
| 203 | - $key = $app . '-' . $path; |
|
| 204 | - if(isset(self::$alreadyRegistered[$key])) { |
|
| 205 | - return; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - self::$alreadyRegistered[$key] = true; |
|
| 209 | - |
|
| 210 | - // Register on PSR-4 composer autoloader |
|
| 211 | - $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 212 | - \OC::$server->registerNamespace($app, $appNamespace); |
|
| 213 | - |
|
| 214 | - if (file_exists($path . '/composer/autoload.php')) { |
|
| 215 | - require_once $path . '/composer/autoload.php'; |
|
| 216 | - } else { |
|
| 217 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 218 | - // Register on legacy autoloader |
|
| 219 | - \OC::$loader->addValidRoot($path); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - // Register Test namespace only when testing |
|
| 223 | - if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 224 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Load app.php from the given app |
|
| 230 | - * |
|
| 231 | - * @param string $app app name |
|
| 232 | - */ |
|
| 233 | - private static function requireAppFile($app) { |
|
| 234 | - try { |
|
| 235 | - // encapsulated here to avoid variable scope conflicts |
|
| 236 | - require_once $app . '/appinfo/app.php'; |
|
| 237 | - } catch (Error $ex) { |
|
| 238 | - \OC::$server->getLogger()->logException($ex); |
|
| 239 | - $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 240 | - if (!in_array($app, $blacklist)) { |
|
| 241 | - self::disable($app); |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * check if an app is of a specific type |
|
| 248 | - * |
|
| 249 | - * @param string $app |
|
| 250 | - * @param string|array $types |
|
| 251 | - * @return bool |
|
| 252 | - */ |
|
| 253 | - public static function isType($app, $types) { |
|
| 254 | - if (is_string($types)) { |
|
| 255 | - $types = array($types); |
|
| 256 | - } |
|
| 257 | - $appTypes = self::getAppTypes($app); |
|
| 258 | - foreach ($types as $type) { |
|
| 259 | - if (array_search($type, $appTypes) !== false) { |
|
| 260 | - return true; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - return false; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * get the types of an app |
|
| 268 | - * |
|
| 269 | - * @param string $app |
|
| 270 | - * @return array |
|
| 271 | - */ |
|
| 272 | - private static function getAppTypes($app) { |
|
| 273 | - //load the cache |
|
| 274 | - if (count(self::$appTypes) == 0) { |
|
| 275 | - self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - if (isset(self::$appTypes[$app])) { |
|
| 279 | - return explode(',', self::$appTypes[$app]); |
|
| 280 | - } else { |
|
| 281 | - return array(); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * read app types from info.xml and cache them in the database |
|
| 287 | - */ |
|
| 288 | - public static function setAppTypes($app) { |
|
| 289 | - $appData = self::getAppInfo($app); |
|
| 290 | - if(!is_array($appData)) { |
|
| 291 | - return; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - if (isset($appData['types'])) { |
|
| 295 | - $appTypes = implode(',', $appData['types']); |
|
| 296 | - } else { |
|
| 297 | - $appTypes = ''; |
|
| 298 | - $appData['types'] = []; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes); |
|
| 302 | - |
|
| 303 | - if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) { |
|
| 304 | - $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes'); |
|
| 305 | - if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 306 | - \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes'); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * get all enabled apps |
|
| 313 | - */ |
|
| 314 | - protected static $enabledAppsCache = array(); |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Returns apps enabled for the current user. |
|
| 318 | - * |
|
| 319 | - * @param bool $forceRefresh whether to refresh the cache |
|
| 320 | - * @param bool $all whether to return apps for all users, not only the |
|
| 321 | - * currently logged in one |
|
| 322 | - * @return string[] |
|
| 323 | - */ |
|
| 324 | - public static function getEnabledApps($forceRefresh = false, $all = false) { |
|
| 325 | - if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 326 | - return array(); |
|
| 327 | - } |
|
| 328 | - // in incognito mode or when logged out, $user will be false, |
|
| 329 | - // which is also the case during an upgrade |
|
| 330 | - $appManager = \OC::$server->getAppManager(); |
|
| 331 | - if ($all) { |
|
| 332 | - $user = null; |
|
| 333 | - } else { |
|
| 334 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - if (is_null($user)) { |
|
| 338 | - $apps = $appManager->getInstalledApps(); |
|
| 339 | - } else { |
|
| 340 | - $apps = $appManager->getEnabledAppsForUser($user); |
|
| 341 | - } |
|
| 342 | - $apps = array_filter($apps, function ($app) { |
|
| 343 | - return $app !== 'files';//we add this manually |
|
| 344 | - }); |
|
| 345 | - sort($apps); |
|
| 346 | - array_unshift($apps, 'files'); |
|
| 347 | - return $apps; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * checks whether or not an app is enabled |
|
| 352 | - * |
|
| 353 | - * @param string $app app |
|
| 354 | - * @return bool |
|
| 355 | - * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId) |
|
| 356 | - * |
|
| 357 | - * This function checks whether or not an app is enabled. |
|
| 358 | - */ |
|
| 359 | - public static function isEnabled($app) { |
|
| 360 | - return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * enables an app |
|
| 365 | - * |
|
| 366 | - * @param string $appId |
|
| 367 | - * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 368 | - * @throws \Exception |
|
| 369 | - * @return void |
|
| 370 | - * |
|
| 371 | - * This function set an app as enabled in appconfig. |
|
| 372 | - */ |
|
| 373 | - public function enable($appId, |
|
| 374 | - $groups = null) { |
|
| 375 | - self::$enabledAppsCache = []; // flush |
|
| 376 | - |
|
| 377 | - // Check if app is already downloaded |
|
| 378 | - $installer = \OC::$server->query(Installer::class); |
|
| 379 | - $isDownloaded = $installer->isDownloaded($appId); |
|
| 380 | - |
|
| 381 | - if(!$isDownloaded) { |
|
| 382 | - $installer->downloadApp($appId); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - $installer->installApp($appId); |
|
| 386 | - |
|
| 387 | - $appManager = \OC::$server->getAppManager(); |
|
| 388 | - if (!is_null($groups)) { |
|
| 389 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 390 | - $groupsList = []; |
|
| 391 | - foreach ($groups as $group) { |
|
| 392 | - $groupItem = $groupManager->get($group); |
|
| 393 | - if ($groupItem instanceof \OCP\IGroup) { |
|
| 394 | - $groupsList[] = $groupManager->get($group); |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - $appManager->enableAppForGroups($appId, $groupsList); |
|
| 398 | - } else { |
|
| 399 | - $appManager->enableApp($appId); |
|
| 400 | - } |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * @param string $app |
|
| 405 | - * @return bool |
|
| 406 | - */ |
|
| 407 | - public static function removeApp($app) { |
|
| 408 | - if (\OC::$server->getAppManager()->isShipped($app)) { |
|
| 409 | - return false; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - $installer = \OC::$server->query(Installer::class); |
|
| 413 | - return $installer->removeApp($app); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * This function set an app as disabled in appconfig. |
|
| 418 | - * |
|
| 419 | - * @param string $app app |
|
| 420 | - * @throws Exception |
|
| 421 | - */ |
|
| 422 | - public static function disable($app) { |
|
| 423 | - // flush |
|
| 424 | - self::$enabledAppsCache = array(); |
|
| 425 | - |
|
| 426 | - // run uninstall steps |
|
| 427 | - $appData = OC_App::getAppInfo($app); |
|
| 428 | - if (!is_null($appData)) { |
|
| 429 | - OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - // emit disable hook - needed anymore ? |
|
| 433 | - \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 434 | - |
|
| 435 | - // finally disable it |
|
| 436 | - $appManager = \OC::$server->getAppManager(); |
|
| 437 | - $appManager->disableApp($app); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - // This is private as well. It simply works, so don't ask for more details |
|
| 441 | - private static function proceedNavigation($list) { |
|
| 442 | - usort($list, function($a, $b) { |
|
| 443 | - if (isset($a['order']) && isset($b['order'])) { |
|
| 444 | - return ($a['order'] < $b['order']) ? -1 : 1; |
|
| 445 | - } else if (isset($a['order']) || isset($b['order'])) { |
|
| 446 | - return isset($a['order']) ? -1 : 1; |
|
| 447 | - } else { |
|
| 448 | - return ($a['name'] < $b['name']) ? -1 : 1; |
|
| 449 | - } |
|
| 450 | - }); |
|
| 451 | - |
|
| 452 | - $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 453 | - foreach ($list as $index => &$navEntry) { |
|
| 454 | - if ($navEntry['id'] == $activeApp) { |
|
| 455 | - $navEntry['active'] = true; |
|
| 456 | - } else { |
|
| 457 | - $navEntry['active'] = false; |
|
| 458 | - } |
|
| 459 | - } |
|
| 460 | - unset($navEntry); |
|
| 461 | - |
|
| 462 | - return $list; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Get the path where to install apps |
|
| 467 | - * |
|
| 468 | - * @return string|false |
|
| 469 | - */ |
|
| 470 | - public static function getInstallPath() { |
|
| 471 | - if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 472 | - return false; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 476 | - if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 477 | - return $dir['path']; |
|
| 478 | - } |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 482 | - return null; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * search for an app in all app-directories |
|
| 488 | - * |
|
| 489 | - * @param string $appId |
|
| 490 | - * @return false|string |
|
| 491 | - */ |
|
| 492 | - public static function findAppInDirectories($appId) { |
|
| 493 | - $sanitizedAppId = self::cleanAppId($appId); |
|
| 494 | - if($sanitizedAppId !== $appId) { |
|
| 495 | - return false; |
|
| 496 | - } |
|
| 497 | - static $app_dir = array(); |
|
| 498 | - |
|
| 499 | - if (isset($app_dir[$appId])) { |
|
| 500 | - return $app_dir[$appId].'0'; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - $possibleApps = array(); |
|
| 504 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 505 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 506 | - $possibleApps[] = $dir; |
|
| 507 | - } |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - if (empty($possibleApps)) { |
|
| 511 | - return false; |
|
| 512 | - } elseif (count($possibleApps) === 1) { |
|
| 513 | - $dir = array_shift($possibleApps); |
|
| 514 | - $app_dir[$appId] = $dir; |
|
| 515 | - return $dir.'1'; |
|
| 516 | - } else { |
|
| 517 | - $versionToLoad = array(); |
|
| 518 | - foreach ($possibleApps as $possibleApp) { |
|
| 519 | - $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 520 | - if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 521 | - $versionToLoad = array( |
|
| 522 | - 'dir' => $possibleApp, |
|
| 523 | - 'version' => $version, |
|
| 524 | - ); |
|
| 525 | - } |
|
| 526 | - } |
|
| 527 | - $app_dir[$appId] = $versionToLoad['dir']; |
|
| 528 | - return $versionToLoad['dir'].'2'; |
|
| 529 | - //TODO - write test |
|
| 530 | - } |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * Get the directory for the given app. |
|
| 535 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 536 | - * |
|
| 537 | - * @param string $appId |
|
| 538 | - * @return string|false |
|
| 539 | - */ |
|
| 540 | - public static function getAppPath($appId) { |
|
| 541 | - if ($appId === null || trim($appId) === '') { |
|
| 542 | - return false; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 546 | - return $dir['path'] . '/' . $appId; |
|
| 547 | - } |
|
| 548 | - return false; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Get the path for the given app on the access |
|
| 553 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 554 | - * |
|
| 555 | - * @param string $appId |
|
| 556 | - * @return string|false |
|
| 557 | - */ |
|
| 558 | - public static function getAppWebPath($appId) { |
|
| 559 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 560 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 561 | - } |
|
| 562 | - return false; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * get the last version of the app from appinfo/info.xml |
|
| 567 | - * |
|
| 568 | - * @param string $appId |
|
| 569 | - * @param bool $useCache |
|
| 570 | - * @return string |
|
| 571 | - */ |
|
| 572 | - public static function getAppVersion($appId, $useCache = true) { |
|
| 573 | - if($useCache && isset(self::$appVersion[$appId])) { |
|
| 574 | - return self::$appVersion[$appId]; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - $file = self::getAppPath($appId); |
|
| 578 | - self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0'; |
|
| 579 | - return self::$appVersion[$appId]; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * get app's version based on it's path |
|
| 584 | - * |
|
| 585 | - * @param string $path |
|
| 586 | - * @return string |
|
| 587 | - */ |
|
| 588 | - public static function getAppVersionByPath($path) { |
|
| 589 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 590 | - $appData = self::getAppInfo($infoFile, true); |
|
| 591 | - return isset($appData['version']) ? $appData['version'] : ''; |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Read all app metadata from the info.xml file |
|
| 597 | - * |
|
| 598 | - * @param string $appId id of the app or the path of the info.xml file |
|
| 599 | - * @param bool $path |
|
| 600 | - * @param string $lang |
|
| 601 | - * @return array|null |
|
| 602 | - * @note all data is read from info.xml, not just pre-defined fields |
|
| 603 | - */ |
|
| 604 | - public static function getAppInfo($appId, $path = false, $lang = null) { |
|
| 605 | - if ($path) { |
|
| 606 | - $file = $appId; |
|
| 607 | - } else { |
|
| 608 | - if ($lang === null && isset(self::$appInfo[$appId])) { |
|
| 609 | - return self::$appInfo[$appId]; |
|
| 610 | - } |
|
| 611 | - $appPath = self::getAppPath($appId); |
|
| 612 | - if($appPath === false) { |
|
| 613 | - return null; |
|
| 614 | - } |
|
| 615 | - $file = $appPath . '/appinfo/info.xml'; |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - $parser = new InfoParser(\OC::$server->getMemCacheFactory()->createLocal('core.appinfo')); |
|
| 619 | - $data = $parser->parse($file); |
|
| 620 | - |
|
| 621 | - if (is_array($data)) { |
|
| 622 | - $data = OC_App::parseAppInfo($data, $lang); |
|
| 623 | - } |
|
| 624 | - if(isset($data['ocsid'])) { |
|
| 625 | - $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
|
| 626 | - if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 627 | - $data['ocsid'] = $storedId; |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - if ($lang === null) { |
|
| 632 | - self::$appInfo[$appId] = $data; |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - return $data; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Returns the navigation |
|
| 640 | - * |
|
| 641 | - * @return array |
|
| 642 | - * |
|
| 643 | - * This function returns an array containing all entries added. The |
|
| 644 | - * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 645 | - * given for each app the following keys exist: |
|
| 646 | - * - active: boolean, signals if the user is on this navigation entry |
|
| 647 | - */ |
|
| 648 | - public static function getNavigation() { |
|
| 649 | - $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 650 | - return self::proceedNavigation($entries); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Returns the Settings Navigation |
|
| 655 | - * |
|
| 656 | - * @return string[] |
|
| 657 | - * |
|
| 658 | - * This function returns an array containing all settings pages added. The |
|
| 659 | - * entries are sorted by the key 'order' ascending. |
|
| 660 | - */ |
|
| 661 | - public static function getSettingsNavigation() { |
|
| 662 | - $entries = OC::$server->getNavigationManager()->getAll('settings'); |
|
| 663 | - return self::proceedNavigation($entries); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * get the id of loaded app |
|
| 668 | - * |
|
| 669 | - * @return string |
|
| 670 | - */ |
|
| 671 | - public static function getCurrentApp() { |
|
| 672 | - $request = \OC::$server->getRequest(); |
|
| 673 | - $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 674 | - $topFolder = substr($script, 0, strpos($script, '/')); |
|
| 675 | - if (empty($topFolder)) { |
|
| 676 | - $path_info = $request->getPathInfo(); |
|
| 677 | - if ($path_info) { |
|
| 678 | - $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 679 | - } |
|
| 680 | - } |
|
| 681 | - if ($topFolder == 'apps') { |
|
| 682 | - $length = strlen($topFolder); |
|
| 683 | - return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1); |
|
| 684 | - } else { |
|
| 685 | - return $topFolder; |
|
| 686 | - } |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * @param string $type |
|
| 691 | - * @return array |
|
| 692 | - */ |
|
| 693 | - public static function getForms($type) { |
|
| 694 | - $forms = array(); |
|
| 695 | - switch ($type) { |
|
| 696 | - case 'admin': |
|
| 697 | - $source = self::$adminForms; |
|
| 698 | - break; |
|
| 699 | - case 'personal': |
|
| 700 | - $source = self::$personalForms; |
|
| 701 | - break; |
|
| 702 | - default: |
|
| 703 | - return array(); |
|
| 704 | - } |
|
| 705 | - foreach ($source as $form) { |
|
| 706 | - $forms[] = include $form; |
|
| 707 | - } |
|
| 708 | - return $forms; |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - /** |
|
| 712 | - * register an admin form to be shown |
|
| 713 | - * |
|
| 714 | - * @param string $app |
|
| 715 | - * @param string $page |
|
| 716 | - */ |
|
| 717 | - public static function registerAdmin($app, $page) { |
|
| 718 | - self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - /** |
|
| 722 | - * register a personal form to be shown |
|
| 723 | - * @param string $app |
|
| 724 | - * @param string $page |
|
| 725 | - */ |
|
| 726 | - public static function registerPersonal($app, $page) { |
|
| 727 | - self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * @param array $entry |
|
| 732 | - */ |
|
| 733 | - public static function registerLogIn(array $entry) { |
|
| 734 | - self::$altLogin[] = $entry; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * @return array |
|
| 739 | - */ |
|
| 740 | - public static function getAlternativeLogIns() { |
|
| 741 | - return self::$altLogin; |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - /** |
|
| 745 | - * get a list of all apps in the apps folder |
|
| 746 | - * |
|
| 747 | - * @return array an array of app names (string IDs) |
|
| 748 | - * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 749 | - */ |
|
| 750 | - public static function getAllApps() { |
|
| 751 | - |
|
| 752 | - $apps = array(); |
|
| 753 | - |
|
| 754 | - foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 755 | - if (!is_readable($apps_dir['path'])) { |
|
| 756 | - \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 757 | - continue; |
|
| 758 | - } |
|
| 759 | - $dh = opendir($apps_dir['path']); |
|
| 760 | - |
|
| 761 | - if (is_resource($dh)) { |
|
| 762 | - while (($file = readdir($dh)) !== false) { |
|
| 763 | - |
|
| 764 | - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 765 | - |
|
| 766 | - $apps[] = $file; |
|
| 767 | - } |
|
| 768 | - } |
|
| 769 | - } |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - $apps = array_unique($apps); |
|
| 773 | - |
|
| 774 | - return $apps; |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - /** |
|
| 778 | - * List all apps, this is used in apps.php |
|
| 779 | - * |
|
| 780 | - * @return array |
|
| 781 | - */ |
|
| 782 | - public function listAllApps() { |
|
| 783 | - $installedApps = OC_App::getAllApps(); |
|
| 784 | - |
|
| 785 | - $appManager = \OC::$server->getAppManager(); |
|
| 786 | - //we don't want to show configuration for these |
|
| 787 | - $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 788 | - $appList = array(); |
|
| 789 | - $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 790 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 791 | - |
|
| 792 | - foreach ($installedApps as $app) { |
|
| 793 | - if (array_search($app, $blacklist) === false) { |
|
| 794 | - |
|
| 795 | - $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 796 | - if (!is_array($info)) { |
|
| 797 | - \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 798 | - continue; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - if (!isset($info['name'])) { |
|
| 802 | - \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 803 | - continue; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no'); |
|
| 807 | - $info['groups'] = null; |
|
| 808 | - if ($enabled === 'yes') { |
|
| 809 | - $active = true; |
|
| 810 | - } else if ($enabled === 'no') { |
|
| 811 | - $active = false; |
|
| 812 | - } else { |
|
| 813 | - $active = true; |
|
| 814 | - $info['groups'] = $enabled; |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - $info['active'] = $active; |
|
| 818 | - |
|
| 819 | - if ($appManager->isShipped($app)) { |
|
| 820 | - $info['internal'] = true; |
|
| 821 | - $info['level'] = self::officialApp; |
|
| 822 | - $info['removable'] = false; |
|
| 823 | - } else { |
|
| 824 | - $info['internal'] = false; |
|
| 825 | - $info['removable'] = true; |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - $appPath = self::getAppPath($app); |
|
| 829 | - if($appPath !== false) { |
|
| 830 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 831 | - if (file_exists($appIcon)) { |
|
| 832 | - $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 833 | - $info['previewAsIcon'] = true; |
|
| 834 | - } else { |
|
| 835 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 836 | - if (file_exists($appIcon)) { |
|
| 837 | - $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 838 | - $info['previewAsIcon'] = true; |
|
| 839 | - } |
|
| 840 | - } |
|
| 841 | - } |
|
| 842 | - // fix documentation |
|
| 843 | - if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 844 | - foreach ($info['documentation'] as $key => $url) { |
|
| 845 | - // If it is not an absolute URL we assume it is a key |
|
| 846 | - // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 847 | - if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 848 | - $url = $urlGenerator->linkToDocs($url); |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - $info['documentation'][$key] = $url; |
|
| 852 | - } |
|
| 853 | - } |
|
| 854 | - |
|
| 855 | - $info['version'] = OC_App::getAppVersion($app); |
|
| 856 | - $appList[] = $info; |
|
| 857 | - } |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - return $appList; |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - public static function shouldUpgrade($app) { |
|
| 864 | - $versions = self::getAppVersions(); |
|
| 865 | - $currentVersion = OC_App::getAppVersion($app); |
|
| 866 | - if ($currentVersion && isset($versions[$app])) { |
|
| 867 | - $installedVersion = $versions[$app]; |
|
| 868 | - if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 869 | - return true; |
|
| 870 | - } |
|
| 871 | - } |
|
| 872 | - return false; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - /** |
|
| 876 | - * Adjust the number of version parts of $version1 to match |
|
| 877 | - * the number of version parts of $version2. |
|
| 878 | - * |
|
| 879 | - * @param string $version1 version to adjust |
|
| 880 | - * @param string $version2 version to take the number of parts from |
|
| 881 | - * @return string shortened $version1 |
|
| 882 | - */ |
|
| 883 | - private static function adjustVersionParts($version1, $version2) { |
|
| 884 | - $version1 = explode('.', $version1); |
|
| 885 | - $version2 = explode('.', $version2); |
|
| 886 | - // reduce $version1 to match the number of parts in $version2 |
|
| 887 | - while (count($version1) > count($version2)) { |
|
| 888 | - array_pop($version1); |
|
| 889 | - } |
|
| 890 | - // if $version1 does not have enough parts, add some |
|
| 891 | - while (count($version1) < count($version2)) { |
|
| 892 | - $version1[] = '0'; |
|
| 893 | - } |
|
| 894 | - return implode('.', $version1); |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - /** |
|
| 898 | - * Check whether the current ownCloud version matches the given |
|
| 899 | - * application's version requirements. |
|
| 900 | - * |
|
| 901 | - * The comparison is made based on the number of parts that the |
|
| 902 | - * app info version has. For example for ownCloud 6.0.3 if the |
|
| 903 | - * app info version is expecting version 6.0, the comparison is |
|
| 904 | - * made on the first two parts of the ownCloud version. |
|
| 905 | - * This means that it's possible to specify "requiremin" => 6 |
|
| 906 | - * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 907 | - * |
|
| 908 | - * @param string $ocVersion ownCloud version to check against |
|
| 909 | - * @param array $appInfo app info (from xml) |
|
| 910 | - * |
|
| 911 | - * @return boolean true if compatible, otherwise false |
|
| 912 | - */ |
|
| 913 | - public static function isAppCompatible($ocVersion, $appInfo) { |
|
| 914 | - $requireMin = ''; |
|
| 915 | - $requireMax = ''; |
|
| 916 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 917 | - $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 918 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 919 | - $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 920 | - } else if (isset($appInfo['requiremin'])) { |
|
| 921 | - $requireMin = $appInfo['requiremin']; |
|
| 922 | - } else if (isset($appInfo['require'])) { |
|
| 923 | - $requireMin = $appInfo['require']; |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 927 | - $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 928 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 929 | - $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 930 | - } else if (isset($appInfo['requiremax'])) { |
|
| 931 | - $requireMax = $appInfo['requiremax']; |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - if (is_array($ocVersion)) { |
|
| 935 | - $ocVersion = implode('.', $ocVersion); |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - if (!empty($requireMin) |
|
| 939 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 940 | - ) { |
|
| 941 | - |
|
| 942 | - return false; |
|
| 943 | - } |
|
| 944 | - |
|
| 945 | - if (!empty($requireMax) |
|
| 946 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 947 | - ) { |
|
| 948 | - return false; |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - return true; |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * get the installed version of all apps |
|
| 956 | - */ |
|
| 957 | - public static function getAppVersions() { |
|
| 958 | - static $versions; |
|
| 959 | - |
|
| 960 | - if(!$versions) { |
|
| 961 | - $appConfig = \OC::$server->getAppConfig(); |
|
| 962 | - $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 963 | - } |
|
| 964 | - return $versions; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - /** |
|
| 968 | - * @param string $app |
|
| 969 | - * @param \OCP\IConfig $config |
|
| 970 | - * @param \OCP\IL10N $l |
|
| 971 | - * @return bool |
|
| 972 | - * |
|
| 973 | - * @throws Exception if app is not compatible with this version of ownCloud |
|
| 974 | - * @throws Exception if no app-name was specified |
|
| 975 | - */ |
|
| 976 | - public function installApp($app, |
|
| 977 | - \OCP\IConfig $config, |
|
| 978 | - \OCP\IL10N $l) { |
|
| 979 | - if ($app !== false) { |
|
| 980 | - // check if the app is compatible with this version of ownCloud |
|
| 981 | - $info = self::getAppInfo($app); |
|
| 982 | - if(!is_array($info)) { |
|
| 983 | - throw new \Exception( |
|
| 984 | - $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 985 | - [$info['name']] |
|
| 986 | - ) |
|
| 987 | - ); |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - $version = \OCP\Util::getVersion(); |
|
| 991 | - if (!self::isAppCompatible($version, $info)) { |
|
| 992 | - throw new \Exception( |
|
| 993 | - $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 994 | - array($info['name']) |
|
| 995 | - ) |
|
| 996 | - ); |
|
| 997 | - } |
|
| 998 | - |
|
| 999 | - // check for required dependencies |
|
| 1000 | - self::checkAppDependencies($config, $l, $info); |
|
| 1001 | - |
|
| 1002 | - $config->setAppValue($app, 'enabled', 'yes'); |
|
| 1003 | - if (isset($appData['id'])) { |
|
| 1004 | - $config->setAppValue($app, 'ocsid', $appData['id']); |
|
| 1005 | - } |
|
| 1006 | - |
|
| 1007 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1008 | - $appPath = self::getAppPath($app); |
|
| 1009 | - self::registerAutoloading($app, $appPath); |
|
| 1010 | - \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 1011 | - } |
|
| 1012 | - |
|
| 1013 | - \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
|
| 1014 | - } else { |
|
| 1015 | - if(empty($appName) ) { |
|
| 1016 | - throw new \Exception($l->t("No app name specified")); |
|
| 1017 | - } else { |
|
| 1018 | - throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
|
| 1019 | - } |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - return $app; |
|
| 1023 | - } |
|
| 1024 | - |
|
| 1025 | - /** |
|
| 1026 | - * update the database for the app and call the update script |
|
| 1027 | - * |
|
| 1028 | - * @param string $appId |
|
| 1029 | - * @return bool |
|
| 1030 | - */ |
|
| 1031 | - public static function updateApp($appId) { |
|
| 1032 | - $appPath = self::getAppPath($appId); |
|
| 1033 | - if($appPath === false) { |
|
| 1034 | - return false; |
|
| 1035 | - } |
|
| 1036 | - self::registerAutoloading($appId, $appPath); |
|
| 1037 | - |
|
| 1038 | - $appData = self::getAppInfo($appId); |
|
| 1039 | - self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 1040 | - |
|
| 1041 | - if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1042 | - OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1043 | - } else { |
|
| 1044 | - $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
|
| 1045 | - $ms->migrate(); |
|
| 1046 | - } |
|
| 1047 | - |
|
| 1048 | - self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 1049 | - self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 1050 | - unset(self::$appVersion[$appId]); |
|
| 1051 | - |
|
| 1052 | - // run upgrade code |
|
| 1053 | - if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1054 | - self::loadApp($appId); |
|
| 1055 | - include $appPath . '/appinfo/update.php'; |
|
| 1056 | - } |
|
| 1057 | - self::setupBackgroundJobs($appData['background-jobs']); |
|
| 1058 | - if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1059 | - \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - //set remote/public handlers |
|
| 1063 | - if (array_key_exists('ocsid', $appData)) { |
|
| 1064 | - \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 1065 | - } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1066 | - \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 1067 | - } |
|
| 1068 | - foreach ($appData['remote'] as $name => $path) { |
|
| 1069 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1070 | - } |
|
| 1071 | - foreach ($appData['public'] as $name => $path) { |
|
| 1072 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - self::setAppTypes($appId); |
|
| 1076 | - |
|
| 1077 | - $version = \OC_App::getAppVersion($appId); |
|
| 1078 | - \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version); |
|
| 1079 | - |
|
| 1080 | - \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 1081 | - ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 1082 | - )); |
|
| 1083 | - |
|
| 1084 | - return true; |
|
| 1085 | - } |
|
| 1086 | - |
|
| 1087 | - /** |
|
| 1088 | - * @param string $appId |
|
| 1089 | - * @param string[] $steps |
|
| 1090 | - * @throws \OC\NeedsUpdateException |
|
| 1091 | - */ |
|
| 1092 | - public static function executeRepairSteps($appId, array $steps) { |
|
| 1093 | - if (empty($steps)) { |
|
| 1094 | - return; |
|
| 1095 | - } |
|
| 1096 | - // load the app |
|
| 1097 | - self::loadApp($appId); |
|
| 1098 | - |
|
| 1099 | - $dispatcher = OC::$server->getEventDispatcher(); |
|
| 1100 | - |
|
| 1101 | - // load the steps |
|
| 1102 | - $r = new Repair([], $dispatcher); |
|
| 1103 | - foreach ($steps as $step) { |
|
| 1104 | - try { |
|
| 1105 | - $r->addStep($step); |
|
| 1106 | - } catch (Exception $ex) { |
|
| 1107 | - $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 1108 | - \OC::$server->getLogger()->logException($ex); |
|
| 1109 | - } |
|
| 1110 | - } |
|
| 1111 | - // run the steps |
|
| 1112 | - $r->run(); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - public static function setupBackgroundJobs(array $jobs) { |
|
| 1116 | - $queue = \OC::$server->getJobList(); |
|
| 1117 | - foreach ($jobs as $job) { |
|
| 1118 | - $queue->add($job); |
|
| 1119 | - } |
|
| 1120 | - } |
|
| 1121 | - |
|
| 1122 | - /** |
|
| 1123 | - * @param string $appId |
|
| 1124 | - * @param string[] $steps |
|
| 1125 | - */ |
|
| 1126 | - private static function setupLiveMigrations($appId, array $steps) { |
|
| 1127 | - $queue = \OC::$server->getJobList(); |
|
| 1128 | - foreach ($steps as $step) { |
|
| 1129 | - $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1130 | - 'app' => $appId, |
|
| 1131 | - 'step' => $step]); |
|
| 1132 | - } |
|
| 1133 | - } |
|
| 1134 | - |
|
| 1135 | - /** |
|
| 1136 | - * @param string $appId |
|
| 1137 | - * @return \OC\Files\View|false |
|
| 1138 | - */ |
|
| 1139 | - public static function getStorage($appId) { |
|
| 1140 | - if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check |
|
| 1141 | - if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1142 | - $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1143 | - if (!$view->file_exists($appId)) { |
|
| 1144 | - $view->mkdir($appId); |
|
| 1145 | - } |
|
| 1146 | - return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1147 | - } else { |
|
| 1148 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1149 | - return false; |
|
| 1150 | - } |
|
| 1151 | - } else { |
|
| 1152 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1153 | - return false; |
|
| 1154 | - } |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - protected static function findBestL10NOption($options, $lang) { |
|
| 1158 | - $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1159 | - |
|
| 1160 | - $lang = strtolower($lang); |
|
| 1161 | - $similarLang = $lang; |
|
| 1162 | - if (strpos($similarLang, '_')) { |
|
| 1163 | - // For "de_DE" we want to find "de" and the other way around |
|
| 1164 | - $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - foreach ($options as $option) { |
|
| 1168 | - if (is_array($option)) { |
|
| 1169 | - if ($fallback === false) { |
|
| 1170 | - $fallback = $option['@value']; |
|
| 1171 | - } |
|
| 1172 | - |
|
| 1173 | - if (!isset($option['@attributes']['lang'])) { |
|
| 1174 | - continue; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1178 | - if ($attributeLang === $lang) { |
|
| 1179 | - return $option['@value']; |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - if ($attributeLang === $similarLang) { |
|
| 1183 | - $similarLangFallback = $option['@value']; |
|
| 1184 | - } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1185 | - if ($similarLangFallback === false) { |
|
| 1186 | - $similarLangFallback = $option['@value']; |
|
| 1187 | - } |
|
| 1188 | - } |
|
| 1189 | - } else { |
|
| 1190 | - $englishFallback = $option; |
|
| 1191 | - } |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - if ($similarLangFallback !== false) { |
|
| 1195 | - return $similarLangFallback; |
|
| 1196 | - } else if ($englishFallback !== false) { |
|
| 1197 | - return $englishFallback; |
|
| 1198 | - } |
|
| 1199 | - return (string) $fallback; |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - /** |
|
| 1203 | - * parses the app data array and enhanced the 'description' value |
|
| 1204 | - * |
|
| 1205 | - * @param array $data the app data |
|
| 1206 | - * @param string $lang |
|
| 1207 | - * @return array improved app data |
|
| 1208 | - */ |
|
| 1209 | - public static function parseAppInfo(array $data, $lang = null) { |
|
| 1210 | - |
|
| 1211 | - if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1212 | - $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1213 | - } |
|
| 1214 | - if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1215 | - $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1216 | - } |
|
| 1217 | - if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1218 | - $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1219 | - } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1220 | - $data['description'] = trim($data['description']); |
|
| 1221 | - } else { |
|
| 1222 | - $data['description'] = ''; |
|
| 1223 | - } |
|
| 1224 | - |
|
| 1225 | - return $data; |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - /** |
|
| 1229 | - * @param \OCP\IConfig $config |
|
| 1230 | - * @param \OCP\IL10N $l |
|
| 1231 | - * @param array $info |
|
| 1232 | - * @throws \Exception |
|
| 1233 | - */ |
|
| 1234 | - public static function checkAppDependencies($config, $l, $info) { |
|
| 1235 | - $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1236 | - $missing = $dependencyAnalyzer->analyze($info); |
|
| 1237 | - if (!empty($missing)) { |
|
| 1238 | - $missingMsg = implode(PHP_EOL, $missing); |
|
| 1239 | - throw new \Exception( |
|
| 1240 | - $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1241 | - [$info['name'], $missingMsg] |
|
| 1242 | - ) |
|
| 1243 | - ); |
|
| 1244 | - } |
|
| 1245 | - } |
|
| 66 | + static private $appVersion = []; |
|
| 67 | + static private $adminForms = array(); |
|
| 68 | + static private $personalForms = array(); |
|
| 69 | + static private $appInfo = array(); |
|
| 70 | + static private $appTypes = array(); |
|
| 71 | + static private $loadedApps = array(); |
|
| 72 | + static private $altLogin = array(); |
|
| 73 | + static private $alreadyRegistered = []; |
|
| 74 | + const officialApp = 200; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * clean the appId |
|
| 78 | + * |
|
| 79 | + * @param string|boolean $app AppId that needs to be cleaned |
|
| 80 | + * @return string |
|
| 81 | + */ |
|
| 82 | + public static function cleanAppId($app) { |
|
| 83 | + return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Check if an app is loaded |
|
| 88 | + * |
|
| 89 | + * @param string $app |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public static function isAppLoaded($app) { |
|
| 93 | + return in_array($app, self::$loadedApps, true); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * loads all apps |
|
| 98 | + * |
|
| 99 | + * @param string[] | string | null $types |
|
| 100 | + * @return bool |
|
| 101 | + * |
|
| 102 | + * This function walks through the ownCloud directory and loads all apps |
|
| 103 | + * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 104 | + * exists. |
|
| 105 | + * |
|
| 106 | + * if $types is set, only apps of those types will be loaded |
|
| 107 | + */ |
|
| 108 | + public static function loadApps($types = null) { |
|
| 109 | + if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + // Load the enabled apps here |
|
| 113 | + $apps = self::getEnabledApps(); |
|
| 114 | + |
|
| 115 | + // Add each apps' folder as allowed class path |
|
| 116 | + foreach($apps as $app) { |
|
| 117 | + $path = self::getAppPath($app); |
|
| 118 | + if($path !== false) { |
|
| 119 | + self::registerAutoloading($app, $path); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // prevent app.php from printing output |
|
| 124 | + ob_start(); |
|
| 125 | + foreach ($apps as $app) { |
|
| 126 | + if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 127 | + self::loadApp($app); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + ob_end_clean(); |
|
| 131 | + |
|
| 132 | + return true; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * load a single app |
|
| 137 | + * |
|
| 138 | + * @param string $app |
|
| 139 | + */ |
|
| 140 | + public static function loadApp($app) { |
|
| 141 | + self::$loadedApps[] = $app; |
|
| 142 | + $appPath = self::getAppPath($app); |
|
| 143 | + if($appPath === false) { |
|
| 144 | + return; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // in case someone calls loadApp() directly |
|
| 148 | + self::registerAutoloading($app, $appPath); |
|
| 149 | + |
|
| 150 | + if (is_file($appPath . '/appinfo/app.php')) { |
|
| 151 | + \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 152 | + self::requireAppFile($app); |
|
| 153 | + if (self::isType($app, array('authentication'))) { |
|
| 154 | + // since authentication apps affect the "is app enabled for group" check, |
|
| 155 | + // the enabled apps cache needs to be cleared to make sure that the |
|
| 156 | + // next time getEnableApps() is called it will also include apps that were |
|
| 157 | + // enabled for groups |
|
| 158 | + self::$enabledAppsCache = array(); |
|
| 159 | + } |
|
| 160 | + \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $info = self::getAppInfo($app); |
|
| 164 | + if (!empty($info['activity']['filters'])) { |
|
| 165 | + foreach ($info['activity']['filters'] as $filter) { |
|
| 166 | + \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + if (!empty($info['activity']['settings'])) { |
|
| 170 | + foreach ($info['activity']['settings'] as $setting) { |
|
| 171 | + \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + if (!empty($info['activity']['providers'])) { |
|
| 175 | + foreach ($info['activity']['providers'] as $provider) { |
|
| 176 | + \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + if (!empty($info['collaboration']['plugins'])) { |
|
| 180 | + // deal with one or many plugin entries |
|
| 181 | + $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ? |
|
| 182 | + [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin']; |
|
| 183 | + foreach ($plugins as $plugin) { |
|
| 184 | + if($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 185 | + $pluginInfo = [ |
|
| 186 | + 'shareType' => $plugin['@attributes']['share-type'], |
|
| 187 | + 'class' => $plugin['@value'], |
|
| 188 | + ]; |
|
| 189 | + \OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo); |
|
| 190 | + } else if ($plugin['@attributes']['type'] === 'autocomplete-sort') { |
|
| 191 | + \OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @internal |
|
| 199 | + * @param string $app |
|
| 200 | + * @param string $path |
|
| 201 | + */ |
|
| 202 | + public static function registerAutoloading($app, $path) { |
|
| 203 | + $key = $app . '-' . $path; |
|
| 204 | + if(isset(self::$alreadyRegistered[$key])) { |
|
| 205 | + return; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + self::$alreadyRegistered[$key] = true; |
|
| 209 | + |
|
| 210 | + // Register on PSR-4 composer autoloader |
|
| 211 | + $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 212 | + \OC::$server->registerNamespace($app, $appNamespace); |
|
| 213 | + |
|
| 214 | + if (file_exists($path . '/composer/autoload.php')) { |
|
| 215 | + require_once $path . '/composer/autoload.php'; |
|
| 216 | + } else { |
|
| 217 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 218 | + // Register on legacy autoloader |
|
| 219 | + \OC::$loader->addValidRoot($path); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + // Register Test namespace only when testing |
|
| 223 | + if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 224 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Load app.php from the given app |
|
| 230 | + * |
|
| 231 | + * @param string $app app name |
|
| 232 | + */ |
|
| 233 | + private static function requireAppFile($app) { |
|
| 234 | + try { |
|
| 235 | + // encapsulated here to avoid variable scope conflicts |
|
| 236 | + require_once $app . '/appinfo/app.php'; |
|
| 237 | + } catch (Error $ex) { |
|
| 238 | + \OC::$server->getLogger()->logException($ex); |
|
| 239 | + $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 240 | + if (!in_array($app, $blacklist)) { |
|
| 241 | + self::disable($app); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * check if an app is of a specific type |
|
| 248 | + * |
|
| 249 | + * @param string $app |
|
| 250 | + * @param string|array $types |
|
| 251 | + * @return bool |
|
| 252 | + */ |
|
| 253 | + public static function isType($app, $types) { |
|
| 254 | + if (is_string($types)) { |
|
| 255 | + $types = array($types); |
|
| 256 | + } |
|
| 257 | + $appTypes = self::getAppTypes($app); |
|
| 258 | + foreach ($types as $type) { |
|
| 259 | + if (array_search($type, $appTypes) !== false) { |
|
| 260 | + return true; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + return false; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * get the types of an app |
|
| 268 | + * |
|
| 269 | + * @param string $app |
|
| 270 | + * @return array |
|
| 271 | + */ |
|
| 272 | + private static function getAppTypes($app) { |
|
| 273 | + //load the cache |
|
| 274 | + if (count(self::$appTypes) == 0) { |
|
| 275 | + self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + if (isset(self::$appTypes[$app])) { |
|
| 279 | + return explode(',', self::$appTypes[$app]); |
|
| 280 | + } else { |
|
| 281 | + return array(); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * read app types from info.xml and cache them in the database |
|
| 287 | + */ |
|
| 288 | + public static function setAppTypes($app) { |
|
| 289 | + $appData = self::getAppInfo($app); |
|
| 290 | + if(!is_array($appData)) { |
|
| 291 | + return; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + if (isset($appData['types'])) { |
|
| 295 | + $appTypes = implode(',', $appData['types']); |
|
| 296 | + } else { |
|
| 297 | + $appTypes = ''; |
|
| 298 | + $appData['types'] = []; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes); |
|
| 302 | + |
|
| 303 | + if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) { |
|
| 304 | + $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes'); |
|
| 305 | + if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 306 | + \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes'); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * get all enabled apps |
|
| 313 | + */ |
|
| 314 | + protected static $enabledAppsCache = array(); |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Returns apps enabled for the current user. |
|
| 318 | + * |
|
| 319 | + * @param bool $forceRefresh whether to refresh the cache |
|
| 320 | + * @param bool $all whether to return apps for all users, not only the |
|
| 321 | + * currently logged in one |
|
| 322 | + * @return string[] |
|
| 323 | + */ |
|
| 324 | + public static function getEnabledApps($forceRefresh = false, $all = false) { |
|
| 325 | + if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 326 | + return array(); |
|
| 327 | + } |
|
| 328 | + // in incognito mode or when logged out, $user will be false, |
|
| 329 | + // which is also the case during an upgrade |
|
| 330 | + $appManager = \OC::$server->getAppManager(); |
|
| 331 | + if ($all) { |
|
| 332 | + $user = null; |
|
| 333 | + } else { |
|
| 334 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + if (is_null($user)) { |
|
| 338 | + $apps = $appManager->getInstalledApps(); |
|
| 339 | + } else { |
|
| 340 | + $apps = $appManager->getEnabledAppsForUser($user); |
|
| 341 | + } |
|
| 342 | + $apps = array_filter($apps, function ($app) { |
|
| 343 | + return $app !== 'files';//we add this manually |
|
| 344 | + }); |
|
| 345 | + sort($apps); |
|
| 346 | + array_unshift($apps, 'files'); |
|
| 347 | + return $apps; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * checks whether or not an app is enabled |
|
| 352 | + * |
|
| 353 | + * @param string $app app |
|
| 354 | + * @return bool |
|
| 355 | + * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId) |
|
| 356 | + * |
|
| 357 | + * This function checks whether or not an app is enabled. |
|
| 358 | + */ |
|
| 359 | + public static function isEnabled($app) { |
|
| 360 | + return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * enables an app |
|
| 365 | + * |
|
| 366 | + * @param string $appId |
|
| 367 | + * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 368 | + * @throws \Exception |
|
| 369 | + * @return void |
|
| 370 | + * |
|
| 371 | + * This function set an app as enabled in appconfig. |
|
| 372 | + */ |
|
| 373 | + public function enable($appId, |
|
| 374 | + $groups = null) { |
|
| 375 | + self::$enabledAppsCache = []; // flush |
|
| 376 | + |
|
| 377 | + // Check if app is already downloaded |
|
| 378 | + $installer = \OC::$server->query(Installer::class); |
|
| 379 | + $isDownloaded = $installer->isDownloaded($appId); |
|
| 380 | + |
|
| 381 | + if(!$isDownloaded) { |
|
| 382 | + $installer->downloadApp($appId); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + $installer->installApp($appId); |
|
| 386 | + |
|
| 387 | + $appManager = \OC::$server->getAppManager(); |
|
| 388 | + if (!is_null($groups)) { |
|
| 389 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 390 | + $groupsList = []; |
|
| 391 | + foreach ($groups as $group) { |
|
| 392 | + $groupItem = $groupManager->get($group); |
|
| 393 | + if ($groupItem instanceof \OCP\IGroup) { |
|
| 394 | + $groupsList[] = $groupManager->get($group); |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + $appManager->enableAppForGroups($appId, $groupsList); |
|
| 398 | + } else { |
|
| 399 | + $appManager->enableApp($appId); |
|
| 400 | + } |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * @param string $app |
|
| 405 | + * @return bool |
|
| 406 | + */ |
|
| 407 | + public static function removeApp($app) { |
|
| 408 | + if (\OC::$server->getAppManager()->isShipped($app)) { |
|
| 409 | + return false; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + $installer = \OC::$server->query(Installer::class); |
|
| 413 | + return $installer->removeApp($app); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * This function set an app as disabled in appconfig. |
|
| 418 | + * |
|
| 419 | + * @param string $app app |
|
| 420 | + * @throws Exception |
|
| 421 | + */ |
|
| 422 | + public static function disable($app) { |
|
| 423 | + // flush |
|
| 424 | + self::$enabledAppsCache = array(); |
|
| 425 | + |
|
| 426 | + // run uninstall steps |
|
| 427 | + $appData = OC_App::getAppInfo($app); |
|
| 428 | + if (!is_null($appData)) { |
|
| 429 | + OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + // emit disable hook - needed anymore ? |
|
| 433 | + \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 434 | + |
|
| 435 | + // finally disable it |
|
| 436 | + $appManager = \OC::$server->getAppManager(); |
|
| 437 | + $appManager->disableApp($app); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + // This is private as well. It simply works, so don't ask for more details |
|
| 441 | + private static function proceedNavigation($list) { |
|
| 442 | + usort($list, function($a, $b) { |
|
| 443 | + if (isset($a['order']) && isset($b['order'])) { |
|
| 444 | + return ($a['order'] < $b['order']) ? -1 : 1; |
|
| 445 | + } else if (isset($a['order']) || isset($b['order'])) { |
|
| 446 | + return isset($a['order']) ? -1 : 1; |
|
| 447 | + } else { |
|
| 448 | + return ($a['name'] < $b['name']) ? -1 : 1; |
|
| 449 | + } |
|
| 450 | + }); |
|
| 451 | + |
|
| 452 | + $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 453 | + foreach ($list as $index => &$navEntry) { |
|
| 454 | + if ($navEntry['id'] == $activeApp) { |
|
| 455 | + $navEntry['active'] = true; |
|
| 456 | + } else { |
|
| 457 | + $navEntry['active'] = false; |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | + unset($navEntry); |
|
| 461 | + |
|
| 462 | + return $list; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Get the path where to install apps |
|
| 467 | + * |
|
| 468 | + * @return string|false |
|
| 469 | + */ |
|
| 470 | + public static function getInstallPath() { |
|
| 471 | + if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 472 | + return false; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 476 | + if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 477 | + return $dir['path']; |
|
| 478 | + } |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 482 | + return null; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * search for an app in all app-directories |
|
| 488 | + * |
|
| 489 | + * @param string $appId |
|
| 490 | + * @return false|string |
|
| 491 | + */ |
|
| 492 | + public static function findAppInDirectories($appId) { |
|
| 493 | + $sanitizedAppId = self::cleanAppId($appId); |
|
| 494 | + if($sanitizedAppId !== $appId) { |
|
| 495 | + return false; |
|
| 496 | + } |
|
| 497 | + static $app_dir = array(); |
|
| 498 | + |
|
| 499 | + if (isset($app_dir[$appId])) { |
|
| 500 | + return $app_dir[$appId].'0'; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + $possibleApps = array(); |
|
| 504 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 505 | + if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 506 | + $possibleApps[] = $dir; |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + if (empty($possibleApps)) { |
|
| 511 | + return false; |
|
| 512 | + } elseif (count($possibleApps) === 1) { |
|
| 513 | + $dir = array_shift($possibleApps); |
|
| 514 | + $app_dir[$appId] = $dir; |
|
| 515 | + return $dir.'1'; |
|
| 516 | + } else { |
|
| 517 | + $versionToLoad = array(); |
|
| 518 | + foreach ($possibleApps as $possibleApp) { |
|
| 519 | + $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 520 | + if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 521 | + $versionToLoad = array( |
|
| 522 | + 'dir' => $possibleApp, |
|
| 523 | + 'version' => $version, |
|
| 524 | + ); |
|
| 525 | + } |
|
| 526 | + } |
|
| 527 | + $app_dir[$appId] = $versionToLoad['dir']; |
|
| 528 | + return $versionToLoad['dir'].'2'; |
|
| 529 | + //TODO - write test |
|
| 530 | + } |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * Get the directory for the given app. |
|
| 535 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 536 | + * |
|
| 537 | + * @param string $appId |
|
| 538 | + * @return string|false |
|
| 539 | + */ |
|
| 540 | + public static function getAppPath($appId) { |
|
| 541 | + if ($appId === null || trim($appId) === '') { |
|
| 542 | + return false; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 546 | + return $dir['path'] . '/' . $appId; |
|
| 547 | + } |
|
| 548 | + return false; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Get the path for the given app on the access |
|
| 553 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 554 | + * |
|
| 555 | + * @param string $appId |
|
| 556 | + * @return string|false |
|
| 557 | + */ |
|
| 558 | + public static function getAppWebPath($appId) { |
|
| 559 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 560 | + return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 561 | + } |
|
| 562 | + return false; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + /** |
|
| 566 | + * get the last version of the app from appinfo/info.xml |
|
| 567 | + * |
|
| 568 | + * @param string $appId |
|
| 569 | + * @param bool $useCache |
|
| 570 | + * @return string |
|
| 571 | + */ |
|
| 572 | + public static function getAppVersion($appId, $useCache = true) { |
|
| 573 | + if($useCache && isset(self::$appVersion[$appId])) { |
|
| 574 | + return self::$appVersion[$appId]; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + $file = self::getAppPath($appId); |
|
| 578 | + self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0'; |
|
| 579 | + return self::$appVersion[$appId]; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * get app's version based on it's path |
|
| 584 | + * |
|
| 585 | + * @param string $path |
|
| 586 | + * @return string |
|
| 587 | + */ |
|
| 588 | + public static function getAppVersionByPath($path) { |
|
| 589 | + $infoFile = $path . '/appinfo/info.xml'; |
|
| 590 | + $appData = self::getAppInfo($infoFile, true); |
|
| 591 | + return isset($appData['version']) ? $appData['version'] : ''; |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Read all app metadata from the info.xml file |
|
| 597 | + * |
|
| 598 | + * @param string $appId id of the app or the path of the info.xml file |
|
| 599 | + * @param bool $path |
|
| 600 | + * @param string $lang |
|
| 601 | + * @return array|null |
|
| 602 | + * @note all data is read from info.xml, not just pre-defined fields |
|
| 603 | + */ |
|
| 604 | + public static function getAppInfo($appId, $path = false, $lang = null) { |
|
| 605 | + if ($path) { |
|
| 606 | + $file = $appId; |
|
| 607 | + } else { |
|
| 608 | + if ($lang === null && isset(self::$appInfo[$appId])) { |
|
| 609 | + return self::$appInfo[$appId]; |
|
| 610 | + } |
|
| 611 | + $appPath = self::getAppPath($appId); |
|
| 612 | + if($appPath === false) { |
|
| 613 | + return null; |
|
| 614 | + } |
|
| 615 | + $file = $appPath . '/appinfo/info.xml'; |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + $parser = new InfoParser(\OC::$server->getMemCacheFactory()->createLocal('core.appinfo')); |
|
| 619 | + $data = $parser->parse($file); |
|
| 620 | + |
|
| 621 | + if (is_array($data)) { |
|
| 622 | + $data = OC_App::parseAppInfo($data, $lang); |
|
| 623 | + } |
|
| 624 | + if(isset($data['ocsid'])) { |
|
| 625 | + $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
|
| 626 | + if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 627 | + $data['ocsid'] = $storedId; |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + if ($lang === null) { |
|
| 632 | + self::$appInfo[$appId] = $data; |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + return $data; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Returns the navigation |
|
| 640 | + * |
|
| 641 | + * @return array |
|
| 642 | + * |
|
| 643 | + * This function returns an array containing all entries added. The |
|
| 644 | + * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 645 | + * given for each app the following keys exist: |
|
| 646 | + * - active: boolean, signals if the user is on this navigation entry |
|
| 647 | + */ |
|
| 648 | + public static function getNavigation() { |
|
| 649 | + $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 650 | + return self::proceedNavigation($entries); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Returns the Settings Navigation |
|
| 655 | + * |
|
| 656 | + * @return string[] |
|
| 657 | + * |
|
| 658 | + * This function returns an array containing all settings pages added. The |
|
| 659 | + * entries are sorted by the key 'order' ascending. |
|
| 660 | + */ |
|
| 661 | + public static function getSettingsNavigation() { |
|
| 662 | + $entries = OC::$server->getNavigationManager()->getAll('settings'); |
|
| 663 | + return self::proceedNavigation($entries); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * get the id of loaded app |
|
| 668 | + * |
|
| 669 | + * @return string |
|
| 670 | + */ |
|
| 671 | + public static function getCurrentApp() { |
|
| 672 | + $request = \OC::$server->getRequest(); |
|
| 673 | + $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 674 | + $topFolder = substr($script, 0, strpos($script, '/')); |
|
| 675 | + if (empty($topFolder)) { |
|
| 676 | + $path_info = $request->getPathInfo(); |
|
| 677 | + if ($path_info) { |
|
| 678 | + $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 679 | + } |
|
| 680 | + } |
|
| 681 | + if ($topFolder == 'apps') { |
|
| 682 | + $length = strlen($topFolder); |
|
| 683 | + return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1); |
|
| 684 | + } else { |
|
| 685 | + return $topFolder; |
|
| 686 | + } |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * @param string $type |
|
| 691 | + * @return array |
|
| 692 | + */ |
|
| 693 | + public static function getForms($type) { |
|
| 694 | + $forms = array(); |
|
| 695 | + switch ($type) { |
|
| 696 | + case 'admin': |
|
| 697 | + $source = self::$adminForms; |
|
| 698 | + break; |
|
| 699 | + case 'personal': |
|
| 700 | + $source = self::$personalForms; |
|
| 701 | + break; |
|
| 702 | + default: |
|
| 703 | + return array(); |
|
| 704 | + } |
|
| 705 | + foreach ($source as $form) { |
|
| 706 | + $forms[] = include $form; |
|
| 707 | + } |
|
| 708 | + return $forms; |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + /** |
|
| 712 | + * register an admin form to be shown |
|
| 713 | + * |
|
| 714 | + * @param string $app |
|
| 715 | + * @param string $page |
|
| 716 | + */ |
|
| 717 | + public static function registerAdmin($app, $page) { |
|
| 718 | + self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + /** |
|
| 722 | + * register a personal form to be shown |
|
| 723 | + * @param string $app |
|
| 724 | + * @param string $page |
|
| 725 | + */ |
|
| 726 | + public static function registerPersonal($app, $page) { |
|
| 727 | + self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * @param array $entry |
|
| 732 | + */ |
|
| 733 | + public static function registerLogIn(array $entry) { |
|
| 734 | + self::$altLogin[] = $entry; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * @return array |
|
| 739 | + */ |
|
| 740 | + public static function getAlternativeLogIns() { |
|
| 741 | + return self::$altLogin; |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + /** |
|
| 745 | + * get a list of all apps in the apps folder |
|
| 746 | + * |
|
| 747 | + * @return array an array of app names (string IDs) |
|
| 748 | + * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 749 | + */ |
|
| 750 | + public static function getAllApps() { |
|
| 751 | + |
|
| 752 | + $apps = array(); |
|
| 753 | + |
|
| 754 | + foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 755 | + if (!is_readable($apps_dir['path'])) { |
|
| 756 | + \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 757 | + continue; |
|
| 758 | + } |
|
| 759 | + $dh = opendir($apps_dir['path']); |
|
| 760 | + |
|
| 761 | + if (is_resource($dh)) { |
|
| 762 | + while (($file = readdir($dh)) !== false) { |
|
| 763 | + |
|
| 764 | + if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 765 | + |
|
| 766 | + $apps[] = $file; |
|
| 767 | + } |
|
| 768 | + } |
|
| 769 | + } |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + $apps = array_unique($apps); |
|
| 773 | + |
|
| 774 | + return $apps; |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + /** |
|
| 778 | + * List all apps, this is used in apps.php |
|
| 779 | + * |
|
| 780 | + * @return array |
|
| 781 | + */ |
|
| 782 | + public function listAllApps() { |
|
| 783 | + $installedApps = OC_App::getAllApps(); |
|
| 784 | + |
|
| 785 | + $appManager = \OC::$server->getAppManager(); |
|
| 786 | + //we don't want to show configuration for these |
|
| 787 | + $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 788 | + $appList = array(); |
|
| 789 | + $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 790 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 791 | + |
|
| 792 | + foreach ($installedApps as $app) { |
|
| 793 | + if (array_search($app, $blacklist) === false) { |
|
| 794 | + |
|
| 795 | + $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 796 | + if (!is_array($info)) { |
|
| 797 | + \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 798 | + continue; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + if (!isset($info['name'])) { |
|
| 802 | + \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 803 | + continue; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no'); |
|
| 807 | + $info['groups'] = null; |
|
| 808 | + if ($enabled === 'yes') { |
|
| 809 | + $active = true; |
|
| 810 | + } else if ($enabled === 'no') { |
|
| 811 | + $active = false; |
|
| 812 | + } else { |
|
| 813 | + $active = true; |
|
| 814 | + $info['groups'] = $enabled; |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + $info['active'] = $active; |
|
| 818 | + |
|
| 819 | + if ($appManager->isShipped($app)) { |
|
| 820 | + $info['internal'] = true; |
|
| 821 | + $info['level'] = self::officialApp; |
|
| 822 | + $info['removable'] = false; |
|
| 823 | + } else { |
|
| 824 | + $info['internal'] = false; |
|
| 825 | + $info['removable'] = true; |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + $appPath = self::getAppPath($app); |
|
| 829 | + if($appPath !== false) { |
|
| 830 | + $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 831 | + if (file_exists($appIcon)) { |
|
| 832 | + $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 833 | + $info['previewAsIcon'] = true; |
|
| 834 | + } else { |
|
| 835 | + $appIcon = $appPath . '/img/app.svg'; |
|
| 836 | + if (file_exists($appIcon)) { |
|
| 837 | + $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 838 | + $info['previewAsIcon'] = true; |
|
| 839 | + } |
|
| 840 | + } |
|
| 841 | + } |
|
| 842 | + // fix documentation |
|
| 843 | + if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 844 | + foreach ($info['documentation'] as $key => $url) { |
|
| 845 | + // If it is not an absolute URL we assume it is a key |
|
| 846 | + // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 847 | + if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 848 | + $url = $urlGenerator->linkToDocs($url); |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + $info['documentation'][$key] = $url; |
|
| 852 | + } |
|
| 853 | + } |
|
| 854 | + |
|
| 855 | + $info['version'] = OC_App::getAppVersion($app); |
|
| 856 | + $appList[] = $info; |
|
| 857 | + } |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + return $appList; |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + public static function shouldUpgrade($app) { |
|
| 864 | + $versions = self::getAppVersions(); |
|
| 865 | + $currentVersion = OC_App::getAppVersion($app); |
|
| 866 | + if ($currentVersion && isset($versions[$app])) { |
|
| 867 | + $installedVersion = $versions[$app]; |
|
| 868 | + if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 869 | + return true; |
|
| 870 | + } |
|
| 871 | + } |
|
| 872 | + return false; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + /** |
|
| 876 | + * Adjust the number of version parts of $version1 to match |
|
| 877 | + * the number of version parts of $version2. |
|
| 878 | + * |
|
| 879 | + * @param string $version1 version to adjust |
|
| 880 | + * @param string $version2 version to take the number of parts from |
|
| 881 | + * @return string shortened $version1 |
|
| 882 | + */ |
|
| 883 | + private static function adjustVersionParts($version1, $version2) { |
|
| 884 | + $version1 = explode('.', $version1); |
|
| 885 | + $version2 = explode('.', $version2); |
|
| 886 | + // reduce $version1 to match the number of parts in $version2 |
|
| 887 | + while (count($version1) > count($version2)) { |
|
| 888 | + array_pop($version1); |
|
| 889 | + } |
|
| 890 | + // if $version1 does not have enough parts, add some |
|
| 891 | + while (count($version1) < count($version2)) { |
|
| 892 | + $version1[] = '0'; |
|
| 893 | + } |
|
| 894 | + return implode('.', $version1); |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + /** |
|
| 898 | + * Check whether the current ownCloud version matches the given |
|
| 899 | + * application's version requirements. |
|
| 900 | + * |
|
| 901 | + * The comparison is made based on the number of parts that the |
|
| 902 | + * app info version has. For example for ownCloud 6.0.3 if the |
|
| 903 | + * app info version is expecting version 6.0, the comparison is |
|
| 904 | + * made on the first two parts of the ownCloud version. |
|
| 905 | + * This means that it's possible to specify "requiremin" => 6 |
|
| 906 | + * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 907 | + * |
|
| 908 | + * @param string $ocVersion ownCloud version to check against |
|
| 909 | + * @param array $appInfo app info (from xml) |
|
| 910 | + * |
|
| 911 | + * @return boolean true if compatible, otherwise false |
|
| 912 | + */ |
|
| 913 | + public static function isAppCompatible($ocVersion, $appInfo) { |
|
| 914 | + $requireMin = ''; |
|
| 915 | + $requireMax = ''; |
|
| 916 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 917 | + $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 918 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 919 | + $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 920 | + } else if (isset($appInfo['requiremin'])) { |
|
| 921 | + $requireMin = $appInfo['requiremin']; |
|
| 922 | + } else if (isset($appInfo['require'])) { |
|
| 923 | + $requireMin = $appInfo['require']; |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 927 | + $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 928 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 929 | + $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 930 | + } else if (isset($appInfo['requiremax'])) { |
|
| 931 | + $requireMax = $appInfo['requiremax']; |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + if (is_array($ocVersion)) { |
|
| 935 | + $ocVersion = implode('.', $ocVersion); |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + if (!empty($requireMin) |
|
| 939 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 940 | + ) { |
|
| 941 | + |
|
| 942 | + return false; |
|
| 943 | + } |
|
| 944 | + |
|
| 945 | + if (!empty($requireMax) |
|
| 946 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 947 | + ) { |
|
| 948 | + return false; |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + return true; |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * get the installed version of all apps |
|
| 956 | + */ |
|
| 957 | + public static function getAppVersions() { |
|
| 958 | + static $versions; |
|
| 959 | + |
|
| 960 | + if(!$versions) { |
|
| 961 | + $appConfig = \OC::$server->getAppConfig(); |
|
| 962 | + $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 963 | + } |
|
| 964 | + return $versions; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + /** |
|
| 968 | + * @param string $app |
|
| 969 | + * @param \OCP\IConfig $config |
|
| 970 | + * @param \OCP\IL10N $l |
|
| 971 | + * @return bool |
|
| 972 | + * |
|
| 973 | + * @throws Exception if app is not compatible with this version of ownCloud |
|
| 974 | + * @throws Exception if no app-name was specified |
|
| 975 | + */ |
|
| 976 | + public function installApp($app, |
|
| 977 | + \OCP\IConfig $config, |
|
| 978 | + \OCP\IL10N $l) { |
|
| 979 | + if ($app !== false) { |
|
| 980 | + // check if the app is compatible with this version of ownCloud |
|
| 981 | + $info = self::getAppInfo($app); |
|
| 982 | + if(!is_array($info)) { |
|
| 983 | + throw new \Exception( |
|
| 984 | + $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 985 | + [$info['name']] |
|
| 986 | + ) |
|
| 987 | + ); |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + $version = \OCP\Util::getVersion(); |
|
| 991 | + if (!self::isAppCompatible($version, $info)) { |
|
| 992 | + throw new \Exception( |
|
| 993 | + $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 994 | + array($info['name']) |
|
| 995 | + ) |
|
| 996 | + ); |
|
| 997 | + } |
|
| 998 | + |
|
| 999 | + // check for required dependencies |
|
| 1000 | + self::checkAppDependencies($config, $l, $info); |
|
| 1001 | + |
|
| 1002 | + $config->setAppValue($app, 'enabled', 'yes'); |
|
| 1003 | + if (isset($appData['id'])) { |
|
| 1004 | + $config->setAppValue($app, 'ocsid', $appData['id']); |
|
| 1005 | + } |
|
| 1006 | + |
|
| 1007 | + if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1008 | + $appPath = self::getAppPath($app); |
|
| 1009 | + self::registerAutoloading($app, $appPath); |
|
| 1010 | + \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 1011 | + } |
|
| 1012 | + |
|
| 1013 | + \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
|
| 1014 | + } else { |
|
| 1015 | + if(empty($appName) ) { |
|
| 1016 | + throw new \Exception($l->t("No app name specified")); |
|
| 1017 | + } else { |
|
| 1018 | + throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
|
| 1019 | + } |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + return $app; |
|
| 1023 | + } |
|
| 1024 | + |
|
| 1025 | + /** |
|
| 1026 | + * update the database for the app and call the update script |
|
| 1027 | + * |
|
| 1028 | + * @param string $appId |
|
| 1029 | + * @return bool |
|
| 1030 | + */ |
|
| 1031 | + public static function updateApp($appId) { |
|
| 1032 | + $appPath = self::getAppPath($appId); |
|
| 1033 | + if($appPath === false) { |
|
| 1034 | + return false; |
|
| 1035 | + } |
|
| 1036 | + self::registerAutoloading($appId, $appPath); |
|
| 1037 | + |
|
| 1038 | + $appData = self::getAppInfo($appId); |
|
| 1039 | + self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 1040 | + |
|
| 1041 | + if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1042 | + OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1043 | + } else { |
|
| 1044 | + $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
|
| 1045 | + $ms->migrate(); |
|
| 1046 | + } |
|
| 1047 | + |
|
| 1048 | + self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 1049 | + self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 1050 | + unset(self::$appVersion[$appId]); |
|
| 1051 | + |
|
| 1052 | + // run upgrade code |
|
| 1053 | + if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1054 | + self::loadApp($appId); |
|
| 1055 | + include $appPath . '/appinfo/update.php'; |
|
| 1056 | + } |
|
| 1057 | + self::setupBackgroundJobs($appData['background-jobs']); |
|
| 1058 | + if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1059 | + \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + //set remote/public handlers |
|
| 1063 | + if (array_key_exists('ocsid', $appData)) { |
|
| 1064 | + \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 1065 | + } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1066 | + \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 1067 | + } |
|
| 1068 | + foreach ($appData['remote'] as $name => $path) { |
|
| 1069 | + \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1070 | + } |
|
| 1071 | + foreach ($appData['public'] as $name => $path) { |
|
| 1072 | + \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + self::setAppTypes($appId); |
|
| 1076 | + |
|
| 1077 | + $version = \OC_App::getAppVersion($appId); |
|
| 1078 | + \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version); |
|
| 1079 | + |
|
| 1080 | + \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 1081 | + ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 1082 | + )); |
|
| 1083 | + |
|
| 1084 | + return true; |
|
| 1085 | + } |
|
| 1086 | + |
|
| 1087 | + /** |
|
| 1088 | + * @param string $appId |
|
| 1089 | + * @param string[] $steps |
|
| 1090 | + * @throws \OC\NeedsUpdateException |
|
| 1091 | + */ |
|
| 1092 | + public static function executeRepairSteps($appId, array $steps) { |
|
| 1093 | + if (empty($steps)) { |
|
| 1094 | + return; |
|
| 1095 | + } |
|
| 1096 | + // load the app |
|
| 1097 | + self::loadApp($appId); |
|
| 1098 | + |
|
| 1099 | + $dispatcher = OC::$server->getEventDispatcher(); |
|
| 1100 | + |
|
| 1101 | + // load the steps |
|
| 1102 | + $r = new Repair([], $dispatcher); |
|
| 1103 | + foreach ($steps as $step) { |
|
| 1104 | + try { |
|
| 1105 | + $r->addStep($step); |
|
| 1106 | + } catch (Exception $ex) { |
|
| 1107 | + $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 1108 | + \OC::$server->getLogger()->logException($ex); |
|
| 1109 | + } |
|
| 1110 | + } |
|
| 1111 | + // run the steps |
|
| 1112 | + $r->run(); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + public static function setupBackgroundJobs(array $jobs) { |
|
| 1116 | + $queue = \OC::$server->getJobList(); |
|
| 1117 | + foreach ($jobs as $job) { |
|
| 1118 | + $queue->add($job); |
|
| 1119 | + } |
|
| 1120 | + } |
|
| 1121 | + |
|
| 1122 | + /** |
|
| 1123 | + * @param string $appId |
|
| 1124 | + * @param string[] $steps |
|
| 1125 | + */ |
|
| 1126 | + private static function setupLiveMigrations($appId, array $steps) { |
|
| 1127 | + $queue = \OC::$server->getJobList(); |
|
| 1128 | + foreach ($steps as $step) { |
|
| 1129 | + $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1130 | + 'app' => $appId, |
|
| 1131 | + 'step' => $step]); |
|
| 1132 | + } |
|
| 1133 | + } |
|
| 1134 | + |
|
| 1135 | + /** |
|
| 1136 | + * @param string $appId |
|
| 1137 | + * @return \OC\Files\View|false |
|
| 1138 | + */ |
|
| 1139 | + public static function getStorage($appId) { |
|
| 1140 | + if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check |
|
| 1141 | + if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1142 | + $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1143 | + if (!$view->file_exists($appId)) { |
|
| 1144 | + $view->mkdir($appId); |
|
| 1145 | + } |
|
| 1146 | + return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1147 | + } else { |
|
| 1148 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1149 | + return false; |
|
| 1150 | + } |
|
| 1151 | + } else { |
|
| 1152 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1153 | + return false; |
|
| 1154 | + } |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + protected static function findBestL10NOption($options, $lang) { |
|
| 1158 | + $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1159 | + |
|
| 1160 | + $lang = strtolower($lang); |
|
| 1161 | + $similarLang = $lang; |
|
| 1162 | + if (strpos($similarLang, '_')) { |
|
| 1163 | + // For "de_DE" we want to find "de" and the other way around |
|
| 1164 | + $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + foreach ($options as $option) { |
|
| 1168 | + if (is_array($option)) { |
|
| 1169 | + if ($fallback === false) { |
|
| 1170 | + $fallback = $option['@value']; |
|
| 1171 | + } |
|
| 1172 | + |
|
| 1173 | + if (!isset($option['@attributes']['lang'])) { |
|
| 1174 | + continue; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1178 | + if ($attributeLang === $lang) { |
|
| 1179 | + return $option['@value']; |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + if ($attributeLang === $similarLang) { |
|
| 1183 | + $similarLangFallback = $option['@value']; |
|
| 1184 | + } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1185 | + if ($similarLangFallback === false) { |
|
| 1186 | + $similarLangFallback = $option['@value']; |
|
| 1187 | + } |
|
| 1188 | + } |
|
| 1189 | + } else { |
|
| 1190 | + $englishFallback = $option; |
|
| 1191 | + } |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + if ($similarLangFallback !== false) { |
|
| 1195 | + return $similarLangFallback; |
|
| 1196 | + } else if ($englishFallback !== false) { |
|
| 1197 | + return $englishFallback; |
|
| 1198 | + } |
|
| 1199 | + return (string) $fallback; |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + /** |
|
| 1203 | + * parses the app data array and enhanced the 'description' value |
|
| 1204 | + * |
|
| 1205 | + * @param array $data the app data |
|
| 1206 | + * @param string $lang |
|
| 1207 | + * @return array improved app data |
|
| 1208 | + */ |
|
| 1209 | + public static function parseAppInfo(array $data, $lang = null) { |
|
| 1210 | + |
|
| 1211 | + if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1212 | + $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1213 | + } |
|
| 1214 | + if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1215 | + $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1216 | + } |
|
| 1217 | + if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1218 | + $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1219 | + } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1220 | + $data['description'] = trim($data['description']); |
|
| 1221 | + } else { |
|
| 1222 | + $data['description'] = ''; |
|
| 1223 | + } |
|
| 1224 | + |
|
| 1225 | + return $data; |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + /** |
|
| 1229 | + * @param \OCP\IConfig $config |
|
| 1230 | + * @param \OCP\IL10N $l |
|
| 1231 | + * @param array $info |
|
| 1232 | + * @throws \Exception |
|
| 1233 | + */ |
|
| 1234 | + public static function checkAppDependencies($config, $l, $info) { |
|
| 1235 | + $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1236 | + $missing = $dependencyAnalyzer->analyze($info); |
|
| 1237 | + if (!empty($missing)) { |
|
| 1238 | + $missingMsg = implode(PHP_EOL, $missing); |
|
| 1239 | + throw new \Exception( |
|
| 1240 | + $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1241 | + [$info['name'], $missingMsg] |
|
| 1242 | + ) |
|
| 1243 | + ); |
|
| 1244 | + } |
|
| 1245 | + } |
|
| 1246 | 1246 | } |
@@ -45,287 +45,287 @@ |
||
| 45 | 45 | |
| 46 | 46 | class SCSSCacher { |
| 47 | 47 | |
| 48 | - /** @var ILogger */ |
|
| 49 | - protected $logger; |
|
| 50 | - |
|
| 51 | - /** @var IAppData */ |
|
| 52 | - protected $appData; |
|
| 53 | - |
|
| 54 | - /** @var IURLGenerator */ |
|
| 55 | - protected $urlGenerator; |
|
| 56 | - |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - protected $config; |
|
| 59 | - |
|
| 60 | - /** @var string */ |
|
| 61 | - protected $serverRoot; |
|
| 62 | - |
|
| 63 | - /** @var ICache */ |
|
| 64 | - protected $depsCache; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param ILogger $logger |
|
| 68 | - * @param Factory $appDataFactory |
|
| 69 | - * @param IURLGenerator $urlGenerator |
|
| 70 | - * @param IConfig $config |
|
| 71 | - * @param \OC_Defaults $defaults |
|
| 72 | - * @param string $serverRoot |
|
| 73 | - * @param ICache $depsCache |
|
| 74 | - */ |
|
| 75 | - public function __construct(ILogger $logger, |
|
| 76 | - Factory $appDataFactory, |
|
| 77 | - IURLGenerator $urlGenerator, |
|
| 78 | - IConfig $config, |
|
| 79 | - \OC_Defaults $defaults, |
|
| 80 | - $serverRoot, |
|
| 81 | - ICache $depsCache) { |
|
| 82 | - $this->logger = $logger; |
|
| 83 | - $this->appData = $appDataFactory->get('css'); |
|
| 84 | - $this->urlGenerator = $urlGenerator; |
|
| 85 | - $this->config = $config; |
|
| 86 | - $this->defaults = $defaults; |
|
| 87 | - $this->serverRoot = $serverRoot; |
|
| 88 | - $this->depsCache = $depsCache; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Process the caching process if needed |
|
| 93 | - * @param string $root Root path to the nextcloud installation |
|
| 94 | - * @param string $file |
|
| 95 | - * @param string $app The app name |
|
| 96 | - * @return boolean |
|
| 97 | - */ |
|
| 98 | - public function process($root, $file, $app) { |
|
| 99 | - $path = explode('/', $root . '/' . $file); |
|
| 100 | - |
|
| 101 | - $fileNameSCSS = array_pop($path); |
|
| 102 | - $fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)); |
|
| 103 | - |
|
| 104 | - $path = implode('/', $path); |
|
| 105 | - $webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT); |
|
| 106 | - |
|
| 107 | - try { |
|
| 108 | - $folder = $this->appData->getFolder($app); |
|
| 109 | - } catch(NotFoundException $e) { |
|
| 110 | - // creating css appdata folder |
|
| 111 | - $folder = $this->appData->newFolder($app); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 116 | - return true; |
|
| 117 | - } |
|
| 118 | - return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @param $appName |
|
| 123 | - * @param $fileName |
|
| 124 | - * @return ISimpleFile |
|
| 125 | - */ |
|
| 126 | - public function getCachedCSS($appName, $fileName) { |
|
| 127 | - $folder = $this->appData->getFolder($appName); |
|
| 128 | - return $folder->getFile($this->prependBaseurlPrefix($fileName)); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Check if the file is cached or not |
|
| 133 | - * @param string $fileNameCSS |
|
| 134 | - * @param ISimpleFolder $folder |
|
| 135 | - * @return boolean |
|
| 136 | - */ |
|
| 137 | - private function isCached($fileNameCSS, ISimpleFolder $folder) { |
|
| 138 | - try { |
|
| 139 | - $cachedFile = $folder->getFile($fileNameCSS); |
|
| 140 | - if ($cachedFile->getSize() > 0) { |
|
| 141 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 142 | - $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 143 | - if ($deps === null) { |
|
| 144 | - $depFile = $folder->getFile($depFileName); |
|
| 145 | - $deps = $depFile->getContent(); |
|
| 146 | - //Set to memcache for next run |
|
| 147 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 148 | - } |
|
| 149 | - $deps = json_decode($deps, true); |
|
| 150 | - |
|
| 151 | - foreach ($deps as $file=>$mtime) { |
|
| 152 | - if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 153 | - return false; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - return true; |
|
| 158 | - } catch(NotFoundException $e) { |
|
| 159 | - return false; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Check if the variables file has changed |
|
| 165 | - * @return bool |
|
| 166 | - */ |
|
| 167 | - private function variablesChanged() { |
|
| 168 | - $injectedVariables = $this->getInjectedVariables(); |
|
| 169 | - if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 170 | - $this->resetCache(); |
|
| 171 | - $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
|
| 172 | - return true; |
|
| 173 | - } |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Cache the file with AppData |
|
| 179 | - * @param string $path |
|
| 180 | - * @param string $fileNameCSS |
|
| 181 | - * @param string $fileNameSCSS |
|
| 182 | - * @param ISimpleFolder $folder |
|
| 183 | - * @param string $webDir |
|
| 184 | - * @return boolean |
|
| 185 | - */ |
|
| 186 | - private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) { |
|
| 187 | - $scss = new Compiler(); |
|
| 188 | - $scss->setImportPaths([ |
|
| 189 | - $path, |
|
| 190 | - $this->serverRoot . '/core/css/', |
|
| 191 | - ]); |
|
| 192 | - // Continue after throw |
|
| 193 | - $scss->setIgnoreErrors(true); |
|
| 194 | - if($this->config->getSystemValue('debug')) { |
|
| 195 | - // Debug mode |
|
| 196 | - $scss->setFormatter(Expanded::class); |
|
| 197 | - $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
|
| 198 | - } else { |
|
| 199 | - // Compression |
|
| 200 | - $scss->setFormatter(Crunched::class); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - try { |
|
| 204 | - $cachedfile = $folder->getFile($fileNameCSS); |
|
| 205 | - } catch(NotFoundException $e) { |
|
| 206 | - $cachedfile = $folder->newFile($fileNameCSS); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 210 | - try { |
|
| 211 | - $depFile = $folder->getFile($depFileName); |
|
| 212 | - } catch (NotFoundException $e) { |
|
| 213 | - $depFile = $folder->newFile($depFileName); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - // Compile |
|
| 217 | - try { |
|
| 218 | - $compiledScss = $scss->compile( |
|
| 219 | - '@import "variables.scss";' . |
|
| 220 | - $this->getInjectedVariables() . |
|
| 221 | - '@import "'.$fileNameSCSS.'";'); |
|
| 222 | - } catch(ParserException $e) { |
|
| 223 | - $this->logger->error($e, ['app' => 'core']); |
|
| 224 | - return false; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Gzip file |
|
| 228 | - try { |
|
| 229 | - $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 230 | - } catch (NotFoundException $e) { |
|
| 231 | - $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - try { |
|
| 235 | - $data = $this->rebaseUrls($compiledScss, $webDir); |
|
| 236 | - $cachedfile->putContent($data); |
|
| 237 | - $deps = json_encode($scss->getParsedFiles()); |
|
| 238 | - $depFile->putContent($deps); |
|
| 239 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 240 | - $gzipFile->putContent(gzencode($data, 9)); |
|
| 241 | - $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
|
| 242 | - return true; |
|
| 243 | - } catch(NotPermittedException $e) { |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Reset scss cache by deleting all generated css files |
|
| 250 | - * We need to regenerate all files when variables change |
|
| 251 | - */ |
|
| 252 | - private function resetCache() { |
|
| 253 | - $appDirectory = $this->appData->getDirectoryListing(); |
|
| 254 | - if(empty($appDirectory)){ |
|
| 255 | - return; |
|
| 256 | - } |
|
| 257 | - foreach ($appDirectory as $folder) { |
|
| 258 | - foreach ($folder->getDirectoryListing() as $file) { |
|
| 259 | - if (substr($file->getName(), -3) === "css" || substr($file->getName(), -4) === "deps") { |
|
| 260 | - $file->delete(); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @return string SCSS code for variables from OC_Defaults |
|
| 268 | - */ |
|
| 269 | - private function getInjectedVariables() { |
|
| 270 | - $variables = ''; |
|
| 271 | - foreach ($this->defaults->getScssVariables() as $key => $value) { |
|
| 272 | - $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 273 | - } |
|
| 274 | - return $variables; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Add the correct uri prefix to make uri valid again |
|
| 279 | - * @param string $css |
|
| 280 | - * @param string $webDir |
|
| 281 | - * @return string |
|
| 282 | - */ |
|
| 283 | - private function rebaseUrls($css, $webDir) { |
|
| 284 | - $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x'; |
|
| 285 | - $subst = 'url(\''.$webDir.'/$1\')'; |
|
| 286 | - return preg_replace($re, $subst, $css); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Return the cached css file uri |
|
| 291 | - * @param string $appName the app name |
|
| 292 | - * @param string $fileName |
|
| 293 | - * @return string |
|
| 294 | - */ |
|
| 295 | - public function getCachedSCSS($appName, $fileName) { |
|
| 296 | - $tmpfileLoc = explode('/', $fileName); |
|
| 297 | - $fileName = array_pop($tmpfileLoc); |
|
| 298 | - $fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)); |
|
| 299 | - |
|
| 300 | - return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Prepend hashed base url to the css file |
|
| 305 | - * @param $cssFile |
|
| 306 | - * @return string |
|
| 307 | - */ |
|
| 308 | - private function prependBaseurlPrefix($cssFile) { |
|
| 309 | - $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 310 | - return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get WebDir root |
|
| 315 | - * @param string $path the css file path |
|
| 316 | - * @param string $appName the app name |
|
| 317 | - * @param string $serverRoot the server root path |
|
| 318 | - * @param string $webRoot the nextcloud installation root path |
|
| 319 | - * @return string the webDir |
|
| 320 | - */ |
|
| 321 | - private function getWebDir($path, $appName, $serverRoot, $webRoot) { |
|
| 322 | - // Detect if path is within server root AND if path is within an app path |
|
| 323 | - if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName) !== false) { |
|
| 324 | - // Get the file path within the app directory |
|
| 325 | - $appDirectoryPath = explode($appName, $path)[1]; |
|
| 326 | - // Remove the webroot |
|
| 327 | - return str_replace($webRoot, '', $appWebPath.$appDirectoryPath); |
|
| 328 | - } |
|
| 329 | - return $webRoot.substr($path, strlen($serverRoot)); |
|
| 330 | - } |
|
| 48 | + /** @var ILogger */ |
|
| 49 | + protected $logger; |
|
| 50 | + |
|
| 51 | + /** @var IAppData */ |
|
| 52 | + protected $appData; |
|
| 53 | + |
|
| 54 | + /** @var IURLGenerator */ |
|
| 55 | + protected $urlGenerator; |
|
| 56 | + |
|
| 57 | + /** @var IConfig */ |
|
| 58 | + protected $config; |
|
| 59 | + |
|
| 60 | + /** @var string */ |
|
| 61 | + protected $serverRoot; |
|
| 62 | + |
|
| 63 | + /** @var ICache */ |
|
| 64 | + protected $depsCache; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param ILogger $logger |
|
| 68 | + * @param Factory $appDataFactory |
|
| 69 | + * @param IURLGenerator $urlGenerator |
|
| 70 | + * @param IConfig $config |
|
| 71 | + * @param \OC_Defaults $defaults |
|
| 72 | + * @param string $serverRoot |
|
| 73 | + * @param ICache $depsCache |
|
| 74 | + */ |
|
| 75 | + public function __construct(ILogger $logger, |
|
| 76 | + Factory $appDataFactory, |
|
| 77 | + IURLGenerator $urlGenerator, |
|
| 78 | + IConfig $config, |
|
| 79 | + \OC_Defaults $defaults, |
|
| 80 | + $serverRoot, |
|
| 81 | + ICache $depsCache) { |
|
| 82 | + $this->logger = $logger; |
|
| 83 | + $this->appData = $appDataFactory->get('css'); |
|
| 84 | + $this->urlGenerator = $urlGenerator; |
|
| 85 | + $this->config = $config; |
|
| 86 | + $this->defaults = $defaults; |
|
| 87 | + $this->serverRoot = $serverRoot; |
|
| 88 | + $this->depsCache = $depsCache; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Process the caching process if needed |
|
| 93 | + * @param string $root Root path to the nextcloud installation |
|
| 94 | + * @param string $file |
|
| 95 | + * @param string $app The app name |
|
| 96 | + * @return boolean |
|
| 97 | + */ |
|
| 98 | + public function process($root, $file, $app) { |
|
| 99 | + $path = explode('/', $root . '/' . $file); |
|
| 100 | + |
|
| 101 | + $fileNameSCSS = array_pop($path); |
|
| 102 | + $fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)); |
|
| 103 | + |
|
| 104 | + $path = implode('/', $path); |
|
| 105 | + $webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT); |
|
| 106 | + |
|
| 107 | + try { |
|
| 108 | + $folder = $this->appData->getFolder($app); |
|
| 109 | + } catch(NotFoundException $e) { |
|
| 110 | + // creating css appdata folder |
|
| 111 | + $folder = $this->appData->newFolder($app); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 116 | + return true; |
|
| 117 | + } |
|
| 118 | + return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param $appName |
|
| 123 | + * @param $fileName |
|
| 124 | + * @return ISimpleFile |
|
| 125 | + */ |
|
| 126 | + public function getCachedCSS($appName, $fileName) { |
|
| 127 | + $folder = $this->appData->getFolder($appName); |
|
| 128 | + return $folder->getFile($this->prependBaseurlPrefix($fileName)); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Check if the file is cached or not |
|
| 133 | + * @param string $fileNameCSS |
|
| 134 | + * @param ISimpleFolder $folder |
|
| 135 | + * @return boolean |
|
| 136 | + */ |
|
| 137 | + private function isCached($fileNameCSS, ISimpleFolder $folder) { |
|
| 138 | + try { |
|
| 139 | + $cachedFile = $folder->getFile($fileNameCSS); |
|
| 140 | + if ($cachedFile->getSize() > 0) { |
|
| 141 | + $depFileName = $fileNameCSS . '.deps'; |
|
| 142 | + $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 143 | + if ($deps === null) { |
|
| 144 | + $depFile = $folder->getFile($depFileName); |
|
| 145 | + $deps = $depFile->getContent(); |
|
| 146 | + //Set to memcache for next run |
|
| 147 | + $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 148 | + } |
|
| 149 | + $deps = json_decode($deps, true); |
|
| 150 | + |
|
| 151 | + foreach ($deps as $file=>$mtime) { |
|
| 152 | + if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + return true; |
|
| 158 | + } catch(NotFoundException $e) { |
|
| 159 | + return false; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Check if the variables file has changed |
|
| 165 | + * @return bool |
|
| 166 | + */ |
|
| 167 | + private function variablesChanged() { |
|
| 168 | + $injectedVariables = $this->getInjectedVariables(); |
|
| 169 | + if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 170 | + $this->resetCache(); |
|
| 171 | + $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
|
| 172 | + return true; |
|
| 173 | + } |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Cache the file with AppData |
|
| 179 | + * @param string $path |
|
| 180 | + * @param string $fileNameCSS |
|
| 181 | + * @param string $fileNameSCSS |
|
| 182 | + * @param ISimpleFolder $folder |
|
| 183 | + * @param string $webDir |
|
| 184 | + * @return boolean |
|
| 185 | + */ |
|
| 186 | + private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) { |
|
| 187 | + $scss = new Compiler(); |
|
| 188 | + $scss->setImportPaths([ |
|
| 189 | + $path, |
|
| 190 | + $this->serverRoot . '/core/css/', |
|
| 191 | + ]); |
|
| 192 | + // Continue after throw |
|
| 193 | + $scss->setIgnoreErrors(true); |
|
| 194 | + if($this->config->getSystemValue('debug')) { |
|
| 195 | + // Debug mode |
|
| 196 | + $scss->setFormatter(Expanded::class); |
|
| 197 | + $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
|
| 198 | + } else { |
|
| 199 | + // Compression |
|
| 200 | + $scss->setFormatter(Crunched::class); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + try { |
|
| 204 | + $cachedfile = $folder->getFile($fileNameCSS); |
|
| 205 | + } catch(NotFoundException $e) { |
|
| 206 | + $cachedfile = $folder->newFile($fileNameCSS); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + $depFileName = $fileNameCSS . '.deps'; |
|
| 210 | + try { |
|
| 211 | + $depFile = $folder->getFile($depFileName); |
|
| 212 | + } catch (NotFoundException $e) { |
|
| 213 | + $depFile = $folder->newFile($depFileName); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + // Compile |
|
| 217 | + try { |
|
| 218 | + $compiledScss = $scss->compile( |
|
| 219 | + '@import "variables.scss";' . |
|
| 220 | + $this->getInjectedVariables() . |
|
| 221 | + '@import "'.$fileNameSCSS.'";'); |
|
| 222 | + } catch(ParserException $e) { |
|
| 223 | + $this->logger->error($e, ['app' => 'core']); |
|
| 224 | + return false; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Gzip file |
|
| 228 | + try { |
|
| 229 | + $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 230 | + } catch (NotFoundException $e) { |
|
| 231 | + $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + try { |
|
| 235 | + $data = $this->rebaseUrls($compiledScss, $webDir); |
|
| 236 | + $cachedfile->putContent($data); |
|
| 237 | + $deps = json_encode($scss->getParsedFiles()); |
|
| 238 | + $depFile->putContent($deps); |
|
| 239 | + $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 240 | + $gzipFile->putContent(gzencode($data, 9)); |
|
| 241 | + $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
|
| 242 | + return true; |
|
| 243 | + } catch(NotPermittedException $e) { |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Reset scss cache by deleting all generated css files |
|
| 250 | + * We need to regenerate all files when variables change |
|
| 251 | + */ |
|
| 252 | + private function resetCache() { |
|
| 253 | + $appDirectory = $this->appData->getDirectoryListing(); |
|
| 254 | + if(empty($appDirectory)){ |
|
| 255 | + return; |
|
| 256 | + } |
|
| 257 | + foreach ($appDirectory as $folder) { |
|
| 258 | + foreach ($folder->getDirectoryListing() as $file) { |
|
| 259 | + if (substr($file->getName(), -3) === "css" || substr($file->getName(), -4) === "deps") { |
|
| 260 | + $file->delete(); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @return string SCSS code for variables from OC_Defaults |
|
| 268 | + */ |
|
| 269 | + private function getInjectedVariables() { |
|
| 270 | + $variables = ''; |
|
| 271 | + foreach ($this->defaults->getScssVariables() as $key => $value) { |
|
| 272 | + $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 273 | + } |
|
| 274 | + return $variables; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Add the correct uri prefix to make uri valid again |
|
| 279 | + * @param string $css |
|
| 280 | + * @param string $webDir |
|
| 281 | + * @return string |
|
| 282 | + */ |
|
| 283 | + private function rebaseUrls($css, $webDir) { |
|
| 284 | + $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x'; |
|
| 285 | + $subst = 'url(\''.$webDir.'/$1\')'; |
|
| 286 | + return preg_replace($re, $subst, $css); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Return the cached css file uri |
|
| 291 | + * @param string $appName the app name |
|
| 292 | + * @param string $fileName |
|
| 293 | + * @return string |
|
| 294 | + */ |
|
| 295 | + public function getCachedSCSS($appName, $fileName) { |
|
| 296 | + $tmpfileLoc = explode('/', $fileName); |
|
| 297 | + $fileName = array_pop($tmpfileLoc); |
|
| 298 | + $fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)); |
|
| 299 | + |
|
| 300 | + return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Prepend hashed base url to the css file |
|
| 305 | + * @param $cssFile |
|
| 306 | + * @return string |
|
| 307 | + */ |
|
| 308 | + private function prependBaseurlPrefix($cssFile) { |
|
| 309 | + $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 310 | + return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get WebDir root |
|
| 315 | + * @param string $path the css file path |
|
| 316 | + * @param string $appName the app name |
|
| 317 | + * @param string $serverRoot the server root path |
|
| 318 | + * @param string $webRoot the nextcloud installation root path |
|
| 319 | + * @return string the webDir |
|
| 320 | + */ |
|
| 321 | + private function getWebDir($path, $appName, $serverRoot, $webRoot) { |
|
| 322 | + // Detect if path is within server root AND if path is within an app path |
|
| 323 | + if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName) !== false) { |
|
| 324 | + // Get the file path within the app directory |
|
| 325 | + $appDirectoryPath = explode($appName, $path)[1]; |
|
| 326 | + // Remove the webroot |
|
| 327 | + return str_replace($webRoot, '', $appWebPath.$appDirectoryPath); |
|
| 328 | + } |
|
| 329 | + return $webRoot.substr($path, strlen($serverRoot)); |
|
| 330 | + } |
|
| 331 | 331 | } |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * @return boolean |
| 97 | 97 | */ |
| 98 | 98 | public function process($root, $file, $app) { |
| 99 | - $path = explode('/', $root . '/' . $file); |
|
| 99 | + $path = explode('/', $root.'/'.$file); |
|
| 100 | 100 | |
| 101 | 101 | $fileNameSCSS = array_pop($path); |
| 102 | 102 | $fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)); |
@@ -106,13 +106,13 @@ discard block |
||
| 106 | 106 | |
| 107 | 107 | try { |
| 108 | 108 | $folder = $this->appData->getFolder($app); |
| 109 | - } catch(NotFoundException $e) { |
|
| 109 | + } catch (NotFoundException $e) { |
|
| 110 | 110 | // creating css appdata folder |
| 111 | 111 | $folder = $this->appData->newFolder($app); |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | |
| 115 | - if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 115 | + if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 116 | 116 | return true; |
| 117 | 117 | } |
| 118 | 118 | return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
@@ -138,13 +138,13 @@ discard block |
||
| 138 | 138 | try { |
| 139 | 139 | $cachedFile = $folder->getFile($fileNameCSS); |
| 140 | 140 | if ($cachedFile->getSize() > 0) { |
| 141 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 142 | - $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 141 | + $depFileName = $fileNameCSS.'.deps'; |
|
| 142 | + $deps = $this->depsCache->get($folder->getName().'-'.$depFileName); |
|
| 143 | 143 | if ($deps === null) { |
| 144 | 144 | $depFile = $folder->getFile($depFileName); |
| 145 | 145 | $deps = $depFile->getContent(); |
| 146 | 146 | //Set to memcache for next run |
| 147 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 147 | + $this->depsCache->set($folder->getName().'-'.$depFileName, $deps); |
|
| 148 | 148 | } |
| 149 | 149 | $deps = json_decode($deps, true); |
| 150 | 150 | |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | return true; |
| 158 | - } catch(NotFoundException $e) { |
|
| 158 | + } catch (NotFoundException $e) { |
|
| 159 | 159 | return false; |
| 160 | 160 | } |
| 161 | 161 | } |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | */ |
| 167 | 167 | private function variablesChanged() { |
| 168 | 168 | $injectedVariables = $this->getInjectedVariables(); |
| 169 | - if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 169 | + if ($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 170 | 170 | $this->resetCache(); |
| 171 | 171 | $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
| 172 | 172 | return true; |
@@ -187,11 +187,11 @@ discard block |
||
| 187 | 187 | $scss = new Compiler(); |
| 188 | 188 | $scss->setImportPaths([ |
| 189 | 189 | $path, |
| 190 | - $this->serverRoot . '/core/css/', |
|
| 190 | + $this->serverRoot.'/core/css/', |
|
| 191 | 191 | ]); |
| 192 | 192 | // Continue after throw |
| 193 | 193 | $scss->setIgnoreErrors(true); |
| 194 | - if($this->config->getSystemValue('debug')) { |
|
| 194 | + if ($this->config->getSystemValue('debug')) { |
|
| 195 | 195 | // Debug mode |
| 196 | 196 | $scss->setFormatter(Expanded::class); |
| 197 | 197 | $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
@@ -202,11 +202,11 @@ discard block |
||
| 202 | 202 | |
| 203 | 203 | try { |
| 204 | 204 | $cachedfile = $folder->getFile($fileNameCSS); |
| 205 | - } catch(NotFoundException $e) { |
|
| 205 | + } catch (NotFoundException $e) { |
|
| 206 | 206 | $cachedfile = $folder->newFile($fileNameCSS); |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 209 | + $depFileName = $fileNameCSS.'.deps'; |
|
| 210 | 210 | try { |
| 211 | 211 | $depFile = $folder->getFile($depFileName); |
| 212 | 212 | } catch (NotFoundException $e) { |
@@ -216,19 +216,19 @@ discard block |
||
| 216 | 216 | // Compile |
| 217 | 217 | try { |
| 218 | 218 | $compiledScss = $scss->compile( |
| 219 | - '@import "variables.scss";' . |
|
| 220 | - $this->getInjectedVariables() . |
|
| 219 | + '@import "variables.scss";'. |
|
| 220 | + $this->getInjectedVariables(). |
|
| 221 | 221 | '@import "'.$fileNameSCSS.'";'); |
| 222 | - } catch(ParserException $e) { |
|
| 222 | + } catch (ParserException $e) { |
|
| 223 | 223 | $this->logger->error($e, ['app' => 'core']); |
| 224 | 224 | return false; |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | // Gzip file |
| 228 | 228 | try { |
| 229 | - $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 229 | + $gzipFile = $folder->getFile($fileNameCSS.'.gzip'); # Safari doesn't like .gz |
|
| 230 | 230 | } catch (NotFoundException $e) { |
| 231 | - $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 231 | + $gzipFile = $folder->newFile($fileNameCSS.'.gzip'); # Safari doesn't like .gz |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | try { |
@@ -236,11 +236,11 @@ discard block |
||
| 236 | 236 | $cachedfile->putContent($data); |
| 237 | 237 | $deps = json_encode($scss->getParsedFiles()); |
| 238 | 238 | $depFile->putContent($deps); |
| 239 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 239 | + $this->depsCache->set($folder->getName().'-'.$depFileName, $deps); |
|
| 240 | 240 | $gzipFile->putContent(gzencode($data, 9)); |
| 241 | 241 | $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
| 242 | 242 | return true; |
| 243 | - } catch(NotPermittedException $e) { |
|
| 243 | + } catch (NotPermittedException $e) { |
|
| 244 | 244 | return false; |
| 245 | 245 | } |
| 246 | 246 | } |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | */ |
| 252 | 252 | private function resetCache() { |
| 253 | 253 | $appDirectory = $this->appData->getDirectoryListing(); |
| 254 | - if(empty($appDirectory)){ |
|
| 254 | + if (empty($appDirectory)) { |
|
| 255 | 255 | return; |
| 256 | 256 | } |
| 257 | 257 | foreach ($appDirectory as $folder) { |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | private function getInjectedVariables() { |
| 270 | 270 | $variables = ''; |
| 271 | 271 | foreach ($this->defaults->getScssVariables() as $key => $value) { |
| 272 | - $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 272 | + $variables .= '$'.$key.': '.$value.';'; |
|
| 273 | 273 | } |
| 274 | 274 | return $variables; |
| 275 | 275 | } |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | */ |
| 308 | 308 | private function prependBaseurlPrefix($cssFile) { |
| 309 | 309 | $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
| 310 | - return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile; |
|
| 310 | + return substr(md5($this->urlGenerator->getBaseUrl().$frontendController), 0, 8).'-'.$cssFile; |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | /** |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | */ |
| 321 | 321 | private function getWebDir($path, $appName, $serverRoot, $webRoot) { |
| 322 | 322 | // Detect if path is within server root AND if path is within an app path |
| 323 | - if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName) !== false) { |
|
| 323 | + if (strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName) !== false) { |
|
| 324 | 324 | // Get the file path within the app directory |
| 325 | 325 | $appDirectoryPath = explode($appName, $path)[1]; |
| 326 | 326 | // Remove the webroot |