@@ -52,563 +52,563 @@ |
||
| 52 | 52 | * This class provides the functionality needed to install, update and remove apps |
| 53 | 53 | */ |
| 54 | 54 | class Installer { |
| 55 | - /** @var AppFetcher */ |
|
| 56 | - private $appFetcher; |
|
| 57 | - /** @var IClientService */ |
|
| 58 | - private $clientService; |
|
| 59 | - /** @var ITempManager */ |
|
| 60 | - private $tempManager; |
|
| 61 | - /** @var ILogger */ |
|
| 62 | - private $logger; |
|
| 63 | - /** @var IConfig */ |
|
| 64 | - private $config; |
|
| 65 | - /** @var array - for caching the result of app fetcher */ |
|
| 66 | - private $apps = null; |
|
| 67 | - /** @var bool|null - for caching the result of the ready status */ |
|
| 68 | - private $isInstanceReadyForUpdates = null; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param AppFetcher $appFetcher |
|
| 72 | - * @param IClientService $clientService |
|
| 73 | - * @param ITempManager $tempManager |
|
| 74 | - * @param ILogger $logger |
|
| 75 | - * @param IConfig $config |
|
| 76 | - */ |
|
| 77 | - public function __construct(AppFetcher $appFetcher, |
|
| 78 | - IClientService $clientService, |
|
| 79 | - ITempManager $tempManager, |
|
| 80 | - ILogger $logger, |
|
| 81 | - IConfig $config) { |
|
| 82 | - $this->appFetcher = $appFetcher; |
|
| 83 | - $this->clientService = $clientService; |
|
| 84 | - $this->tempManager = $tempManager; |
|
| 85 | - $this->logger = $logger; |
|
| 86 | - $this->config = $config; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Installs an app that is located in one of the app folders already |
|
| 91 | - * |
|
| 92 | - * @param string $appId App to install |
|
| 93 | - * @throws \Exception |
|
| 94 | - * @return string app ID |
|
| 95 | - */ |
|
| 96 | - public function installApp($appId) { |
|
| 97 | - $app = \OC_App::findAppInDirectories($appId); |
|
| 98 | - if($app === false) { |
|
| 99 | - throw new \Exception('App not found in any app directory'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $basedir = $app['path'].'/'.$appId; |
|
| 103 | - $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true); |
|
| 104 | - |
|
| 105 | - $l = \OC::$server->getL10N('core'); |
|
| 106 | - |
|
| 107 | - if(!is_array($info)) { |
|
| 108 | - throw new \Exception( |
|
| 109 | - $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 110 | - [$appId] |
|
| 111 | - ) |
|
| 112 | - ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $version = implode('.', \OCP\Util::getVersion()); |
|
| 116 | - if (!\OC_App::isAppCompatible($version, $info)) { |
|
| 117 | - throw new \Exception( |
|
| 118 | - // TODO $l |
|
| 119 | - $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 120 | - [$info['name']] |
|
| 121 | - ) |
|
| 122 | - ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // check for required dependencies |
|
| 126 | - \OC_App::checkAppDependencies($this->config, $l, $info); |
|
| 127 | - \OC_App::registerAutoloading($appId, $basedir); |
|
| 128 | - |
|
| 129 | - //install the database |
|
| 130 | - if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 131 | - if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) { |
|
| 132 | - OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 133 | - } else { |
|
| 134 | - OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 135 | - } |
|
| 136 | - } else { |
|
| 137 | - $ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection()); |
|
| 138 | - $ms->migrate(); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 142 | - |
|
| 143 | - //run appinfo/install.php |
|
| 144 | - if(!isset($data['noinstall']) or $data['noinstall']==false) { |
|
| 145 | - self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $appData = OC_App::getAppInfo($appId); |
|
| 149 | - OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']); |
|
| 150 | - |
|
| 151 | - //set the installed version |
|
| 152 | - \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false)); |
|
| 153 | - \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
|
| 154 | - |
|
| 155 | - //set remote/public handlers |
|
| 156 | - foreach($info['remote'] as $name=>$path) { |
|
| 157 | - \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
|
| 158 | - } |
|
| 159 | - foreach($info['public'] as $name=>$path) { |
|
| 160 | - \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - OC_App::setAppTypes($info['id']); |
|
| 164 | - |
|
| 165 | - return $info['id']; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @brief checks whether or not an app is installed |
|
| 170 | - * @param string $app app |
|
| 171 | - * @returns bool |
|
| 172 | - * |
|
| 173 | - * Checks whether or not an app is installed, i.e. registered in apps table. |
|
| 174 | - */ |
|
| 175 | - public static function isInstalled( $app ) { |
|
| 176 | - return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Updates the specified app from the appstore |
|
| 181 | - * |
|
| 182 | - * @param string $appId |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - public function updateAppstoreApp($appId) { |
|
| 186 | - if($this->isUpdateAvailable($appId)) { |
|
| 187 | - try { |
|
| 188 | - $this->downloadApp($appId); |
|
| 189 | - } catch (\Exception $e) { |
|
| 190 | - $this->logger->logException($e, [ |
|
| 191 | - 'level' => \OCP\Util::ERROR, |
|
| 192 | - 'app' => 'core', |
|
| 193 | - ]); |
|
| 194 | - return false; |
|
| 195 | - } |
|
| 196 | - return OC_App::updateApp($appId); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Downloads an app and puts it into the app directory |
|
| 204 | - * |
|
| 205 | - * @param string $appId |
|
| 206 | - * |
|
| 207 | - * @throws \Exception If the installation was not successful |
|
| 208 | - */ |
|
| 209 | - public function downloadApp($appId) { |
|
| 210 | - $appId = strtolower($appId); |
|
| 211 | - |
|
| 212 | - $apps = $this->appFetcher->get(); |
|
| 213 | - foreach($apps as $app) { |
|
| 214 | - if($app['id'] === $appId) { |
|
| 215 | - // Load the certificate |
|
| 216 | - $certificate = new X509(); |
|
| 217 | - $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 218 | - $loadedCertificate = $certificate->loadX509($app['certificate']); |
|
| 219 | - |
|
| 220 | - // Verify if the certificate has been revoked |
|
| 221 | - $crl = new X509(); |
|
| 222 | - $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 223 | - $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 224 | - if($crl->validateSignature() !== true) { |
|
| 225 | - throw new \Exception('Could not validate CRL signature'); |
|
| 226 | - } |
|
| 227 | - $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
|
| 228 | - $revoked = $crl->getRevoked($csn); |
|
| 229 | - if ($revoked !== false) { |
|
| 230 | - throw new \Exception( |
|
| 231 | - sprintf( |
|
| 232 | - 'Certificate "%s" has been revoked', |
|
| 233 | - $csn |
|
| 234 | - ) |
|
| 235 | - ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
|
| 239 | - if($certificate->validateSignature() !== true) { |
|
| 240 | - throw new \Exception( |
|
| 241 | - sprintf( |
|
| 242 | - 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
|
| 243 | - $appId |
|
| 244 | - ) |
|
| 245 | - ); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Verify if the certificate is issued for the requested app id |
|
| 249 | - $certInfo = openssl_x509_parse($app['certificate']); |
|
| 250 | - if(!isset($certInfo['subject']['CN'])) { |
|
| 251 | - throw new \Exception( |
|
| 252 | - sprintf( |
|
| 253 | - 'App with id %s has a cert with no CN', |
|
| 254 | - $appId |
|
| 255 | - ) |
|
| 256 | - ); |
|
| 257 | - } |
|
| 258 | - if($certInfo['subject']['CN'] !== $appId) { |
|
| 259 | - throw new \Exception( |
|
| 260 | - sprintf( |
|
| 261 | - 'App with id %s has a cert issued to %s', |
|
| 262 | - $appId, |
|
| 263 | - $certInfo['subject']['CN'] |
|
| 264 | - ) |
|
| 265 | - ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - // Download the release |
|
| 269 | - $tempFile = $this->tempManager->getTemporaryFile('.tar.gz'); |
|
| 270 | - $client = $this->clientService->newClient(); |
|
| 271 | - $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]); |
|
| 272 | - |
|
| 273 | - // Check if the signature actually matches the downloaded content |
|
| 274 | - $certificate = openssl_get_publickey($app['certificate']); |
|
| 275 | - $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 276 | - openssl_free_key($certificate); |
|
| 277 | - |
|
| 278 | - if($verified === true) { |
|
| 279 | - // Seems to match, let's proceed |
|
| 280 | - $extractDir = $this->tempManager->getTemporaryFolder(); |
|
| 281 | - $archive = new TAR($tempFile); |
|
| 282 | - |
|
| 283 | - if($archive) { |
|
| 284 | - if (!$archive->extract($extractDir)) { |
|
| 285 | - throw new \Exception( |
|
| 286 | - sprintf( |
|
| 287 | - 'Could not extract app %s', |
|
| 288 | - $appId |
|
| 289 | - ) |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - $allFiles = scandir($extractDir); |
|
| 293 | - $folders = array_diff($allFiles, ['.', '..']); |
|
| 294 | - $folders = array_values($folders); |
|
| 295 | - |
|
| 296 | - if(count($folders) > 1) { |
|
| 297 | - throw new \Exception( |
|
| 298 | - sprintf( |
|
| 299 | - 'Extracted app %s has more than 1 folder', |
|
| 300 | - $appId |
|
| 301 | - ) |
|
| 302 | - ); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - // Check if appinfo/info.xml has the same app ID as well |
|
| 306 | - $loadEntities = libxml_disable_entity_loader(false); |
|
| 307 | - $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 308 | - libxml_disable_entity_loader($loadEntities); |
|
| 309 | - if((string)$xml->id !== $appId) { |
|
| 310 | - throw new \Exception( |
|
| 311 | - sprintf( |
|
| 312 | - 'App for id %s has a wrong app ID in info.xml: %s', |
|
| 313 | - $appId, |
|
| 314 | - (string)$xml->id |
|
| 315 | - ) |
|
| 316 | - ); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // Check if the version is lower than before |
|
| 320 | - $currentVersion = OC_App::getAppVersion($appId); |
|
| 321 | - $newVersion = (string)$xml->version; |
|
| 322 | - if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 323 | - throw new \Exception( |
|
| 324 | - sprintf( |
|
| 325 | - 'App for id %s has version %s and tried to update to lower version %s', |
|
| 326 | - $appId, |
|
| 327 | - $currentVersion, |
|
| 328 | - $newVersion |
|
| 329 | - ) |
|
| 330 | - ); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 334 | - // Remove old app with the ID if existent |
|
| 335 | - OC_Helper::rmdirr($baseDir); |
|
| 336 | - // Move to app folder |
|
| 337 | - if(@mkdir($baseDir)) { |
|
| 338 | - $extractDir .= '/' . $folders[0]; |
|
| 339 | - OC_Helper::copyr($extractDir, $baseDir); |
|
| 340 | - } |
|
| 341 | - OC_Helper::copyr($extractDir, $baseDir); |
|
| 342 | - OC_Helper::rmdirr($extractDir); |
|
| 343 | - return; |
|
| 344 | - } else { |
|
| 345 | - throw new \Exception( |
|
| 346 | - sprintf( |
|
| 347 | - 'Could not extract app with ID %s to %s', |
|
| 348 | - $appId, |
|
| 349 | - $extractDir |
|
| 350 | - ) |
|
| 351 | - ); |
|
| 352 | - } |
|
| 353 | - } else { |
|
| 354 | - // Signature does not match |
|
| 355 | - throw new \Exception( |
|
| 356 | - sprintf( |
|
| 357 | - 'App with id %s has invalid signature', |
|
| 358 | - $appId |
|
| 359 | - ) |
|
| 360 | - ); |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - throw new \Exception( |
|
| 366 | - sprintf( |
|
| 367 | - 'Could not download app %s', |
|
| 368 | - $appId |
|
| 369 | - ) |
|
| 370 | - ); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Check if an update for the app is available |
|
| 375 | - * |
|
| 376 | - * @param string $appId |
|
| 377 | - * @return string|false false or the version number of the update |
|
| 378 | - */ |
|
| 379 | - public function isUpdateAvailable($appId) { |
|
| 380 | - if ($this->isInstanceReadyForUpdates === null) { |
|
| 381 | - $installPath = OC_App::getInstallPath(); |
|
| 382 | - if ($installPath === false || $installPath === null) { |
|
| 383 | - $this->isInstanceReadyForUpdates = false; |
|
| 384 | - } else { |
|
| 385 | - $this->isInstanceReadyForUpdates = true; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if ($this->isInstanceReadyForUpdates === false) { |
|
| 390 | - return false; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - if ($this->isInstalledFromGit($appId) === true) { |
|
| 394 | - return false; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ($this->apps === null) { |
|
| 398 | - $this->apps = $this->appFetcher->get(); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - foreach($this->apps as $app) { |
|
| 402 | - if($app['id'] === $appId) { |
|
| 403 | - $currentVersion = OC_App::getAppVersion($appId); |
|
| 404 | - $newestVersion = $app['releases'][0]['version']; |
|
| 405 | - if (version_compare($newestVersion, $currentVersion, '>')) { |
|
| 406 | - return $newestVersion; |
|
| 407 | - } else { |
|
| 408 | - return false; |
|
| 409 | - } |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - return false; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Check if app has been installed from git |
|
| 418 | - * @param string $name name of the application to remove |
|
| 419 | - * @return boolean |
|
| 420 | - * |
|
| 421 | - * The function will check if the path contains a .git folder |
|
| 422 | - */ |
|
| 423 | - private function isInstalledFromGit($appId) { |
|
| 424 | - $app = \OC_App::findAppInDirectories($appId); |
|
| 425 | - if($app === false) { |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - $basedir = $app['path'].'/'.$appId; |
|
| 429 | - return file_exists($basedir.'/.git/'); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Check if app is already downloaded |
|
| 434 | - * @param string $name name of the application to remove |
|
| 435 | - * @return boolean |
|
| 436 | - * |
|
| 437 | - * The function will check if the app is already downloaded in the apps repository |
|
| 438 | - */ |
|
| 439 | - public function isDownloaded($name) { |
|
| 440 | - foreach(\OC::$APPSROOTS as $dir) { |
|
| 441 | - $dirToTest = $dir['path']; |
|
| 442 | - $dirToTest .= '/'; |
|
| 443 | - $dirToTest .= $name; |
|
| 444 | - $dirToTest .= '/'; |
|
| 445 | - |
|
| 446 | - if (is_dir($dirToTest)) { |
|
| 447 | - return true; |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - return false; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Removes an app |
|
| 456 | - * @param string $appId ID of the application to remove |
|
| 457 | - * @return boolean |
|
| 458 | - * |
|
| 459 | - * |
|
| 460 | - * This function works as follows |
|
| 461 | - * -# call uninstall repair steps |
|
| 462 | - * -# removing the files |
|
| 463 | - * |
|
| 464 | - * The function will not delete preferences, tables and the configuration, |
|
| 465 | - * this has to be done by the function oc_app_uninstall(). |
|
| 466 | - */ |
|
| 467 | - public function removeApp($appId) { |
|
| 468 | - if($this->isDownloaded( $appId )) { |
|
| 469 | - if (\OC::$server->getAppManager()->isShipped($appId)) { |
|
| 470 | - return false; |
|
| 471 | - } |
|
| 472 | - $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 473 | - OC_Helper::rmdirr($appDir); |
|
| 474 | - return true; |
|
| 475 | - }else{ |
|
| 476 | - \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
|
| 477 | - |
|
| 478 | - return false; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Installs the app within the bundle and marks the bundle as installed |
|
| 485 | - * |
|
| 486 | - * @param Bundle $bundle |
|
| 487 | - * @throws \Exception If app could not get installed |
|
| 488 | - */ |
|
| 489 | - public function installAppBundle(Bundle $bundle) { |
|
| 490 | - $appIds = $bundle->getAppIdentifiers(); |
|
| 491 | - foreach($appIds as $appId) { |
|
| 492 | - if(!$this->isDownloaded($appId)) { |
|
| 493 | - $this->downloadApp($appId); |
|
| 494 | - } |
|
| 495 | - $this->installApp($appId); |
|
| 496 | - $app = new OC_App(); |
|
| 497 | - $app->enable($appId); |
|
| 498 | - } |
|
| 499 | - $bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true); |
|
| 500 | - $bundles[] = $bundle->getIdentifier(); |
|
| 501 | - $this->config->setAppValue('core', 'installed.bundles', json_encode($bundles)); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Installs shipped apps |
|
| 506 | - * |
|
| 507 | - * This function installs all apps found in the 'apps' directory that should be enabled by default; |
|
| 508 | - * @param bool $softErrors When updating we ignore errors and simply log them, better to have a |
|
| 509 | - * working ownCloud at the end instead of an aborted update. |
|
| 510 | - * @return array Array of error messages (appid => Exception) |
|
| 511 | - */ |
|
| 512 | - public static function installShippedApps($softErrors = false) { |
|
| 513 | - $errors = []; |
|
| 514 | - foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 515 | - if($dir = opendir( $app_dir['path'] )) { |
|
| 516 | - while( false !== ( $filename = readdir( $dir ))) { |
|
| 517 | - if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 518 | - if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 519 | - if(!Installer::isInstalled($filename)) { |
|
| 520 | - $info=OC_App::getAppInfo($filename); |
|
| 521 | - $enabled = isset($info['default_enable']); |
|
| 522 | - if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
|
| 523 | - && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
|
| 524 | - if ($softErrors) { |
|
| 525 | - try { |
|
| 526 | - Installer::installShippedApp($filename); |
|
| 527 | - } catch (HintException $e) { |
|
| 528 | - if ($e->getPrevious() instanceof TableExistsException) { |
|
| 529 | - $errors[$filename] = $e; |
|
| 530 | - continue; |
|
| 531 | - } |
|
| 532 | - throw $e; |
|
| 533 | - } |
|
| 534 | - } else { |
|
| 535 | - Installer::installShippedApp($filename); |
|
| 536 | - } |
|
| 537 | - \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes'); |
|
| 538 | - } |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - closedir( $dir ); |
|
| 544 | - } |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - return $errors; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * install an app already placed in the app folder |
|
| 552 | - * @param string $app id of the app to install |
|
| 553 | - * @return integer |
|
| 554 | - */ |
|
| 555 | - public static function installShippedApp($app) { |
|
| 556 | - //install the database |
|
| 557 | - $appPath = OC_App::getAppPath($app); |
|
| 558 | - \OC_App::registerAutoloading($app, $appPath); |
|
| 559 | - |
|
| 560 | - if(is_file("$appPath/appinfo/database.xml")) { |
|
| 561 | - try { |
|
| 562 | - OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
|
| 563 | - } catch (TableExistsException $e) { |
|
| 564 | - throw new HintException( |
|
| 565 | - 'Failed to enable app ' . $app, |
|
| 566 | - 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.', |
|
| 567 | - 0, $e |
|
| 568 | - ); |
|
| 569 | - } |
|
| 570 | - } else { |
|
| 571 | - $ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection()); |
|
| 572 | - $ms->migrate(); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - //run appinfo/install.php |
|
| 576 | - self::includeAppScript("$appPath/appinfo/install.php"); |
|
| 577 | - |
|
| 578 | - $info = OC_App::getAppInfo($app); |
|
| 579 | - if (is_null($info)) { |
|
| 580 | - return false; |
|
| 581 | - } |
|
| 582 | - \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 583 | - |
|
| 584 | - OC_App::executeRepairSteps($app, $info['repair-steps']['install']); |
|
| 585 | - |
|
| 586 | - $config = \OC::$server->getConfig(); |
|
| 587 | - |
|
| 588 | - $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app)); |
|
| 589 | - if (array_key_exists('ocsid', $info)) { |
|
| 590 | - $config->setAppValue($app, 'ocsid', $info['ocsid']); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - //set remote/public handlers |
|
| 594 | - foreach($info['remote'] as $name=>$path) { |
|
| 595 | - $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
|
| 596 | - } |
|
| 597 | - foreach($info['public'] as $name=>$path) { |
|
| 598 | - $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - OC_App::setAppTypes($info['id']); |
|
| 602 | - |
|
| 603 | - return $info['id']; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * @param string $script |
|
| 608 | - */ |
|
| 609 | - private static function includeAppScript($script) { |
|
| 610 | - if ( file_exists($script) ){ |
|
| 611 | - include $script; |
|
| 612 | - } |
|
| 613 | - } |
|
| 55 | + /** @var AppFetcher */ |
|
| 56 | + private $appFetcher; |
|
| 57 | + /** @var IClientService */ |
|
| 58 | + private $clientService; |
|
| 59 | + /** @var ITempManager */ |
|
| 60 | + private $tempManager; |
|
| 61 | + /** @var ILogger */ |
|
| 62 | + private $logger; |
|
| 63 | + /** @var IConfig */ |
|
| 64 | + private $config; |
|
| 65 | + /** @var array - for caching the result of app fetcher */ |
|
| 66 | + private $apps = null; |
|
| 67 | + /** @var bool|null - for caching the result of the ready status */ |
|
| 68 | + private $isInstanceReadyForUpdates = null; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param AppFetcher $appFetcher |
|
| 72 | + * @param IClientService $clientService |
|
| 73 | + * @param ITempManager $tempManager |
|
| 74 | + * @param ILogger $logger |
|
| 75 | + * @param IConfig $config |
|
| 76 | + */ |
|
| 77 | + public function __construct(AppFetcher $appFetcher, |
|
| 78 | + IClientService $clientService, |
|
| 79 | + ITempManager $tempManager, |
|
| 80 | + ILogger $logger, |
|
| 81 | + IConfig $config) { |
|
| 82 | + $this->appFetcher = $appFetcher; |
|
| 83 | + $this->clientService = $clientService; |
|
| 84 | + $this->tempManager = $tempManager; |
|
| 85 | + $this->logger = $logger; |
|
| 86 | + $this->config = $config; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Installs an app that is located in one of the app folders already |
|
| 91 | + * |
|
| 92 | + * @param string $appId App to install |
|
| 93 | + * @throws \Exception |
|
| 94 | + * @return string app ID |
|
| 95 | + */ |
|
| 96 | + public function installApp($appId) { |
|
| 97 | + $app = \OC_App::findAppInDirectories($appId); |
|
| 98 | + if($app === false) { |
|
| 99 | + throw new \Exception('App not found in any app directory'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $basedir = $app['path'].'/'.$appId; |
|
| 103 | + $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true); |
|
| 104 | + |
|
| 105 | + $l = \OC::$server->getL10N('core'); |
|
| 106 | + |
|
| 107 | + if(!is_array($info)) { |
|
| 108 | + throw new \Exception( |
|
| 109 | + $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 110 | + [$appId] |
|
| 111 | + ) |
|
| 112 | + ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $version = implode('.', \OCP\Util::getVersion()); |
|
| 116 | + if (!\OC_App::isAppCompatible($version, $info)) { |
|
| 117 | + throw new \Exception( |
|
| 118 | + // TODO $l |
|
| 119 | + $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 120 | + [$info['name']] |
|
| 121 | + ) |
|
| 122 | + ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // check for required dependencies |
|
| 126 | + \OC_App::checkAppDependencies($this->config, $l, $info); |
|
| 127 | + \OC_App::registerAutoloading($appId, $basedir); |
|
| 128 | + |
|
| 129 | + //install the database |
|
| 130 | + if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 131 | + if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) { |
|
| 132 | + OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 133 | + } else { |
|
| 134 | + OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 135 | + } |
|
| 136 | + } else { |
|
| 137 | + $ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection()); |
|
| 138 | + $ms->migrate(); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 142 | + |
|
| 143 | + //run appinfo/install.php |
|
| 144 | + if(!isset($data['noinstall']) or $data['noinstall']==false) { |
|
| 145 | + self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $appData = OC_App::getAppInfo($appId); |
|
| 149 | + OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']); |
|
| 150 | + |
|
| 151 | + //set the installed version |
|
| 152 | + \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false)); |
|
| 153 | + \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
|
| 154 | + |
|
| 155 | + //set remote/public handlers |
|
| 156 | + foreach($info['remote'] as $name=>$path) { |
|
| 157 | + \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
|
| 158 | + } |
|
| 159 | + foreach($info['public'] as $name=>$path) { |
|
| 160 | + \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + OC_App::setAppTypes($info['id']); |
|
| 164 | + |
|
| 165 | + return $info['id']; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @brief checks whether or not an app is installed |
|
| 170 | + * @param string $app app |
|
| 171 | + * @returns bool |
|
| 172 | + * |
|
| 173 | + * Checks whether or not an app is installed, i.e. registered in apps table. |
|
| 174 | + */ |
|
| 175 | + public static function isInstalled( $app ) { |
|
| 176 | + return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Updates the specified app from the appstore |
|
| 181 | + * |
|
| 182 | + * @param string $appId |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + public function updateAppstoreApp($appId) { |
|
| 186 | + if($this->isUpdateAvailable($appId)) { |
|
| 187 | + try { |
|
| 188 | + $this->downloadApp($appId); |
|
| 189 | + } catch (\Exception $e) { |
|
| 190 | + $this->logger->logException($e, [ |
|
| 191 | + 'level' => \OCP\Util::ERROR, |
|
| 192 | + 'app' => 'core', |
|
| 193 | + ]); |
|
| 194 | + return false; |
|
| 195 | + } |
|
| 196 | + return OC_App::updateApp($appId); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Downloads an app and puts it into the app directory |
|
| 204 | + * |
|
| 205 | + * @param string $appId |
|
| 206 | + * |
|
| 207 | + * @throws \Exception If the installation was not successful |
|
| 208 | + */ |
|
| 209 | + public function downloadApp($appId) { |
|
| 210 | + $appId = strtolower($appId); |
|
| 211 | + |
|
| 212 | + $apps = $this->appFetcher->get(); |
|
| 213 | + foreach($apps as $app) { |
|
| 214 | + if($app['id'] === $appId) { |
|
| 215 | + // Load the certificate |
|
| 216 | + $certificate = new X509(); |
|
| 217 | + $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 218 | + $loadedCertificate = $certificate->loadX509($app['certificate']); |
|
| 219 | + |
|
| 220 | + // Verify if the certificate has been revoked |
|
| 221 | + $crl = new X509(); |
|
| 222 | + $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 223 | + $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 224 | + if($crl->validateSignature() !== true) { |
|
| 225 | + throw new \Exception('Could not validate CRL signature'); |
|
| 226 | + } |
|
| 227 | + $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
|
| 228 | + $revoked = $crl->getRevoked($csn); |
|
| 229 | + if ($revoked !== false) { |
|
| 230 | + throw new \Exception( |
|
| 231 | + sprintf( |
|
| 232 | + 'Certificate "%s" has been revoked', |
|
| 233 | + $csn |
|
| 234 | + ) |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
|
| 239 | + if($certificate->validateSignature() !== true) { |
|
| 240 | + throw new \Exception( |
|
| 241 | + sprintf( |
|
| 242 | + 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
|
| 243 | + $appId |
|
| 244 | + ) |
|
| 245 | + ); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Verify if the certificate is issued for the requested app id |
|
| 249 | + $certInfo = openssl_x509_parse($app['certificate']); |
|
| 250 | + if(!isset($certInfo['subject']['CN'])) { |
|
| 251 | + throw new \Exception( |
|
| 252 | + sprintf( |
|
| 253 | + 'App with id %s has a cert with no CN', |
|
| 254 | + $appId |
|
| 255 | + ) |
|
| 256 | + ); |
|
| 257 | + } |
|
| 258 | + if($certInfo['subject']['CN'] !== $appId) { |
|
| 259 | + throw new \Exception( |
|
| 260 | + sprintf( |
|
| 261 | + 'App with id %s has a cert issued to %s', |
|
| 262 | + $appId, |
|
| 263 | + $certInfo['subject']['CN'] |
|
| 264 | + ) |
|
| 265 | + ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + // Download the release |
|
| 269 | + $tempFile = $this->tempManager->getTemporaryFile('.tar.gz'); |
|
| 270 | + $client = $this->clientService->newClient(); |
|
| 271 | + $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]); |
|
| 272 | + |
|
| 273 | + // Check if the signature actually matches the downloaded content |
|
| 274 | + $certificate = openssl_get_publickey($app['certificate']); |
|
| 275 | + $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 276 | + openssl_free_key($certificate); |
|
| 277 | + |
|
| 278 | + if($verified === true) { |
|
| 279 | + // Seems to match, let's proceed |
|
| 280 | + $extractDir = $this->tempManager->getTemporaryFolder(); |
|
| 281 | + $archive = new TAR($tempFile); |
|
| 282 | + |
|
| 283 | + if($archive) { |
|
| 284 | + if (!$archive->extract($extractDir)) { |
|
| 285 | + throw new \Exception( |
|
| 286 | + sprintf( |
|
| 287 | + 'Could not extract app %s', |
|
| 288 | + $appId |
|
| 289 | + ) |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + $allFiles = scandir($extractDir); |
|
| 293 | + $folders = array_diff($allFiles, ['.', '..']); |
|
| 294 | + $folders = array_values($folders); |
|
| 295 | + |
|
| 296 | + if(count($folders) > 1) { |
|
| 297 | + throw new \Exception( |
|
| 298 | + sprintf( |
|
| 299 | + 'Extracted app %s has more than 1 folder', |
|
| 300 | + $appId |
|
| 301 | + ) |
|
| 302 | + ); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + // Check if appinfo/info.xml has the same app ID as well |
|
| 306 | + $loadEntities = libxml_disable_entity_loader(false); |
|
| 307 | + $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 308 | + libxml_disable_entity_loader($loadEntities); |
|
| 309 | + if((string)$xml->id !== $appId) { |
|
| 310 | + throw new \Exception( |
|
| 311 | + sprintf( |
|
| 312 | + 'App for id %s has a wrong app ID in info.xml: %s', |
|
| 313 | + $appId, |
|
| 314 | + (string)$xml->id |
|
| 315 | + ) |
|
| 316 | + ); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // Check if the version is lower than before |
|
| 320 | + $currentVersion = OC_App::getAppVersion($appId); |
|
| 321 | + $newVersion = (string)$xml->version; |
|
| 322 | + if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 323 | + throw new \Exception( |
|
| 324 | + sprintf( |
|
| 325 | + 'App for id %s has version %s and tried to update to lower version %s', |
|
| 326 | + $appId, |
|
| 327 | + $currentVersion, |
|
| 328 | + $newVersion |
|
| 329 | + ) |
|
| 330 | + ); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 334 | + // Remove old app with the ID if existent |
|
| 335 | + OC_Helper::rmdirr($baseDir); |
|
| 336 | + // Move to app folder |
|
| 337 | + if(@mkdir($baseDir)) { |
|
| 338 | + $extractDir .= '/' . $folders[0]; |
|
| 339 | + OC_Helper::copyr($extractDir, $baseDir); |
|
| 340 | + } |
|
| 341 | + OC_Helper::copyr($extractDir, $baseDir); |
|
| 342 | + OC_Helper::rmdirr($extractDir); |
|
| 343 | + return; |
|
| 344 | + } else { |
|
| 345 | + throw new \Exception( |
|
| 346 | + sprintf( |
|
| 347 | + 'Could not extract app with ID %s to %s', |
|
| 348 | + $appId, |
|
| 349 | + $extractDir |
|
| 350 | + ) |
|
| 351 | + ); |
|
| 352 | + } |
|
| 353 | + } else { |
|
| 354 | + // Signature does not match |
|
| 355 | + throw new \Exception( |
|
| 356 | + sprintf( |
|
| 357 | + 'App with id %s has invalid signature', |
|
| 358 | + $appId |
|
| 359 | + ) |
|
| 360 | + ); |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + throw new \Exception( |
|
| 366 | + sprintf( |
|
| 367 | + 'Could not download app %s', |
|
| 368 | + $appId |
|
| 369 | + ) |
|
| 370 | + ); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Check if an update for the app is available |
|
| 375 | + * |
|
| 376 | + * @param string $appId |
|
| 377 | + * @return string|false false or the version number of the update |
|
| 378 | + */ |
|
| 379 | + public function isUpdateAvailable($appId) { |
|
| 380 | + if ($this->isInstanceReadyForUpdates === null) { |
|
| 381 | + $installPath = OC_App::getInstallPath(); |
|
| 382 | + if ($installPath === false || $installPath === null) { |
|
| 383 | + $this->isInstanceReadyForUpdates = false; |
|
| 384 | + } else { |
|
| 385 | + $this->isInstanceReadyForUpdates = true; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if ($this->isInstanceReadyForUpdates === false) { |
|
| 390 | + return false; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + if ($this->isInstalledFromGit($appId) === true) { |
|
| 394 | + return false; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ($this->apps === null) { |
|
| 398 | + $this->apps = $this->appFetcher->get(); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + foreach($this->apps as $app) { |
|
| 402 | + if($app['id'] === $appId) { |
|
| 403 | + $currentVersion = OC_App::getAppVersion($appId); |
|
| 404 | + $newestVersion = $app['releases'][0]['version']; |
|
| 405 | + if (version_compare($newestVersion, $currentVersion, '>')) { |
|
| 406 | + return $newestVersion; |
|
| 407 | + } else { |
|
| 408 | + return false; |
|
| 409 | + } |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + return false; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Check if app has been installed from git |
|
| 418 | + * @param string $name name of the application to remove |
|
| 419 | + * @return boolean |
|
| 420 | + * |
|
| 421 | + * The function will check if the path contains a .git folder |
|
| 422 | + */ |
|
| 423 | + private function isInstalledFromGit($appId) { |
|
| 424 | + $app = \OC_App::findAppInDirectories($appId); |
|
| 425 | + if($app === false) { |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + $basedir = $app['path'].'/'.$appId; |
|
| 429 | + return file_exists($basedir.'/.git/'); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Check if app is already downloaded |
|
| 434 | + * @param string $name name of the application to remove |
|
| 435 | + * @return boolean |
|
| 436 | + * |
|
| 437 | + * The function will check if the app is already downloaded in the apps repository |
|
| 438 | + */ |
|
| 439 | + public function isDownloaded($name) { |
|
| 440 | + foreach(\OC::$APPSROOTS as $dir) { |
|
| 441 | + $dirToTest = $dir['path']; |
|
| 442 | + $dirToTest .= '/'; |
|
| 443 | + $dirToTest .= $name; |
|
| 444 | + $dirToTest .= '/'; |
|
| 445 | + |
|
| 446 | + if (is_dir($dirToTest)) { |
|
| 447 | + return true; |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + return false; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Removes an app |
|
| 456 | + * @param string $appId ID of the application to remove |
|
| 457 | + * @return boolean |
|
| 458 | + * |
|
| 459 | + * |
|
| 460 | + * This function works as follows |
|
| 461 | + * -# call uninstall repair steps |
|
| 462 | + * -# removing the files |
|
| 463 | + * |
|
| 464 | + * The function will not delete preferences, tables and the configuration, |
|
| 465 | + * this has to be done by the function oc_app_uninstall(). |
|
| 466 | + */ |
|
| 467 | + public function removeApp($appId) { |
|
| 468 | + if($this->isDownloaded( $appId )) { |
|
| 469 | + if (\OC::$server->getAppManager()->isShipped($appId)) { |
|
| 470 | + return false; |
|
| 471 | + } |
|
| 472 | + $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 473 | + OC_Helper::rmdirr($appDir); |
|
| 474 | + return true; |
|
| 475 | + }else{ |
|
| 476 | + \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
|
| 477 | + |
|
| 478 | + return false; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Installs the app within the bundle and marks the bundle as installed |
|
| 485 | + * |
|
| 486 | + * @param Bundle $bundle |
|
| 487 | + * @throws \Exception If app could not get installed |
|
| 488 | + */ |
|
| 489 | + public function installAppBundle(Bundle $bundle) { |
|
| 490 | + $appIds = $bundle->getAppIdentifiers(); |
|
| 491 | + foreach($appIds as $appId) { |
|
| 492 | + if(!$this->isDownloaded($appId)) { |
|
| 493 | + $this->downloadApp($appId); |
|
| 494 | + } |
|
| 495 | + $this->installApp($appId); |
|
| 496 | + $app = new OC_App(); |
|
| 497 | + $app->enable($appId); |
|
| 498 | + } |
|
| 499 | + $bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true); |
|
| 500 | + $bundles[] = $bundle->getIdentifier(); |
|
| 501 | + $this->config->setAppValue('core', 'installed.bundles', json_encode($bundles)); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Installs shipped apps |
|
| 506 | + * |
|
| 507 | + * This function installs all apps found in the 'apps' directory that should be enabled by default; |
|
| 508 | + * @param bool $softErrors When updating we ignore errors and simply log them, better to have a |
|
| 509 | + * working ownCloud at the end instead of an aborted update. |
|
| 510 | + * @return array Array of error messages (appid => Exception) |
|
| 511 | + */ |
|
| 512 | + public static function installShippedApps($softErrors = false) { |
|
| 513 | + $errors = []; |
|
| 514 | + foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 515 | + if($dir = opendir( $app_dir['path'] )) { |
|
| 516 | + while( false !== ( $filename = readdir( $dir ))) { |
|
| 517 | + if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 518 | + if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 519 | + if(!Installer::isInstalled($filename)) { |
|
| 520 | + $info=OC_App::getAppInfo($filename); |
|
| 521 | + $enabled = isset($info['default_enable']); |
|
| 522 | + if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
|
| 523 | + && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
|
| 524 | + if ($softErrors) { |
|
| 525 | + try { |
|
| 526 | + Installer::installShippedApp($filename); |
|
| 527 | + } catch (HintException $e) { |
|
| 528 | + if ($e->getPrevious() instanceof TableExistsException) { |
|
| 529 | + $errors[$filename] = $e; |
|
| 530 | + continue; |
|
| 531 | + } |
|
| 532 | + throw $e; |
|
| 533 | + } |
|
| 534 | + } else { |
|
| 535 | + Installer::installShippedApp($filename); |
|
| 536 | + } |
|
| 537 | + \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes'); |
|
| 538 | + } |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + closedir( $dir ); |
|
| 544 | + } |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + return $errors; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * install an app already placed in the app folder |
|
| 552 | + * @param string $app id of the app to install |
|
| 553 | + * @return integer |
|
| 554 | + */ |
|
| 555 | + public static function installShippedApp($app) { |
|
| 556 | + //install the database |
|
| 557 | + $appPath = OC_App::getAppPath($app); |
|
| 558 | + \OC_App::registerAutoloading($app, $appPath); |
|
| 559 | + |
|
| 560 | + if(is_file("$appPath/appinfo/database.xml")) { |
|
| 561 | + try { |
|
| 562 | + OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
|
| 563 | + } catch (TableExistsException $e) { |
|
| 564 | + throw new HintException( |
|
| 565 | + 'Failed to enable app ' . $app, |
|
| 566 | + 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.', |
|
| 567 | + 0, $e |
|
| 568 | + ); |
|
| 569 | + } |
|
| 570 | + } else { |
|
| 571 | + $ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection()); |
|
| 572 | + $ms->migrate(); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + //run appinfo/install.php |
|
| 576 | + self::includeAppScript("$appPath/appinfo/install.php"); |
|
| 577 | + |
|
| 578 | + $info = OC_App::getAppInfo($app); |
|
| 579 | + if (is_null($info)) { |
|
| 580 | + return false; |
|
| 581 | + } |
|
| 582 | + \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 583 | + |
|
| 584 | + OC_App::executeRepairSteps($app, $info['repair-steps']['install']); |
|
| 585 | + |
|
| 586 | + $config = \OC::$server->getConfig(); |
|
| 587 | + |
|
| 588 | + $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app)); |
|
| 589 | + if (array_key_exists('ocsid', $info)) { |
|
| 590 | + $config->setAppValue($app, 'ocsid', $info['ocsid']); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + //set remote/public handlers |
|
| 594 | + foreach($info['remote'] as $name=>$path) { |
|
| 595 | + $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
|
| 596 | + } |
|
| 597 | + foreach($info['public'] as $name=>$path) { |
|
| 598 | + $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + OC_App::setAppTypes($info['id']); |
|
| 602 | + |
|
| 603 | + return $info['id']; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * @param string $script |
|
| 608 | + */ |
|
| 609 | + private static function includeAppScript($script) { |
|
| 610 | + if ( file_exists($script) ){ |
|
| 611 | + include $script; |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | 614 | } |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | public function installApp($appId) { |
| 97 | 97 | $app = \OC_App::findAppInDirectories($appId); |
| 98 | - if($app === false) { |
|
| 98 | + if ($app === false) { |
|
| 99 | 99 | throw new \Exception('App not found in any app directory'); |
| 100 | 100 | } |
| 101 | 101 | |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | $l = \OC::$server->getL10N('core'); |
| 106 | 106 | |
| 107 | - if(!is_array($info)) { |
|
| 107 | + if (!is_array($info)) { |
|
| 108 | 108 | throw new \Exception( |
| 109 | 109 | $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
| 110 | 110 | [$appId] |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | \OC_App::registerAutoloading($appId, $basedir); |
| 128 | 128 | |
| 129 | 129 | //install the database |
| 130 | - if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 130 | + if (is_file($basedir.'/appinfo/database.xml')) { |
|
| 131 | 131 | if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) { |
| 132 | 132 | OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
| 133 | 133 | } else { |
@@ -141,8 +141,8 @@ discard block |
||
| 141 | 141 | \OC_App::setupBackgroundJobs($info['background-jobs']); |
| 142 | 142 | |
| 143 | 143 | //run appinfo/install.php |
| 144 | - if(!isset($data['noinstall']) or $data['noinstall']==false) { |
|
| 145 | - self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 144 | + if (!isset($data['noinstall']) or $data['noinstall'] == false) { |
|
| 145 | + self::includeAppScript($basedir.'/appinfo/install.php'); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | $appData = OC_App::getAppInfo($appId); |
@@ -153,10 +153,10 @@ discard block |
||
| 153 | 153 | \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
| 154 | 154 | |
| 155 | 155 | //set remote/public handlers |
| 156 | - foreach($info['remote'] as $name=>$path) { |
|
| 156 | + foreach ($info['remote'] as $name=>$path) { |
|
| 157 | 157 | \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
| 158 | 158 | } |
| 159 | - foreach($info['public'] as $name=>$path) { |
|
| 159 | + foreach ($info['public'] as $name=>$path) { |
|
| 160 | 160 | \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
| 161 | 161 | } |
| 162 | 162 | |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * Checks whether or not an app is installed, i.e. registered in apps table. |
| 174 | 174 | */ |
| 175 | - public static function isInstalled( $app ) { |
|
| 175 | + public static function isInstalled($app) { |
|
| 176 | 176 | return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
| 177 | 177 | } |
| 178 | 178 | |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | * @return bool |
| 184 | 184 | */ |
| 185 | 185 | public function updateAppstoreApp($appId) { |
| 186 | - if($this->isUpdateAvailable($appId)) { |
|
| 186 | + if ($this->isUpdateAvailable($appId)) { |
|
| 187 | 187 | try { |
| 188 | 188 | $this->downloadApp($appId); |
| 189 | 189 | } catch (\Exception $e) { |
@@ -210,18 +210,18 @@ discard block |
||
| 210 | 210 | $appId = strtolower($appId); |
| 211 | 211 | |
| 212 | 212 | $apps = $this->appFetcher->get(); |
| 213 | - foreach($apps as $app) { |
|
| 214 | - if($app['id'] === $appId) { |
|
| 213 | + foreach ($apps as $app) { |
|
| 214 | + if ($app['id'] === $appId) { |
|
| 215 | 215 | // Load the certificate |
| 216 | 216 | $certificate = new X509(); |
| 217 | - $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 217 | + $certificate->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt')); |
|
| 218 | 218 | $loadedCertificate = $certificate->loadX509($app['certificate']); |
| 219 | 219 | |
| 220 | 220 | // Verify if the certificate has been revoked |
| 221 | 221 | $crl = new X509(); |
| 222 | - $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 223 | - $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 224 | - if($crl->validateSignature() !== true) { |
|
| 222 | + $crl->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt')); |
|
| 223 | + $crl->loadCRL(file_get_contents(__DIR__.'/../../resources/codesigning/root.crl')); |
|
| 224 | + if ($crl->validateSignature() !== true) { |
|
| 225 | 225 | throw new \Exception('Could not validate CRL signature'); |
| 226 | 226 | } |
| 227 | 227 | $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
| 239 | - if($certificate->validateSignature() !== true) { |
|
| 239 | + if ($certificate->validateSignature() !== true) { |
|
| 240 | 240 | throw new \Exception( |
| 241 | 241 | sprintf( |
| 242 | 242 | 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | |
| 248 | 248 | // Verify if the certificate is issued for the requested app id |
| 249 | 249 | $certInfo = openssl_x509_parse($app['certificate']); |
| 250 | - if(!isset($certInfo['subject']['CN'])) { |
|
| 250 | + if (!isset($certInfo['subject']['CN'])) { |
|
| 251 | 251 | throw new \Exception( |
| 252 | 252 | sprintf( |
| 253 | 253 | 'App with id %s has a cert with no CN', |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | ) |
| 256 | 256 | ); |
| 257 | 257 | } |
| 258 | - if($certInfo['subject']['CN'] !== $appId) { |
|
| 258 | + if ($certInfo['subject']['CN'] !== $appId) { |
|
| 259 | 259 | throw new \Exception( |
| 260 | 260 | sprintf( |
| 261 | 261 | 'App with id %s has a cert issued to %s', |
@@ -272,15 +272,15 @@ discard block |
||
| 272 | 272 | |
| 273 | 273 | // Check if the signature actually matches the downloaded content |
| 274 | 274 | $certificate = openssl_get_publickey($app['certificate']); |
| 275 | - $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 275 | + $verified = (bool) openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 276 | 276 | openssl_free_key($certificate); |
| 277 | 277 | |
| 278 | - if($verified === true) { |
|
| 278 | + if ($verified === true) { |
|
| 279 | 279 | // Seems to match, let's proceed |
| 280 | 280 | $extractDir = $this->tempManager->getTemporaryFolder(); |
| 281 | 281 | $archive = new TAR($tempFile); |
| 282 | 282 | |
| 283 | - if($archive) { |
|
| 283 | + if ($archive) { |
|
| 284 | 284 | if (!$archive->extract($extractDir)) { |
| 285 | 285 | throw new \Exception( |
| 286 | 286 | sprintf( |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | $folders = array_diff($allFiles, ['.', '..']); |
| 294 | 294 | $folders = array_values($folders); |
| 295 | 295 | |
| 296 | - if(count($folders) > 1) { |
|
| 296 | + if (count($folders) > 1) { |
|
| 297 | 297 | throw new \Exception( |
| 298 | 298 | sprintf( |
| 299 | 299 | 'Extracted app %s has more than 1 folder', |
@@ -304,22 +304,22 @@ discard block |
||
| 304 | 304 | |
| 305 | 305 | // Check if appinfo/info.xml has the same app ID as well |
| 306 | 306 | $loadEntities = libxml_disable_entity_loader(false); |
| 307 | - $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 307 | + $xml = simplexml_load_file($extractDir.'/'.$folders[0].'/appinfo/info.xml'); |
|
| 308 | 308 | libxml_disable_entity_loader($loadEntities); |
| 309 | - if((string)$xml->id !== $appId) { |
|
| 309 | + if ((string) $xml->id !== $appId) { |
|
| 310 | 310 | throw new \Exception( |
| 311 | 311 | sprintf( |
| 312 | 312 | 'App for id %s has a wrong app ID in info.xml: %s', |
| 313 | 313 | $appId, |
| 314 | - (string)$xml->id |
|
| 314 | + (string) $xml->id |
|
| 315 | 315 | ) |
| 316 | 316 | ); |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | // Check if the version is lower than before |
| 320 | 320 | $currentVersion = OC_App::getAppVersion($appId); |
| 321 | - $newVersion = (string)$xml->version; |
|
| 322 | - if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 321 | + $newVersion = (string) $xml->version; |
|
| 322 | + if (version_compare($currentVersion, $newVersion) === 1) { |
|
| 323 | 323 | throw new \Exception( |
| 324 | 324 | sprintf( |
| 325 | 325 | 'App for id %s has version %s and tried to update to lower version %s', |
@@ -330,12 +330,12 @@ discard block |
||
| 330 | 330 | ); |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 333 | + $baseDir = OC_App::getInstallPath().'/'.$appId; |
|
| 334 | 334 | // Remove old app with the ID if existent |
| 335 | 335 | OC_Helper::rmdirr($baseDir); |
| 336 | 336 | // Move to app folder |
| 337 | - if(@mkdir($baseDir)) { |
|
| 338 | - $extractDir .= '/' . $folders[0]; |
|
| 337 | + if (@mkdir($baseDir)) { |
|
| 338 | + $extractDir .= '/'.$folders[0]; |
|
| 339 | 339 | OC_Helper::copyr($extractDir, $baseDir); |
| 340 | 340 | } |
| 341 | 341 | OC_Helper::copyr($extractDir, $baseDir); |
@@ -398,8 +398,8 @@ discard block |
||
| 398 | 398 | $this->apps = $this->appFetcher->get(); |
| 399 | 399 | } |
| 400 | 400 | |
| 401 | - foreach($this->apps as $app) { |
|
| 402 | - if($app['id'] === $appId) { |
|
| 401 | + foreach ($this->apps as $app) { |
|
| 402 | + if ($app['id'] === $appId) { |
|
| 403 | 403 | $currentVersion = OC_App::getAppVersion($appId); |
| 404 | 404 | $newestVersion = $app['releases'][0]['version']; |
| 405 | 405 | if (version_compare($newestVersion, $currentVersion, '>')) { |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | */ |
| 423 | 423 | private function isInstalledFromGit($appId) { |
| 424 | 424 | $app = \OC_App::findAppInDirectories($appId); |
| 425 | - if($app === false) { |
|
| 425 | + if ($app === false) { |
|
| 426 | 426 | return false; |
| 427 | 427 | } |
| 428 | 428 | $basedir = $app['path'].'/'.$appId; |
@@ -437,7 +437,7 @@ discard block |
||
| 437 | 437 | * The function will check if the app is already downloaded in the apps repository |
| 438 | 438 | */ |
| 439 | 439 | public function isDownloaded($name) { |
| 440 | - foreach(\OC::$APPSROOTS as $dir) { |
|
| 440 | + foreach (\OC::$APPSROOTS as $dir) { |
|
| 441 | 441 | $dirToTest = $dir['path']; |
| 442 | 442 | $dirToTest .= '/'; |
| 443 | 443 | $dirToTest .= $name; |
@@ -465,14 +465,14 @@ discard block |
||
| 465 | 465 | * this has to be done by the function oc_app_uninstall(). |
| 466 | 466 | */ |
| 467 | 467 | public function removeApp($appId) { |
| 468 | - if($this->isDownloaded( $appId )) { |
|
| 468 | + if ($this->isDownloaded($appId)) { |
|
| 469 | 469 | if (\OC::$server->getAppManager()->isShipped($appId)) { |
| 470 | 470 | return false; |
| 471 | 471 | } |
| 472 | - $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 472 | + $appDir = OC_App::getInstallPath().'/'.$appId; |
|
| 473 | 473 | OC_Helper::rmdirr($appDir); |
| 474 | 474 | return true; |
| 475 | - }else{ |
|
| 475 | + } else { |
|
| 476 | 476 | \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
| 477 | 477 | |
| 478 | 478 | return false; |
@@ -488,8 +488,8 @@ discard block |
||
| 488 | 488 | */ |
| 489 | 489 | public function installAppBundle(Bundle $bundle) { |
| 490 | 490 | $appIds = $bundle->getAppIdentifiers(); |
| 491 | - foreach($appIds as $appId) { |
|
| 492 | - if(!$this->isDownloaded($appId)) { |
|
| 491 | + foreach ($appIds as $appId) { |
|
| 492 | + if (!$this->isDownloaded($appId)) { |
|
| 493 | 493 | $this->downloadApp($appId); |
| 494 | 494 | } |
| 495 | 495 | $this->installApp($appId); |
@@ -511,13 +511,13 @@ discard block |
||
| 511 | 511 | */ |
| 512 | 512 | public static function installShippedApps($softErrors = false) { |
| 513 | 513 | $errors = []; |
| 514 | - foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 515 | - if($dir = opendir( $app_dir['path'] )) { |
|
| 516 | - while( false !== ( $filename = readdir( $dir ))) { |
|
| 517 | - if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 518 | - if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 519 | - if(!Installer::isInstalled($filename)) { |
|
| 520 | - $info=OC_App::getAppInfo($filename); |
|
| 514 | + foreach (\OC::$APPSROOTS as $app_dir) { |
|
| 515 | + if ($dir = opendir($app_dir['path'])) { |
|
| 516 | + while (false !== ($filename = readdir($dir))) { |
|
| 517 | + if ($filename[0] !== '.' and is_dir($app_dir['path']."/$filename")) { |
|
| 518 | + if (file_exists($app_dir['path']."/$filename/appinfo/info.xml")) { |
|
| 519 | + if (!Installer::isInstalled($filename)) { |
|
| 520 | + $info = OC_App::getAppInfo($filename); |
|
| 521 | 521 | $enabled = isset($info['default_enable']); |
| 522 | 522 | if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
| 523 | 523 | && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
@@ -540,7 +540,7 @@ discard block |
||
| 540 | 540 | } |
| 541 | 541 | } |
| 542 | 542 | } |
| 543 | - closedir( $dir ); |
|
| 543 | + closedir($dir); |
|
| 544 | 544 | } |
| 545 | 545 | } |
| 546 | 546 | |
@@ -557,12 +557,12 @@ discard block |
||
| 557 | 557 | $appPath = OC_App::getAppPath($app); |
| 558 | 558 | \OC_App::registerAutoloading($app, $appPath); |
| 559 | 559 | |
| 560 | - if(is_file("$appPath/appinfo/database.xml")) { |
|
| 560 | + if (is_file("$appPath/appinfo/database.xml")) { |
|
| 561 | 561 | try { |
| 562 | 562 | OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
| 563 | 563 | } catch (TableExistsException $e) { |
| 564 | 564 | throw new HintException( |
| 565 | - 'Failed to enable app ' . $app, |
|
| 565 | + 'Failed to enable app '.$app, |
|
| 566 | 566 | 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.', |
| 567 | 567 | 0, $e |
| 568 | 568 | ); |
@@ -591,10 +591,10 @@ discard block |
||
| 591 | 591 | } |
| 592 | 592 | |
| 593 | 593 | //set remote/public handlers |
| 594 | - foreach($info['remote'] as $name=>$path) { |
|
| 594 | + foreach ($info['remote'] as $name=>$path) { |
|
| 595 | 595 | $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
| 596 | 596 | } |
| 597 | - foreach($info['public'] as $name=>$path) { |
|
| 597 | + foreach ($info['public'] as $name=>$path) { |
|
| 598 | 598 | $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
| 599 | 599 | } |
| 600 | 600 | |
@@ -607,7 +607,7 @@ discard block |
||
| 607 | 607 | * @param string $script |
| 608 | 608 | */ |
| 609 | 609 | private static function includeAppScript($script) { |
| 610 | - if ( file_exists($script) ){ |
|
| 610 | + if (file_exists($script)) { |
|
| 611 | 611 | include $script; |
| 612 | 612 | } |
| 613 | 613 | } |