@@ -63,1069 +63,1069 @@ |
||
| 63 | 63 | * upgrading and removing apps. |
| 64 | 64 | */ |
| 65 | 65 | class OC_App { |
| 66 | - static private $adminForms = []; |
|
| 67 | - static private $personalForms = []; |
|
| 68 | - static private $appTypes = []; |
|
| 69 | - static private $loadedApps = []; |
|
| 70 | - static private $altLogin = []; |
|
| 71 | - static private $alreadyRegistered = []; |
|
| 72 | - const officialApp = 200; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * clean the appId |
|
| 76 | - * |
|
| 77 | - * @param string $app AppId that needs to be cleaned |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public static function cleanAppId(string $app): string { |
|
| 81 | - return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Check if an app is loaded |
|
| 86 | - * |
|
| 87 | - * @param string $app |
|
| 88 | - * @return bool |
|
| 89 | - */ |
|
| 90 | - public static function isAppLoaded(string $app): bool { |
|
| 91 | - return in_array($app, self::$loadedApps, true); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * loads all apps |
|
| 96 | - * |
|
| 97 | - * @param string[] $types |
|
| 98 | - * @return bool |
|
| 99 | - * |
|
| 100 | - * This function walks through the ownCloud directory and loads all apps |
|
| 101 | - * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 102 | - * exists. |
|
| 103 | - * |
|
| 104 | - * if $types is set to non-empty array, only apps of those types will be loaded |
|
| 105 | - */ |
|
| 106 | - public static function loadApps(array $types = []): bool { |
|
| 107 | - if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - // Load the enabled apps here |
|
| 111 | - $apps = self::getEnabledApps(); |
|
| 112 | - |
|
| 113 | - // Add each apps' folder as allowed class path |
|
| 114 | - foreach($apps as $app) { |
|
| 115 | - $path = self::getAppPath($app); |
|
| 116 | - if($path !== false) { |
|
| 117 | - self::registerAutoloading($app, $path); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // prevent app.php from printing output |
|
| 122 | - ob_start(); |
|
| 123 | - foreach ($apps as $app) { |
|
| 124 | - if (($types === [] or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 125 | - self::loadApp($app); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - ob_end_clean(); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * load a single app |
|
| 135 | - * |
|
| 136 | - * @param string $app |
|
| 137 | - * @throws Exception |
|
| 138 | - */ |
|
| 139 | - public static function loadApp(string $app) { |
|
| 140 | - self::$loadedApps[] = $app; |
|
| 141 | - $appPath = self::getAppPath($app); |
|
| 142 | - if($appPath === false) { |
|
| 143 | - return; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // in case someone calls loadApp() directly |
|
| 147 | - self::registerAutoloading($app, $appPath); |
|
| 148 | - |
|
| 149 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 150 | - \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 151 | - try { |
|
| 152 | - self::requireAppFile($app); |
|
| 153 | - } catch (Error $ex) { |
|
| 154 | - \OC::$server->getLogger()->logException($ex); |
|
| 155 | - if (!\OC::$server->getAppManager()->isShipped($app)) { |
|
| 156 | - // Only disable apps which are not shipped |
|
| 157 | - self::disable($app); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - if (self::isType($app, array('authentication'))) { |
|
| 161 | - // since authentication apps affect the "is app enabled for group" check, |
|
| 162 | - // the enabled apps cache needs to be cleared to make sure that the |
|
| 163 | - // next time getEnableApps() is called it will also include apps that were |
|
| 164 | - // enabled for groups |
|
| 165 | - self::$enabledAppsCache = []; |
|
| 166 | - } |
|
| 167 | - \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $info = self::getAppInfo($app); |
|
| 171 | - if (!empty($info['activity']['filters'])) { |
|
| 172 | - foreach ($info['activity']['filters'] as $filter) { |
|
| 173 | - \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - if (!empty($info['activity']['settings'])) { |
|
| 177 | - foreach ($info['activity']['settings'] as $setting) { |
|
| 178 | - \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - if (!empty($info['activity']['providers'])) { |
|
| 182 | - foreach ($info['activity']['providers'] as $provider) { |
|
| 183 | - \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - if (!empty($info['settings']['admin'])) { |
|
| 188 | - foreach ($info['settings']['admin'] as $setting) { |
|
| 189 | - \OC::$server->getSettingsManager()->registerSetting('admin', $setting); |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - if (!empty($info['settings']['admin-section'])) { |
|
| 193 | - foreach ($info['settings']['admin-section'] as $section) { |
|
| 194 | - \OC::$server->getSettingsManager()->registerSection('admin', $section); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - if (!empty($info['settings']['personal'])) { |
|
| 198 | - foreach ($info['settings']['personal'] as $setting) { |
|
| 199 | - \OC::$server->getSettingsManager()->registerSetting('personal', $setting); |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - if (!empty($info['settings']['personal-section'])) { |
|
| 203 | - foreach ($info['settings']['personal-section'] as $section) { |
|
| 204 | - \OC::$server->getSettingsManager()->registerSection('personal', $section); |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if (!empty($info['collaboration']['plugins'])) { |
|
| 209 | - // deal with one or many plugin entries |
|
| 210 | - $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ? |
|
| 211 | - [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin']; |
|
| 212 | - foreach ($plugins as $plugin) { |
|
| 213 | - if($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 214 | - $pluginInfo = [ |
|
| 215 | - 'shareType' => $plugin['@attributes']['share-type'], |
|
| 216 | - 'class' => $plugin['@value'], |
|
| 217 | - ]; |
|
| 218 | - \OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo); |
|
| 219 | - } else if ($plugin['@attributes']['type'] === 'autocomplete-sort') { |
|
| 220 | - \OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']); |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @internal |
|
| 228 | - * @param string $app |
|
| 229 | - * @param string $path |
|
| 230 | - */ |
|
| 231 | - public static function registerAutoloading(string $app, string $path) { |
|
| 232 | - $key = $app . '-' . $path; |
|
| 233 | - if(isset(self::$alreadyRegistered[$key])) { |
|
| 234 | - return; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - self::$alreadyRegistered[$key] = true; |
|
| 238 | - |
|
| 239 | - // Register on PSR-4 composer autoloader |
|
| 240 | - $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 241 | - \OC::$server->registerNamespace($app, $appNamespace); |
|
| 242 | - |
|
| 243 | - if (file_exists($path . '/composer/autoload.php')) { |
|
| 244 | - require_once $path . '/composer/autoload.php'; |
|
| 245 | - } else { |
|
| 246 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 247 | - // Register on legacy autoloader |
|
| 248 | - \OC::$loader->addValidRoot($path); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - // Register Test namespace only when testing |
|
| 252 | - if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 253 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Load app.php from the given app |
|
| 259 | - * |
|
| 260 | - * @param string $app app name |
|
| 261 | - * @throws Error |
|
| 262 | - */ |
|
| 263 | - private static function requireAppFile(string $app) { |
|
| 264 | - // encapsulated here to avoid variable scope conflicts |
|
| 265 | - require_once $app . '/appinfo/app.php'; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * check if an app is of a specific type |
|
| 270 | - * |
|
| 271 | - * @param string $app |
|
| 272 | - * @param array $types |
|
| 273 | - * @return bool |
|
| 274 | - */ |
|
| 275 | - public static function isType(string $app, array $types): bool { |
|
| 276 | - $appTypes = self::getAppTypes($app); |
|
| 277 | - foreach ($types as $type) { |
|
| 278 | - if (array_search($type, $appTypes) !== false) { |
|
| 279 | - return true; |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - return false; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * get the types of an app |
|
| 287 | - * |
|
| 288 | - * @param string $app |
|
| 289 | - * @return array |
|
| 290 | - */ |
|
| 291 | - private static function getAppTypes(string $app): array { |
|
| 292 | - //load the cache |
|
| 293 | - if (count(self::$appTypes) == 0) { |
|
| 294 | - self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - if (isset(self::$appTypes[$app])) { |
|
| 298 | - return explode(',', self::$appTypes[$app]); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return []; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * read app types from info.xml and cache them in the database |
|
| 306 | - */ |
|
| 307 | - public static function setAppTypes(string $app) { |
|
| 308 | - $appManager = \OC::$server->getAppManager(); |
|
| 309 | - $appData = $appManager->getAppInfo($app); |
|
| 310 | - if(!is_array($appData)) { |
|
| 311 | - return; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - if (isset($appData['types'])) { |
|
| 315 | - $appTypes = implode(',', $appData['types']); |
|
| 316 | - } else { |
|
| 317 | - $appTypes = ''; |
|
| 318 | - $appData['types'] = []; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $config = \OC::$server->getConfig(); |
|
| 322 | - $config->setAppValue($app, 'types', $appTypes); |
|
| 323 | - |
|
| 324 | - if ($appManager->hasProtectedAppType($appData['types'])) { |
|
| 325 | - $enabled = $config->getAppValue($app, 'enabled', 'yes'); |
|
| 326 | - if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 327 | - $config->setAppValue($app, 'enabled', 'yes'); |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * get all enabled apps |
|
| 334 | - */ |
|
| 335 | - protected static $enabledAppsCache = []; |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Returns apps enabled for the current user. |
|
| 339 | - * |
|
| 340 | - * @param bool $forceRefresh whether to refresh the cache |
|
| 341 | - * @param bool $all whether to return apps for all users, not only the |
|
| 342 | - * currently logged in one |
|
| 343 | - * @return string[] |
|
| 344 | - */ |
|
| 345 | - public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array { |
|
| 346 | - if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 347 | - return []; |
|
| 348 | - } |
|
| 349 | - // in incognito mode or when logged out, $user will be false, |
|
| 350 | - // which is also the case during an upgrade |
|
| 351 | - $appManager = \OC::$server->getAppManager(); |
|
| 352 | - if ($all) { |
|
| 353 | - $user = null; |
|
| 354 | - } else { |
|
| 355 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - if (is_null($user)) { |
|
| 359 | - $apps = $appManager->getInstalledApps(); |
|
| 360 | - } else { |
|
| 361 | - $apps = $appManager->getEnabledAppsForUser($user); |
|
| 362 | - } |
|
| 363 | - $apps = array_filter($apps, function ($app) { |
|
| 364 | - return $app !== 'files';//we add this manually |
|
| 365 | - }); |
|
| 366 | - sort($apps); |
|
| 367 | - array_unshift($apps, 'files'); |
|
| 368 | - return $apps; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * checks whether or not an app is enabled |
|
| 373 | - * |
|
| 374 | - * @param string $app app |
|
| 375 | - * @return bool |
|
| 376 | - * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId) |
|
| 377 | - * |
|
| 378 | - * This function checks whether or not an app is enabled. |
|
| 379 | - */ |
|
| 380 | - public static function isEnabled(string $app): bool { |
|
| 381 | - return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * enables an app |
|
| 386 | - * |
|
| 387 | - * @param string $appId |
|
| 388 | - * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 389 | - * @throws \Exception |
|
| 390 | - * @return void |
|
| 391 | - * |
|
| 392 | - * This function set an app as enabled in appconfig. |
|
| 393 | - */ |
|
| 394 | - public function enable(string $appId, |
|
| 395 | - array $groups = []) { |
|
| 396 | - self::$enabledAppsCache = []; // flush |
|
| 397 | - |
|
| 398 | - // Check if app is already downloaded |
|
| 399 | - $installer = \OC::$server->query(Installer::class); |
|
| 400 | - $isDownloaded = $installer->isDownloaded($appId); |
|
| 401 | - |
|
| 402 | - if(!$isDownloaded) { |
|
| 403 | - $installer->downloadApp($appId); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - $installer->installApp($appId); |
|
| 407 | - |
|
| 408 | - $appManager = \OC::$server->getAppManager(); |
|
| 409 | - if ($groups !== []) { |
|
| 410 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 411 | - $groupsList = []; |
|
| 412 | - foreach ($groups as $group) { |
|
| 413 | - $groupItem = $groupManager->get($group); |
|
| 414 | - if ($groupItem instanceof \OCP\IGroup) { |
|
| 415 | - $groupsList[] = $groupManager->get($group); |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - $appManager->enableAppForGroups($appId, $groupsList); |
|
| 419 | - } else { |
|
| 420 | - $appManager->enableApp($appId); |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * This function set an app as disabled in appconfig. |
|
| 426 | - * |
|
| 427 | - * @param string $app app |
|
| 428 | - * @throws Exception |
|
| 429 | - */ |
|
| 430 | - public static function disable(string $app) { |
|
| 431 | - // flush |
|
| 432 | - self::$enabledAppsCache = []; |
|
| 433 | - |
|
| 434 | - // run uninstall steps |
|
| 435 | - $appData = OC_App::getAppInfo($app); |
|
| 436 | - if (!is_null($appData)) { |
|
| 437 | - OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - // emit disable hook - needed anymore ? |
|
| 441 | - \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 442 | - |
|
| 443 | - // finally disable it |
|
| 444 | - $appManager = \OC::$server->getAppManager(); |
|
| 445 | - $appManager->disableApp($app); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Get the path where to install apps |
|
| 450 | - * |
|
| 451 | - * @return string|false |
|
| 452 | - */ |
|
| 453 | - public static function getInstallPath() { |
|
| 454 | - if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 455 | - return false; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 459 | - if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 460 | - return $dir['path']; |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 465 | - return null; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * search for an app in all app-directories |
|
| 471 | - * |
|
| 472 | - * @param string $appId |
|
| 473 | - * @return false|string |
|
| 474 | - */ |
|
| 475 | - public static function findAppInDirectories(string $appId) { |
|
| 476 | - $sanitizedAppId = self::cleanAppId($appId); |
|
| 477 | - if($sanitizedAppId !== $appId) { |
|
| 478 | - return false; |
|
| 479 | - } |
|
| 480 | - static $app_dir = []; |
|
| 481 | - |
|
| 482 | - if (isset($app_dir[$appId])) { |
|
| 483 | - return $app_dir[$appId]; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - $possibleApps = []; |
|
| 487 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 488 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 489 | - $possibleApps[] = $dir; |
|
| 490 | - } |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - if (empty($possibleApps)) { |
|
| 494 | - return false; |
|
| 495 | - } elseif (count($possibleApps) === 1) { |
|
| 496 | - $dir = array_shift($possibleApps); |
|
| 497 | - $app_dir[$appId] = $dir; |
|
| 498 | - return $dir; |
|
| 499 | - } else { |
|
| 500 | - $versionToLoad = []; |
|
| 501 | - foreach ($possibleApps as $possibleApp) { |
|
| 502 | - $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 503 | - if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 504 | - $versionToLoad = array( |
|
| 505 | - 'dir' => $possibleApp, |
|
| 506 | - 'version' => $version, |
|
| 507 | - ); |
|
| 508 | - } |
|
| 509 | - } |
|
| 510 | - $app_dir[$appId] = $versionToLoad['dir']; |
|
| 511 | - return $versionToLoad['dir']; |
|
| 512 | - //TODO - write test |
|
| 513 | - } |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Get the directory for the given app. |
|
| 518 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 519 | - * |
|
| 520 | - * @param string $appId |
|
| 521 | - * @return string|false |
|
| 522 | - */ |
|
| 523 | - public static function getAppPath(string $appId) { |
|
| 524 | - if ($appId === null || trim($appId) === '') { |
|
| 525 | - return false; |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 529 | - return $dir['path'] . '/' . $appId; |
|
| 530 | - } |
|
| 531 | - return false; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * Get the path for the given app on the access |
|
| 536 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 537 | - * |
|
| 538 | - * @param string $appId |
|
| 539 | - * @return string|false |
|
| 540 | - */ |
|
| 541 | - public static function getAppWebPath(string $appId) { |
|
| 542 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 543 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 544 | - } |
|
| 545 | - return false; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * get the last version of the app from appinfo/info.xml |
|
| 550 | - * |
|
| 551 | - * @param string $appId |
|
| 552 | - * @param bool $useCache |
|
| 553 | - * @return string |
|
| 554 | - * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion() |
|
| 555 | - */ |
|
| 556 | - public static function getAppVersion(string $appId, bool $useCache = true): string { |
|
| 557 | - return \OC::$server->getAppManager()->getAppVersion($appId, $useCache); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * get app's version based on it's path |
|
| 562 | - * |
|
| 563 | - * @param string $path |
|
| 564 | - * @return string |
|
| 565 | - */ |
|
| 566 | - public static function getAppVersionByPath(string $path): string { |
|
| 567 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 568 | - $appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true); |
|
| 569 | - return isset($appData['version']) ? $appData['version'] : ''; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - |
|
| 573 | - /** |
|
| 574 | - * Read all app metadata from the info.xml file |
|
| 575 | - * |
|
| 576 | - * @param string $appId id of the app or the path of the info.xml file |
|
| 577 | - * @param bool $path |
|
| 578 | - * @param string $lang |
|
| 579 | - * @return array|null |
|
| 580 | - * @note all data is read from info.xml, not just pre-defined fields |
|
| 581 | - * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppInfo() |
|
| 582 | - */ |
|
| 583 | - public static function getAppInfo(string $appId, bool $path = false, string $lang = null) { |
|
| 584 | - return \OC::$server->getAppManager()->getAppInfo($appId, $path, $lang); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Returns the navigation |
|
| 589 | - * |
|
| 590 | - * @return array |
|
| 591 | - * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll() |
|
| 592 | - * |
|
| 593 | - * This function returns an array containing all entries added. The |
|
| 594 | - * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 595 | - * given for each app the following keys exist: |
|
| 596 | - * - active: boolean, signals if the user is on this navigation entry |
|
| 597 | - */ |
|
| 598 | - public static function getNavigation(): array { |
|
| 599 | - return OC::$server->getNavigationManager()->getAll(); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Returns the Settings Navigation |
|
| 604 | - * |
|
| 605 | - * @return string[] |
|
| 606 | - * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll('settings') |
|
| 607 | - * |
|
| 608 | - * This function returns an array containing all settings pages added. The |
|
| 609 | - * entries are sorted by the key 'order' ascending. |
|
| 610 | - */ |
|
| 611 | - public static function getSettingsNavigation(): array { |
|
| 612 | - return OC::$server->getNavigationManager()->getAll('settings'); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * get the id of loaded app |
|
| 617 | - * |
|
| 618 | - * @return string |
|
| 619 | - */ |
|
| 620 | - public static function getCurrentApp(): string { |
|
| 621 | - $request = \OC::$server->getRequest(); |
|
| 622 | - $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 623 | - $topFolder = substr($script, 0, strpos($script, '/') ?: 0); |
|
| 624 | - if (empty($topFolder)) { |
|
| 625 | - $path_info = $request->getPathInfo(); |
|
| 626 | - if ($path_info) { |
|
| 627 | - $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - if ($topFolder == 'apps') { |
|
| 631 | - $length = strlen($topFolder); |
|
| 632 | - return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1) ?: ''; |
|
| 633 | - } else { |
|
| 634 | - return $topFolder; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * @param string $type |
|
| 640 | - * @return array |
|
| 641 | - */ |
|
| 642 | - public static function getForms(string $type): array { |
|
| 643 | - $forms = []; |
|
| 644 | - switch ($type) { |
|
| 645 | - case 'admin': |
|
| 646 | - $source = self::$adminForms; |
|
| 647 | - break; |
|
| 648 | - case 'personal': |
|
| 649 | - $source = self::$personalForms; |
|
| 650 | - break; |
|
| 651 | - default: |
|
| 652 | - return []; |
|
| 653 | - } |
|
| 654 | - foreach ($source as $form) { |
|
| 655 | - $forms[] = include $form; |
|
| 656 | - } |
|
| 657 | - return $forms; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - /** |
|
| 661 | - * register an admin form to be shown |
|
| 662 | - * |
|
| 663 | - * @param string $app |
|
| 664 | - * @param string $page |
|
| 665 | - */ |
|
| 666 | - public static function registerAdmin(string $app, string $page) { |
|
| 667 | - self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * register a personal form to be shown |
|
| 672 | - * @param string $app |
|
| 673 | - * @param string $page |
|
| 674 | - */ |
|
| 675 | - public static function registerPersonal(string $app, string $page) { |
|
| 676 | - self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * @param array $entry |
|
| 681 | - */ |
|
| 682 | - public static function registerLogIn(array $entry) { |
|
| 683 | - self::$altLogin[] = $entry; |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * @return array |
|
| 688 | - */ |
|
| 689 | - public static function getAlternativeLogIns(): array { |
|
| 690 | - return self::$altLogin; |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - /** |
|
| 694 | - * get a list of all apps in the apps folder |
|
| 695 | - * |
|
| 696 | - * @return array an array of app names (string IDs) |
|
| 697 | - * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 698 | - */ |
|
| 699 | - public static function getAllApps(): array { |
|
| 700 | - |
|
| 701 | - $apps = []; |
|
| 702 | - |
|
| 703 | - foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 704 | - if (!is_readable($apps_dir['path'])) { |
|
| 705 | - \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 706 | - continue; |
|
| 707 | - } |
|
| 708 | - $dh = opendir($apps_dir['path']); |
|
| 709 | - |
|
| 710 | - if (is_resource($dh)) { |
|
| 711 | - while (($file = readdir($dh)) !== false) { |
|
| 712 | - |
|
| 713 | - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 714 | - |
|
| 715 | - $apps[] = $file; |
|
| 716 | - } |
|
| 717 | - } |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - $apps = array_unique($apps); |
|
| 722 | - |
|
| 723 | - return $apps; |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * List all apps, this is used in apps.php |
|
| 728 | - * |
|
| 729 | - * @return array |
|
| 730 | - */ |
|
| 731 | - public function listAllApps(): array { |
|
| 732 | - $installedApps = OC_App::getAllApps(); |
|
| 733 | - |
|
| 734 | - $appManager = \OC::$server->getAppManager(); |
|
| 735 | - //we don't want to show configuration for these |
|
| 736 | - $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 737 | - $appList = []; |
|
| 738 | - $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 739 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 740 | - |
|
| 741 | - foreach ($installedApps as $app) { |
|
| 742 | - if (array_search($app, $blacklist) === false) { |
|
| 743 | - |
|
| 744 | - $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 745 | - if (!is_array($info)) { |
|
| 746 | - \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 747 | - continue; |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - if (!isset($info['name'])) { |
|
| 751 | - \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 752 | - continue; |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no'); |
|
| 756 | - $info['groups'] = null; |
|
| 757 | - if ($enabled === 'yes') { |
|
| 758 | - $active = true; |
|
| 759 | - } else if ($enabled === 'no') { |
|
| 760 | - $active = false; |
|
| 761 | - } else { |
|
| 762 | - $active = true; |
|
| 763 | - $info['groups'] = $enabled; |
|
| 764 | - } |
|
| 765 | - |
|
| 766 | - $info['active'] = $active; |
|
| 767 | - |
|
| 768 | - if ($appManager->isShipped($app)) { |
|
| 769 | - $info['internal'] = true; |
|
| 770 | - $info['level'] = self::officialApp; |
|
| 771 | - $info['removable'] = false; |
|
| 772 | - } else { |
|
| 773 | - $info['internal'] = false; |
|
| 774 | - $info['removable'] = true; |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - $appPath = self::getAppPath($app); |
|
| 778 | - if($appPath !== false) { |
|
| 779 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 780 | - if (file_exists($appIcon)) { |
|
| 781 | - $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 782 | - $info['previewAsIcon'] = true; |
|
| 783 | - } else { |
|
| 784 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 785 | - if (file_exists($appIcon)) { |
|
| 786 | - $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 787 | - $info['previewAsIcon'] = true; |
|
| 788 | - } |
|
| 789 | - } |
|
| 790 | - } |
|
| 791 | - // fix documentation |
|
| 792 | - if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 793 | - foreach ($info['documentation'] as $key => $url) { |
|
| 794 | - // If it is not an absolute URL we assume it is a key |
|
| 795 | - // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 796 | - if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 797 | - $url = $urlGenerator->linkToDocs($url); |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - $info['documentation'][$key] = $url; |
|
| 801 | - } |
|
| 802 | - } |
|
| 803 | - |
|
| 804 | - $info['version'] = OC_App::getAppVersion($app); |
|
| 805 | - $appList[] = $info; |
|
| 806 | - } |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - return $appList; |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - public static function shouldUpgrade(string $app): bool { |
|
| 813 | - $versions = self::getAppVersions(); |
|
| 814 | - $currentVersion = OC_App::getAppVersion($app); |
|
| 815 | - if ($currentVersion && isset($versions[$app])) { |
|
| 816 | - $installedVersion = $versions[$app]; |
|
| 817 | - if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 818 | - return true; |
|
| 819 | - } |
|
| 820 | - } |
|
| 821 | - return false; |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * Adjust the number of version parts of $version1 to match |
|
| 826 | - * the number of version parts of $version2. |
|
| 827 | - * |
|
| 828 | - * @param string $version1 version to adjust |
|
| 829 | - * @param string $version2 version to take the number of parts from |
|
| 830 | - * @return string shortened $version1 |
|
| 831 | - */ |
|
| 832 | - private static function adjustVersionParts(string $version1, string $version2): string { |
|
| 833 | - $version1 = explode('.', $version1); |
|
| 834 | - $version2 = explode('.', $version2); |
|
| 835 | - // reduce $version1 to match the number of parts in $version2 |
|
| 836 | - while (count($version1) > count($version2)) { |
|
| 837 | - array_pop($version1); |
|
| 838 | - } |
|
| 839 | - // if $version1 does not have enough parts, add some |
|
| 840 | - while (count($version1) < count($version2)) { |
|
| 841 | - $version1[] = '0'; |
|
| 842 | - } |
|
| 843 | - return implode('.', $version1); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Check whether the current ownCloud version matches the given |
|
| 848 | - * application's version requirements. |
|
| 849 | - * |
|
| 850 | - * The comparison is made based on the number of parts that the |
|
| 851 | - * app info version has. For example for ownCloud 6.0.3 if the |
|
| 852 | - * app info version is expecting version 6.0, the comparison is |
|
| 853 | - * made on the first two parts of the ownCloud version. |
|
| 854 | - * This means that it's possible to specify "requiremin" => 6 |
|
| 855 | - * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 856 | - * |
|
| 857 | - * @param string $ocVersion ownCloud version to check against |
|
| 858 | - * @param array $appInfo app info (from xml) |
|
| 859 | - * |
|
| 860 | - * @return boolean true if compatible, otherwise false |
|
| 861 | - */ |
|
| 862 | - public static function isAppCompatible(string $ocVersion, array $appInfo): bool { |
|
| 863 | - $requireMin = ''; |
|
| 864 | - $requireMax = ''; |
|
| 865 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 866 | - $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 867 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 868 | - $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 869 | - } else if (isset($appInfo['requiremin'])) { |
|
| 870 | - $requireMin = $appInfo['requiremin']; |
|
| 871 | - } else if (isset($appInfo['require'])) { |
|
| 872 | - $requireMin = $appInfo['require']; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 876 | - $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 877 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 878 | - $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 879 | - } else if (isset($appInfo['requiremax'])) { |
|
| 880 | - $requireMax = $appInfo['requiremax']; |
|
| 881 | - } |
|
| 882 | - |
|
| 883 | - if (!empty($requireMin) |
|
| 884 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 885 | - ) { |
|
| 886 | - |
|
| 887 | - return false; |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - if (!empty($requireMax) |
|
| 891 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 892 | - ) { |
|
| 893 | - return false; |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - return true; |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - /** |
|
| 900 | - * get the installed version of all apps |
|
| 901 | - */ |
|
| 902 | - public static function getAppVersions() { |
|
| 903 | - static $versions; |
|
| 904 | - |
|
| 905 | - if(!$versions) { |
|
| 906 | - $appConfig = \OC::$server->getAppConfig(); |
|
| 907 | - $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 908 | - } |
|
| 909 | - return $versions; |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - /** |
|
| 913 | - * update the database for the app and call the update script |
|
| 914 | - * |
|
| 915 | - * @param string $appId |
|
| 916 | - * @return bool |
|
| 917 | - */ |
|
| 918 | - public static function updateApp(string $appId): bool { |
|
| 919 | - $appPath = self::getAppPath($appId); |
|
| 920 | - if($appPath === false) { |
|
| 921 | - return false; |
|
| 922 | - } |
|
| 923 | - self::registerAutoloading($appId, $appPath); |
|
| 924 | - |
|
| 925 | - $appData = self::getAppInfo($appId); |
|
| 926 | - self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 927 | - |
|
| 928 | - if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 929 | - OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 930 | - } else { |
|
| 931 | - $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
|
| 932 | - $ms->migrate(); |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 936 | - self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 937 | - // update appversion in app manager |
|
| 938 | - \OC::$server->getAppManager()->getAppVersion($appId, false); |
|
| 939 | - |
|
| 940 | - // run upgrade code |
|
| 941 | - if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 942 | - self::loadApp($appId); |
|
| 943 | - include $appPath . '/appinfo/update.php'; |
|
| 944 | - } |
|
| 945 | - self::setupBackgroundJobs($appData['background-jobs']); |
|
| 946 | - |
|
| 947 | - //set remote/public handlers |
|
| 948 | - if (array_key_exists('ocsid', $appData)) { |
|
| 949 | - \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 950 | - } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 951 | - \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 952 | - } |
|
| 953 | - foreach ($appData['remote'] as $name => $path) { |
|
| 954 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 955 | - } |
|
| 956 | - foreach ($appData['public'] as $name => $path) { |
|
| 957 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 958 | - } |
|
| 959 | - |
|
| 960 | - self::setAppTypes($appId); |
|
| 961 | - |
|
| 962 | - $version = \OC_App::getAppVersion($appId); |
|
| 963 | - \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version); |
|
| 964 | - |
|
| 965 | - \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 966 | - ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 967 | - )); |
|
| 968 | - |
|
| 969 | - return true; |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - /** |
|
| 973 | - * @param string $appId |
|
| 974 | - * @param string[] $steps |
|
| 975 | - * @throws \OC\NeedsUpdateException |
|
| 976 | - */ |
|
| 977 | - public static function executeRepairSteps(string $appId, array $steps) { |
|
| 978 | - if (empty($steps)) { |
|
| 979 | - return; |
|
| 980 | - } |
|
| 981 | - // load the app |
|
| 982 | - self::loadApp($appId); |
|
| 983 | - |
|
| 984 | - $dispatcher = OC::$server->getEventDispatcher(); |
|
| 985 | - |
|
| 986 | - // load the steps |
|
| 987 | - $r = new Repair([], $dispatcher); |
|
| 988 | - foreach ($steps as $step) { |
|
| 989 | - try { |
|
| 990 | - $r->addStep($step); |
|
| 991 | - } catch (Exception $ex) { |
|
| 992 | - $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 993 | - \OC::$server->getLogger()->logException($ex); |
|
| 994 | - } |
|
| 995 | - } |
|
| 996 | - // run the steps |
|
| 997 | - $r->run(); |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - public static function setupBackgroundJobs(array $jobs) { |
|
| 1001 | - $queue = \OC::$server->getJobList(); |
|
| 1002 | - foreach ($jobs as $job) { |
|
| 1003 | - $queue->add($job); |
|
| 1004 | - } |
|
| 1005 | - } |
|
| 1006 | - |
|
| 1007 | - /** |
|
| 1008 | - * @param string $appId |
|
| 1009 | - * @param string[] $steps |
|
| 1010 | - */ |
|
| 1011 | - private static function setupLiveMigrations(string $appId, array $steps) { |
|
| 1012 | - $queue = \OC::$server->getJobList(); |
|
| 1013 | - foreach ($steps as $step) { |
|
| 1014 | - $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1015 | - 'app' => $appId, |
|
| 1016 | - 'step' => $step]); |
|
| 1017 | - } |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - /** |
|
| 1021 | - * @param string $appId |
|
| 1022 | - * @return \OC\Files\View|false |
|
| 1023 | - */ |
|
| 1024 | - public static function getStorage(string $appId) { |
|
| 1025 | - if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check |
|
| 1026 | - if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1027 | - $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1028 | - if (!$view->file_exists($appId)) { |
|
| 1029 | - $view->mkdir($appId); |
|
| 1030 | - } |
|
| 1031 | - return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1032 | - } else { |
|
| 1033 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1034 | - return false; |
|
| 1035 | - } |
|
| 1036 | - } else { |
|
| 1037 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1038 | - return false; |
|
| 1039 | - } |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - protected static function findBestL10NOption(array $options, string $lang): string { |
|
| 1043 | - $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1044 | - |
|
| 1045 | - $lang = strtolower($lang); |
|
| 1046 | - $similarLang = $lang; |
|
| 1047 | - if (strpos($similarLang, '_')) { |
|
| 1048 | - // For "de_DE" we want to find "de" and the other way around |
|
| 1049 | - $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - foreach ($options as $option) { |
|
| 1053 | - if (is_array($option)) { |
|
| 1054 | - if ($fallback === false) { |
|
| 1055 | - $fallback = $option['@value']; |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - if (!isset($option['@attributes']['lang'])) { |
|
| 1059 | - continue; |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1063 | - if ($attributeLang === $lang) { |
|
| 1064 | - return $option['@value']; |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - if ($attributeLang === $similarLang) { |
|
| 1068 | - $similarLangFallback = $option['@value']; |
|
| 1069 | - } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1070 | - if ($similarLangFallback === false) { |
|
| 1071 | - $similarLangFallback = $option['@value']; |
|
| 1072 | - } |
|
| 1073 | - } |
|
| 1074 | - } else { |
|
| 1075 | - $englishFallback = $option; |
|
| 1076 | - } |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - if ($similarLangFallback !== false) { |
|
| 1080 | - return $similarLangFallback; |
|
| 1081 | - } else if ($englishFallback !== false) { |
|
| 1082 | - return $englishFallback; |
|
| 1083 | - } |
|
| 1084 | - return (string) $fallback; |
|
| 1085 | - } |
|
| 1086 | - |
|
| 1087 | - /** |
|
| 1088 | - * parses the app data array and enhanced the 'description' value |
|
| 1089 | - * |
|
| 1090 | - * @param array $data the app data |
|
| 1091 | - * @param string $lang |
|
| 1092 | - * @return array improved app data |
|
| 1093 | - */ |
|
| 1094 | - public static function parseAppInfo(array $data, $lang = null): array { |
|
| 1095 | - |
|
| 1096 | - if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1097 | - $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1098 | - } |
|
| 1099 | - if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1100 | - $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1101 | - } |
|
| 1102 | - if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1103 | - $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1104 | - } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1105 | - $data['description'] = trim($data['description']); |
|
| 1106 | - } else { |
|
| 1107 | - $data['description'] = ''; |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - return $data; |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - /** |
|
| 1114 | - * @param \OCP\IConfig $config |
|
| 1115 | - * @param \OCP\IL10N $l |
|
| 1116 | - * @param array $info |
|
| 1117 | - * @throws \Exception |
|
| 1118 | - */ |
|
| 1119 | - public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info) { |
|
| 1120 | - $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1121 | - $missing = $dependencyAnalyzer->analyze($info); |
|
| 1122 | - if (!empty($missing)) { |
|
| 1123 | - $missingMsg = implode(PHP_EOL, $missing); |
|
| 1124 | - throw new \Exception( |
|
| 1125 | - $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1126 | - [$info['name'], $missingMsg] |
|
| 1127 | - ) |
|
| 1128 | - ); |
|
| 1129 | - } |
|
| 1130 | - } |
|
| 66 | + static private $adminForms = []; |
|
| 67 | + static private $personalForms = []; |
|
| 68 | + static private $appTypes = []; |
|
| 69 | + static private $loadedApps = []; |
|
| 70 | + static private $altLogin = []; |
|
| 71 | + static private $alreadyRegistered = []; |
|
| 72 | + const officialApp = 200; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * clean the appId |
|
| 76 | + * |
|
| 77 | + * @param string $app AppId that needs to be cleaned |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public static function cleanAppId(string $app): string { |
|
| 81 | + return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Check if an app is loaded |
|
| 86 | + * |
|
| 87 | + * @param string $app |
|
| 88 | + * @return bool |
|
| 89 | + */ |
|
| 90 | + public static function isAppLoaded(string $app): bool { |
|
| 91 | + return in_array($app, self::$loadedApps, true); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * loads all apps |
|
| 96 | + * |
|
| 97 | + * @param string[] $types |
|
| 98 | + * @return bool |
|
| 99 | + * |
|
| 100 | + * This function walks through the ownCloud directory and loads all apps |
|
| 101 | + * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 102 | + * exists. |
|
| 103 | + * |
|
| 104 | + * if $types is set to non-empty array, only apps of those types will be loaded |
|
| 105 | + */ |
|
| 106 | + public static function loadApps(array $types = []): bool { |
|
| 107 | + if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + // Load the enabled apps here |
|
| 111 | + $apps = self::getEnabledApps(); |
|
| 112 | + |
|
| 113 | + // Add each apps' folder as allowed class path |
|
| 114 | + foreach($apps as $app) { |
|
| 115 | + $path = self::getAppPath($app); |
|
| 116 | + if($path !== false) { |
|
| 117 | + self::registerAutoloading($app, $path); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // prevent app.php from printing output |
|
| 122 | + ob_start(); |
|
| 123 | + foreach ($apps as $app) { |
|
| 124 | + if (($types === [] or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 125 | + self::loadApp($app); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + ob_end_clean(); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * load a single app |
|
| 135 | + * |
|
| 136 | + * @param string $app |
|
| 137 | + * @throws Exception |
|
| 138 | + */ |
|
| 139 | + public static function loadApp(string $app) { |
|
| 140 | + self::$loadedApps[] = $app; |
|
| 141 | + $appPath = self::getAppPath($app); |
|
| 142 | + if($appPath === false) { |
|
| 143 | + return; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // in case someone calls loadApp() directly |
|
| 147 | + self::registerAutoloading($app, $appPath); |
|
| 148 | + |
|
| 149 | + if (is_file($appPath . '/appinfo/app.php')) { |
|
| 150 | + \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 151 | + try { |
|
| 152 | + self::requireAppFile($app); |
|
| 153 | + } catch (Error $ex) { |
|
| 154 | + \OC::$server->getLogger()->logException($ex); |
|
| 155 | + if (!\OC::$server->getAppManager()->isShipped($app)) { |
|
| 156 | + // Only disable apps which are not shipped |
|
| 157 | + self::disable($app); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + if (self::isType($app, array('authentication'))) { |
|
| 161 | + // since authentication apps affect the "is app enabled for group" check, |
|
| 162 | + // the enabled apps cache needs to be cleared to make sure that the |
|
| 163 | + // next time getEnableApps() is called it will also include apps that were |
|
| 164 | + // enabled for groups |
|
| 165 | + self::$enabledAppsCache = []; |
|
| 166 | + } |
|
| 167 | + \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $info = self::getAppInfo($app); |
|
| 171 | + if (!empty($info['activity']['filters'])) { |
|
| 172 | + foreach ($info['activity']['filters'] as $filter) { |
|
| 173 | + \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + if (!empty($info['activity']['settings'])) { |
|
| 177 | + foreach ($info['activity']['settings'] as $setting) { |
|
| 178 | + \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + if (!empty($info['activity']['providers'])) { |
|
| 182 | + foreach ($info['activity']['providers'] as $provider) { |
|
| 183 | + \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + if (!empty($info['settings']['admin'])) { |
|
| 188 | + foreach ($info['settings']['admin'] as $setting) { |
|
| 189 | + \OC::$server->getSettingsManager()->registerSetting('admin', $setting); |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + if (!empty($info['settings']['admin-section'])) { |
|
| 193 | + foreach ($info['settings']['admin-section'] as $section) { |
|
| 194 | + \OC::$server->getSettingsManager()->registerSection('admin', $section); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + if (!empty($info['settings']['personal'])) { |
|
| 198 | + foreach ($info['settings']['personal'] as $setting) { |
|
| 199 | + \OC::$server->getSettingsManager()->registerSetting('personal', $setting); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + if (!empty($info['settings']['personal-section'])) { |
|
| 203 | + foreach ($info['settings']['personal-section'] as $section) { |
|
| 204 | + \OC::$server->getSettingsManager()->registerSection('personal', $section); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if (!empty($info['collaboration']['plugins'])) { |
|
| 209 | + // deal with one or many plugin entries |
|
| 210 | + $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ? |
|
| 211 | + [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin']; |
|
| 212 | + foreach ($plugins as $plugin) { |
|
| 213 | + if($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 214 | + $pluginInfo = [ |
|
| 215 | + 'shareType' => $plugin['@attributes']['share-type'], |
|
| 216 | + 'class' => $plugin['@value'], |
|
| 217 | + ]; |
|
| 218 | + \OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo); |
|
| 219 | + } else if ($plugin['@attributes']['type'] === 'autocomplete-sort') { |
|
| 220 | + \OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']); |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @internal |
|
| 228 | + * @param string $app |
|
| 229 | + * @param string $path |
|
| 230 | + */ |
|
| 231 | + public static function registerAutoloading(string $app, string $path) { |
|
| 232 | + $key = $app . '-' . $path; |
|
| 233 | + if(isset(self::$alreadyRegistered[$key])) { |
|
| 234 | + return; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + self::$alreadyRegistered[$key] = true; |
|
| 238 | + |
|
| 239 | + // Register on PSR-4 composer autoloader |
|
| 240 | + $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 241 | + \OC::$server->registerNamespace($app, $appNamespace); |
|
| 242 | + |
|
| 243 | + if (file_exists($path . '/composer/autoload.php')) { |
|
| 244 | + require_once $path . '/composer/autoload.php'; |
|
| 245 | + } else { |
|
| 246 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 247 | + // Register on legacy autoloader |
|
| 248 | + \OC::$loader->addValidRoot($path); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + // Register Test namespace only when testing |
|
| 252 | + if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 253 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Load app.php from the given app |
|
| 259 | + * |
|
| 260 | + * @param string $app app name |
|
| 261 | + * @throws Error |
|
| 262 | + */ |
|
| 263 | + private static function requireAppFile(string $app) { |
|
| 264 | + // encapsulated here to avoid variable scope conflicts |
|
| 265 | + require_once $app . '/appinfo/app.php'; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * check if an app is of a specific type |
|
| 270 | + * |
|
| 271 | + * @param string $app |
|
| 272 | + * @param array $types |
|
| 273 | + * @return bool |
|
| 274 | + */ |
|
| 275 | + public static function isType(string $app, array $types): bool { |
|
| 276 | + $appTypes = self::getAppTypes($app); |
|
| 277 | + foreach ($types as $type) { |
|
| 278 | + if (array_search($type, $appTypes) !== false) { |
|
| 279 | + return true; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + return false; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * get the types of an app |
|
| 287 | + * |
|
| 288 | + * @param string $app |
|
| 289 | + * @return array |
|
| 290 | + */ |
|
| 291 | + private static function getAppTypes(string $app): array { |
|
| 292 | + //load the cache |
|
| 293 | + if (count(self::$appTypes) == 0) { |
|
| 294 | + self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + if (isset(self::$appTypes[$app])) { |
|
| 298 | + return explode(',', self::$appTypes[$app]); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return []; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * read app types from info.xml and cache them in the database |
|
| 306 | + */ |
|
| 307 | + public static function setAppTypes(string $app) { |
|
| 308 | + $appManager = \OC::$server->getAppManager(); |
|
| 309 | + $appData = $appManager->getAppInfo($app); |
|
| 310 | + if(!is_array($appData)) { |
|
| 311 | + return; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + if (isset($appData['types'])) { |
|
| 315 | + $appTypes = implode(',', $appData['types']); |
|
| 316 | + } else { |
|
| 317 | + $appTypes = ''; |
|
| 318 | + $appData['types'] = []; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $config = \OC::$server->getConfig(); |
|
| 322 | + $config->setAppValue($app, 'types', $appTypes); |
|
| 323 | + |
|
| 324 | + if ($appManager->hasProtectedAppType($appData['types'])) { |
|
| 325 | + $enabled = $config->getAppValue($app, 'enabled', 'yes'); |
|
| 326 | + if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 327 | + $config->setAppValue($app, 'enabled', 'yes'); |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * get all enabled apps |
|
| 334 | + */ |
|
| 335 | + protected static $enabledAppsCache = []; |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Returns apps enabled for the current user. |
|
| 339 | + * |
|
| 340 | + * @param bool $forceRefresh whether to refresh the cache |
|
| 341 | + * @param bool $all whether to return apps for all users, not only the |
|
| 342 | + * currently logged in one |
|
| 343 | + * @return string[] |
|
| 344 | + */ |
|
| 345 | + public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array { |
|
| 346 | + if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 347 | + return []; |
|
| 348 | + } |
|
| 349 | + // in incognito mode or when logged out, $user will be false, |
|
| 350 | + // which is also the case during an upgrade |
|
| 351 | + $appManager = \OC::$server->getAppManager(); |
|
| 352 | + if ($all) { |
|
| 353 | + $user = null; |
|
| 354 | + } else { |
|
| 355 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + if (is_null($user)) { |
|
| 359 | + $apps = $appManager->getInstalledApps(); |
|
| 360 | + } else { |
|
| 361 | + $apps = $appManager->getEnabledAppsForUser($user); |
|
| 362 | + } |
|
| 363 | + $apps = array_filter($apps, function ($app) { |
|
| 364 | + return $app !== 'files';//we add this manually |
|
| 365 | + }); |
|
| 366 | + sort($apps); |
|
| 367 | + array_unshift($apps, 'files'); |
|
| 368 | + return $apps; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * checks whether or not an app is enabled |
|
| 373 | + * |
|
| 374 | + * @param string $app app |
|
| 375 | + * @return bool |
|
| 376 | + * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId) |
|
| 377 | + * |
|
| 378 | + * This function checks whether or not an app is enabled. |
|
| 379 | + */ |
|
| 380 | + public static function isEnabled(string $app): bool { |
|
| 381 | + return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * enables an app |
|
| 386 | + * |
|
| 387 | + * @param string $appId |
|
| 388 | + * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 389 | + * @throws \Exception |
|
| 390 | + * @return void |
|
| 391 | + * |
|
| 392 | + * This function set an app as enabled in appconfig. |
|
| 393 | + */ |
|
| 394 | + public function enable(string $appId, |
|
| 395 | + array $groups = []) { |
|
| 396 | + self::$enabledAppsCache = []; // flush |
|
| 397 | + |
|
| 398 | + // Check if app is already downloaded |
|
| 399 | + $installer = \OC::$server->query(Installer::class); |
|
| 400 | + $isDownloaded = $installer->isDownloaded($appId); |
|
| 401 | + |
|
| 402 | + if(!$isDownloaded) { |
|
| 403 | + $installer->downloadApp($appId); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + $installer->installApp($appId); |
|
| 407 | + |
|
| 408 | + $appManager = \OC::$server->getAppManager(); |
|
| 409 | + if ($groups !== []) { |
|
| 410 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 411 | + $groupsList = []; |
|
| 412 | + foreach ($groups as $group) { |
|
| 413 | + $groupItem = $groupManager->get($group); |
|
| 414 | + if ($groupItem instanceof \OCP\IGroup) { |
|
| 415 | + $groupsList[] = $groupManager->get($group); |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + $appManager->enableAppForGroups($appId, $groupsList); |
|
| 419 | + } else { |
|
| 420 | + $appManager->enableApp($appId); |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * This function set an app as disabled in appconfig. |
|
| 426 | + * |
|
| 427 | + * @param string $app app |
|
| 428 | + * @throws Exception |
|
| 429 | + */ |
|
| 430 | + public static function disable(string $app) { |
|
| 431 | + // flush |
|
| 432 | + self::$enabledAppsCache = []; |
|
| 433 | + |
|
| 434 | + // run uninstall steps |
|
| 435 | + $appData = OC_App::getAppInfo($app); |
|
| 436 | + if (!is_null($appData)) { |
|
| 437 | + OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + // emit disable hook - needed anymore ? |
|
| 441 | + \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 442 | + |
|
| 443 | + // finally disable it |
|
| 444 | + $appManager = \OC::$server->getAppManager(); |
|
| 445 | + $appManager->disableApp($app); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * Get the path where to install apps |
|
| 450 | + * |
|
| 451 | + * @return string|false |
|
| 452 | + */ |
|
| 453 | + public static function getInstallPath() { |
|
| 454 | + if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 455 | + return false; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 459 | + if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 460 | + return $dir['path']; |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 465 | + return null; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * search for an app in all app-directories |
|
| 471 | + * |
|
| 472 | + * @param string $appId |
|
| 473 | + * @return false|string |
|
| 474 | + */ |
|
| 475 | + public static function findAppInDirectories(string $appId) { |
|
| 476 | + $sanitizedAppId = self::cleanAppId($appId); |
|
| 477 | + if($sanitizedAppId !== $appId) { |
|
| 478 | + return false; |
|
| 479 | + } |
|
| 480 | + static $app_dir = []; |
|
| 481 | + |
|
| 482 | + if (isset($app_dir[$appId])) { |
|
| 483 | + return $app_dir[$appId]; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + $possibleApps = []; |
|
| 487 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 488 | + if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 489 | + $possibleApps[] = $dir; |
|
| 490 | + } |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + if (empty($possibleApps)) { |
|
| 494 | + return false; |
|
| 495 | + } elseif (count($possibleApps) === 1) { |
|
| 496 | + $dir = array_shift($possibleApps); |
|
| 497 | + $app_dir[$appId] = $dir; |
|
| 498 | + return $dir; |
|
| 499 | + } else { |
|
| 500 | + $versionToLoad = []; |
|
| 501 | + foreach ($possibleApps as $possibleApp) { |
|
| 502 | + $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 503 | + if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 504 | + $versionToLoad = array( |
|
| 505 | + 'dir' => $possibleApp, |
|
| 506 | + 'version' => $version, |
|
| 507 | + ); |
|
| 508 | + } |
|
| 509 | + } |
|
| 510 | + $app_dir[$appId] = $versionToLoad['dir']; |
|
| 511 | + return $versionToLoad['dir']; |
|
| 512 | + //TODO - write test |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Get the directory for the given app. |
|
| 518 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 519 | + * |
|
| 520 | + * @param string $appId |
|
| 521 | + * @return string|false |
|
| 522 | + */ |
|
| 523 | + public static function getAppPath(string $appId) { |
|
| 524 | + if ($appId === null || trim($appId) === '') { |
|
| 525 | + return false; |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 529 | + return $dir['path'] . '/' . $appId; |
|
| 530 | + } |
|
| 531 | + return false; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * Get the path for the given app on the access |
|
| 536 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 537 | + * |
|
| 538 | + * @param string $appId |
|
| 539 | + * @return string|false |
|
| 540 | + */ |
|
| 541 | + public static function getAppWebPath(string $appId) { |
|
| 542 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 543 | + return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 544 | + } |
|
| 545 | + return false; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * get the last version of the app from appinfo/info.xml |
|
| 550 | + * |
|
| 551 | + * @param string $appId |
|
| 552 | + * @param bool $useCache |
|
| 553 | + * @return string |
|
| 554 | + * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion() |
|
| 555 | + */ |
|
| 556 | + public static function getAppVersion(string $appId, bool $useCache = true): string { |
|
| 557 | + return \OC::$server->getAppManager()->getAppVersion($appId, $useCache); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * get app's version based on it's path |
|
| 562 | + * |
|
| 563 | + * @param string $path |
|
| 564 | + * @return string |
|
| 565 | + */ |
|
| 566 | + public static function getAppVersionByPath(string $path): string { |
|
| 567 | + $infoFile = $path . '/appinfo/info.xml'; |
|
| 568 | + $appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true); |
|
| 569 | + return isset($appData['version']) ? $appData['version'] : ''; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + |
|
| 573 | + /** |
|
| 574 | + * Read all app metadata from the info.xml file |
|
| 575 | + * |
|
| 576 | + * @param string $appId id of the app or the path of the info.xml file |
|
| 577 | + * @param bool $path |
|
| 578 | + * @param string $lang |
|
| 579 | + * @return array|null |
|
| 580 | + * @note all data is read from info.xml, not just pre-defined fields |
|
| 581 | + * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppInfo() |
|
| 582 | + */ |
|
| 583 | + public static function getAppInfo(string $appId, bool $path = false, string $lang = null) { |
|
| 584 | + return \OC::$server->getAppManager()->getAppInfo($appId, $path, $lang); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Returns the navigation |
|
| 589 | + * |
|
| 590 | + * @return array |
|
| 591 | + * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll() |
|
| 592 | + * |
|
| 593 | + * This function returns an array containing all entries added. The |
|
| 594 | + * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 595 | + * given for each app the following keys exist: |
|
| 596 | + * - active: boolean, signals if the user is on this navigation entry |
|
| 597 | + */ |
|
| 598 | + public static function getNavigation(): array { |
|
| 599 | + return OC::$server->getNavigationManager()->getAll(); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Returns the Settings Navigation |
|
| 604 | + * |
|
| 605 | + * @return string[] |
|
| 606 | + * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll('settings') |
|
| 607 | + * |
|
| 608 | + * This function returns an array containing all settings pages added. The |
|
| 609 | + * entries are sorted by the key 'order' ascending. |
|
| 610 | + */ |
|
| 611 | + public static function getSettingsNavigation(): array { |
|
| 612 | + return OC::$server->getNavigationManager()->getAll('settings'); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * get the id of loaded app |
|
| 617 | + * |
|
| 618 | + * @return string |
|
| 619 | + */ |
|
| 620 | + public static function getCurrentApp(): string { |
|
| 621 | + $request = \OC::$server->getRequest(); |
|
| 622 | + $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 623 | + $topFolder = substr($script, 0, strpos($script, '/') ?: 0); |
|
| 624 | + if (empty($topFolder)) { |
|
| 625 | + $path_info = $request->getPathInfo(); |
|
| 626 | + if ($path_info) { |
|
| 627 | + $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + if ($topFolder == 'apps') { |
|
| 631 | + $length = strlen($topFolder); |
|
| 632 | + return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1) ?: ''; |
|
| 633 | + } else { |
|
| 634 | + return $topFolder; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * @param string $type |
|
| 640 | + * @return array |
|
| 641 | + */ |
|
| 642 | + public static function getForms(string $type): array { |
|
| 643 | + $forms = []; |
|
| 644 | + switch ($type) { |
|
| 645 | + case 'admin': |
|
| 646 | + $source = self::$adminForms; |
|
| 647 | + break; |
|
| 648 | + case 'personal': |
|
| 649 | + $source = self::$personalForms; |
|
| 650 | + break; |
|
| 651 | + default: |
|
| 652 | + return []; |
|
| 653 | + } |
|
| 654 | + foreach ($source as $form) { |
|
| 655 | + $forms[] = include $form; |
|
| 656 | + } |
|
| 657 | + return $forms; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + /** |
|
| 661 | + * register an admin form to be shown |
|
| 662 | + * |
|
| 663 | + * @param string $app |
|
| 664 | + * @param string $page |
|
| 665 | + */ |
|
| 666 | + public static function registerAdmin(string $app, string $page) { |
|
| 667 | + self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * register a personal form to be shown |
|
| 672 | + * @param string $app |
|
| 673 | + * @param string $page |
|
| 674 | + */ |
|
| 675 | + public static function registerPersonal(string $app, string $page) { |
|
| 676 | + self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * @param array $entry |
|
| 681 | + */ |
|
| 682 | + public static function registerLogIn(array $entry) { |
|
| 683 | + self::$altLogin[] = $entry; |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * @return array |
|
| 688 | + */ |
|
| 689 | + public static function getAlternativeLogIns(): array { |
|
| 690 | + return self::$altLogin; |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + /** |
|
| 694 | + * get a list of all apps in the apps folder |
|
| 695 | + * |
|
| 696 | + * @return array an array of app names (string IDs) |
|
| 697 | + * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 698 | + */ |
|
| 699 | + public static function getAllApps(): array { |
|
| 700 | + |
|
| 701 | + $apps = []; |
|
| 702 | + |
|
| 703 | + foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 704 | + if (!is_readable($apps_dir['path'])) { |
|
| 705 | + \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 706 | + continue; |
|
| 707 | + } |
|
| 708 | + $dh = opendir($apps_dir['path']); |
|
| 709 | + |
|
| 710 | + if (is_resource($dh)) { |
|
| 711 | + while (($file = readdir($dh)) !== false) { |
|
| 712 | + |
|
| 713 | + if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 714 | + |
|
| 715 | + $apps[] = $file; |
|
| 716 | + } |
|
| 717 | + } |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + $apps = array_unique($apps); |
|
| 722 | + |
|
| 723 | + return $apps; |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * List all apps, this is used in apps.php |
|
| 728 | + * |
|
| 729 | + * @return array |
|
| 730 | + */ |
|
| 731 | + public function listAllApps(): array { |
|
| 732 | + $installedApps = OC_App::getAllApps(); |
|
| 733 | + |
|
| 734 | + $appManager = \OC::$server->getAppManager(); |
|
| 735 | + //we don't want to show configuration for these |
|
| 736 | + $blacklist = $appManager->getAlwaysEnabledApps(); |
|
| 737 | + $appList = []; |
|
| 738 | + $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 739 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 740 | + |
|
| 741 | + foreach ($installedApps as $app) { |
|
| 742 | + if (array_search($app, $blacklist) === false) { |
|
| 743 | + |
|
| 744 | + $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 745 | + if (!is_array($info)) { |
|
| 746 | + \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 747 | + continue; |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + if (!isset($info['name'])) { |
|
| 751 | + \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 752 | + continue; |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no'); |
|
| 756 | + $info['groups'] = null; |
|
| 757 | + if ($enabled === 'yes') { |
|
| 758 | + $active = true; |
|
| 759 | + } else if ($enabled === 'no') { |
|
| 760 | + $active = false; |
|
| 761 | + } else { |
|
| 762 | + $active = true; |
|
| 763 | + $info['groups'] = $enabled; |
|
| 764 | + } |
|
| 765 | + |
|
| 766 | + $info['active'] = $active; |
|
| 767 | + |
|
| 768 | + if ($appManager->isShipped($app)) { |
|
| 769 | + $info['internal'] = true; |
|
| 770 | + $info['level'] = self::officialApp; |
|
| 771 | + $info['removable'] = false; |
|
| 772 | + } else { |
|
| 773 | + $info['internal'] = false; |
|
| 774 | + $info['removable'] = true; |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + $appPath = self::getAppPath($app); |
|
| 778 | + if($appPath !== false) { |
|
| 779 | + $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 780 | + if (file_exists($appIcon)) { |
|
| 781 | + $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 782 | + $info['previewAsIcon'] = true; |
|
| 783 | + } else { |
|
| 784 | + $appIcon = $appPath . '/img/app.svg'; |
|
| 785 | + if (file_exists($appIcon)) { |
|
| 786 | + $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
|
| 787 | + $info['previewAsIcon'] = true; |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | + } |
|
| 791 | + // fix documentation |
|
| 792 | + if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 793 | + foreach ($info['documentation'] as $key => $url) { |
|
| 794 | + // If it is not an absolute URL we assume it is a key |
|
| 795 | + // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 796 | + if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 797 | + $url = $urlGenerator->linkToDocs($url); |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + $info['documentation'][$key] = $url; |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | + |
|
| 804 | + $info['version'] = OC_App::getAppVersion($app); |
|
| 805 | + $appList[] = $info; |
|
| 806 | + } |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + return $appList; |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + public static function shouldUpgrade(string $app): bool { |
|
| 813 | + $versions = self::getAppVersions(); |
|
| 814 | + $currentVersion = OC_App::getAppVersion($app); |
|
| 815 | + if ($currentVersion && isset($versions[$app])) { |
|
| 816 | + $installedVersion = $versions[$app]; |
|
| 817 | + if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 818 | + return true; |
|
| 819 | + } |
|
| 820 | + } |
|
| 821 | + return false; |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Adjust the number of version parts of $version1 to match |
|
| 826 | + * the number of version parts of $version2. |
|
| 827 | + * |
|
| 828 | + * @param string $version1 version to adjust |
|
| 829 | + * @param string $version2 version to take the number of parts from |
|
| 830 | + * @return string shortened $version1 |
|
| 831 | + */ |
|
| 832 | + private static function adjustVersionParts(string $version1, string $version2): string { |
|
| 833 | + $version1 = explode('.', $version1); |
|
| 834 | + $version2 = explode('.', $version2); |
|
| 835 | + // reduce $version1 to match the number of parts in $version2 |
|
| 836 | + while (count($version1) > count($version2)) { |
|
| 837 | + array_pop($version1); |
|
| 838 | + } |
|
| 839 | + // if $version1 does not have enough parts, add some |
|
| 840 | + while (count($version1) < count($version2)) { |
|
| 841 | + $version1[] = '0'; |
|
| 842 | + } |
|
| 843 | + return implode('.', $version1); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Check whether the current ownCloud version matches the given |
|
| 848 | + * application's version requirements. |
|
| 849 | + * |
|
| 850 | + * The comparison is made based on the number of parts that the |
|
| 851 | + * app info version has. For example for ownCloud 6.0.3 if the |
|
| 852 | + * app info version is expecting version 6.0, the comparison is |
|
| 853 | + * made on the first two parts of the ownCloud version. |
|
| 854 | + * This means that it's possible to specify "requiremin" => 6 |
|
| 855 | + * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 856 | + * |
|
| 857 | + * @param string $ocVersion ownCloud version to check against |
|
| 858 | + * @param array $appInfo app info (from xml) |
|
| 859 | + * |
|
| 860 | + * @return boolean true if compatible, otherwise false |
|
| 861 | + */ |
|
| 862 | + public static function isAppCompatible(string $ocVersion, array $appInfo): bool { |
|
| 863 | + $requireMin = ''; |
|
| 864 | + $requireMax = ''; |
|
| 865 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 866 | + $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 867 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 868 | + $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 869 | + } else if (isset($appInfo['requiremin'])) { |
|
| 870 | + $requireMin = $appInfo['requiremin']; |
|
| 871 | + } else if (isset($appInfo['require'])) { |
|
| 872 | + $requireMin = $appInfo['require']; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 876 | + $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 877 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 878 | + $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 879 | + } else if (isset($appInfo['requiremax'])) { |
|
| 880 | + $requireMax = $appInfo['requiremax']; |
|
| 881 | + } |
|
| 882 | + |
|
| 883 | + if (!empty($requireMin) |
|
| 884 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 885 | + ) { |
|
| 886 | + |
|
| 887 | + return false; |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + if (!empty($requireMax) |
|
| 891 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 892 | + ) { |
|
| 893 | + return false; |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + return true; |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + /** |
|
| 900 | + * get the installed version of all apps |
|
| 901 | + */ |
|
| 902 | + public static function getAppVersions() { |
|
| 903 | + static $versions; |
|
| 904 | + |
|
| 905 | + if(!$versions) { |
|
| 906 | + $appConfig = \OC::$server->getAppConfig(); |
|
| 907 | + $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 908 | + } |
|
| 909 | + return $versions; |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + /** |
|
| 913 | + * update the database for the app and call the update script |
|
| 914 | + * |
|
| 915 | + * @param string $appId |
|
| 916 | + * @return bool |
|
| 917 | + */ |
|
| 918 | + public static function updateApp(string $appId): bool { |
|
| 919 | + $appPath = self::getAppPath($appId); |
|
| 920 | + if($appPath === false) { |
|
| 921 | + return false; |
|
| 922 | + } |
|
| 923 | + self::registerAutoloading($appId, $appPath); |
|
| 924 | + |
|
| 925 | + $appData = self::getAppInfo($appId); |
|
| 926 | + self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 927 | + |
|
| 928 | + if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 929 | + OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 930 | + } else { |
|
| 931 | + $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
|
| 932 | + $ms->migrate(); |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 936 | + self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 937 | + // update appversion in app manager |
|
| 938 | + \OC::$server->getAppManager()->getAppVersion($appId, false); |
|
| 939 | + |
|
| 940 | + // run upgrade code |
|
| 941 | + if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 942 | + self::loadApp($appId); |
|
| 943 | + include $appPath . '/appinfo/update.php'; |
|
| 944 | + } |
|
| 945 | + self::setupBackgroundJobs($appData['background-jobs']); |
|
| 946 | + |
|
| 947 | + //set remote/public handlers |
|
| 948 | + if (array_key_exists('ocsid', $appData)) { |
|
| 949 | + \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 950 | + } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 951 | + \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 952 | + } |
|
| 953 | + foreach ($appData['remote'] as $name => $path) { |
|
| 954 | + \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 955 | + } |
|
| 956 | + foreach ($appData['public'] as $name => $path) { |
|
| 957 | + \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 958 | + } |
|
| 959 | + |
|
| 960 | + self::setAppTypes($appId); |
|
| 961 | + |
|
| 962 | + $version = \OC_App::getAppVersion($appId); |
|
| 963 | + \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version); |
|
| 964 | + |
|
| 965 | + \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 966 | + ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 967 | + )); |
|
| 968 | + |
|
| 969 | + return true; |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + /** |
|
| 973 | + * @param string $appId |
|
| 974 | + * @param string[] $steps |
|
| 975 | + * @throws \OC\NeedsUpdateException |
|
| 976 | + */ |
|
| 977 | + public static function executeRepairSteps(string $appId, array $steps) { |
|
| 978 | + if (empty($steps)) { |
|
| 979 | + return; |
|
| 980 | + } |
|
| 981 | + // load the app |
|
| 982 | + self::loadApp($appId); |
|
| 983 | + |
|
| 984 | + $dispatcher = OC::$server->getEventDispatcher(); |
|
| 985 | + |
|
| 986 | + // load the steps |
|
| 987 | + $r = new Repair([], $dispatcher); |
|
| 988 | + foreach ($steps as $step) { |
|
| 989 | + try { |
|
| 990 | + $r->addStep($step); |
|
| 991 | + } catch (Exception $ex) { |
|
| 992 | + $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 993 | + \OC::$server->getLogger()->logException($ex); |
|
| 994 | + } |
|
| 995 | + } |
|
| 996 | + // run the steps |
|
| 997 | + $r->run(); |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + public static function setupBackgroundJobs(array $jobs) { |
|
| 1001 | + $queue = \OC::$server->getJobList(); |
|
| 1002 | + foreach ($jobs as $job) { |
|
| 1003 | + $queue->add($job); |
|
| 1004 | + } |
|
| 1005 | + } |
|
| 1006 | + |
|
| 1007 | + /** |
|
| 1008 | + * @param string $appId |
|
| 1009 | + * @param string[] $steps |
|
| 1010 | + */ |
|
| 1011 | + private static function setupLiveMigrations(string $appId, array $steps) { |
|
| 1012 | + $queue = \OC::$server->getJobList(); |
|
| 1013 | + foreach ($steps as $step) { |
|
| 1014 | + $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1015 | + 'app' => $appId, |
|
| 1016 | + 'step' => $step]); |
|
| 1017 | + } |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + /** |
|
| 1021 | + * @param string $appId |
|
| 1022 | + * @return \OC\Files\View|false |
|
| 1023 | + */ |
|
| 1024 | + public static function getStorage(string $appId) { |
|
| 1025 | + if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check |
|
| 1026 | + if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1027 | + $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1028 | + if (!$view->file_exists($appId)) { |
|
| 1029 | + $view->mkdir($appId); |
|
| 1030 | + } |
|
| 1031 | + return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1032 | + } else { |
|
| 1033 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1034 | + return false; |
|
| 1035 | + } |
|
| 1036 | + } else { |
|
| 1037 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1038 | + return false; |
|
| 1039 | + } |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + protected static function findBestL10NOption(array $options, string $lang): string { |
|
| 1043 | + $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1044 | + |
|
| 1045 | + $lang = strtolower($lang); |
|
| 1046 | + $similarLang = $lang; |
|
| 1047 | + if (strpos($similarLang, '_')) { |
|
| 1048 | + // For "de_DE" we want to find "de" and the other way around |
|
| 1049 | + $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + foreach ($options as $option) { |
|
| 1053 | + if (is_array($option)) { |
|
| 1054 | + if ($fallback === false) { |
|
| 1055 | + $fallback = $option['@value']; |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + if (!isset($option['@attributes']['lang'])) { |
|
| 1059 | + continue; |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1063 | + if ($attributeLang === $lang) { |
|
| 1064 | + return $option['@value']; |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + if ($attributeLang === $similarLang) { |
|
| 1068 | + $similarLangFallback = $option['@value']; |
|
| 1069 | + } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1070 | + if ($similarLangFallback === false) { |
|
| 1071 | + $similarLangFallback = $option['@value']; |
|
| 1072 | + } |
|
| 1073 | + } |
|
| 1074 | + } else { |
|
| 1075 | + $englishFallback = $option; |
|
| 1076 | + } |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + if ($similarLangFallback !== false) { |
|
| 1080 | + return $similarLangFallback; |
|
| 1081 | + } else if ($englishFallback !== false) { |
|
| 1082 | + return $englishFallback; |
|
| 1083 | + } |
|
| 1084 | + return (string) $fallback; |
|
| 1085 | + } |
|
| 1086 | + |
|
| 1087 | + /** |
|
| 1088 | + * parses the app data array and enhanced the 'description' value |
|
| 1089 | + * |
|
| 1090 | + * @param array $data the app data |
|
| 1091 | + * @param string $lang |
|
| 1092 | + * @return array improved app data |
|
| 1093 | + */ |
|
| 1094 | + public static function parseAppInfo(array $data, $lang = null): array { |
|
| 1095 | + |
|
| 1096 | + if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1097 | + $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1098 | + } |
|
| 1099 | + if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1100 | + $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1101 | + } |
|
| 1102 | + if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1103 | + $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1104 | + } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1105 | + $data['description'] = trim($data['description']); |
|
| 1106 | + } else { |
|
| 1107 | + $data['description'] = ''; |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + return $data; |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + /** |
|
| 1114 | + * @param \OCP\IConfig $config |
|
| 1115 | + * @param \OCP\IL10N $l |
|
| 1116 | + * @param array $info |
|
| 1117 | + * @throws \Exception |
|
| 1118 | + */ |
|
| 1119 | + public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info) { |
|
| 1120 | + $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1121 | + $missing = $dependencyAnalyzer->analyze($info); |
|
| 1122 | + if (!empty($missing)) { |
|
| 1123 | + $missingMsg = implode(PHP_EOL, $missing); |
|
| 1124 | + throw new \Exception( |
|
| 1125 | + $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1126 | + [$info['name'], $missingMsg] |
|
| 1127 | + ) |
|
| 1128 | + ); |
|
| 1129 | + } |
|
| 1130 | + } |
|
| 1131 | 1131 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * @copyright Copyright (c) 2016, Lukas Reschke <[email protected]> |
@@ -111,9 +111,9 @@ discard block |
||
| 111 | 111 | $apps = self::getEnabledApps(); |
| 112 | 112 | |
| 113 | 113 | // Add each apps' folder as allowed class path |
| 114 | - foreach($apps as $app) { |
|
| 114 | + foreach ($apps as $app) { |
|
| 115 | 115 | $path = self::getAppPath($app); |
| 116 | - if($path !== false) { |
|
| 116 | + if ($path !== false) { |
|
| 117 | 117 | self::registerAutoloading($app, $path); |
| 118 | 118 | } |
| 119 | 119 | } |
@@ -139,15 +139,15 @@ discard block |
||
| 139 | 139 | public static function loadApp(string $app) { |
| 140 | 140 | self::$loadedApps[] = $app; |
| 141 | 141 | $appPath = self::getAppPath($app); |
| 142 | - if($appPath === false) { |
|
| 142 | + if ($appPath === false) { |
|
| 143 | 143 | return; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // in case someone calls loadApp() directly |
| 147 | 147 | self::registerAutoloading($app, $appPath); |
| 148 | 148 | |
| 149 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 150 | - \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 149 | + if (is_file($appPath.'/appinfo/app.php')) { |
|
| 150 | + \OC::$server->getEventLogger()->start('load_app_'.$app, 'Load app: '.$app); |
|
| 151 | 151 | try { |
| 152 | 152 | self::requireAppFile($app); |
| 153 | 153 | } catch (Error $ex) { |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | // enabled for groups |
| 165 | 165 | self::$enabledAppsCache = []; |
| 166 | 166 | } |
| 167 | - \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 167 | + \OC::$server->getEventLogger()->end('load_app_'.$app); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | $info = self::getAppInfo($app); |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ? |
| 211 | 211 | [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin']; |
| 212 | 212 | foreach ($plugins as $plugin) { |
| 213 | - if($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 213 | + if ($plugin['@attributes']['type'] === 'collaborator-search') { |
|
| 214 | 214 | $pluginInfo = [ |
| 215 | 215 | 'shareType' => $plugin['@attributes']['share-type'], |
| 216 | 216 | 'class' => $plugin['@value'], |
@@ -229,8 +229,8 @@ discard block |
||
| 229 | 229 | * @param string $path |
| 230 | 230 | */ |
| 231 | 231 | public static function registerAutoloading(string $app, string $path) { |
| 232 | - $key = $app . '-' . $path; |
|
| 233 | - if(isset(self::$alreadyRegistered[$key])) { |
|
| 232 | + $key = $app.'-'.$path; |
|
| 233 | + if (isset(self::$alreadyRegistered[$key])) { |
|
| 234 | 234 | return; |
| 235 | 235 | } |
| 236 | 236 | |
@@ -240,17 +240,17 @@ discard block |
||
| 240 | 240 | $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
| 241 | 241 | \OC::$server->registerNamespace($app, $appNamespace); |
| 242 | 242 | |
| 243 | - if (file_exists($path . '/composer/autoload.php')) { |
|
| 244 | - require_once $path . '/composer/autoload.php'; |
|
| 243 | + if (file_exists($path.'/composer/autoload.php')) { |
|
| 244 | + require_once $path.'/composer/autoload.php'; |
|
| 245 | 245 | } else { |
| 246 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 246 | + \OC::$composerAutoloader->addPsr4($appNamespace.'\\', $path.'/lib/', true); |
|
| 247 | 247 | // Register on legacy autoloader |
| 248 | 248 | \OC::$loader->addValidRoot($path); |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | // Register Test namespace only when testing |
| 252 | 252 | if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
| 253 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 253 | + \OC::$composerAutoloader->addPsr4($appNamespace.'\\Tests\\', $path.'/tests/', true); |
|
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | */ |
| 263 | 263 | private static function requireAppFile(string $app) { |
| 264 | 264 | // encapsulated here to avoid variable scope conflicts |
| 265 | - require_once $app . '/appinfo/app.php'; |
|
| 265 | + require_once $app.'/appinfo/app.php'; |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | public static function setAppTypes(string $app) { |
| 308 | 308 | $appManager = \OC::$server->getAppManager(); |
| 309 | 309 | $appData = $appManager->getAppInfo($app); |
| 310 | - if(!is_array($appData)) { |
|
| 310 | + if (!is_array($appData)) { |
|
| 311 | 311 | return; |
| 312 | 312 | } |
| 313 | 313 | |
@@ -360,8 +360,8 @@ discard block |
||
| 360 | 360 | } else { |
| 361 | 361 | $apps = $appManager->getEnabledAppsForUser($user); |
| 362 | 362 | } |
| 363 | - $apps = array_filter($apps, function ($app) { |
|
| 364 | - return $app !== 'files';//we add this manually |
|
| 363 | + $apps = array_filter($apps, function($app) { |
|
| 364 | + return $app !== 'files'; //we add this manually |
|
| 365 | 365 | }); |
| 366 | 366 | sort($apps); |
| 367 | 367 | array_unshift($apps, 'files'); |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | $installer = \OC::$server->query(Installer::class); |
| 400 | 400 | $isDownloaded = $installer->isDownloaded($appId); |
| 401 | 401 | |
| 402 | - if(!$isDownloaded) { |
|
| 402 | + if (!$isDownloaded) { |
|
| 403 | 403 | $installer->downloadApp($appId); |
| 404 | 404 | } |
| 405 | 405 | |
@@ -474,7 +474,7 @@ discard block |
||
| 474 | 474 | */ |
| 475 | 475 | public static function findAppInDirectories(string $appId) { |
| 476 | 476 | $sanitizedAppId = self::cleanAppId($appId); |
| 477 | - if($sanitizedAppId !== $appId) { |
|
| 477 | + if ($sanitizedAppId !== $appId) { |
|
| 478 | 478 | return false; |
| 479 | 479 | } |
| 480 | 480 | static $app_dir = []; |
@@ -485,7 +485,7 @@ discard block |
||
| 485 | 485 | |
| 486 | 486 | $possibleApps = []; |
| 487 | 487 | foreach (OC::$APPSROOTS as $dir) { |
| 488 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 488 | + if (file_exists($dir['path'].'/'.$appId)) { |
|
| 489 | 489 | $possibleApps[] = $dir; |
| 490 | 490 | } |
| 491 | 491 | } |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | } |
| 527 | 527 | |
| 528 | 528 | if (($dir = self::findAppInDirectories($appId)) != false) { |
| 529 | - return $dir['path'] . '/' . $appId; |
|
| 529 | + return $dir['path'].'/'.$appId; |
|
| 530 | 530 | } |
| 531 | 531 | return false; |
| 532 | 532 | } |
@@ -540,7 +540,7 @@ discard block |
||
| 540 | 540 | */ |
| 541 | 541 | public static function getAppWebPath(string $appId) { |
| 542 | 542 | if (($dir = self::findAppInDirectories($appId)) != false) { |
| 543 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 543 | + return OC::$WEBROOT.$dir['url'].'/'.$appId; |
|
| 544 | 544 | } |
| 545 | 545 | return false; |
| 546 | 546 | } |
@@ -564,7 +564,7 @@ discard block |
||
| 564 | 564 | * @return string |
| 565 | 565 | */ |
| 566 | 566 | public static function getAppVersionByPath(string $path): string { |
| 567 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 567 | + $infoFile = $path.'/appinfo/info.xml'; |
|
| 568 | 568 | $appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true); |
| 569 | 569 | return isset($appData['version']) ? $appData['version'] : ''; |
| 570 | 570 | } |
@@ -664,7 +664,7 @@ discard block |
||
| 664 | 664 | * @param string $page |
| 665 | 665 | */ |
| 666 | 666 | public static function registerAdmin(string $app, string $page) { |
| 667 | - self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 667 | + self::$adminForms[] = $app.'/'.$page.'.php'; |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | /** |
@@ -673,7 +673,7 @@ discard block |
||
| 673 | 673 | * @param string $page |
| 674 | 674 | */ |
| 675 | 675 | public static function registerPersonal(string $app, string $page) { |
| 676 | - self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 676 | + self::$personalForms[] = $app.'/'.$page.'.php'; |
|
| 677 | 677 | } |
| 678 | 678 | |
| 679 | 679 | /** |
@@ -702,7 +702,7 @@ discard block |
||
| 702 | 702 | |
| 703 | 703 | foreach (OC::$APPSROOTS as $apps_dir) { |
| 704 | 704 | if (!is_readable($apps_dir['path'])) { |
| 705 | - \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 705 | + \OCP\Util::writeLog('core', 'unable to read app folder : '.$apps_dir['path'], \OCP\Util::WARN); |
|
| 706 | 706 | continue; |
| 707 | 707 | } |
| 708 | 708 | $dh = opendir($apps_dir['path']); |
@@ -710,7 +710,7 @@ discard block |
||
| 710 | 710 | if (is_resource($dh)) { |
| 711 | 711 | while (($file = readdir($dh)) !== false) { |
| 712 | 712 | |
| 713 | - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 713 | + if ($file[0] != '.' and is_dir($apps_dir['path'].'/'.$file) and is_file($apps_dir['path'].'/'.$file.'/appinfo/info.xml')) { |
|
| 714 | 714 | |
| 715 | 715 | $apps[] = $file; |
| 716 | 716 | } |
@@ -743,12 +743,12 @@ discard block |
||
| 743 | 743 | |
| 744 | 744 | $info = OC_App::getAppInfo($app, false, $langCode); |
| 745 | 745 | if (!is_array($info)) { |
| 746 | - \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 746 | + \OCP\Util::writeLog('core', 'Could not read app info file for app "'.$app.'"', \OCP\Util::ERROR); |
|
| 747 | 747 | continue; |
| 748 | 748 | } |
| 749 | 749 | |
| 750 | 750 | if (!isset($info['name'])) { |
| 751 | - \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 751 | + \OCP\Util::writeLog('core', 'App id "'.$app.'" has no name in appinfo', \OCP\Util::ERROR); |
|
| 752 | 752 | continue; |
| 753 | 753 | } |
| 754 | 754 | |
@@ -775,13 +775,13 @@ discard block |
||
| 775 | 775 | } |
| 776 | 776 | |
| 777 | 777 | $appPath = self::getAppPath($app); |
| 778 | - if($appPath !== false) { |
|
| 779 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 778 | + if ($appPath !== false) { |
|
| 779 | + $appIcon = $appPath.'/img/'.$app.'.svg'; |
|
| 780 | 780 | if (file_exists($appIcon)) { |
| 781 | - $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg'); |
|
| 781 | + $info['preview'] = $urlGenerator->imagePath($app, $app.'.svg'); |
|
| 782 | 782 | $info['previewAsIcon'] = true; |
| 783 | 783 | } else { |
| 784 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 784 | + $appIcon = $appPath.'/img/app.svg'; |
|
| 785 | 785 | if (file_exists($appIcon)) { |
| 786 | 786 | $info['preview'] = $urlGenerator->imagePath($app, 'app.svg'); |
| 787 | 787 | $info['previewAsIcon'] = true; |
@@ -902,7 +902,7 @@ discard block |
||
| 902 | 902 | public static function getAppVersions() { |
| 903 | 903 | static $versions; |
| 904 | 904 | |
| 905 | - if(!$versions) { |
|
| 905 | + if (!$versions) { |
|
| 906 | 906 | $appConfig = \OC::$server->getAppConfig(); |
| 907 | 907 | $versions = $appConfig->getValues(false, 'installed_version'); |
| 908 | 908 | } |
@@ -917,7 +917,7 @@ discard block |
||
| 917 | 917 | */ |
| 918 | 918 | public static function updateApp(string $appId): bool { |
| 919 | 919 | $appPath = self::getAppPath($appId); |
| 920 | - if($appPath === false) { |
|
| 920 | + if ($appPath === false) { |
|
| 921 | 921 | return false; |
| 922 | 922 | } |
| 923 | 923 | self::registerAutoloading($appId, $appPath); |
@@ -925,8 +925,8 @@ discard block |
||
| 925 | 925 | $appData = self::getAppInfo($appId); |
| 926 | 926 | self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
| 927 | 927 | |
| 928 | - if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 929 | - OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 928 | + if (file_exists($appPath.'/appinfo/database.xml')) { |
|
| 929 | + OC_DB::updateDbFromStructure($appPath.'/appinfo/database.xml'); |
|
| 930 | 930 | } else { |
| 931 | 931 | $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection()); |
| 932 | 932 | $ms->migrate(); |
@@ -938,23 +938,23 @@ discard block |
||
| 938 | 938 | \OC::$server->getAppManager()->getAppVersion($appId, false); |
| 939 | 939 | |
| 940 | 940 | // run upgrade code |
| 941 | - if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 941 | + if (file_exists($appPath.'/appinfo/update.php')) { |
|
| 942 | 942 | self::loadApp($appId); |
| 943 | - include $appPath . '/appinfo/update.php'; |
|
| 943 | + include $appPath.'/appinfo/update.php'; |
|
| 944 | 944 | } |
| 945 | 945 | self::setupBackgroundJobs($appData['background-jobs']); |
| 946 | 946 | |
| 947 | 947 | //set remote/public handlers |
| 948 | 948 | if (array_key_exists('ocsid', $appData)) { |
| 949 | 949 | \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
| 950 | - } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 950 | + } elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 951 | 951 | \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
| 952 | 952 | } |
| 953 | 953 | foreach ($appData['remote'] as $name => $path) { |
| 954 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 954 | + \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $appId.'/'.$path); |
|
| 955 | 955 | } |
| 956 | 956 | foreach ($appData['public'] as $name => $path) { |
| 957 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 957 | + \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $appId.'/'.$path); |
|
| 958 | 958 | } |
| 959 | 959 | |
| 960 | 960 | self::setAppTypes($appId); |
@@ -1024,17 +1024,17 @@ discard block |
||
| 1024 | 1024 | public static function getStorage(string $appId) { |
| 1025 | 1025 | if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check |
| 1026 | 1026 | if (\OC::$server->getUserSession()->isLoggedIn()) { |
| 1027 | - $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1027 | + $view = new \OC\Files\View('/'.OC_User::getUser()); |
|
| 1028 | 1028 | if (!$view->file_exists($appId)) { |
| 1029 | 1029 | $view->mkdir($appId); |
| 1030 | 1030 | } |
| 1031 | - return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1031 | + return new \OC\Files\View('/'.OC_User::getUser().'/'.$appId); |
|
| 1032 | 1032 | } else { |
| 1033 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1033 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.', user not logged in', \OCP\Util::ERROR); |
|
| 1034 | 1034 | return false; |
| 1035 | 1035 | } |
| 1036 | 1036 | } else { |
| 1037 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1037 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.' not enabled', \OCP\Util::ERROR); |
|
| 1038 | 1038 | return false; |
| 1039 | 1039 | } |
| 1040 | 1040 | } |
@@ -1066,9 +1066,9 @@ discard block |
||
| 1066 | 1066 | |
| 1067 | 1067 | if ($attributeLang === $similarLang) { |
| 1068 | 1068 | $similarLangFallback = $option['@value']; |
| 1069 | - } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1069 | + } else if (strpos($attributeLang, $similarLang.'_') === 0) { |
|
| 1070 | 1070 | if ($similarLangFallback === false) { |
| 1071 | - $similarLangFallback = $option['@value']; |
|
| 1071 | + $similarLangFallback = $option['@value']; |
|
| 1072 | 1072 | } |
| 1073 | 1073 | } |
| 1074 | 1074 | } else { |
@@ -1103,7 +1103,7 @@ discard block |
||
| 1103 | 1103 | $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
| 1104 | 1104 | } else if (isset($data['description']) && is_string($data['description'])) { |
| 1105 | 1105 | $data['description'] = trim($data['description']); |
| 1106 | - } else { |
|
| 1106 | + } else { |
|
| 1107 | 1107 | $data['description'] = ''; |
| 1108 | 1108 | } |
| 1109 | 1109 | |
@@ -54,556 +54,556 @@ |
||
| 54 | 54 | */ |
| 55 | 55 | class Updater extends BasicEmitter { |
| 56 | 56 | |
| 57 | - /** @var ILogger $log */ |
|
| 58 | - private $log; |
|
| 59 | - |
|
| 60 | - /** @var IConfig */ |
|
| 61 | - private $config; |
|
| 62 | - |
|
| 63 | - /** @var Checker */ |
|
| 64 | - private $checker; |
|
| 65 | - |
|
| 66 | - /** @var Installer */ |
|
| 67 | - private $installer; |
|
| 68 | - |
|
| 69 | - private $logLevelNames = [ |
|
| 70 | - 0 => 'Debug', |
|
| 71 | - 1 => 'Info', |
|
| 72 | - 2 => 'Warning', |
|
| 73 | - 3 => 'Error', |
|
| 74 | - 4 => 'Fatal', |
|
| 75 | - ]; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param IConfig $config |
|
| 79 | - * @param Checker $checker |
|
| 80 | - * @param ILogger $log |
|
| 81 | - * @param Installer $installer |
|
| 82 | - */ |
|
| 83 | - public function __construct(IConfig $config, |
|
| 84 | - Checker $checker, |
|
| 85 | - ILogger $log = null, |
|
| 86 | - Installer $installer) { |
|
| 87 | - $this->log = $log; |
|
| 88 | - $this->config = $config; |
|
| 89 | - $this->checker = $checker; |
|
| 90 | - $this->installer = $installer; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * runs the update actions in maintenance mode, does not upgrade the source files |
|
| 95 | - * except the main .htaccess file |
|
| 96 | - * |
|
| 97 | - * @return bool true if the operation succeeded, false otherwise |
|
| 98 | - */ |
|
| 99 | - public function upgrade() { |
|
| 100 | - $this->emitRepairEvents(); |
|
| 101 | - $this->logAllEvents(); |
|
| 102 | - |
|
| 103 | - $logLevel = $this->config->getSystemValue('loglevel', Util::WARN); |
|
| 104 | - $this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]); |
|
| 105 | - $this->config->setSystemValue('loglevel', Util::DEBUG); |
|
| 106 | - |
|
| 107 | - $wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); |
|
| 108 | - |
|
| 109 | - if(!$wasMaintenanceModeEnabled) { |
|
| 110 | - $this->config->setSystemValue('maintenance', true); |
|
| 111 | - $this->emit('\OC\Updater', 'maintenanceEnabled'); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $installedVersion = $this->config->getSystemValue('version', '0.0.0'); |
|
| 115 | - $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
| 116 | - $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core')); |
|
| 117 | - |
|
| 118 | - $success = true; |
|
| 119 | - try { |
|
| 120 | - $this->doUpgrade($currentVersion, $installedVersion); |
|
| 121 | - } catch (HintException $exception) { |
|
| 122 | - $this->log->logException($exception, ['app' => 'core']); |
|
| 123 | - $this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint())); |
|
| 124 | - $success = false; |
|
| 125 | - } catch (\Exception $exception) { |
|
| 126 | - $this->log->logException($exception, ['app' => 'core']); |
|
| 127 | - $this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage())); |
|
| 128 | - $success = false; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $this->emit('\OC\Updater', 'updateEnd', array($success)); |
|
| 132 | - |
|
| 133 | - if(!$wasMaintenanceModeEnabled && $success) { |
|
| 134 | - $this->config->setSystemValue('maintenance', false); |
|
| 135 | - $this->emit('\OC\Updater', 'maintenanceDisabled'); |
|
| 136 | - } else { |
|
| 137 | - $this->emit('\OC\Updater', 'maintenanceActive'); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]); |
|
| 141 | - $this->config->setSystemValue('loglevel', $logLevel); |
|
| 142 | - $this->config->setSystemValue('installed', true); |
|
| 143 | - |
|
| 144 | - return $success; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Return version from which this version is allowed to upgrade from |
|
| 149 | - * |
|
| 150 | - * @return array allowed previous versions per vendor |
|
| 151 | - */ |
|
| 152 | - private function getAllowedPreviousVersions() { |
|
| 153 | - // this should really be a JSON file |
|
| 154 | - require \OC::$SERVERROOT . '/version.php'; |
|
| 155 | - /** @var array $OC_VersionCanBeUpgradedFrom */ |
|
| 156 | - return $OC_VersionCanBeUpgradedFrom; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Return vendor from which this version was published |
|
| 161 | - * |
|
| 162 | - * @return string Get the vendor |
|
| 163 | - */ |
|
| 164 | - private function getVendor() { |
|
| 165 | - // this should really be a JSON file |
|
| 166 | - require \OC::$SERVERROOT . '/version.php'; |
|
| 167 | - /** @var string $vendor */ |
|
| 168 | - return (string) $vendor; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Whether an upgrade to a specified version is possible |
|
| 173 | - * @param string $oldVersion |
|
| 174 | - * @param string $newVersion |
|
| 175 | - * @param array $allowedPreviousVersions |
|
| 176 | - * @return bool |
|
| 177 | - */ |
|
| 178 | - public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) { |
|
| 179 | - $version = explode('.', $oldVersion); |
|
| 180 | - $majorMinor = $version[0] . '.' . $version[1]; |
|
| 181 | - |
|
| 182 | - $currentVendor = $this->config->getAppValue('core', 'vendor', ''); |
|
| 183 | - |
|
| 184 | - // Vendor was not set correctly on install, so we have to white-list known versions |
|
| 185 | - if ($currentVendor === '' && isset($allowedPreviousVersions['owncloud'][$oldVersion])) { |
|
| 186 | - $currentVendor = 'owncloud'; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - if ($currentVendor === 'nextcloud') { |
|
| 190 | - return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) |
|
| 191 | - && (version_compare($oldVersion, $newVersion, '<=') || |
|
| 192 | - $this->config->getSystemValue('debug', false)); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // Check if the instance can be migrated |
|
| 196 | - return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) || |
|
| 197 | - isset($allowedPreviousVersions[$currentVendor][$oldVersion]); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * runs the update actions in maintenance mode, does not upgrade the source files |
|
| 202 | - * except the main .htaccess file |
|
| 203 | - * |
|
| 204 | - * @param string $currentVersion current version to upgrade to |
|
| 205 | - * @param string $installedVersion previous version from which to upgrade from |
|
| 206 | - * |
|
| 207 | - * @throws \Exception |
|
| 208 | - */ |
|
| 209 | - private function doUpgrade($currentVersion, $installedVersion) { |
|
| 210 | - // Stop update if the update is over several major versions |
|
| 211 | - $allowedPreviousVersions = $this->getAllowedPreviousVersions(); |
|
| 212 | - if (!$this->isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersions)) { |
|
| 213 | - throw new \Exception('Updates between multiple major versions and downgrades are unsupported.'); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - // Update .htaccess files |
|
| 217 | - try { |
|
| 218 | - Setup::updateHtaccess(); |
|
| 219 | - Setup::protectDataDirectory(); |
|
| 220 | - } catch (\Exception $e) { |
|
| 221 | - throw new \Exception($e->getMessage()); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // create empty file in data dir, so we can later find |
|
| 225 | - // out that this is indeed an ownCloud data directory |
|
| 226 | - // (in case it didn't exist before) |
|
| 227 | - file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', ''); |
|
| 228 | - |
|
| 229 | - // pre-upgrade repairs |
|
| 230 | - $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher()); |
|
| 231 | - $repair->run(); |
|
| 232 | - |
|
| 233 | - $this->doCoreUpgrade(); |
|
| 234 | - |
|
| 235 | - try { |
|
| 236 | - // TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378 |
|
| 237 | - Setup::installBackgroundJobs(); |
|
| 238 | - } catch (\Exception $e) { |
|
| 239 | - throw new \Exception($e->getMessage()); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // update all shipped apps |
|
| 243 | - $this->checkAppsRequirements(); |
|
| 244 | - $this->doAppUpgrade(); |
|
| 245 | - |
|
| 246 | - // Update the appfetchers version so it downloads the correct list from the appstore |
|
| 247 | - \OC::$server->getAppFetcher()->setVersion($currentVersion); |
|
| 248 | - |
|
| 249 | - // upgrade appstore apps |
|
| 250 | - $this->upgradeAppStoreApps(\OC::$server->getAppManager()->getInstalledApps()); |
|
| 251 | - |
|
| 252 | - // install new shipped apps on upgrade |
|
| 253 | - OC_App::loadApps(['authentication']); |
|
| 254 | - $errors = Installer::installShippedApps(true); |
|
| 255 | - foreach ($errors as $appId => $exception) { |
|
| 256 | - /** @var \Exception $exception */ |
|
| 257 | - $this->log->logException($exception, ['app' => $appId]); |
|
| 258 | - $this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - // post-upgrade repairs |
|
| 262 | - $repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher()); |
|
| 263 | - $repair->run(); |
|
| 264 | - |
|
| 265 | - //Invalidate update feed |
|
| 266 | - $this->config->setAppValue('core', 'lastupdatedat', 0); |
|
| 267 | - |
|
| 268 | - // Check for code integrity if not disabled |
|
| 269 | - if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) { |
|
| 270 | - $this->emit('\OC\Updater', 'startCheckCodeIntegrity'); |
|
| 271 | - $this->checker->runInstanceVerification(); |
|
| 272 | - $this->emit('\OC\Updater', 'finishedCheckCodeIntegrity'); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - // only set the final version if everything went well |
|
| 276 | - $this->config->setSystemValue('version', implode('.', Util::getVersion())); |
|
| 277 | - $this->config->setAppValue('core', 'vendor', $this->getVendor()); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - protected function doCoreUpgrade() { |
|
| 281 | - $this->emit('\OC\Updater', 'dbUpgradeBefore'); |
|
| 282 | - |
|
| 283 | - // execute core migrations |
|
| 284 | - $ms = new MigrationService('core', \OC::$server->getDatabaseConnection()); |
|
| 285 | - $ms->migrate(); |
|
| 286 | - |
|
| 287 | - $this->emit('\OC\Updater', 'dbUpgrade'); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @param string $version the oc version to check app compatibility with |
|
| 292 | - */ |
|
| 293 | - protected function checkAppUpgrade($version) { |
|
| 294 | - $apps = \OC_App::getEnabledApps(); |
|
| 295 | - $this->emit('\OC\Updater', 'appUpgradeCheckBefore'); |
|
| 296 | - |
|
| 297 | - $appManager = \OC::$server->getAppManager(); |
|
| 298 | - foreach ($apps as $appId) { |
|
| 299 | - $info = \OC_App::getAppInfo($appId); |
|
| 300 | - $compatible = \OC_App::isAppCompatible($version, $info); |
|
| 301 | - $isShipped = $appManager->isShipped($appId); |
|
| 302 | - |
|
| 303 | - if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) { |
|
| 304 | - /** |
|
| 305 | - * FIXME: The preupdate check is performed before the database migration, otherwise database changes |
|
| 306 | - * are not possible anymore within it. - Consider this when touching the code. |
|
| 307 | - * @link https://github.com/owncloud/core/issues/10980 |
|
| 308 | - * @see \OC_App::updateApp |
|
| 309 | - */ |
|
| 310 | - if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) { |
|
| 311 | - $this->includePreUpdate($appId); |
|
| 312 | - } |
|
| 313 | - if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) { |
|
| 314 | - $this->emit('\OC\Updater', 'appSimulateUpdate', array($appId)); |
|
| 315 | - \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml'); |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $this->emit('\OC\Updater', 'appUpgradeCheck'); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Includes the pre-update file. Done here to prevent namespace mixups. |
|
| 325 | - * @param string $appId |
|
| 326 | - */ |
|
| 327 | - private function includePreUpdate($appId) { |
|
| 328 | - include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php'; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * upgrades all apps within a major ownCloud upgrade. Also loads "priority" |
|
| 333 | - * (types authentication, filesystem, logging, in that order) afterwards. |
|
| 334 | - * |
|
| 335 | - * @throws NeedsUpdateException |
|
| 336 | - */ |
|
| 337 | - protected function doAppUpgrade() { |
|
| 338 | - $apps = \OC_App::getEnabledApps(); |
|
| 339 | - $priorityTypes = array('authentication', 'filesystem', 'logging'); |
|
| 340 | - $pseudoOtherType = 'other'; |
|
| 341 | - $stacks = array($pseudoOtherType => array()); |
|
| 342 | - |
|
| 343 | - foreach ($apps as $appId) { |
|
| 344 | - $priorityType = false; |
|
| 345 | - foreach ($priorityTypes as $type) { |
|
| 346 | - if(!isset($stacks[$type])) { |
|
| 347 | - $stacks[$type] = array(); |
|
| 348 | - } |
|
| 349 | - if (\OC_App::isType($appId, [$type])) { |
|
| 350 | - $stacks[$type][] = $appId; |
|
| 351 | - $priorityType = true; |
|
| 352 | - break; |
|
| 353 | - } |
|
| 354 | - } |
|
| 355 | - if (!$priorityType) { |
|
| 356 | - $stacks[$pseudoOtherType][] = $appId; |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - foreach ($stacks as $type => $stack) { |
|
| 360 | - foreach ($stack as $appId) { |
|
| 361 | - if (\OC_App::shouldUpgrade($appId)) { |
|
| 362 | - $this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OC_App::getAppVersion($appId)]); |
|
| 363 | - \OC_App::updateApp($appId); |
|
| 364 | - $this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]); |
|
| 365 | - } |
|
| 366 | - if($type !== $pseudoOtherType) { |
|
| 367 | - // load authentication, filesystem and logging apps after |
|
| 368 | - // upgrading them. Other apps my need to rely on modifying |
|
| 369 | - // user and/or filesystem aspects. |
|
| 370 | - \OC_App::loadApp($appId); |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * check if the current enabled apps are compatible with the current |
|
| 378 | - * ownCloud version. disable them if not. |
|
| 379 | - * This is important if you upgrade ownCloud and have non ported 3rd |
|
| 380 | - * party apps installed. |
|
| 381 | - * |
|
| 382 | - * @return array |
|
| 383 | - * @throws \Exception |
|
| 384 | - */ |
|
| 385 | - private function checkAppsRequirements() { |
|
| 386 | - $isCoreUpgrade = $this->isCodeUpgrade(); |
|
| 387 | - $apps = OC_App::getEnabledApps(); |
|
| 388 | - $version = implode('.', Util::getVersion()); |
|
| 389 | - $disabledApps = []; |
|
| 390 | - $appManager = \OC::$server->getAppManager(); |
|
| 391 | - foreach ($apps as $app) { |
|
| 392 | - // check if the app is compatible with this version of ownCloud |
|
| 393 | - $info = OC_App::getAppInfo($app); |
|
| 394 | - if(!OC_App::isAppCompatible($version, $info)) { |
|
| 395 | - if ($appManager->isShipped($app)) { |
|
| 396 | - throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update'); |
|
| 397 | - } |
|
| 398 | - OC_App::disable($app); |
|
| 399 | - $this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app)); |
|
| 400 | - } |
|
| 401 | - // no need to disable any app in case this is a non-core upgrade |
|
| 402 | - if (!$isCoreUpgrade) { |
|
| 403 | - continue; |
|
| 404 | - } |
|
| 405 | - // shipped apps will remain enabled |
|
| 406 | - if ($appManager->isShipped($app)) { |
|
| 407 | - continue; |
|
| 408 | - } |
|
| 409 | - // authentication and session apps will remain enabled as well |
|
| 410 | - if (OC_App::isType($app, ['session', 'authentication'])) { |
|
| 411 | - continue; |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - return $disabledApps; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * @return bool |
|
| 419 | - */ |
|
| 420 | - private function isCodeUpgrade() { |
|
| 421 | - $installedVersion = $this->config->getSystemValue('version', '0.0.0'); |
|
| 422 | - $currentVersion = implode('.', Util::getVersion()); |
|
| 423 | - if (version_compare($currentVersion, $installedVersion, '>')) { |
|
| 424 | - return true; |
|
| 425 | - } |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * @param array $disabledApps |
|
| 431 | - * @throws \Exception |
|
| 432 | - */ |
|
| 433 | - private function upgradeAppStoreApps(array $disabledApps) { |
|
| 434 | - foreach($disabledApps as $app) { |
|
| 435 | - try { |
|
| 436 | - $this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]); |
|
| 437 | - if ($this->installer->isUpdateAvailable($app)) { |
|
| 438 | - $this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]); |
|
| 439 | - $this->installer->updateAppstoreApp($app); |
|
| 440 | - } |
|
| 441 | - $this->emit('\OC\Updater', 'checkAppStoreApp', [$app]); |
|
| 442 | - } catch (\Exception $ex) { |
|
| 443 | - $this->log->logException($ex, ['app' => 'core']); |
|
| 444 | - } |
|
| 445 | - } |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Forward messages emitted by the repair routine |
|
| 450 | - */ |
|
| 451 | - private function emitRepairEvents() { |
|
| 452 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 453 | - $dispatcher->addListener('\OC\Repair::warning', function ($event) { |
|
| 454 | - if ($event instanceof GenericEvent) { |
|
| 455 | - $this->emit('\OC\Updater', 'repairWarning', $event->getArguments()); |
|
| 456 | - } |
|
| 457 | - }); |
|
| 458 | - $dispatcher->addListener('\OC\Repair::error', function ($event) { |
|
| 459 | - if ($event instanceof GenericEvent) { |
|
| 460 | - $this->emit('\OC\Updater', 'repairError', $event->getArguments()); |
|
| 461 | - } |
|
| 462 | - }); |
|
| 463 | - $dispatcher->addListener('\OC\Repair::info', function ($event) { |
|
| 464 | - if ($event instanceof GenericEvent) { |
|
| 465 | - $this->emit('\OC\Updater', 'repairInfo', $event->getArguments()); |
|
| 466 | - } |
|
| 467 | - }); |
|
| 468 | - $dispatcher->addListener('\OC\Repair::step', function ($event) { |
|
| 469 | - if ($event instanceof GenericEvent) { |
|
| 470 | - $this->emit('\OC\Updater', 'repairStep', $event->getArguments()); |
|
| 471 | - } |
|
| 472 | - }); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - private function logAllEvents() { |
|
| 476 | - $log = $this->log; |
|
| 477 | - |
|
| 478 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 479 | - $dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($log) { |
|
| 480 | - if (!$event instanceof GenericEvent) { |
|
| 481 | - return; |
|
| 482 | - } |
|
| 483 | - $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']); |
|
| 484 | - }); |
|
| 485 | - $dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) { |
|
| 486 | - if (!$event instanceof GenericEvent) { |
|
| 487 | - return; |
|
| 488 | - } |
|
| 489 | - $log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']); |
|
| 490 | - }); |
|
| 491 | - |
|
| 492 | - $repairListener = function($event) use ($log) { |
|
| 493 | - if (!$event instanceof GenericEvent) { |
|
| 494 | - return; |
|
| 495 | - } |
|
| 496 | - switch ($event->getSubject()) { |
|
| 497 | - case '\OC\Repair::startProgress': |
|
| 498 | - $log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']); |
|
| 499 | - break; |
|
| 500 | - case '\OC\Repair::advance': |
|
| 501 | - $desc = $event->getArgument(1); |
|
| 502 | - if (empty($desc)) { |
|
| 503 | - $desc = ''; |
|
| 504 | - } |
|
| 505 | - $log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']); |
|
| 506 | - |
|
| 507 | - break; |
|
| 508 | - case '\OC\Repair::finishProgress': |
|
| 509 | - $log->info('\OC\Repair::finishProgress', ['app' => 'updater']); |
|
| 510 | - break; |
|
| 511 | - case '\OC\Repair::step': |
|
| 512 | - $log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 513 | - break; |
|
| 514 | - case '\OC\Repair::info': |
|
| 515 | - $log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 516 | - break; |
|
| 517 | - case '\OC\Repair::warning': |
|
| 518 | - $log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 519 | - break; |
|
| 520 | - case '\OC\Repair::error': |
|
| 521 | - $log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 522 | - break; |
|
| 523 | - } |
|
| 524 | - }; |
|
| 525 | - |
|
| 526 | - $dispatcher->addListener('\OC\Repair::startProgress', $repairListener); |
|
| 527 | - $dispatcher->addListener('\OC\Repair::advance', $repairListener); |
|
| 528 | - $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener); |
|
| 529 | - $dispatcher->addListener('\OC\Repair::step', $repairListener); |
|
| 530 | - $dispatcher->addListener('\OC\Repair::info', $repairListener); |
|
| 531 | - $dispatcher->addListener('\OC\Repair::warning', $repairListener); |
|
| 532 | - $dispatcher->addListener('\OC\Repair::error', $repairListener); |
|
| 533 | - |
|
| 534 | - |
|
| 535 | - $this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) { |
|
| 536 | - $log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']); |
|
| 537 | - }); |
|
| 538 | - $this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) { |
|
| 539 | - $log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']); |
|
| 540 | - }); |
|
| 541 | - $this->listen('\OC\Updater', 'maintenanceActive', function () use($log) { |
|
| 542 | - $log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']); |
|
| 543 | - }); |
|
| 544 | - $this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) { |
|
| 545 | - if ($success) { |
|
| 546 | - $log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']); |
|
| 547 | - } else { |
|
| 548 | - $log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']); |
|
| 549 | - } |
|
| 550 | - }); |
|
| 551 | - $this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) { |
|
| 552 | - $log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']); |
|
| 553 | - }); |
|
| 554 | - $this->listen('\OC\Updater', 'dbUpgrade', function () use($log) { |
|
| 555 | - $log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']); |
|
| 556 | - }); |
|
| 557 | - $this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) { |
|
| 558 | - $log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']); |
|
| 559 | - }); |
|
| 560 | - $this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) { |
|
| 561 | - $log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']); |
|
| 562 | - }); |
|
| 563 | - $this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) { |
|
| 564 | - $log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']); |
|
| 565 | - }); |
|
| 566 | - $this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) { |
|
| 567 | - $log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']); |
|
| 568 | - }); |
|
| 569 | - $this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) { |
|
| 570 | - $log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']); |
|
| 571 | - }); |
|
| 572 | - $this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) { |
|
| 573 | - $log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']); |
|
| 574 | - }); |
|
| 575 | - $this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) { |
|
| 576 | - $log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']); |
|
| 577 | - }); |
|
| 578 | - $this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) { |
|
| 579 | - $log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']); |
|
| 580 | - }); |
|
| 581 | - $this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) { |
|
| 582 | - $log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']); |
|
| 583 | - }); |
|
| 584 | - $this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) { |
|
| 585 | - $log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']); |
|
| 586 | - }); |
|
| 587 | - $this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) { |
|
| 588 | - $log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']); |
|
| 589 | - }); |
|
| 590 | - $this->listen('\OC\Updater', 'failure', function ($message) use($log) { |
|
| 591 | - $log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']); |
|
| 592 | - }); |
|
| 593 | - $this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) { |
|
| 594 | - $log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']); |
|
| 595 | - }); |
|
| 596 | - $this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) { |
|
| 597 | - $log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']); |
|
| 598 | - }); |
|
| 599 | - $this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) { |
|
| 600 | - $log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']); |
|
| 601 | - }); |
|
| 602 | - $this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) { |
|
| 603 | - $log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']); |
|
| 604 | - }); |
|
| 605 | - |
|
| 606 | - } |
|
| 57 | + /** @var ILogger $log */ |
|
| 58 | + private $log; |
|
| 59 | + |
|
| 60 | + /** @var IConfig */ |
|
| 61 | + private $config; |
|
| 62 | + |
|
| 63 | + /** @var Checker */ |
|
| 64 | + private $checker; |
|
| 65 | + |
|
| 66 | + /** @var Installer */ |
|
| 67 | + private $installer; |
|
| 68 | + |
|
| 69 | + private $logLevelNames = [ |
|
| 70 | + 0 => 'Debug', |
|
| 71 | + 1 => 'Info', |
|
| 72 | + 2 => 'Warning', |
|
| 73 | + 3 => 'Error', |
|
| 74 | + 4 => 'Fatal', |
|
| 75 | + ]; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param IConfig $config |
|
| 79 | + * @param Checker $checker |
|
| 80 | + * @param ILogger $log |
|
| 81 | + * @param Installer $installer |
|
| 82 | + */ |
|
| 83 | + public function __construct(IConfig $config, |
|
| 84 | + Checker $checker, |
|
| 85 | + ILogger $log = null, |
|
| 86 | + Installer $installer) { |
|
| 87 | + $this->log = $log; |
|
| 88 | + $this->config = $config; |
|
| 89 | + $this->checker = $checker; |
|
| 90 | + $this->installer = $installer; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * runs the update actions in maintenance mode, does not upgrade the source files |
|
| 95 | + * except the main .htaccess file |
|
| 96 | + * |
|
| 97 | + * @return bool true if the operation succeeded, false otherwise |
|
| 98 | + */ |
|
| 99 | + public function upgrade() { |
|
| 100 | + $this->emitRepairEvents(); |
|
| 101 | + $this->logAllEvents(); |
|
| 102 | + |
|
| 103 | + $logLevel = $this->config->getSystemValue('loglevel', Util::WARN); |
|
| 104 | + $this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]); |
|
| 105 | + $this->config->setSystemValue('loglevel', Util::DEBUG); |
|
| 106 | + |
|
| 107 | + $wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); |
|
| 108 | + |
|
| 109 | + if(!$wasMaintenanceModeEnabled) { |
|
| 110 | + $this->config->setSystemValue('maintenance', true); |
|
| 111 | + $this->emit('\OC\Updater', 'maintenanceEnabled'); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $installedVersion = $this->config->getSystemValue('version', '0.0.0'); |
|
| 115 | + $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
| 116 | + $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core')); |
|
| 117 | + |
|
| 118 | + $success = true; |
|
| 119 | + try { |
|
| 120 | + $this->doUpgrade($currentVersion, $installedVersion); |
|
| 121 | + } catch (HintException $exception) { |
|
| 122 | + $this->log->logException($exception, ['app' => 'core']); |
|
| 123 | + $this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint())); |
|
| 124 | + $success = false; |
|
| 125 | + } catch (\Exception $exception) { |
|
| 126 | + $this->log->logException($exception, ['app' => 'core']); |
|
| 127 | + $this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage())); |
|
| 128 | + $success = false; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $this->emit('\OC\Updater', 'updateEnd', array($success)); |
|
| 132 | + |
|
| 133 | + if(!$wasMaintenanceModeEnabled && $success) { |
|
| 134 | + $this->config->setSystemValue('maintenance', false); |
|
| 135 | + $this->emit('\OC\Updater', 'maintenanceDisabled'); |
|
| 136 | + } else { |
|
| 137 | + $this->emit('\OC\Updater', 'maintenanceActive'); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]); |
|
| 141 | + $this->config->setSystemValue('loglevel', $logLevel); |
|
| 142 | + $this->config->setSystemValue('installed', true); |
|
| 143 | + |
|
| 144 | + return $success; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Return version from which this version is allowed to upgrade from |
|
| 149 | + * |
|
| 150 | + * @return array allowed previous versions per vendor |
|
| 151 | + */ |
|
| 152 | + private function getAllowedPreviousVersions() { |
|
| 153 | + // this should really be a JSON file |
|
| 154 | + require \OC::$SERVERROOT . '/version.php'; |
|
| 155 | + /** @var array $OC_VersionCanBeUpgradedFrom */ |
|
| 156 | + return $OC_VersionCanBeUpgradedFrom; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Return vendor from which this version was published |
|
| 161 | + * |
|
| 162 | + * @return string Get the vendor |
|
| 163 | + */ |
|
| 164 | + private function getVendor() { |
|
| 165 | + // this should really be a JSON file |
|
| 166 | + require \OC::$SERVERROOT . '/version.php'; |
|
| 167 | + /** @var string $vendor */ |
|
| 168 | + return (string) $vendor; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Whether an upgrade to a specified version is possible |
|
| 173 | + * @param string $oldVersion |
|
| 174 | + * @param string $newVersion |
|
| 175 | + * @param array $allowedPreviousVersions |
|
| 176 | + * @return bool |
|
| 177 | + */ |
|
| 178 | + public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) { |
|
| 179 | + $version = explode('.', $oldVersion); |
|
| 180 | + $majorMinor = $version[0] . '.' . $version[1]; |
|
| 181 | + |
|
| 182 | + $currentVendor = $this->config->getAppValue('core', 'vendor', ''); |
|
| 183 | + |
|
| 184 | + // Vendor was not set correctly on install, so we have to white-list known versions |
|
| 185 | + if ($currentVendor === '' && isset($allowedPreviousVersions['owncloud'][$oldVersion])) { |
|
| 186 | + $currentVendor = 'owncloud'; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + if ($currentVendor === 'nextcloud') { |
|
| 190 | + return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) |
|
| 191 | + && (version_compare($oldVersion, $newVersion, '<=') || |
|
| 192 | + $this->config->getSystemValue('debug', false)); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // Check if the instance can be migrated |
|
| 196 | + return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) || |
|
| 197 | + isset($allowedPreviousVersions[$currentVendor][$oldVersion]); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * runs the update actions in maintenance mode, does not upgrade the source files |
|
| 202 | + * except the main .htaccess file |
|
| 203 | + * |
|
| 204 | + * @param string $currentVersion current version to upgrade to |
|
| 205 | + * @param string $installedVersion previous version from which to upgrade from |
|
| 206 | + * |
|
| 207 | + * @throws \Exception |
|
| 208 | + */ |
|
| 209 | + private function doUpgrade($currentVersion, $installedVersion) { |
|
| 210 | + // Stop update if the update is over several major versions |
|
| 211 | + $allowedPreviousVersions = $this->getAllowedPreviousVersions(); |
|
| 212 | + if (!$this->isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersions)) { |
|
| 213 | + throw new \Exception('Updates between multiple major versions and downgrades are unsupported.'); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + // Update .htaccess files |
|
| 217 | + try { |
|
| 218 | + Setup::updateHtaccess(); |
|
| 219 | + Setup::protectDataDirectory(); |
|
| 220 | + } catch (\Exception $e) { |
|
| 221 | + throw new \Exception($e->getMessage()); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // create empty file in data dir, so we can later find |
|
| 225 | + // out that this is indeed an ownCloud data directory |
|
| 226 | + // (in case it didn't exist before) |
|
| 227 | + file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', ''); |
|
| 228 | + |
|
| 229 | + // pre-upgrade repairs |
|
| 230 | + $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher()); |
|
| 231 | + $repair->run(); |
|
| 232 | + |
|
| 233 | + $this->doCoreUpgrade(); |
|
| 234 | + |
|
| 235 | + try { |
|
| 236 | + // TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378 |
|
| 237 | + Setup::installBackgroundJobs(); |
|
| 238 | + } catch (\Exception $e) { |
|
| 239 | + throw new \Exception($e->getMessage()); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // update all shipped apps |
|
| 243 | + $this->checkAppsRequirements(); |
|
| 244 | + $this->doAppUpgrade(); |
|
| 245 | + |
|
| 246 | + // Update the appfetchers version so it downloads the correct list from the appstore |
|
| 247 | + \OC::$server->getAppFetcher()->setVersion($currentVersion); |
|
| 248 | + |
|
| 249 | + // upgrade appstore apps |
|
| 250 | + $this->upgradeAppStoreApps(\OC::$server->getAppManager()->getInstalledApps()); |
|
| 251 | + |
|
| 252 | + // install new shipped apps on upgrade |
|
| 253 | + OC_App::loadApps(['authentication']); |
|
| 254 | + $errors = Installer::installShippedApps(true); |
|
| 255 | + foreach ($errors as $appId => $exception) { |
|
| 256 | + /** @var \Exception $exception */ |
|
| 257 | + $this->log->logException($exception, ['app' => $appId]); |
|
| 258 | + $this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + // post-upgrade repairs |
|
| 262 | + $repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher()); |
|
| 263 | + $repair->run(); |
|
| 264 | + |
|
| 265 | + //Invalidate update feed |
|
| 266 | + $this->config->setAppValue('core', 'lastupdatedat', 0); |
|
| 267 | + |
|
| 268 | + // Check for code integrity if not disabled |
|
| 269 | + if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) { |
|
| 270 | + $this->emit('\OC\Updater', 'startCheckCodeIntegrity'); |
|
| 271 | + $this->checker->runInstanceVerification(); |
|
| 272 | + $this->emit('\OC\Updater', 'finishedCheckCodeIntegrity'); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + // only set the final version if everything went well |
|
| 276 | + $this->config->setSystemValue('version', implode('.', Util::getVersion())); |
|
| 277 | + $this->config->setAppValue('core', 'vendor', $this->getVendor()); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + protected function doCoreUpgrade() { |
|
| 281 | + $this->emit('\OC\Updater', 'dbUpgradeBefore'); |
|
| 282 | + |
|
| 283 | + // execute core migrations |
|
| 284 | + $ms = new MigrationService('core', \OC::$server->getDatabaseConnection()); |
|
| 285 | + $ms->migrate(); |
|
| 286 | + |
|
| 287 | + $this->emit('\OC\Updater', 'dbUpgrade'); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @param string $version the oc version to check app compatibility with |
|
| 292 | + */ |
|
| 293 | + protected function checkAppUpgrade($version) { |
|
| 294 | + $apps = \OC_App::getEnabledApps(); |
|
| 295 | + $this->emit('\OC\Updater', 'appUpgradeCheckBefore'); |
|
| 296 | + |
|
| 297 | + $appManager = \OC::$server->getAppManager(); |
|
| 298 | + foreach ($apps as $appId) { |
|
| 299 | + $info = \OC_App::getAppInfo($appId); |
|
| 300 | + $compatible = \OC_App::isAppCompatible($version, $info); |
|
| 301 | + $isShipped = $appManager->isShipped($appId); |
|
| 302 | + |
|
| 303 | + if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) { |
|
| 304 | + /** |
|
| 305 | + * FIXME: The preupdate check is performed before the database migration, otherwise database changes |
|
| 306 | + * are not possible anymore within it. - Consider this when touching the code. |
|
| 307 | + * @link https://github.com/owncloud/core/issues/10980 |
|
| 308 | + * @see \OC_App::updateApp |
|
| 309 | + */ |
|
| 310 | + if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) { |
|
| 311 | + $this->includePreUpdate($appId); |
|
| 312 | + } |
|
| 313 | + if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) { |
|
| 314 | + $this->emit('\OC\Updater', 'appSimulateUpdate', array($appId)); |
|
| 315 | + \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml'); |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $this->emit('\OC\Updater', 'appUpgradeCheck'); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Includes the pre-update file. Done here to prevent namespace mixups. |
|
| 325 | + * @param string $appId |
|
| 326 | + */ |
|
| 327 | + private function includePreUpdate($appId) { |
|
| 328 | + include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php'; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * upgrades all apps within a major ownCloud upgrade. Also loads "priority" |
|
| 333 | + * (types authentication, filesystem, logging, in that order) afterwards. |
|
| 334 | + * |
|
| 335 | + * @throws NeedsUpdateException |
|
| 336 | + */ |
|
| 337 | + protected function doAppUpgrade() { |
|
| 338 | + $apps = \OC_App::getEnabledApps(); |
|
| 339 | + $priorityTypes = array('authentication', 'filesystem', 'logging'); |
|
| 340 | + $pseudoOtherType = 'other'; |
|
| 341 | + $stacks = array($pseudoOtherType => array()); |
|
| 342 | + |
|
| 343 | + foreach ($apps as $appId) { |
|
| 344 | + $priorityType = false; |
|
| 345 | + foreach ($priorityTypes as $type) { |
|
| 346 | + if(!isset($stacks[$type])) { |
|
| 347 | + $stacks[$type] = array(); |
|
| 348 | + } |
|
| 349 | + if (\OC_App::isType($appId, [$type])) { |
|
| 350 | + $stacks[$type][] = $appId; |
|
| 351 | + $priorityType = true; |
|
| 352 | + break; |
|
| 353 | + } |
|
| 354 | + } |
|
| 355 | + if (!$priorityType) { |
|
| 356 | + $stacks[$pseudoOtherType][] = $appId; |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + foreach ($stacks as $type => $stack) { |
|
| 360 | + foreach ($stack as $appId) { |
|
| 361 | + if (\OC_App::shouldUpgrade($appId)) { |
|
| 362 | + $this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OC_App::getAppVersion($appId)]); |
|
| 363 | + \OC_App::updateApp($appId); |
|
| 364 | + $this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]); |
|
| 365 | + } |
|
| 366 | + if($type !== $pseudoOtherType) { |
|
| 367 | + // load authentication, filesystem and logging apps after |
|
| 368 | + // upgrading them. Other apps my need to rely on modifying |
|
| 369 | + // user and/or filesystem aspects. |
|
| 370 | + \OC_App::loadApp($appId); |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * check if the current enabled apps are compatible with the current |
|
| 378 | + * ownCloud version. disable them if not. |
|
| 379 | + * This is important if you upgrade ownCloud and have non ported 3rd |
|
| 380 | + * party apps installed. |
|
| 381 | + * |
|
| 382 | + * @return array |
|
| 383 | + * @throws \Exception |
|
| 384 | + */ |
|
| 385 | + private function checkAppsRequirements() { |
|
| 386 | + $isCoreUpgrade = $this->isCodeUpgrade(); |
|
| 387 | + $apps = OC_App::getEnabledApps(); |
|
| 388 | + $version = implode('.', Util::getVersion()); |
|
| 389 | + $disabledApps = []; |
|
| 390 | + $appManager = \OC::$server->getAppManager(); |
|
| 391 | + foreach ($apps as $app) { |
|
| 392 | + // check if the app is compatible with this version of ownCloud |
|
| 393 | + $info = OC_App::getAppInfo($app); |
|
| 394 | + if(!OC_App::isAppCompatible($version, $info)) { |
|
| 395 | + if ($appManager->isShipped($app)) { |
|
| 396 | + throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update'); |
|
| 397 | + } |
|
| 398 | + OC_App::disable($app); |
|
| 399 | + $this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app)); |
|
| 400 | + } |
|
| 401 | + // no need to disable any app in case this is a non-core upgrade |
|
| 402 | + if (!$isCoreUpgrade) { |
|
| 403 | + continue; |
|
| 404 | + } |
|
| 405 | + // shipped apps will remain enabled |
|
| 406 | + if ($appManager->isShipped($app)) { |
|
| 407 | + continue; |
|
| 408 | + } |
|
| 409 | + // authentication and session apps will remain enabled as well |
|
| 410 | + if (OC_App::isType($app, ['session', 'authentication'])) { |
|
| 411 | + continue; |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + return $disabledApps; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * @return bool |
|
| 419 | + */ |
|
| 420 | + private function isCodeUpgrade() { |
|
| 421 | + $installedVersion = $this->config->getSystemValue('version', '0.0.0'); |
|
| 422 | + $currentVersion = implode('.', Util::getVersion()); |
|
| 423 | + if (version_compare($currentVersion, $installedVersion, '>')) { |
|
| 424 | + return true; |
|
| 425 | + } |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * @param array $disabledApps |
|
| 431 | + * @throws \Exception |
|
| 432 | + */ |
|
| 433 | + private function upgradeAppStoreApps(array $disabledApps) { |
|
| 434 | + foreach($disabledApps as $app) { |
|
| 435 | + try { |
|
| 436 | + $this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]); |
|
| 437 | + if ($this->installer->isUpdateAvailable($app)) { |
|
| 438 | + $this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]); |
|
| 439 | + $this->installer->updateAppstoreApp($app); |
|
| 440 | + } |
|
| 441 | + $this->emit('\OC\Updater', 'checkAppStoreApp', [$app]); |
|
| 442 | + } catch (\Exception $ex) { |
|
| 443 | + $this->log->logException($ex, ['app' => 'core']); |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * Forward messages emitted by the repair routine |
|
| 450 | + */ |
|
| 451 | + private function emitRepairEvents() { |
|
| 452 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 453 | + $dispatcher->addListener('\OC\Repair::warning', function ($event) { |
|
| 454 | + if ($event instanceof GenericEvent) { |
|
| 455 | + $this->emit('\OC\Updater', 'repairWarning', $event->getArguments()); |
|
| 456 | + } |
|
| 457 | + }); |
|
| 458 | + $dispatcher->addListener('\OC\Repair::error', function ($event) { |
|
| 459 | + if ($event instanceof GenericEvent) { |
|
| 460 | + $this->emit('\OC\Updater', 'repairError', $event->getArguments()); |
|
| 461 | + } |
|
| 462 | + }); |
|
| 463 | + $dispatcher->addListener('\OC\Repair::info', function ($event) { |
|
| 464 | + if ($event instanceof GenericEvent) { |
|
| 465 | + $this->emit('\OC\Updater', 'repairInfo', $event->getArguments()); |
|
| 466 | + } |
|
| 467 | + }); |
|
| 468 | + $dispatcher->addListener('\OC\Repair::step', function ($event) { |
|
| 469 | + if ($event instanceof GenericEvent) { |
|
| 470 | + $this->emit('\OC\Updater', 'repairStep', $event->getArguments()); |
|
| 471 | + } |
|
| 472 | + }); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + private function logAllEvents() { |
|
| 476 | + $log = $this->log; |
|
| 477 | + |
|
| 478 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 479 | + $dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($log) { |
|
| 480 | + if (!$event instanceof GenericEvent) { |
|
| 481 | + return; |
|
| 482 | + } |
|
| 483 | + $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']); |
|
| 484 | + }); |
|
| 485 | + $dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) { |
|
| 486 | + if (!$event instanceof GenericEvent) { |
|
| 487 | + return; |
|
| 488 | + } |
|
| 489 | + $log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']); |
|
| 490 | + }); |
|
| 491 | + |
|
| 492 | + $repairListener = function($event) use ($log) { |
|
| 493 | + if (!$event instanceof GenericEvent) { |
|
| 494 | + return; |
|
| 495 | + } |
|
| 496 | + switch ($event->getSubject()) { |
|
| 497 | + case '\OC\Repair::startProgress': |
|
| 498 | + $log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']); |
|
| 499 | + break; |
|
| 500 | + case '\OC\Repair::advance': |
|
| 501 | + $desc = $event->getArgument(1); |
|
| 502 | + if (empty($desc)) { |
|
| 503 | + $desc = ''; |
|
| 504 | + } |
|
| 505 | + $log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']); |
|
| 506 | + |
|
| 507 | + break; |
|
| 508 | + case '\OC\Repair::finishProgress': |
|
| 509 | + $log->info('\OC\Repair::finishProgress', ['app' => 'updater']); |
|
| 510 | + break; |
|
| 511 | + case '\OC\Repair::step': |
|
| 512 | + $log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 513 | + break; |
|
| 514 | + case '\OC\Repair::info': |
|
| 515 | + $log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 516 | + break; |
|
| 517 | + case '\OC\Repair::warning': |
|
| 518 | + $log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 519 | + break; |
|
| 520 | + case '\OC\Repair::error': |
|
| 521 | + $log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']); |
|
| 522 | + break; |
|
| 523 | + } |
|
| 524 | + }; |
|
| 525 | + |
|
| 526 | + $dispatcher->addListener('\OC\Repair::startProgress', $repairListener); |
|
| 527 | + $dispatcher->addListener('\OC\Repair::advance', $repairListener); |
|
| 528 | + $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener); |
|
| 529 | + $dispatcher->addListener('\OC\Repair::step', $repairListener); |
|
| 530 | + $dispatcher->addListener('\OC\Repair::info', $repairListener); |
|
| 531 | + $dispatcher->addListener('\OC\Repair::warning', $repairListener); |
|
| 532 | + $dispatcher->addListener('\OC\Repair::error', $repairListener); |
|
| 533 | + |
|
| 534 | + |
|
| 535 | + $this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) { |
|
| 536 | + $log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']); |
|
| 537 | + }); |
|
| 538 | + $this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) { |
|
| 539 | + $log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']); |
|
| 540 | + }); |
|
| 541 | + $this->listen('\OC\Updater', 'maintenanceActive', function () use($log) { |
|
| 542 | + $log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']); |
|
| 543 | + }); |
|
| 544 | + $this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) { |
|
| 545 | + if ($success) { |
|
| 546 | + $log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']); |
|
| 547 | + } else { |
|
| 548 | + $log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']); |
|
| 549 | + } |
|
| 550 | + }); |
|
| 551 | + $this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) { |
|
| 552 | + $log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']); |
|
| 553 | + }); |
|
| 554 | + $this->listen('\OC\Updater', 'dbUpgrade', function () use($log) { |
|
| 555 | + $log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']); |
|
| 556 | + }); |
|
| 557 | + $this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) { |
|
| 558 | + $log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']); |
|
| 559 | + }); |
|
| 560 | + $this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) { |
|
| 561 | + $log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']); |
|
| 562 | + }); |
|
| 563 | + $this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) { |
|
| 564 | + $log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']); |
|
| 565 | + }); |
|
| 566 | + $this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) { |
|
| 567 | + $log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']); |
|
| 568 | + }); |
|
| 569 | + $this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) { |
|
| 570 | + $log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']); |
|
| 571 | + }); |
|
| 572 | + $this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) { |
|
| 573 | + $log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']); |
|
| 574 | + }); |
|
| 575 | + $this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) { |
|
| 576 | + $log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']); |
|
| 577 | + }); |
|
| 578 | + $this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) { |
|
| 579 | + $log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']); |
|
| 580 | + }); |
|
| 581 | + $this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) { |
|
| 582 | + $log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']); |
|
| 583 | + }); |
|
| 584 | + $this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) { |
|
| 585 | + $log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']); |
|
| 586 | + }); |
|
| 587 | + $this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) { |
|
| 588 | + $log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']); |
|
| 589 | + }); |
|
| 590 | + $this->listen('\OC\Updater', 'failure', function ($message) use($log) { |
|
| 591 | + $log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']); |
|
| 592 | + }); |
|
| 593 | + $this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) { |
|
| 594 | + $log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']); |
|
| 595 | + }); |
|
| 596 | + $this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) { |
|
| 597 | + $log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']); |
|
| 598 | + }); |
|
| 599 | + $this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) { |
|
| 600 | + $log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']); |
|
| 601 | + }); |
|
| 602 | + $this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) { |
|
| 603 | + $log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']); |
|
| 604 | + }); |
|
| 605 | + |
|
| 606 | + } |
|
| 607 | 607 | |
| 608 | 608 | } |
| 609 | 609 | |