@@ -58,531 +58,531 @@  | 
                                                    ||
| 58 | 58 | use OCP\Security\ISecureRandom;  | 
                                                        
| 59 | 59 | |
| 60 | 60 |  class Setup { | 
                                                        
| 61 | - /** @var SystemConfig */  | 
                                                        |
| 62 | - protected $config;  | 
                                                        |
| 63 | - /** @var IniGetWrapper */  | 
                                                        |
| 64 | - protected $iniWrapper;  | 
                                                        |
| 65 | - /** @var IL10N */  | 
                                                        |
| 66 | - protected $l10n;  | 
                                                        |
| 67 | - /** @var Defaults */  | 
                                                        |
| 68 | - protected $defaults;  | 
                                                        |
| 69 | - /** @var ILogger */  | 
                                                        |
| 70 | - protected $logger;  | 
                                                        |
| 71 | - /** @var ISecureRandom */  | 
                                                        |
| 72 | - protected $random;  | 
                                                        |
| 73 | - /** @var Installer */  | 
                                                        |
| 74 | - protected $installer;  | 
                                                        |
| 75 | -  | 
                                                        |
| 76 | - /**  | 
                                                        |
| 77 | - * @param SystemConfig $config  | 
                                                        |
| 78 | - * @param IniGetWrapper $iniWrapper  | 
                                                        |
| 79 | - * @param IL10N $l10n  | 
                                                        |
| 80 | - * @param Defaults $defaults  | 
                                                        |
| 81 | - * @param ILogger $logger  | 
                                                        |
| 82 | - * @param ISecureRandom $random  | 
                                                        |
| 83 | - * @param Installer $installer  | 
                                                        |
| 84 | - */  | 
                                                        |
| 85 | - public function __construct(  | 
                                                        |
| 86 | - SystemConfig $config,  | 
                                                        |
| 87 | - IniGetWrapper $iniWrapper,  | 
                                                        |
| 88 | - IL10N $l10n,  | 
                                                        |
| 89 | - Defaults $defaults,  | 
                                                        |
| 90 | - ILogger $logger,  | 
                                                        |
| 91 | - ISecureRandom $random,  | 
                                                        |
| 92 | - Installer $installer  | 
                                                        |
| 93 | -	) { | 
                                                        |
| 94 | - $this->config = $config;  | 
                                                        |
| 95 | - $this->iniWrapper = $iniWrapper;  | 
                                                        |
| 96 | - $this->l10n = $l10n;  | 
                                                        |
| 97 | - $this->defaults = $defaults;  | 
                                                        |
| 98 | - $this->logger = $logger;  | 
                                                        |
| 99 | - $this->random = $random;  | 
                                                        |
| 100 | - $this->installer = $installer;  | 
                                                        |
| 101 | - }  | 
                                                        |
| 102 | -  | 
                                                        |
| 103 | - static protected $dbSetupClasses = [  | 
                                                        |
| 104 | - 'mysql' => \OC\Setup\MySQL::class,  | 
                                                        |
| 105 | - 'pgsql' => \OC\Setup\PostgreSQL::class,  | 
                                                        |
| 106 | - 'oci' => \OC\Setup\OCI::class,  | 
                                                        |
| 107 | - 'sqlite' => \OC\Setup\Sqlite::class,  | 
                                                        |
| 108 | - 'sqlite3' => \OC\Setup\Sqlite::class,  | 
                                                        |
| 109 | - ];  | 
                                                        |
| 110 | -  | 
                                                        |
| 111 | - /**  | 
                                                        |
| 112 | - * Wrapper around the "class_exists" PHP function to be able to mock it  | 
                                                        |
| 113 | - *  | 
                                                        |
| 114 | - * @param string $name  | 
                                                        |
| 115 | - * @return bool  | 
                                                        |
| 116 | - */  | 
                                                        |
| 117 | -	protected function class_exists($name) { | 
                                                        |
| 118 | - return class_exists($name);  | 
                                                        |
| 119 | - }  | 
                                                        |
| 120 | -  | 
                                                        |
| 121 | - /**  | 
                                                        |
| 122 | - * Wrapper around the "is_callable" PHP function to be able to mock it  | 
                                                        |
| 123 | - *  | 
                                                        |
| 124 | - * @param string $name  | 
                                                        |
| 125 | - * @return bool  | 
                                                        |
| 126 | - */  | 
                                                        |
| 127 | -	protected function is_callable($name) { | 
                                                        |
| 128 | - return is_callable($name);  | 
                                                        |
| 129 | - }  | 
                                                        |
| 130 | -  | 
                                                        |
| 131 | - /**  | 
                                                        |
| 132 | - * Wrapper around \PDO::getAvailableDrivers  | 
                                                        |
| 133 | - *  | 
                                                        |
| 134 | - * @return array  | 
                                                        |
| 135 | - */  | 
                                                        |
| 136 | -	protected function getAvailableDbDriversForPdo() { | 
                                                        |
| 137 | - return \PDO::getAvailableDrivers();  | 
                                                        |
| 138 | - }  | 
                                                        |
| 139 | -  | 
                                                        |
| 140 | - /**  | 
                                                        |
| 141 | - * Get the available and supported databases of this instance  | 
                                                        |
| 142 | - *  | 
                                                        |
| 143 | - * @param bool $allowAllDatabases  | 
                                                        |
| 144 | - * @return array  | 
                                                        |
| 145 | - * @throws Exception  | 
                                                        |
| 146 | - */  | 
                                                        |
| 147 | -	public function getSupportedDatabases($allowAllDatabases = false) { | 
                                                        |
| 148 | - $availableDatabases = [  | 
                                                        |
| 149 | - 'sqlite' => [  | 
                                                        |
| 150 | - 'type' => 'pdo',  | 
                                                        |
| 151 | - 'call' => 'sqlite',  | 
                                                        |
| 152 | - 'name' => 'SQLite',  | 
                                                        |
| 153 | - ],  | 
                                                        |
| 154 | - 'mysql' => [  | 
                                                        |
| 155 | - 'type' => 'pdo',  | 
                                                        |
| 156 | - 'call' => 'mysql',  | 
                                                        |
| 157 | - 'name' => 'MySQL/MariaDB',  | 
                                                        |
| 158 | - ],  | 
                                                        |
| 159 | - 'pgsql' => [  | 
                                                        |
| 160 | - 'type' => 'pdo',  | 
                                                        |
| 161 | - 'call' => 'pgsql',  | 
                                                        |
| 162 | - 'name' => 'PostgreSQL',  | 
                                                        |
| 163 | - ],  | 
                                                        |
| 164 | - 'oci' => [  | 
                                                        |
| 165 | - 'type' => 'function',  | 
                                                        |
| 166 | - 'call' => 'oci_connect',  | 
                                                        |
| 167 | - 'name' => 'Oracle',  | 
                                                        |
| 168 | - ],  | 
                                                        |
| 169 | - ];  | 
                                                        |
| 170 | -		if ($allowAllDatabases) { | 
                                                        |
| 171 | - $configuredDatabases = array_keys($availableDatabases);  | 
                                                        |
| 172 | -		} else { | 
                                                        |
| 173 | -			$configuredDatabases = $this->config->getValue('supportedDatabases', | 
                                                        |
| 174 | - ['sqlite', 'mysql', 'pgsql']);  | 
                                                        |
| 175 | - }  | 
                                                        |
| 176 | -		if (!is_array($configuredDatabases)) { | 
                                                        |
| 177 | -			throw new Exception('Supported databases are not properly configured.'); | 
                                                        |
| 178 | - }  | 
                                                        |
| 179 | -  | 
                                                        |
| 180 | - $supportedDatabases = array();  | 
                                                        |
| 181 | -  | 
                                                        |
| 182 | -		foreach ($configuredDatabases as $database) { | 
                                                        |
| 183 | -			if (array_key_exists($database, $availableDatabases)) { | 
                                                        |
| 184 | - $working = false;  | 
                                                        |
| 185 | - $type = $availableDatabases[$database]['type'];  | 
                                                        |
| 186 | - $call = $availableDatabases[$database]['call'];  | 
                                                        |
| 187 | -  | 
                                                        |
| 188 | -				if ($type === 'function') { | 
                                                        |
| 189 | - $working = $this->is_callable($call);  | 
                                                        |
| 190 | -				} elseif ($type === 'pdo') { | 
                                                        |
| 191 | - $working = in_array($call, $this->getAvailableDbDriversForPdo(), true);  | 
                                                        |
| 192 | - }  | 
                                                        |
| 193 | -				if ($working) { | 
                                                        |
| 194 | - $supportedDatabases[$database] = $availableDatabases[$database]['name'];  | 
                                                        |
| 195 | - }  | 
                                                        |
| 196 | - }  | 
                                                        |
| 197 | - }  | 
                                                        |
| 198 | -  | 
                                                        |
| 199 | - return $supportedDatabases;  | 
                                                        |
| 200 | - }  | 
                                                        |
| 201 | -  | 
                                                        |
| 202 | - /**  | 
                                                        |
| 203 | - * Gathers system information like database type and does  | 
                                                        |
| 204 | - * a few system checks.  | 
                                                        |
| 205 | - *  | 
                                                        |
| 206 | - * @return array of system info, including an "errors" value  | 
                                                        |
| 207 | - * in case of errors/warnings  | 
                                                        |
| 208 | - */  | 
                                                        |
| 209 | -	public function getSystemInfo($allowAllDatabases = false) { | 
                                                        |
| 210 | - $databases = $this->getSupportedDatabases($allowAllDatabases);  | 
                                                        |
| 211 | -  | 
                                                        |
| 212 | -		$dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); | 
                                                        |
| 213 | -  | 
                                                        |
| 214 | - $errors = [];  | 
                                                        |
| 215 | -  | 
                                                        |
| 216 | - // Create data directory to test whether the .htaccess works  | 
                                                        |
| 217 | - // Notice that this is not necessarily the same data directory as the one  | 
                                                        |
| 218 | - // that will effectively be used.  | 
                                                        |
| 219 | -		if (!file_exists($dataDir)) { | 
                                                        |
| 220 | - @mkdir($dataDir);  | 
                                                        |
| 221 | - }  | 
                                                        |
| 222 | - $htAccessWorking = true;  | 
                                                        |
| 223 | -		if (is_dir($dataDir) && is_writable($dataDir)) { | 
                                                        |
| 224 | - // Protect data directory here, so we can test if the protection is working  | 
                                                        |
| 225 | - self::protectDataDirectory();  | 
                                                        |
| 226 | -  | 
                                                        |
| 227 | -			try { | 
                                                        |
| 228 | - $util = new \OC_Util();  | 
                                                        |
| 229 | - $htAccessWorking = $util->isHtaccessWorking(\OC::$server->getConfig());  | 
                                                        |
| 230 | -			} catch (\OC\HintException $e) { | 
                                                        |
| 231 | - $errors[] = [  | 
                                                        |
| 232 | - 'error' => $e->getMessage(),  | 
                                                        |
| 233 | - 'hint' => $e->getHint(),  | 
                                                        |
| 234 | - ];  | 
                                                        |
| 235 | - $htAccessWorking = false;  | 
                                                        |
| 236 | - }  | 
                                                        |
| 237 | - }  | 
                                                        |
| 238 | -  | 
                                                        |
| 239 | -		if (\OC_Util::runningOnMac()) { | 
                                                        |
| 240 | - $errors[] = [  | 
                                                        |
| 241 | - 'error' => $this->l10n->t(  | 
                                                        |
| 242 | - 'Mac OS X is not supported and %s will not work properly on this platform. ' .  | 
                                                        |
| 243 | - 'Use it at your own risk! ',  | 
                                                        |
| 244 | - [$this->defaults->getName()]  | 
                                                        |
| 245 | - ),  | 
                                                        |
| 246 | -				'hint' => $this->l10n->t('For the best results, please consider using a GNU/Linux server instead.'), | 
                                                        |
| 247 | - ];  | 
                                                        |
| 248 | - }  | 
                                                        |
| 249 | -  | 
                                                        |
| 250 | -		if ($this->iniWrapper->getString('open_basedir') !== '' && PHP_INT_SIZE === 4) { | 
                                                        |
| 251 | - $errors[] = [  | 
                                                        |
| 252 | - 'error' => $this->l10n->t(  | 
                                                        |
| 253 | - 'It seems that this %s instance is running on a 32-bit PHP environment and the open_basedir has been configured in php.ini. ' .  | 
                                                        |
| 254 | - 'This will lead to problems with files over 4 GB and is highly discouraged.',  | 
                                                        |
| 255 | - [$this->defaults->getName()]  | 
                                                        |
| 256 | - ),  | 
                                                        |
| 257 | -				'hint' => $this->l10n->t('Please remove the open_basedir setting within your php.ini or switch to 64-bit PHP.'), | 
                                                        |
| 258 | - ];  | 
                                                        |
| 259 | - }  | 
                                                        |
| 260 | -  | 
                                                        |
| 261 | - return array(  | 
                                                        |
| 262 | - 'hasSQLite' => isset($databases['sqlite']),  | 
                                                        |
| 263 | - 'hasMySQL' => isset($databases['mysql']),  | 
                                                        |
| 264 | - 'hasPostgreSQL' => isset($databases['pgsql']),  | 
                                                        |
| 265 | - 'hasOracle' => isset($databases['oci']),  | 
                                                        |
| 266 | - 'databases' => $databases,  | 
                                                        |
| 267 | - 'directory' => $dataDir,  | 
                                                        |
| 268 | - 'htaccessWorking' => $htAccessWorking,  | 
                                                        |
| 269 | - 'errors' => $errors,  | 
                                                        |
| 270 | - );  | 
                                                        |
| 271 | - }  | 
                                                        |
| 272 | -  | 
                                                        |
| 273 | - /**  | 
                                                        |
| 274 | - * @param $options  | 
                                                        |
| 275 | - * @return array  | 
                                                        |
| 276 | - */  | 
                                                        |
| 277 | -	public function install($options) { | 
                                                        |
| 278 | - $l = $this->l10n;  | 
                                                        |
| 279 | -  | 
                                                        |
| 280 | - $error = array();  | 
                                                        |
| 281 | - $dbType = $options['dbtype'];  | 
                                                        |
| 282 | -  | 
                                                        |
| 283 | -		if (empty($options['adminlogin'])) { | 
                                                        |
| 284 | -			$error[] = $l->t('Set an admin username.'); | 
                                                        |
| 285 | - }  | 
                                                        |
| 286 | -		if (empty($options['adminpass'])) { | 
                                                        |
| 287 | -			$error[] = $l->t('Set an admin password.'); | 
                                                        |
| 288 | - }  | 
                                                        |
| 289 | -		if (empty($options['directory'])) { | 
                                                        |
| 290 | - $options['directory'] = \OC::$SERVERROOT . "/data";  | 
                                                        |
| 291 | - }  | 
                                                        |
| 292 | -  | 
                                                        |
| 293 | -		if (!isset(self::$dbSetupClasses[$dbType])) { | 
                                                        |
| 294 | - $dbType = 'sqlite';  | 
                                                        |
| 295 | - }  | 
                                                        |
| 296 | -  | 
                                                        |
| 297 | - $username = htmlspecialchars_decode($options['adminlogin']);  | 
                                                        |
| 298 | - $password = htmlspecialchars_decode($options['adminpass']);  | 
                                                        |
| 299 | - $dataDir = htmlspecialchars_decode($options['directory']);  | 
                                                        |
| 300 | -  | 
                                                        |
| 301 | - $class = self::$dbSetupClasses[$dbType];  | 
                                                        |
| 302 | - /** @var \OC\Setup\AbstractDatabase $dbSetup */  | 
                                                        |
| 303 | - $dbSetup = new $class($l, $this->config, $this->logger, $this->random);  | 
                                                        |
| 304 | - $error = array_merge($error, $dbSetup->validate($options));  | 
                                                        |
| 305 | -  | 
                                                        |
| 306 | - // validate the data directory  | 
                                                        |
| 307 | -		if ((!is_dir($dataDir) && !mkdir($dataDir)) || !is_writable($dataDir)) { | 
                                                        |
| 308 | -			$error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); | 
                                                        |
| 309 | - }  | 
                                                        |
| 310 | -  | 
                                                        |
| 311 | -		if (!empty($error)) { | 
                                                        |
| 312 | - return $error;  | 
                                                        |
| 313 | - }  | 
                                                        |
| 314 | -  | 
                                                        |
| 315 | - $request = \OC::$server->getRequest();  | 
                                                        |
| 316 | -  | 
                                                        |
| 317 | - //no errors, good  | 
                                                        |
| 318 | - if (isset($options['trusted_domains'])  | 
                                                        |
| 319 | -			&& is_array($options['trusted_domains'])) { | 
                                                        |
| 320 | - $trustedDomains = $options['trusted_domains'];  | 
                                                        |
| 321 | -		} else { | 
                                                        |
| 322 | - $trustedDomains = [$request->getInsecureServerHost()];  | 
                                                        |
| 323 | - }  | 
                                                        |
| 324 | -  | 
                                                        |
| 325 | - //use sqlite3 when available, otherwise sqlite2 will be used.  | 
                                                        |
| 326 | -		if ($dbType === 'sqlite' && class_exists('SQLite3')) { | 
                                                        |
| 327 | - $dbType = 'sqlite3';  | 
                                                        |
| 328 | - }  | 
                                                        |
| 329 | -  | 
                                                        |
| 330 | - //generate a random salt that is used to salt the local user passwords  | 
                                                        |
| 331 | - $salt = $this->random->generate(30);  | 
                                                        |
| 332 | - // generate a secret  | 
                                                        |
| 333 | - $secret = $this->random->generate(48);  | 
                                                        |
| 334 | -  | 
                                                        |
| 335 | - //write the config file  | 
                                                        |
| 336 | - $newConfigValues = [  | 
                                                        |
| 337 | - 'passwordsalt' => $salt,  | 
                                                        |
| 338 | - 'secret' => $secret,  | 
                                                        |
| 339 | - 'trusted_domains' => $trustedDomains,  | 
                                                        |
| 340 | - 'datadirectory' => $dataDir,  | 
                                                        |
| 341 | - 'dbtype' => $dbType,  | 
                                                        |
| 342 | -			'version' => implode('.', \OCP\Util::getVersion()), | 
                                                        |
| 343 | - ];  | 
                                                        |
| 344 | -  | 
                                                        |
| 345 | -		if ($this->config->getValue('overwrite.cli.url', null) === null) { | 
                                                        |
| 346 | - $newConfigValues['overwrite.cli.url'] = $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT;  | 
                                                        |
| 347 | - }  | 
                                                        |
| 348 | -  | 
                                                        |
| 349 | - $this->config->setValues($newConfigValues);  | 
                                                        |
| 350 | -  | 
                                                        |
| 351 | -		try { | 
                                                        |
| 352 | - $dbSetup->initialize($options);  | 
                                                        |
| 353 | - $dbSetup->setupDatabase($username);  | 
                                                        |
| 354 | - // apply necessary migrations  | 
                                                        |
| 355 | - $dbSetup->runMigrations();  | 
                                                        |
| 356 | -		} catch (\OC\DatabaseSetupException $e) { | 
                                                        |
| 357 | - $error[] = [  | 
                                                        |
| 358 | - 'error' => $e->getMessage(),  | 
                                                        |
| 359 | - 'hint' => $e->getHint(),  | 
                                                        |
| 360 | - ];  | 
                                                        |
| 361 | - return $error;  | 
                                                        |
| 362 | -		} catch (Exception $e) { | 
                                                        |
| 363 | - $error[] = [  | 
                                                        |
| 364 | - 'error' => 'Error while trying to create admin user: ' . $e->getMessage(),  | 
                                                        |
| 365 | - 'hint' => '',  | 
                                                        |
| 366 | - ];  | 
                                                        |
| 367 | - return $error;  | 
                                                        |
| 368 | - }  | 
                                                        |
| 369 | -  | 
                                                        |
| 370 | - //create the user and group  | 
                                                        |
| 371 | - $user = null;  | 
                                                        |
| 372 | -		try { | 
                                                        |
| 373 | - $user = \OC::$server->getUserManager()->createUser($username, $password);  | 
                                                        |
| 374 | -			if (!$user) { | 
                                                        |
| 375 | - $error[] = "User <$username> could not be created.";  | 
                                                        |
| 376 | - }  | 
                                                        |
| 377 | -		} catch (Exception $exception) { | 
                                                        |
| 378 | - $error[] = $exception->getMessage();  | 
                                                        |
| 379 | - }  | 
                                                        |
| 380 | -  | 
                                                        |
| 381 | -		if (empty($error)) { | 
                                                        |
| 382 | - $config = \OC::$server->getConfig();  | 
                                                        |
| 383 | -			$config->setAppValue('core', 'installedat', microtime(true)); | 
                                                        |
| 384 | -			$config->setAppValue('core', 'lastupdatedat', microtime(true)); | 
                                                        |
| 385 | -			$config->setAppValue('core', 'vendor', $this->getVendor()); | 
                                                        |
| 386 | -  | 
                                                        |
| 387 | -			$group = \OC::$server->getGroupManager()->createGroup('admin'); | 
                                                        |
| 388 | -			if ($group instanceof IGroup) { | 
                                                        |
| 389 | - $group->addUser($user);  | 
                                                        |
| 390 | - }  | 
                                                        |
| 391 | -  | 
                                                        |
| 392 | - // Install shipped apps and specified app bundles  | 
                                                        |
| 393 | - Installer::installShippedApps();  | 
                                                        |
| 394 | -			$bundleFetcher = new BundleFetcher(\OC::$server->getL10N('lib')); | 
                                                        |
| 395 | - $defaultInstallationBundles = $bundleFetcher->getDefaultInstallationBundle();  | 
                                                        |
| 396 | -			foreach ($defaultInstallationBundles as $bundle) { | 
                                                        |
| 397 | -				try { | 
                                                        |
| 398 | - $this->installer->installAppBundle($bundle);  | 
                                                        |
| 399 | -				} catch (Exception $e) { | 
                                                        |
| 400 | - }  | 
                                                        |
| 401 | - }  | 
                                                        |
| 402 | -  | 
                                                        |
| 403 | - // create empty file in data dir, so we can later find  | 
                                                        |
| 404 | - // out that this is indeed an ownCloud data directory  | 
                                                        |
| 405 | -			file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', ''); | 
                                                        |
| 406 | -  | 
                                                        |
| 407 | - // Update .htaccess files  | 
                                                        |
| 408 | - self::updateHtaccess();  | 
                                                        |
| 409 | - self::protectDataDirectory();  | 
                                                        |
| 410 | -  | 
                                                        |
| 411 | - self::installBackgroundJobs();  | 
                                                        |
| 412 | -  | 
                                                        |
| 413 | - //and we are done  | 
                                                        |
| 414 | -			$config->setSystemValue('installed', true); | 
                                                        |
| 415 | -  | 
                                                        |
| 416 | - // Create a session token for the newly created user  | 
                                                        |
| 417 | - // The token provider requires a working db, so it's not injected on setup  | 
                                                        |
| 418 | - /* @var $userSession User\Session */  | 
                                                        |
| 419 | - $userSession = \OC::$server->getUserSession();  | 
                                                        |
| 420 | - $defaultTokenProvider = \OC::$server->query(DefaultTokenProvider::class);  | 
                                                        |
| 421 | - $userSession->setTokenProvider($defaultTokenProvider);  | 
                                                        |
| 422 | - $userSession->login($username, $password);  | 
                                                        |
| 423 | - $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password);  | 
                                                        |
| 424 | -  | 
                                                        |
| 425 | - $session = $userSession->getSession();  | 
                                                        |
| 426 | -			$session->set('last-password-confirm', \OC::$server->query(ITimeFactory::class)->getTime()); | 
                                                        |
| 427 | -  | 
                                                        |
| 428 | - // Set email for admin  | 
                                                        |
| 429 | -			if (!empty($options['adminemail'])) { | 
                                                        |
| 430 | - $config->setUserValue($user->getUID(), 'settings', 'email', $options['adminemail']);  | 
                                                        |
| 431 | - }  | 
                                                        |
| 432 | - }  | 
                                                        |
| 433 | -  | 
                                                        |
| 434 | - return $error;  | 
                                                        |
| 435 | - }  | 
                                                        |
| 436 | -  | 
                                                        |
| 437 | -	public static function installBackgroundJobs() { | 
                                                        |
| 438 | - $jobList = \OC::$server->getJobList();  | 
                                                        |
| 439 | - $jobList->add(DefaultTokenCleanupJob::class);  | 
                                                        |
| 440 | - $jobList->add(Rotate::class);  | 
                                                        |
| 441 | - $jobList->add(BackgroundCleanupJob::class);  | 
                                                        |
| 442 | - }  | 
                                                        |
| 443 | -  | 
                                                        |
| 444 | - /**  | 
                                                        |
| 445 | - * @return string Absolute path to htaccess  | 
                                                        |
| 446 | - */  | 
                                                        |
| 447 | -	private function pathToHtaccess() { | 
                                                        |
| 448 | - return \OC::$SERVERROOT . '/.htaccess';  | 
                                                        |
| 449 | - }  | 
                                                        |
| 450 | -  | 
                                                        |
| 451 | - /**  | 
                                                        |
| 452 | - * Find webroot from config  | 
                                                        |
| 453 | - *  | 
                                                        |
| 454 | - * @param SystemConfig $config  | 
                                                        |
| 455 | - * @return string  | 
                                                        |
| 456 | - * @throws InvalidArgumentException when invalid value for overwrite.cli.url  | 
                                                        |
| 457 | - */  | 
                                                        |
| 458 | -	private static function findWebRoot(SystemConfig $config): string { | 
                                                        |
| 459 | - // For CLI read the value from overwrite.cli.url  | 
                                                        |
| 460 | -		if (\OC::$CLI) { | 
                                                        |
| 461 | -			$webRoot = $config->getValue('overwrite.cli.url', ''); | 
                                                        |
| 462 | -			if ($webRoot === '') { | 
                                                        |
| 463 | -				throw new InvalidArgumentException('overwrite.cli.url is empty'); | 
                                                        |
| 464 | - }  | 
                                                        |
| 465 | -			if (!filter_var($webRoot, FILTER_VALIDATE_URL)) { | 
                                                        |
| 466 | -				throw new InvalidArgumentException('invalid value for overwrite.cli.url'); | 
                                                        |
| 467 | - }  | 
                                                        |
| 468 | - $webRoot = rtrim(parse_url($webRoot, PHP_URL_PATH), '/');  | 
                                                        |
| 469 | -		} else { | 
                                                        |
| 470 | - $webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';  | 
                                                        |
| 471 | - }  | 
                                                        |
| 472 | -  | 
                                                        |
| 473 | - return $webRoot;  | 
                                                        |
| 474 | - }  | 
                                                        |
| 475 | -  | 
                                                        |
| 476 | - /**  | 
                                                        |
| 477 | - * Append the correct ErrorDocument path for Apache hosts  | 
                                                        |
| 478 | - *  | 
                                                        |
| 479 | - * @return bool True when success, False otherwise  | 
                                                        |
| 480 | - * @throws \OCP\AppFramework\QueryException  | 
                                                        |
| 481 | - */  | 
                                                        |
| 482 | -	public static function updateHtaccess() { | 
                                                        |
| 483 | - $config = \OC::$server->getSystemConfig();  | 
                                                        |
| 484 | -  | 
                                                        |
| 485 | -		try { | 
                                                        |
| 486 | - $webRoot = self::findWebRoot($config);  | 
                                                        |
| 487 | -		} catch (InvalidArgumentException $e) { | 
                                                        |
| 488 | - return false;  | 
                                                        |
| 489 | - }  | 
                                                        |
| 490 | -  | 
                                                        |
| 491 | - $setupHelper = new \OC\Setup(  | 
                                                        |
| 492 | - $config,  | 
                                                        |
| 493 | - \OC::$server->getIniWrapper(),  | 
                                                        |
| 494 | -			\OC::$server->getL10N('lib'), | 
                                                        |
| 495 | - \OC::$server->query(Defaults::class),  | 
                                                        |
| 496 | - \OC::$server->getLogger(),  | 
                                                        |
| 497 | - \OC::$server->getSecureRandom(),  | 
                                                        |
| 498 | - \OC::$server->query(Installer::class)  | 
                                                        |
| 499 | - );  | 
                                                        |
| 500 | -  | 
                                                        |
| 501 | - $htaccessContent = file_get_contents($setupHelper->pathToHtaccess());  | 
                                                        |
| 502 | - $content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n";  | 
                                                        |
| 503 | - $htaccessContent = explode($content, $htaccessContent, 2)[0];  | 
                                                        |
| 504 | -  | 
                                                        |
| 505 | - //custom 403 error page  | 
                                                        |
| 506 | - $content .= "\nErrorDocument 403 " . $webRoot . '/';  | 
                                                        |
| 507 | -  | 
                                                        |
| 508 | - //custom 404 error page  | 
                                                        |
| 509 | - $content .= "\nErrorDocument 404 " . $webRoot . '/';  | 
                                                        |
| 510 | -  | 
                                                        |
| 511 | - // Add rewrite rules if the RewriteBase is configured  | 
                                                        |
| 512 | -		$rewriteBase = $config->getValue('htaccess.RewriteBase', ''); | 
                                                        |
| 513 | -		if ($rewriteBase !== '') { | 
                                                        |
| 514 | - $content .= "\n<IfModule mod_rewrite.c>";  | 
                                                        |
| 515 | - $content .= "\n Options -MultiViews";  | 
                                                        |
| 516 | - $content .= "\n RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]";  | 
                                                        |
| 517 | - $content .= "\n RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]";  | 
                                                        |
| 518 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !\\.(css|js|svg|gif|png|html|ttf|woff2?|ico|jpg|jpeg|map)$"; | 
                                                        |
| 519 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !core/img/favicon.ico$"; | 
                                                        |
| 520 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !core/img/manifest.json$"; | 
                                                        |
| 521 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/remote.php"; | 
                                                        |
| 522 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/public.php"; | 
                                                        |
| 523 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/cron.php"; | 
                                                        |
| 524 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/core/ajax/update.php"; | 
                                                        |
| 525 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/status.php"; | 
                                                        |
| 526 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs/v1.php"; | 
                                                        |
| 527 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs/v2.php"; | 
                                                        |
| 528 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/robots.txt"; | 
                                                        |
| 529 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/updater/"; | 
                                                        |
| 530 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs-provider/"; | 
                                                        |
| 531 | -			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocm-provider/"; | 
                                                        |
| 532 | -			$content .= "\n  RewriteCond %{REQUEST_URI} !^/\\.well-known/(acme-challenge|pki-validation)/.*"; | 
                                                        |
| 533 | - $content .= "\n RewriteRule . index.php [PT,E=PATH_INFO:$1]";  | 
                                                        |
| 534 | - $content .= "\n RewriteBase " . $rewriteBase;  | 
                                                        |
| 535 | - $content .= "\n <IfModule mod_env.c>";  | 
                                                        |
| 536 | - $content .= "\n SetEnv front_controller_active true";  | 
                                                        |
| 537 | - $content .= "\n <IfModule mod_dir.c>";  | 
                                                        |
| 538 | - $content .= "\n DirectorySlash off";  | 
                                                        |
| 539 | - $content .= "\n </IfModule>";  | 
                                                        |
| 540 | - $content .= "\n </IfModule>";  | 
                                                        |
| 541 | - $content .= "\n</IfModule>";  | 
                                                        |
| 542 | - }  | 
                                                        |
| 543 | -  | 
                                                        |
| 544 | -		if ($content !== '') { | 
                                                        |
| 545 | - //suppress errors in case we don't have permissions for it  | 
                                                        |
| 546 | - return (bool)@file_put_contents($setupHelper->pathToHtaccess(), $htaccessContent . $content . "\n");  | 
                                                        |
| 547 | - }  | 
                                                        |
| 548 | -  | 
                                                        |
| 549 | - return false;  | 
                                                        |
| 550 | - }  | 
                                                        |
| 551 | -  | 
                                                        |
| 552 | -	public static function protectDataDirectory() { | 
                                                        |
| 553 | - //Require all denied  | 
                                                        |
| 554 | -		$now = date('Y-m-d H:i:s'); | 
                                                        |
| 555 | - $content = "# Generated by Nextcloud on $now\n";  | 
                                                        |
| 556 | - $content .= "# line below if for Apache 2.4\n";  | 
                                                        |
| 557 | - $content .= "<ifModule mod_authz_core.c>\n";  | 
                                                        |
| 558 | - $content .= "Require all denied\n";  | 
                                                        |
| 559 | - $content .= "</ifModule>\n\n";  | 
                                                        |
| 560 | - $content .= "# line below if for Apache 2.2\n";  | 
                                                        |
| 561 | - $content .= "<ifModule !mod_authz_core.c>\n";  | 
                                                        |
| 562 | - $content .= "deny from all\n";  | 
                                                        |
| 563 | - $content .= "Satisfy All\n";  | 
                                                        |
| 564 | - $content .= "</ifModule>\n\n";  | 
                                                        |
| 565 | - $content .= "# section for Apache 2.2 and 2.4\n";  | 
                                                        |
| 566 | - $content .= "<ifModule mod_autoindex.c>\n";  | 
                                                        |
| 567 | - $content .= "IndexIgnore *\n";  | 
                                                        |
| 568 | - $content .= "</ifModule>\n";  | 
                                                        |
| 569 | -  | 
                                                        |
| 570 | -		$baseDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data'); | 
                                                        |
| 571 | - file_put_contents($baseDir . '/.htaccess', $content);  | 
                                                        |
| 572 | - file_put_contents($baseDir . '/index.html', '');  | 
                                                        |
| 573 | - }  | 
                                                        |
| 574 | -  | 
                                                        |
| 575 | - /**  | 
                                                        |
| 576 | - * Return vendor from which this version was published  | 
                                                        |
| 577 | - *  | 
                                                        |
| 578 | - * @return string Get the vendor  | 
                                                        |
| 579 | - *  | 
                                                        |
| 580 | - * Copy of \OC\Updater::getVendor()  | 
                                                        |
| 581 | - */  | 
                                                        |
| 582 | -	private function getVendor() { | 
                                                        |
| 583 | - // this should really be a JSON file  | 
                                                        |
| 584 | - require \OC::$SERVERROOT . '/version.php';  | 
                                                        |
| 585 | - /** @var string $vendor */  | 
                                                        |
| 586 | - return (string)$vendor;  | 
                                                        |
| 587 | - }  | 
                                                        |
| 61 | + /** @var SystemConfig */  | 
                                                        |
| 62 | + protected $config;  | 
                                                        |
| 63 | + /** @var IniGetWrapper */  | 
                                                        |
| 64 | + protected $iniWrapper;  | 
                                                        |
| 65 | + /** @var IL10N */  | 
                                                        |
| 66 | + protected $l10n;  | 
                                                        |
| 67 | + /** @var Defaults */  | 
                                                        |
| 68 | + protected $defaults;  | 
                                                        |
| 69 | + /** @var ILogger */  | 
                                                        |
| 70 | + protected $logger;  | 
                                                        |
| 71 | + /** @var ISecureRandom */  | 
                                                        |
| 72 | + protected $random;  | 
                                                        |
| 73 | + /** @var Installer */  | 
                                                        |
| 74 | + protected $installer;  | 
                                                        |
| 75 | +  | 
                                                        |
| 76 | + /**  | 
                                                        |
| 77 | + * @param SystemConfig $config  | 
                                                        |
| 78 | + * @param IniGetWrapper $iniWrapper  | 
                                                        |
| 79 | + * @param IL10N $l10n  | 
                                                        |
| 80 | + * @param Defaults $defaults  | 
                                                        |
| 81 | + * @param ILogger $logger  | 
                                                        |
| 82 | + * @param ISecureRandom $random  | 
                                                        |
| 83 | + * @param Installer $installer  | 
                                                        |
| 84 | + */  | 
                                                        |
| 85 | + public function __construct(  | 
                                                        |
| 86 | + SystemConfig $config,  | 
                                                        |
| 87 | + IniGetWrapper $iniWrapper,  | 
                                                        |
| 88 | + IL10N $l10n,  | 
                                                        |
| 89 | + Defaults $defaults,  | 
                                                        |
| 90 | + ILogger $logger,  | 
                                                        |
| 91 | + ISecureRandom $random,  | 
                                                        |
| 92 | + Installer $installer  | 
                                                        |
| 93 | +    ) { | 
                                                        |
| 94 | + $this->config = $config;  | 
                                                        |
| 95 | + $this->iniWrapper = $iniWrapper;  | 
                                                        |
| 96 | + $this->l10n = $l10n;  | 
                                                        |
| 97 | + $this->defaults = $defaults;  | 
                                                        |
| 98 | + $this->logger = $logger;  | 
                                                        |
| 99 | + $this->random = $random;  | 
                                                        |
| 100 | + $this->installer = $installer;  | 
                                                        |
| 101 | + }  | 
                                                        |
| 102 | +  | 
                                                        |
| 103 | + static protected $dbSetupClasses = [  | 
                                                        |
| 104 | + 'mysql' => \OC\Setup\MySQL::class,  | 
                                                        |
| 105 | + 'pgsql' => \OC\Setup\PostgreSQL::class,  | 
                                                        |
| 106 | + 'oci' => \OC\Setup\OCI::class,  | 
                                                        |
| 107 | + 'sqlite' => \OC\Setup\Sqlite::class,  | 
                                                        |
| 108 | + 'sqlite3' => \OC\Setup\Sqlite::class,  | 
                                                        |
| 109 | + ];  | 
                                                        |
| 110 | +  | 
                                                        |
| 111 | + /**  | 
                                                        |
| 112 | + * Wrapper around the "class_exists" PHP function to be able to mock it  | 
                                                        |
| 113 | + *  | 
                                                        |
| 114 | + * @param string $name  | 
                                                        |
| 115 | + * @return bool  | 
                                                        |
| 116 | + */  | 
                                                        |
| 117 | +    protected function class_exists($name) { | 
                                                        |
| 118 | + return class_exists($name);  | 
                                                        |
| 119 | + }  | 
                                                        |
| 120 | +  | 
                                                        |
| 121 | + /**  | 
                                                        |
| 122 | + * Wrapper around the "is_callable" PHP function to be able to mock it  | 
                                                        |
| 123 | + *  | 
                                                        |
| 124 | + * @param string $name  | 
                                                        |
| 125 | + * @return bool  | 
                                                        |
| 126 | + */  | 
                                                        |
| 127 | +    protected function is_callable($name) { | 
                                                        |
| 128 | + return is_callable($name);  | 
                                                        |
| 129 | + }  | 
                                                        |
| 130 | +  | 
                                                        |
| 131 | + /**  | 
                                                        |
| 132 | + * Wrapper around \PDO::getAvailableDrivers  | 
                                                        |
| 133 | + *  | 
                                                        |
| 134 | + * @return array  | 
                                                        |
| 135 | + */  | 
                                                        |
| 136 | +    protected function getAvailableDbDriversForPdo() { | 
                                                        |
| 137 | + return \PDO::getAvailableDrivers();  | 
                                                        |
| 138 | + }  | 
                                                        |
| 139 | +  | 
                                                        |
| 140 | + /**  | 
                                                        |
| 141 | + * Get the available and supported databases of this instance  | 
                                                        |
| 142 | + *  | 
                                                        |
| 143 | + * @param bool $allowAllDatabases  | 
                                                        |
| 144 | + * @return array  | 
                                                        |
| 145 | + * @throws Exception  | 
                                                        |
| 146 | + */  | 
                                                        |
| 147 | +    public function getSupportedDatabases($allowAllDatabases = false) { | 
                                                        |
| 148 | + $availableDatabases = [  | 
                                                        |
| 149 | + 'sqlite' => [  | 
                                                        |
| 150 | + 'type' => 'pdo',  | 
                                                        |
| 151 | + 'call' => 'sqlite',  | 
                                                        |
| 152 | + 'name' => 'SQLite',  | 
                                                        |
| 153 | + ],  | 
                                                        |
| 154 | + 'mysql' => [  | 
                                                        |
| 155 | + 'type' => 'pdo',  | 
                                                        |
| 156 | + 'call' => 'mysql',  | 
                                                        |
| 157 | + 'name' => 'MySQL/MariaDB',  | 
                                                        |
| 158 | + ],  | 
                                                        |
| 159 | + 'pgsql' => [  | 
                                                        |
| 160 | + 'type' => 'pdo',  | 
                                                        |
| 161 | + 'call' => 'pgsql',  | 
                                                        |
| 162 | + 'name' => 'PostgreSQL',  | 
                                                        |
| 163 | + ],  | 
                                                        |
| 164 | + 'oci' => [  | 
                                                        |
| 165 | + 'type' => 'function',  | 
                                                        |
| 166 | + 'call' => 'oci_connect',  | 
                                                        |
| 167 | + 'name' => 'Oracle',  | 
                                                        |
| 168 | + ],  | 
                                                        |
| 169 | + ];  | 
                                                        |
| 170 | +        if ($allowAllDatabases) { | 
                                                        |
| 171 | + $configuredDatabases = array_keys($availableDatabases);  | 
                                                        |
| 172 | +        } else { | 
                                                        |
| 173 | +            $configuredDatabases = $this->config->getValue('supportedDatabases', | 
                                                        |
| 174 | + ['sqlite', 'mysql', 'pgsql']);  | 
                                                        |
| 175 | + }  | 
                                                        |
| 176 | +        if (!is_array($configuredDatabases)) { | 
                                                        |
| 177 | +            throw new Exception('Supported databases are not properly configured.'); | 
                                                        |
| 178 | + }  | 
                                                        |
| 179 | +  | 
                                                        |
| 180 | + $supportedDatabases = array();  | 
                                                        |
| 181 | +  | 
                                                        |
| 182 | +        foreach ($configuredDatabases as $database) { | 
                                                        |
| 183 | +            if (array_key_exists($database, $availableDatabases)) { | 
                                                        |
| 184 | + $working = false;  | 
                                                        |
| 185 | + $type = $availableDatabases[$database]['type'];  | 
                                                        |
| 186 | + $call = $availableDatabases[$database]['call'];  | 
                                                        |
| 187 | +  | 
                                                        |
| 188 | +                if ($type === 'function') { | 
                                                        |
| 189 | + $working = $this->is_callable($call);  | 
                                                        |
| 190 | +                } elseif ($type === 'pdo') { | 
                                                        |
| 191 | + $working = in_array($call, $this->getAvailableDbDriversForPdo(), true);  | 
                                                        |
| 192 | + }  | 
                                                        |
| 193 | +                if ($working) { | 
                                                        |
| 194 | + $supportedDatabases[$database] = $availableDatabases[$database]['name'];  | 
                                                        |
| 195 | + }  | 
                                                        |
| 196 | + }  | 
                                                        |
| 197 | + }  | 
                                                        |
| 198 | +  | 
                                                        |
| 199 | + return $supportedDatabases;  | 
                                                        |
| 200 | + }  | 
                                                        |
| 201 | +  | 
                                                        |
| 202 | + /**  | 
                                                        |
| 203 | + * Gathers system information like database type and does  | 
                                                        |
| 204 | + * a few system checks.  | 
                                                        |
| 205 | + *  | 
                                                        |
| 206 | + * @return array of system info, including an "errors" value  | 
                                                        |
| 207 | + * in case of errors/warnings  | 
                                                        |
| 208 | + */  | 
                                                        |
| 209 | +    public function getSystemInfo($allowAllDatabases = false) { | 
                                                        |
| 210 | + $databases = $this->getSupportedDatabases($allowAllDatabases);  | 
                                                        |
| 211 | +  | 
                                                        |
| 212 | +        $dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); | 
                                                        |
| 213 | +  | 
                                                        |
| 214 | + $errors = [];  | 
                                                        |
| 215 | +  | 
                                                        |
| 216 | + // Create data directory to test whether the .htaccess works  | 
                                                        |
| 217 | + // Notice that this is not necessarily the same data directory as the one  | 
                                                        |
| 218 | + // that will effectively be used.  | 
                                                        |
| 219 | +        if (!file_exists($dataDir)) { | 
                                                        |
| 220 | + @mkdir($dataDir);  | 
                                                        |
| 221 | + }  | 
                                                        |
| 222 | + $htAccessWorking = true;  | 
                                                        |
| 223 | +        if (is_dir($dataDir) && is_writable($dataDir)) { | 
                                                        |
| 224 | + // Protect data directory here, so we can test if the protection is working  | 
                                                        |
| 225 | + self::protectDataDirectory();  | 
                                                        |
| 226 | +  | 
                                                        |
| 227 | +            try { | 
                                                        |
| 228 | + $util = new \OC_Util();  | 
                                                        |
| 229 | + $htAccessWorking = $util->isHtaccessWorking(\OC::$server->getConfig());  | 
                                                        |
| 230 | +            } catch (\OC\HintException $e) { | 
                                                        |
| 231 | + $errors[] = [  | 
                                                        |
| 232 | + 'error' => $e->getMessage(),  | 
                                                        |
| 233 | + 'hint' => $e->getHint(),  | 
                                                        |
| 234 | + ];  | 
                                                        |
| 235 | + $htAccessWorking = false;  | 
                                                        |
| 236 | + }  | 
                                                        |
| 237 | + }  | 
                                                        |
| 238 | +  | 
                                                        |
| 239 | +        if (\OC_Util::runningOnMac()) { | 
                                                        |
| 240 | + $errors[] = [  | 
                                                        |
| 241 | + 'error' => $this->l10n->t(  | 
                                                        |
| 242 | + 'Mac OS X is not supported and %s will not work properly on this platform. ' .  | 
                                                        |
| 243 | + 'Use it at your own risk! ',  | 
                                                        |
| 244 | + [$this->defaults->getName()]  | 
                                                        |
| 245 | + ),  | 
                                                        |
| 246 | +                'hint' => $this->l10n->t('For the best results, please consider using a GNU/Linux server instead.'), | 
                                                        |
| 247 | + ];  | 
                                                        |
| 248 | + }  | 
                                                        |
| 249 | +  | 
                                                        |
| 250 | +        if ($this->iniWrapper->getString('open_basedir') !== '' && PHP_INT_SIZE === 4) { | 
                                                        |
| 251 | + $errors[] = [  | 
                                                        |
| 252 | + 'error' => $this->l10n->t(  | 
                                                        |
| 253 | + 'It seems that this %s instance is running on a 32-bit PHP environment and the open_basedir has been configured in php.ini. ' .  | 
                                                        |
| 254 | + 'This will lead to problems with files over 4 GB and is highly discouraged.',  | 
                                                        |
| 255 | + [$this->defaults->getName()]  | 
                                                        |
| 256 | + ),  | 
                                                        |
| 257 | +                'hint' => $this->l10n->t('Please remove the open_basedir setting within your php.ini or switch to 64-bit PHP.'), | 
                                                        |
| 258 | + ];  | 
                                                        |
| 259 | + }  | 
                                                        |
| 260 | +  | 
                                                        |
| 261 | + return array(  | 
                                                        |
| 262 | + 'hasSQLite' => isset($databases['sqlite']),  | 
                                                        |
| 263 | + 'hasMySQL' => isset($databases['mysql']),  | 
                                                        |
| 264 | + 'hasPostgreSQL' => isset($databases['pgsql']),  | 
                                                        |
| 265 | + 'hasOracle' => isset($databases['oci']),  | 
                                                        |
| 266 | + 'databases' => $databases,  | 
                                                        |
| 267 | + 'directory' => $dataDir,  | 
                                                        |
| 268 | + 'htaccessWorking' => $htAccessWorking,  | 
                                                        |
| 269 | + 'errors' => $errors,  | 
                                                        |
| 270 | + );  | 
                                                        |
| 271 | + }  | 
                                                        |
| 272 | +  | 
                                                        |
| 273 | + /**  | 
                                                        |
| 274 | + * @param $options  | 
                                                        |
| 275 | + * @return array  | 
                                                        |
| 276 | + */  | 
                                                        |
| 277 | +    public function install($options) { | 
                                                        |
| 278 | + $l = $this->l10n;  | 
                                                        |
| 279 | +  | 
                                                        |
| 280 | + $error = array();  | 
                                                        |
| 281 | + $dbType = $options['dbtype'];  | 
                                                        |
| 282 | +  | 
                                                        |
| 283 | +        if (empty($options['adminlogin'])) { | 
                                                        |
| 284 | +            $error[] = $l->t('Set an admin username.'); | 
                                                        |
| 285 | + }  | 
                                                        |
| 286 | +        if (empty($options['adminpass'])) { | 
                                                        |
| 287 | +            $error[] = $l->t('Set an admin password.'); | 
                                                        |
| 288 | + }  | 
                                                        |
| 289 | +        if (empty($options['directory'])) { | 
                                                        |
| 290 | + $options['directory'] = \OC::$SERVERROOT . "/data";  | 
                                                        |
| 291 | + }  | 
                                                        |
| 292 | +  | 
                                                        |
| 293 | +        if (!isset(self::$dbSetupClasses[$dbType])) { | 
                                                        |
| 294 | + $dbType = 'sqlite';  | 
                                                        |
| 295 | + }  | 
                                                        |
| 296 | +  | 
                                                        |
| 297 | + $username = htmlspecialchars_decode($options['adminlogin']);  | 
                                                        |
| 298 | + $password = htmlspecialchars_decode($options['adminpass']);  | 
                                                        |
| 299 | + $dataDir = htmlspecialchars_decode($options['directory']);  | 
                                                        |
| 300 | +  | 
                                                        |
| 301 | + $class = self::$dbSetupClasses[$dbType];  | 
                                                        |
| 302 | + /** @var \OC\Setup\AbstractDatabase $dbSetup */  | 
                                                        |
| 303 | + $dbSetup = new $class($l, $this->config, $this->logger, $this->random);  | 
                                                        |
| 304 | + $error = array_merge($error, $dbSetup->validate($options));  | 
                                                        |
| 305 | +  | 
                                                        |
| 306 | + // validate the data directory  | 
                                                        |
| 307 | +        if ((!is_dir($dataDir) && !mkdir($dataDir)) || !is_writable($dataDir)) { | 
                                                        |
| 308 | +            $error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); | 
                                                        |
| 309 | + }  | 
                                                        |
| 310 | +  | 
                                                        |
| 311 | +        if (!empty($error)) { | 
                                                        |
| 312 | + return $error;  | 
                                                        |
| 313 | + }  | 
                                                        |
| 314 | +  | 
                                                        |
| 315 | + $request = \OC::$server->getRequest();  | 
                                                        |
| 316 | +  | 
                                                        |
| 317 | + //no errors, good  | 
                                                        |
| 318 | + if (isset($options['trusted_domains'])  | 
                                                        |
| 319 | +            && is_array($options['trusted_domains'])) { | 
                                                        |
| 320 | + $trustedDomains = $options['trusted_domains'];  | 
                                                        |
| 321 | +        } else { | 
                                                        |
| 322 | + $trustedDomains = [$request->getInsecureServerHost()];  | 
                                                        |
| 323 | + }  | 
                                                        |
| 324 | +  | 
                                                        |
| 325 | + //use sqlite3 when available, otherwise sqlite2 will be used.  | 
                                                        |
| 326 | +        if ($dbType === 'sqlite' && class_exists('SQLite3')) { | 
                                                        |
| 327 | + $dbType = 'sqlite3';  | 
                                                        |
| 328 | + }  | 
                                                        |
| 329 | +  | 
                                                        |
| 330 | + //generate a random salt that is used to salt the local user passwords  | 
                                                        |
| 331 | + $salt = $this->random->generate(30);  | 
                                                        |
| 332 | + // generate a secret  | 
                                                        |
| 333 | + $secret = $this->random->generate(48);  | 
                                                        |
| 334 | +  | 
                                                        |
| 335 | + //write the config file  | 
                                                        |
| 336 | + $newConfigValues = [  | 
                                                        |
| 337 | + 'passwordsalt' => $salt,  | 
                                                        |
| 338 | + 'secret' => $secret,  | 
                                                        |
| 339 | + 'trusted_domains' => $trustedDomains,  | 
                                                        |
| 340 | + 'datadirectory' => $dataDir,  | 
                                                        |
| 341 | + 'dbtype' => $dbType,  | 
                                                        |
| 342 | +            'version' => implode('.', \OCP\Util::getVersion()), | 
                                                        |
| 343 | + ];  | 
                                                        |
| 344 | +  | 
                                                        |
| 345 | +        if ($this->config->getValue('overwrite.cli.url', null) === null) { | 
                                                        |
| 346 | + $newConfigValues['overwrite.cli.url'] = $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT;  | 
                                                        |
| 347 | + }  | 
                                                        |
| 348 | +  | 
                                                        |
| 349 | + $this->config->setValues($newConfigValues);  | 
                                                        |
| 350 | +  | 
                                                        |
| 351 | +        try { | 
                                                        |
| 352 | + $dbSetup->initialize($options);  | 
                                                        |
| 353 | + $dbSetup->setupDatabase($username);  | 
                                                        |
| 354 | + // apply necessary migrations  | 
                                                        |
| 355 | + $dbSetup->runMigrations();  | 
                                                        |
| 356 | +        } catch (\OC\DatabaseSetupException $e) { | 
                                                        |
| 357 | + $error[] = [  | 
                                                        |
| 358 | + 'error' => $e->getMessage(),  | 
                                                        |
| 359 | + 'hint' => $e->getHint(),  | 
                                                        |
| 360 | + ];  | 
                                                        |
| 361 | + return $error;  | 
                                                        |
| 362 | +        } catch (Exception $e) { | 
                                                        |
| 363 | + $error[] = [  | 
                                                        |
| 364 | + 'error' => 'Error while trying to create admin user: ' . $e->getMessage(),  | 
                                                        |
| 365 | + 'hint' => '',  | 
                                                        |
| 366 | + ];  | 
                                                        |
| 367 | + return $error;  | 
                                                        |
| 368 | + }  | 
                                                        |
| 369 | +  | 
                                                        |
| 370 | + //create the user and group  | 
                                                        |
| 371 | + $user = null;  | 
                                                        |
| 372 | +        try { | 
                                                        |
| 373 | + $user = \OC::$server->getUserManager()->createUser($username, $password);  | 
                                                        |
| 374 | +            if (!$user) { | 
                                                        |
| 375 | + $error[] = "User <$username> could not be created.";  | 
                                                        |
| 376 | + }  | 
                                                        |
| 377 | +        } catch (Exception $exception) { | 
                                                        |
| 378 | + $error[] = $exception->getMessage();  | 
                                                        |
| 379 | + }  | 
                                                        |
| 380 | +  | 
                                                        |
| 381 | +        if (empty($error)) { | 
                                                        |
| 382 | + $config = \OC::$server->getConfig();  | 
                                                        |
| 383 | +            $config->setAppValue('core', 'installedat', microtime(true)); | 
                                                        |
| 384 | +            $config->setAppValue('core', 'lastupdatedat', microtime(true)); | 
                                                        |
| 385 | +            $config->setAppValue('core', 'vendor', $this->getVendor()); | 
                                                        |
| 386 | +  | 
                                                        |
| 387 | +            $group = \OC::$server->getGroupManager()->createGroup('admin'); | 
                                                        |
| 388 | +            if ($group instanceof IGroup) { | 
                                                        |
| 389 | + $group->addUser($user);  | 
                                                        |
| 390 | + }  | 
                                                        |
| 391 | +  | 
                                                        |
| 392 | + // Install shipped apps and specified app bundles  | 
                                                        |
| 393 | + Installer::installShippedApps();  | 
                                                        |
| 394 | +            $bundleFetcher = new BundleFetcher(\OC::$server->getL10N('lib')); | 
                                                        |
| 395 | + $defaultInstallationBundles = $bundleFetcher->getDefaultInstallationBundle();  | 
                                                        |
| 396 | +            foreach ($defaultInstallationBundles as $bundle) { | 
                                                        |
| 397 | +                try { | 
                                                        |
| 398 | + $this->installer->installAppBundle($bundle);  | 
                                                        |
| 399 | +                } catch (Exception $e) { | 
                                                        |
| 400 | + }  | 
                                                        |
| 401 | + }  | 
                                                        |
| 402 | +  | 
                                                        |
| 403 | + // create empty file in data dir, so we can later find  | 
                                                        |
| 404 | + // out that this is indeed an ownCloud data directory  | 
                                                        |
| 405 | +            file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', ''); | 
                                                        |
| 406 | +  | 
                                                        |
| 407 | + // Update .htaccess files  | 
                                                        |
| 408 | + self::updateHtaccess();  | 
                                                        |
| 409 | + self::protectDataDirectory();  | 
                                                        |
| 410 | +  | 
                                                        |
| 411 | + self::installBackgroundJobs();  | 
                                                        |
| 412 | +  | 
                                                        |
| 413 | + //and we are done  | 
                                                        |
| 414 | +            $config->setSystemValue('installed', true); | 
                                                        |
| 415 | +  | 
                                                        |
| 416 | + // Create a session token for the newly created user  | 
                                                        |
| 417 | + // The token provider requires a working db, so it's not injected on setup  | 
                                                        |
| 418 | + /* @var $userSession User\Session */  | 
                                                        |
| 419 | + $userSession = \OC::$server->getUserSession();  | 
                                                        |
| 420 | + $defaultTokenProvider = \OC::$server->query(DefaultTokenProvider::class);  | 
                                                        |
| 421 | + $userSession->setTokenProvider($defaultTokenProvider);  | 
                                                        |
| 422 | + $userSession->login($username, $password);  | 
                                                        |
| 423 | + $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password);  | 
                                                        |
| 424 | +  | 
                                                        |
| 425 | + $session = $userSession->getSession();  | 
                                                        |
| 426 | +            $session->set('last-password-confirm', \OC::$server->query(ITimeFactory::class)->getTime()); | 
                                                        |
| 427 | +  | 
                                                        |
| 428 | + // Set email for admin  | 
                                                        |
| 429 | +            if (!empty($options['adminemail'])) { | 
                                                        |
| 430 | + $config->setUserValue($user->getUID(), 'settings', 'email', $options['adminemail']);  | 
                                                        |
| 431 | + }  | 
                                                        |
| 432 | + }  | 
                                                        |
| 433 | +  | 
                                                        |
| 434 | + return $error;  | 
                                                        |
| 435 | + }  | 
                                                        |
| 436 | +  | 
                                                        |
| 437 | +    public static function installBackgroundJobs() { | 
                                                        |
| 438 | + $jobList = \OC::$server->getJobList();  | 
                                                        |
| 439 | + $jobList->add(DefaultTokenCleanupJob::class);  | 
                                                        |
| 440 | + $jobList->add(Rotate::class);  | 
                                                        |
| 441 | + $jobList->add(BackgroundCleanupJob::class);  | 
                                                        |
| 442 | + }  | 
                                                        |
| 443 | +  | 
                                                        |
| 444 | + /**  | 
                                                        |
| 445 | + * @return string Absolute path to htaccess  | 
                                                        |
| 446 | + */  | 
                                                        |
| 447 | +    private function pathToHtaccess() { | 
                                                        |
| 448 | + return \OC::$SERVERROOT . '/.htaccess';  | 
                                                        |
| 449 | + }  | 
                                                        |
| 450 | +  | 
                                                        |
| 451 | + /**  | 
                                                        |
| 452 | + * Find webroot from config  | 
                                                        |
| 453 | + *  | 
                                                        |
| 454 | + * @param SystemConfig $config  | 
                                                        |
| 455 | + * @return string  | 
                                                        |
| 456 | + * @throws InvalidArgumentException when invalid value for overwrite.cli.url  | 
                                                        |
| 457 | + */  | 
                                                        |
| 458 | +    private static function findWebRoot(SystemConfig $config): string { | 
                                                        |
| 459 | + // For CLI read the value from overwrite.cli.url  | 
                                                        |
| 460 | +        if (\OC::$CLI) { | 
                                                        |
| 461 | +            $webRoot = $config->getValue('overwrite.cli.url', ''); | 
                                                        |
| 462 | +            if ($webRoot === '') { | 
                                                        |
| 463 | +                throw new InvalidArgumentException('overwrite.cli.url is empty'); | 
                                                        |
| 464 | + }  | 
                                                        |
| 465 | +            if (!filter_var($webRoot, FILTER_VALIDATE_URL)) { | 
                                                        |
| 466 | +                throw new InvalidArgumentException('invalid value for overwrite.cli.url'); | 
                                                        |
| 467 | + }  | 
                                                        |
| 468 | + $webRoot = rtrim(parse_url($webRoot, PHP_URL_PATH), '/');  | 
                                                        |
| 469 | +        } else { | 
                                                        |
| 470 | + $webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';  | 
                                                        |
| 471 | + }  | 
                                                        |
| 472 | +  | 
                                                        |
| 473 | + return $webRoot;  | 
                                                        |
| 474 | + }  | 
                                                        |
| 475 | +  | 
                                                        |
| 476 | + /**  | 
                                                        |
| 477 | + * Append the correct ErrorDocument path for Apache hosts  | 
                                                        |
| 478 | + *  | 
                                                        |
| 479 | + * @return bool True when success, False otherwise  | 
                                                        |
| 480 | + * @throws \OCP\AppFramework\QueryException  | 
                                                        |
| 481 | + */  | 
                                                        |
| 482 | +    public static function updateHtaccess() { | 
                                                        |
| 483 | + $config = \OC::$server->getSystemConfig();  | 
                                                        |
| 484 | +  | 
                                                        |
| 485 | +        try { | 
                                                        |
| 486 | + $webRoot = self::findWebRoot($config);  | 
                                                        |
| 487 | +        } catch (InvalidArgumentException $e) { | 
                                                        |
| 488 | + return false;  | 
                                                        |
| 489 | + }  | 
                                                        |
| 490 | +  | 
                                                        |
| 491 | + $setupHelper = new \OC\Setup(  | 
                                                        |
| 492 | + $config,  | 
                                                        |
| 493 | + \OC::$server->getIniWrapper(),  | 
                                                        |
| 494 | +            \OC::$server->getL10N('lib'), | 
                                                        |
| 495 | + \OC::$server->query(Defaults::class),  | 
                                                        |
| 496 | + \OC::$server->getLogger(),  | 
                                                        |
| 497 | + \OC::$server->getSecureRandom(),  | 
                                                        |
| 498 | + \OC::$server->query(Installer::class)  | 
                                                        |
| 499 | + );  | 
                                                        |
| 500 | +  | 
                                                        |
| 501 | + $htaccessContent = file_get_contents($setupHelper->pathToHtaccess());  | 
                                                        |
| 502 | + $content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n";  | 
                                                        |
| 503 | + $htaccessContent = explode($content, $htaccessContent, 2)[0];  | 
                                                        |
| 504 | +  | 
                                                        |
| 505 | + //custom 403 error page  | 
                                                        |
| 506 | + $content .= "\nErrorDocument 403 " . $webRoot . '/';  | 
                                                        |
| 507 | +  | 
                                                        |
| 508 | + //custom 404 error page  | 
                                                        |
| 509 | + $content .= "\nErrorDocument 404 " . $webRoot . '/';  | 
                                                        |
| 510 | +  | 
                                                        |
| 511 | + // Add rewrite rules if the RewriteBase is configured  | 
                                                        |
| 512 | +        $rewriteBase = $config->getValue('htaccess.RewriteBase', ''); | 
                                                        |
| 513 | +        if ($rewriteBase !== '') { | 
                                                        |
| 514 | + $content .= "\n<IfModule mod_rewrite.c>";  | 
                                                        |
| 515 | + $content .= "\n Options -MultiViews";  | 
                                                        |
| 516 | + $content .= "\n RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]";  | 
                                                        |
| 517 | + $content .= "\n RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]";  | 
                                                        |
| 518 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !\\.(css|js|svg|gif|png|html|ttf|woff2?|ico|jpg|jpeg|map)$"; | 
                                                        |
| 519 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !core/img/favicon.ico$"; | 
                                                        |
| 520 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !core/img/manifest.json$"; | 
                                                        |
| 521 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/remote.php"; | 
                                                        |
| 522 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/public.php"; | 
                                                        |
| 523 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/cron.php"; | 
                                                        |
| 524 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/core/ajax/update.php"; | 
                                                        |
| 525 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/status.php"; | 
                                                        |
| 526 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs/v1.php"; | 
                                                        |
| 527 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs/v2.php"; | 
                                                        |
| 528 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/robots.txt"; | 
                                                        |
| 529 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/updater/"; | 
                                                        |
| 530 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs-provider/"; | 
                                                        |
| 531 | +            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocm-provider/"; | 
                                                        |
| 532 | +            $content .= "\n  RewriteCond %{REQUEST_URI} !^/\\.well-known/(acme-challenge|pki-validation)/.*"; | 
                                                        |
| 533 | + $content .= "\n RewriteRule . index.php [PT,E=PATH_INFO:$1]";  | 
                                                        |
| 534 | + $content .= "\n RewriteBase " . $rewriteBase;  | 
                                                        |
| 535 | + $content .= "\n <IfModule mod_env.c>";  | 
                                                        |
| 536 | + $content .= "\n SetEnv front_controller_active true";  | 
                                                        |
| 537 | + $content .= "\n <IfModule mod_dir.c>";  | 
                                                        |
| 538 | + $content .= "\n DirectorySlash off";  | 
                                                        |
| 539 | + $content .= "\n </IfModule>";  | 
                                                        |
| 540 | + $content .= "\n </IfModule>";  | 
                                                        |
| 541 | + $content .= "\n</IfModule>";  | 
                                                        |
| 542 | + }  | 
                                                        |
| 543 | +  | 
                                                        |
| 544 | +        if ($content !== '') { | 
                                                        |
| 545 | + //suppress errors in case we don't have permissions for it  | 
                                                        |
| 546 | + return (bool)@file_put_contents($setupHelper->pathToHtaccess(), $htaccessContent . $content . "\n");  | 
                                                        |
| 547 | + }  | 
                                                        |
| 548 | +  | 
                                                        |
| 549 | + return false;  | 
                                                        |
| 550 | + }  | 
                                                        |
| 551 | +  | 
                                                        |
| 552 | +    public static function protectDataDirectory() { | 
                                                        |
| 553 | + //Require all denied  | 
                                                        |
| 554 | +        $now = date('Y-m-d H:i:s'); | 
                                                        |
| 555 | + $content = "# Generated by Nextcloud on $now\n";  | 
                                                        |
| 556 | + $content .= "# line below if for Apache 2.4\n";  | 
                                                        |
| 557 | + $content .= "<ifModule mod_authz_core.c>\n";  | 
                                                        |
| 558 | + $content .= "Require all denied\n";  | 
                                                        |
| 559 | + $content .= "</ifModule>\n\n";  | 
                                                        |
| 560 | + $content .= "# line below if for Apache 2.2\n";  | 
                                                        |
| 561 | + $content .= "<ifModule !mod_authz_core.c>\n";  | 
                                                        |
| 562 | + $content .= "deny from all\n";  | 
                                                        |
| 563 | + $content .= "Satisfy All\n";  | 
                                                        |
| 564 | + $content .= "</ifModule>\n\n";  | 
                                                        |
| 565 | + $content .= "# section for Apache 2.2 and 2.4\n";  | 
                                                        |
| 566 | + $content .= "<ifModule mod_autoindex.c>\n";  | 
                                                        |
| 567 | + $content .= "IndexIgnore *\n";  | 
                                                        |
| 568 | + $content .= "</ifModule>\n";  | 
                                                        |
| 569 | +  | 
                                                        |
| 570 | +        $baseDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data'); | 
                                                        |
| 571 | + file_put_contents($baseDir . '/.htaccess', $content);  | 
                                                        |
| 572 | + file_put_contents($baseDir . '/index.html', '');  | 
                                                        |
| 573 | + }  | 
                                                        |
| 574 | +  | 
                                                        |
| 575 | + /**  | 
                                                        |
| 576 | + * Return vendor from which this version was published  | 
                                                        |
| 577 | + *  | 
                                                        |
| 578 | + * @return string Get the vendor  | 
                                                        |
| 579 | + *  | 
                                                        |
| 580 | + * Copy of \OC\Updater::getVendor()  | 
                                                        |
| 581 | + */  | 
                                                        |
| 582 | +    private function getVendor() { | 
                                                        |
| 583 | + // this should really be a JSON file  | 
                                                        |
| 584 | + require \OC::$SERVERROOT . '/version.php';  | 
                                                        |
| 585 | + /** @var string $vendor */  | 
                                                        |
| 586 | + return (string)$vendor;  | 
                                                        |
| 587 | + }  | 
                                                        |
| 588 | 588 | }  |