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