2011-04-16 22:45:05 +00:00
|
|
|
<?php
|
2024-01-29 14:12:53 +00:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2014-10-27 11:51:26 +00:00
|
|
|
/**
|
2024-05-23 07:26:56 +00:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-10-27 11:51:26 +00:00
|
|
|
*/
|
2015-02-21 19:51:50 +00:00
|
|
|
namespace OC;
|
|
|
|
|
2015-03-10 22:44:29 +00:00
|
|
|
use bantu\IniGetWrapper\IniGetWrapper;
|
2015-02-21 19:51:50 +00:00
|
|
|
use Exception;
|
2018-09-10 20:45:40 +00:00
|
|
|
use InvalidArgumentException;
|
2021-12-01 17:41:31 +00:00
|
|
|
use OC\Authentication\Token\PublicKeyTokenProvider;
|
2022-07-27 08:52:05 +00:00
|
|
|
use OC\Authentication\Token\TokenCleanupJob;
|
2017-10-23 19:26:24 +00:00
|
|
|
use OC\Log\Rotate;
|
2018-05-08 12:47:50 +00:00
|
|
|
use OC\Preview\BackgroundCleanupJob;
|
2023-07-14 13:59:50 +00:00
|
|
|
use OC\TextProcessing\RemoveOldTasksBackgroundJob;
|
2024-09-10 20:50:16 +00:00
|
|
|
use OC\User\BackgroundJobs\CleanupDeletedUsers;
|
2019-12-03 18:31:31 +00:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2024-01-29 14:12:53 +00:00
|
|
|
use OCP\BackgroundJob\IJobList;
|
2017-04-07 20:42:43 +00:00
|
|
|
use OCP\Defaults;
|
2024-03-13 02:21:40 +00:00
|
|
|
use OCP\IAppConfig;
|
2024-01-29 14:12:53 +00:00
|
|
|
use OCP\IConfig;
|
2019-10-09 17:13:27 +00:00
|
|
|
use OCP\IGroup;
|
2024-01-29 14:12:53 +00:00
|
|
|
use OCP\IGroupManager;
|
2015-03-10 22:44:29 +00:00
|
|
|
use OCP\IL10N;
|
2024-01-29 14:12:53 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\L10N\IFactory as IL10NFactory;
|
2023-10-31 11:06:09 +00:00
|
|
|
use OCP\Migration\IOutput;
|
2015-07-29 22:04:30 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
2024-01-29 14:12:53 +00:00
|
|
|
use OCP\Server;
|
2024-09-17 20:38:44 +00:00
|
|
|
use OCP\ServerVersion;
|
2021-04-16 12:26:43 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2011-04-16 22:45:05 +00:00
|
|
|
|
2015-02-21 19:51:50 +00:00
|
|
|
class Setup {
|
2024-01-29 14:12:53 +00:00
|
|
|
protected IL10N $l10n;
|
2014-10-27 11:51:26 +00:00
|
|
|
|
2019-10-14 09:32:05 +00:00
|
|
|
public function __construct(
|
2024-01-29 14:12:53 +00:00
|
|
|
protected SystemConfig $config,
|
|
|
|
protected IniGetWrapper $iniWrapper,
|
|
|
|
IL10NFactory $l10nFactory,
|
|
|
|
protected Defaults $defaults,
|
|
|
|
protected LoggerInterface $logger,
|
|
|
|
protected ISecureRandom $random,
|
|
|
|
protected Installer $installer,
|
2019-10-14 09:32:05 +00:00
|
|
|
) {
|
2024-01-29 14:12:53 +00:00
|
|
|
$this->l10n = $l10nFactory->get('lib');
|
2014-10-27 11:51:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
protected static array $dbSetupClasses = [
|
2017-03-19 20:52:54 +00:00
|
|
|
'mysql' => \OC\Setup\MySQL::class,
|
|
|
|
'pgsql' => \OC\Setup\PostgreSQL::class,
|
2019-10-14 09:32:05 +00:00
|
|
|
'oci' => \OC\Setup\OCI::class,
|
2017-03-19 20:52:54 +00:00
|
|
|
'sqlite' => \OC\Setup\Sqlite::class,
|
|
|
|
'sqlite3' => \OC\Setup\Sqlite::class,
|
|
|
|
];
|
2013-02-09 18:23:36 +00:00
|
|
|
|
2014-10-27 11:51:26 +00:00
|
|
|
/**
|
|
|
|
* Wrapper around the "class_exists" PHP function to be able to mock it
|
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
protected function class_exists(string $name): bool {
|
2014-10-27 11:51:26 +00:00
|
|
|
return class_exists($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around the "is_callable" PHP function to be able to mock it
|
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
protected function is_callable(string $name): bool {
|
2014-10-27 11:51:26 +00:00
|
|
|
return is_callable($name);
|
|
|
|
}
|
|
|
|
|
2015-07-30 10:32:22 +00:00
|
|
|
/**
|
|
|
|
* Wrapper around \PDO::getAvailableDrivers
|
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
protected function getAvailableDbDriversForPdo(): array {
|
2021-07-05 07:49:05 +00:00
|
|
|
if (class_exists(\PDO::class)) {
|
|
|
|
return \PDO::getAvailableDrivers();
|
|
|
|
}
|
|
|
|
return [];
|
2015-07-30 10:32:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 11:51:26 +00:00
|
|
|
/**
|
|
|
|
* Get the available and supported databases of this instance
|
|
|
|
*
|
|
|
|
* @return array
|
2015-03-11 14:47:24 +00:00
|
|
|
* @throws Exception
|
2014-10-27 11:51:26 +00:00
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
public function getSupportedDatabases(bool $allowAllDatabases = false): array {
|
2017-09-26 12:27:42 +00:00
|
|
|
$availableDatabases = [
|
2019-10-14 09:32:05 +00:00
|
|
|
'sqlite' => [
|
2017-01-24 22:14:20 +00:00
|
|
|
'type' => 'pdo',
|
|
|
|
'call' => 'sqlite',
|
2017-09-26 12:27:42 +00:00
|
|
|
'name' => 'SQLite',
|
|
|
|
],
|
|
|
|
'mysql' => [
|
2015-07-30 10:32:22 +00:00
|
|
|
'type' => 'pdo',
|
|
|
|
'call' => 'mysql',
|
2017-09-26 12:27:42 +00:00
|
|
|
'name' => 'MySQL/MariaDB',
|
|
|
|
],
|
|
|
|
'pgsql' => [
|
2016-07-12 12:37:03 +00:00
|
|
|
'type' => 'pdo',
|
|
|
|
'call' => 'pgsql',
|
2017-09-26 12:27:42 +00:00
|
|
|
'name' => 'PostgreSQL',
|
|
|
|
],
|
|
|
|
'oci' => [
|
2014-10-27 11:51:26 +00:00
|
|
|
'type' => 'function',
|
|
|
|
'call' => 'oci_connect',
|
2017-09-26 12:27:42 +00:00
|
|
|
'name' => 'Oracle',
|
|
|
|
],
|
|
|
|
];
|
2015-03-11 14:47:24 +00:00
|
|
|
if ($allowAllDatabases) {
|
|
|
|
$configuredDatabases = array_keys($availableDatabases);
|
|
|
|
} else {
|
2017-03-17 22:37:48 +00:00
|
|
|
$configuredDatabases = $this->config->getValue('supportedDatabases',
|
2017-09-26 12:27:42 +00:00
|
|
|
['sqlite', 'mysql', 'pgsql']);
|
2015-03-11 14:47:24 +00:00
|
|
|
}
|
2019-10-14 09:32:05 +00:00
|
|
|
if (!is_array($configuredDatabases)) {
|
2014-10-27 11:51:26 +00:00
|
|
|
throw new Exception('Supported databases are not properly configured.');
|
|
|
|
}
|
|
|
|
|
2020-03-26 08:30:18 +00:00
|
|
|
$supportedDatabases = [];
|
2014-10-27 11:51:26 +00:00
|
|
|
|
2019-10-14 09:32:05 +00:00
|
|
|
foreach ($configuredDatabases as $database) {
|
|
|
|
if (array_key_exists($database, $availableDatabases)) {
|
2014-10-27 11:51:26 +00:00
|
|
|
$working = false;
|
2015-07-30 10:32:22 +00:00
|
|
|
$type = $availableDatabases[$database]['type'];
|
|
|
|
$call = $availableDatabases[$database]['call'];
|
|
|
|
|
2017-01-24 22:14:20 +00:00
|
|
|
if ($type === 'function') {
|
2015-07-30 10:32:22 +00:00
|
|
|
$working = $this->is_callable($call);
|
2019-10-14 09:32:05 +00:00
|
|
|
} elseif ($type === 'pdo') {
|
2017-09-26 12:27:42 +00:00
|
|
|
$working = in_array($call, $this->getAvailableDbDriversForPdo(), true);
|
2014-10-27 11:51:26 +00:00
|
|
|
}
|
2019-10-14 09:32:05 +00:00
|
|
|
if ($working) {
|
2014-10-27 11:51:26 +00:00
|
|
|
$supportedDatabases[$database] = $availableDatabases[$database]['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $supportedDatabases;
|
|
|
|
}
|
|
|
|
|
2015-03-10 22:44:29 +00:00
|
|
|
/**
|
|
|
|
* Gathers system information like database type and does
|
|
|
|
* a few system checks.
|
|
|
|
*
|
|
|
|
* @return array of system info, including an "errors" value
|
|
|
|
* in case of errors/warnings
|
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
public function getSystemInfo(bool $allowAllDatabases = false): array {
|
2015-03-11 14:47:24 +00:00
|
|
|
$databases = $this->getSupportedDatabases($allowAllDatabases);
|
2015-03-10 22:44:29 +00:00
|
|
|
|
2019-10-14 09:32:05 +00:00
|
|
|
$dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data');
|
2015-03-10 22:44:29 +00:00
|
|
|
|
2017-09-26 12:27:42 +00:00
|
|
|
$errors = [];
|
2015-03-10 22:44:29 +00:00
|
|
|
|
|
|
|
// Create data directory to test whether the .htaccess works
|
|
|
|
// Notice that this is not necessarily the same data directory as the one
|
|
|
|
// that will effectively be used.
|
2019-10-14 09:32:05 +00:00
|
|
|
if (!file_exists($dataDir)) {
|
2016-01-26 17:50:41 +00:00
|
|
|
@mkdir($dataDir);
|
|
|
|
}
|
2015-03-10 22:44:29 +00:00
|
|
|
$htAccessWorking = true;
|
|
|
|
if (is_dir($dataDir) && is_writable($dataDir)) {
|
|
|
|
// Protect data directory here, so we can test if the protection is working
|
2017-09-26 12:27:42 +00:00
|
|
|
self::protectDataDirectory();
|
2015-03-10 22:44:29 +00:00
|
|
|
|
|
|
|
try {
|
2015-03-26 13:51:33 +00:00
|
|
|
$util = new \OC_Util();
|
2024-01-29 14:12:53 +00:00
|
|
|
$htAccessWorking = $util->isHtaccessWorking(Server::get(IConfig::class));
|
2021-06-29 23:20:33 +00:00
|
|
|
} catch (\OCP\HintException $e) {
|
2017-09-26 12:27:42 +00:00
|
|
|
$errors[] = [
|
2015-03-10 22:44:29 +00:00
|
|
|
'error' => $e->getMessage(),
|
2021-01-07 19:04:04 +00:00
|
|
|
'exception' => $e,
|
2017-09-26 12:27:42 +00:00
|
|
|
'hint' => $e->getHint(),
|
|
|
|
];
|
2015-03-10 22:44:29 +00:00
|
|
|
$htAccessWorking = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\OC_Util::runningOnMac()) {
|
2017-09-26 12:27:42 +00:00
|
|
|
$errors[] = [
|
2015-03-10 22:44:29 +00:00
|
|
|
'error' => $this->l10n->t(
|
|
|
|
'Mac OS X is not supported and %s will not work properly on this platform. ' .
|
|
|
|
'Use it at your own risk! ',
|
2021-03-05 09:04:09 +00:00
|
|
|
[$this->defaults->getProductName()]
|
2015-03-10 22:44:29 +00:00
|
|
|
),
|
2017-09-26 12:27:42 +00:00
|
|
|
'hint' => $this->l10n->t('For the best results, please consider using a GNU/Linux server instead.'),
|
|
|
|
];
|
2015-03-10 22:44:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 15:05:03 +00:00
|
|
|
if ($this->iniWrapper->getString('open_basedir') !== '' && PHP_INT_SIZE === 4) {
|
2017-09-26 12:27:42 +00:00
|
|
|
$errors[] = [
|
2015-03-10 22:44:29 +00:00
|
|
|
'error' => $this->l10n->t(
|
2023-01-12 15:05:03 +00:00
|
|
|
'It seems that this %s instance is running on a 32-bit PHP environment and the open_basedir has been configured in php.ini. ' .
|
|
|
|
'This will lead to problems with files over 4 GB and is highly discouraged.',
|
2021-03-05 09:04:09 +00:00
|
|
|
[$this->defaults->getProductName()]
|
2015-03-10 22:44:29 +00:00
|
|
|
),
|
2023-01-12 15:05:03 +00:00
|
|
|
'hint' => $this->l10n->t('Please remove the open_basedir setting within your php.ini or switch to 64-bit PHP.'),
|
2017-09-26 12:27:42 +00:00
|
|
|
];
|
2015-03-10 22:44:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 08:30:18 +00:00
|
|
|
return [
|
2015-03-10 22:44:29 +00:00
|
|
|
'hasSQLite' => isset($databases['sqlite']),
|
|
|
|
'hasMySQL' => isset($databases['mysql']),
|
|
|
|
'hasPostgreSQL' => isset($databases['pgsql']),
|
|
|
|
'hasOracle' => isset($databases['oci']),
|
|
|
|
'databases' => $databases,
|
|
|
|
'directory' => $dataDir,
|
|
|
|
'htaccessWorking' => $htAccessWorking,
|
|
|
|
'errors' => $errors,
|
2020-03-26 08:30:18 +00:00
|
|
|
];
|
2015-03-10 22:44:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 11:51:26 +00:00
|
|
|
/**
|
2024-01-29 14:12:53 +00:00
|
|
|
* @return array<string|array> errors
|
2014-10-27 11:51:26 +00:00
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
public function install(array $options, ?IOutput $output = null): array {
|
2015-03-10 22:44:29 +00:00
|
|
|
$l = $this->l10n;
|
2013-02-09 18:23:36 +00:00
|
|
|
|
2020-03-26 08:30:18 +00:00
|
|
|
$error = [];
|
2014-10-27 18:53:12 +00:00
|
|
|
$dbType = $options['dbtype'];
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2019-10-14 09:32:05 +00:00
|
|
|
if (empty($options['adminlogin'])) {
|
2024-02-13 13:37:09 +00:00
|
|
|
$error[] = $l->t('Set an admin Login.');
|
2011-05-17 20:34:31 +00:00
|
|
|
}
|
2019-10-14 09:32:05 +00:00
|
|
|
if (empty($options['adminpass'])) {
|
2013-02-09 18:23:36 +00:00
|
|
|
$error[] = $l->t('Set an admin password.');
|
2011-05-17 20:34:31 +00:00
|
|
|
}
|
2019-10-14 09:32:05 +00:00
|
|
|
if (empty($options['directory'])) {
|
|
|
|
$options['directory'] = \OC::$SERVERROOT . '/data';
|
2011-05-17 20:34:31 +00:00
|
|
|
}
|
2011-08-07 19:06:53 +00:00
|
|
|
|
2014-10-27 18:53:12 +00:00
|
|
|
if (!isset(self::$dbSetupClasses[$dbType])) {
|
|
|
|
$dbType = 'sqlite';
|
2012-10-26 20:46:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-18 12:15:52 +00:00
|
|
|
$username = htmlspecialchars_decode($options['adminlogin']);
|
|
|
|
$password = htmlspecialchars_decode($options['adminpass']);
|
2014-10-27 18:53:12 +00:00
|
|
|
$dataDir = htmlspecialchars_decode($options['directory']);
|
2014-09-18 12:15:52 +00:00
|
|
|
|
2014-10-27 18:53:12 +00:00
|
|
|
$class = self::$dbSetupClasses[$dbType];
|
2014-09-18 12:15:52 +00:00
|
|
|
/** @var \OC\Setup\AbstractDatabase $dbSetup */
|
2017-07-18 12:20:29 +00:00
|
|
|
$dbSetup = new $class($l, $this->config, $this->logger, $this->random);
|
2013-06-27 18:19:51 +00:00
|
|
|
$error = array_merge($error, $dbSetup->validate($options));
|
2012-10-26 20:46:12 +00:00
|
|
|
|
2014-09-18 12:15:52 +00:00
|
|
|
// validate the data directory
|
2017-09-26 12:27:42 +00:00
|
|
|
if ((!is_dir($dataDir) && !mkdir($dataDir)) || !is_writable($dataDir)) {
|
2021-04-12 11:58:05 +00:00
|
|
|
$error[] = $l->t('Cannot create or write into the data directory %s', [$dataDir]);
|
2014-09-18 12:15:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 12:27:42 +00:00
|
|
|
if (!empty($error)) {
|
2013-03-29 15:28:48 +00:00
|
|
|
return $error;
|
2012-10-26 20:46:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$request = Server::get(IRequest::class);
|
2015-02-10 12:02:48 +00:00
|
|
|
|
2013-03-29 15:28:48 +00:00
|
|
|
//no errors, good
|
2019-10-14 09:32:05 +00:00
|
|
|
if (isset($options['trusted_domains'])
|
|
|
|
&& is_array($options['trusted_domains'])) {
|
2014-06-17 20:58:08 +00:00
|
|
|
$trustedDomains = $options['trusted_domains'];
|
|
|
|
} else {
|
2015-02-18 12:59:37 +00:00
|
|
|
$trustedDomains = [$request->getInsecureServerHost()];
|
2014-06-17 20:58:08 +00:00
|
|
|
}
|
2012-10-26 20:46:12 +00:00
|
|
|
|
2014-10-27 18:53:12 +00:00
|
|
|
//use sqlite3 when available, otherwise sqlite2 will be used.
|
2017-09-26 12:27:42 +00:00
|
|
|
if ($dbType === 'sqlite' && class_exists('SQLite3')) {
|
|
|
|
$dbType = 'sqlite3';
|
2011-04-16 22:45:05 +00:00
|
|
|
}
|
2013-02-14 21:54:48 +00:00
|
|
|
|
2022-09-21 15:44:32 +00:00
|
|
|
//generate a random salt that is used to salt the local passwords
|
2016-01-11 18:59:15 +00:00
|
|
|
$salt = $this->random->generate(30);
|
2014-08-26 17:02:40 +00:00
|
|
|
// generate a secret
|
2016-01-11 18:59:15 +00:00
|
|
|
$secret = $this->random->generate(48);
|
2011-05-06 20:50:18 +00:00
|
|
|
|
2013-03-29 15:28:48 +00:00
|
|
|
//write the config file
|
2018-02-07 10:40:35 +00:00
|
|
|
$newConfigValues = [
|
2019-10-14 09:32:05 +00:00
|
|
|
'passwordsalt' => $salt,
|
|
|
|
'secret' => $secret,
|
|
|
|
'trusted_domains' => $trustedDomains,
|
|
|
|
'datadirectory' => $dataDir,
|
|
|
|
'dbtype' => $dbType,
|
|
|
|
'version' => implode('.', \OCP\Util::getVersion()),
|
2018-02-07 10:40:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->config->getValue('overwrite.cli.url', null) === null) {
|
|
|
|
$newConfigValues['overwrite.cli.url'] = $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->config->setValues($newConfigValues);
|
2014-08-26 17:02:40 +00:00
|
|
|
|
2023-10-31 11:06:09 +00:00
|
|
|
$this->outputDebug($output, 'Configuring database');
|
2020-02-05 10:00:37 +00:00
|
|
|
$dbSetup->initialize($options);
|
2013-03-29 15:28:48 +00:00
|
|
|
try {
|
2013-06-27 18:19:51 +00:00
|
|
|
$dbSetup->setupDatabase($username);
|
2014-11-26 11:30:07 +00:00
|
|
|
} catch (\OC\DatabaseSetupException $e) {
|
2017-09-26 12:27:42 +00:00
|
|
|
$error[] = [
|
2013-03-29 15:28:48 +00:00
|
|
|
'error' => $e->getMessage(),
|
2021-01-07 19:04:04 +00:00
|
|
|
'exception' => $e,
|
2017-09-26 12:27:42 +00:00
|
|
|
'hint' => $e->getHint(),
|
|
|
|
];
|
|
|
|
return $error;
|
2013-03-29 15:28:48 +00:00
|
|
|
} catch (Exception $e) {
|
2017-09-26 12:27:42 +00:00
|
|
|
$error[] = [
|
2022-09-21 15:44:32 +00:00
|
|
|
'error' => 'Error while trying to create admin account: ' . $e->getMessage(),
|
2021-01-07 19:04:04 +00:00
|
|
|
'exception' => $e,
|
2017-09-26 12:27:42 +00:00
|
|
|
'hint' => '',
|
|
|
|
];
|
|
|
|
return $error;
|
2013-01-10 22:43:08 +00:00
|
|
|
}
|
2023-10-31 11:06:09 +00:00
|
|
|
|
|
|
|
$this->outputDebug($output, 'Run server migrations');
|
2020-02-05 10:00:37 +00:00
|
|
|
try {
|
|
|
|
// apply necessary migrations
|
2023-10-31 11:06:09 +00:00
|
|
|
$dbSetup->runMigrations($output);
|
2020-02-05 10:00:37 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$error[] = [
|
|
|
|
'error' => 'Error while trying to initialise the database: ' . $e->getMessage(),
|
2021-01-07 19:04:04 +00:00
|
|
|
'exception' => $e,
|
2020-02-05 10:00:37 +00:00
|
|
|
'hint' => '',
|
|
|
|
];
|
|
|
|
return $error;
|
|
|
|
}
|
2012-10-26 20:46:12 +00:00
|
|
|
|
2022-09-21 15:44:32 +00:00
|
|
|
$this->outputDebug($output, 'Create admin account');
|
2024-02-13 09:22:58 +00:00
|
|
|
|
|
|
|
// create the admin account and group
|
|
|
|
$user = null;
|
2013-03-29 15:28:48 +00:00
|
|
|
try {
|
2024-01-29 14:12:53 +00:00
|
|
|
$user = Server::get(IUserManager::class)->createUser($username, $password);
|
2015-02-21 19:51:50 +00:00
|
|
|
if (!$user) {
|
2022-09-21 15:44:32 +00:00
|
|
|
$error[] = "Account <$username> could not be created.";
|
2024-01-29 14:12:53 +00:00
|
|
|
return $error;
|
2015-02-21 19:51:50 +00:00
|
|
|
}
|
2019-10-14 09:32:05 +00:00
|
|
|
} catch (Exception $exception) {
|
2013-03-29 15:28:48 +00:00
|
|
|
$error[] = $exception->getMessage();
|
2024-01-29 14:12:53 +00:00
|
|
|
return $error;
|
2012-10-26 20:46:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$config = Server::get(IConfig::class);
|
|
|
|
$config->setAppValue('core', 'installedat', (string)microtime(true));
|
2024-03-13 02:21:40 +00:00
|
|
|
$appConfig = Server::get(IAppConfig::class);
|
|
|
|
$appConfig->setValueInt('core', 'lastupdatedat', time());
|
2022-09-19 11:53:49 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$vendorData = $this->getVendorData();
|
|
|
|
$config->setAppValue('core', 'vendor', $vendorData['vendor']);
|
|
|
|
if ($vendorData['channel'] !== 'stable') {
|
|
|
|
$config->setSystemValue('updater.release.channel', $vendorData['channel']);
|
|
|
|
}
|
2012-10-26 20:46:12 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$group = Server::get(IGroupManager::class)->createGroup('admin');
|
|
|
|
if ($group instanceof IGroup) {
|
|
|
|
$group->addUser($user);
|
|
|
|
}
|
2016-04-27 10:01:13 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
// Install shipped apps and specified app bundles
|
|
|
|
$this->outputDebug($output, 'Install default apps');
|
|
|
|
Installer::installShippedApps(false, $output);
|
2013-02-09 18:23:36 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
// create empty file in data dir, so we can later find
|
2024-07-11 18:53:37 +00:00
|
|
|
// out that this is indeed a Nextcloud data directory
|
2024-01-29 14:12:53 +00:00
|
|
|
$this->outputDebug($output, 'Setup data directory');
|
2024-07-11 18:53:37 +00:00
|
|
|
file_put_contents(
|
|
|
|
$config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data') . '/.ncdata',
|
|
|
|
"# Nextcloud data directory\n# Do not change this file",
|
|
|
|
);
|
2014-03-14 12:03:18 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
// Update .htaccess files
|
|
|
|
self::updateHtaccess();
|
|
|
|
self::protectDataDirectory();
|
2012-06-28 19:37:29 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$this->outputDebug($output, 'Install background jobs');
|
|
|
|
self::installBackgroundJobs();
|
2016-04-25 14:40:41 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
//and we are done
|
|
|
|
$config->setSystemValue('installed', true);
|
|
|
|
if (self::shouldRemoveCanInstallFile()) {
|
|
|
|
unlink(\OC::$configDir . '/CAN_INSTALL');
|
|
|
|
}
|
2021-10-14 08:19:40 +00:00
|
|
|
|
2024-09-17 20:38:44 +00:00
|
|
|
$bootstrapCoordinator = Server::get(\OC\AppFramework\Bootstrap\Coordinator::class);
|
2024-01-29 14:12:53 +00:00
|
|
|
$bootstrapCoordinator->runInitialRegistration();
|
|
|
|
|
|
|
|
// Create a session token for the newly created user
|
|
|
|
// The token provider requires a working db, so it's not injected on setup
|
|
|
|
/** @var \OC\User\Session $userSession */
|
|
|
|
$userSession = Server::get(IUserSession::class);
|
|
|
|
$provider = Server::get(PublicKeyTokenProvider::class);
|
|
|
|
$userSession->setTokenProvider($provider);
|
|
|
|
$userSession->login($username, $password);
|
|
|
|
$user = $userSession->getUser();
|
|
|
|
if (!$user) {
|
2022-09-21 15:44:32 +00:00
|
|
|
$error[] = 'No account found in session.';
|
2024-01-29 14:12:53 +00:00
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
$userSession->createSessionToken($request, $user->getUID(), $username, $password);
|
2018-09-24 19:49:43 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$session = $userSession->getSession();
|
|
|
|
$session->set('last-password-confirm', Server::get(ITimeFactory::class)->getTime());
|
2019-12-03 18:31:31 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
// Set email for admin
|
|
|
|
if (!empty($options['adminemail'])) {
|
|
|
|
$user->setSystemEMailAddress($options['adminemail']);
|
2012-06-28 19:37:29 +00:00
|
|
|
}
|
2012-10-26 20:46:12 +00:00
|
|
|
|
2013-03-29 15:28:48 +00:00
|
|
|
return $error;
|
2013-02-10 12:07:59 +00:00
|
|
|
}
|
2013-02-12 00:05:47 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
public static function installBackgroundJobs(): void {
|
|
|
|
$jobList = Server::get(IJobList::class);
|
2022-07-27 08:52:05 +00:00
|
|
|
$jobList->add(TokenCleanupJob::class);
|
2017-10-23 19:26:24 +00:00
|
|
|
$jobList->add(Rotate::class);
|
2018-05-08 12:47:50 +00:00
|
|
|
$jobList->add(BackgroundCleanupJob::class);
|
2023-06-28 12:59:20 +00:00
|
|
|
$jobList->add(RemoveOldTasksBackgroundJob::class);
|
2024-09-10 20:50:16 +00:00
|
|
|
$jobList->add(CleanupDeletedUsers::class);
|
2016-04-25 14:40:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 11:05:54 +00:00
|
|
|
/**
|
|
|
|
* @return string Absolute path to htaccess
|
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
private function pathToHtaccess(): string {
|
2019-10-14 09:32:05 +00:00
|
|
|
return \OC::$SERVERROOT . '/.htaccess';
|
2015-01-08 11:05:54 +00:00
|
|
|
}
|
|
|
|
|
2011-04-18 12:05:21 +00:00
|
|
|
/**
|
2018-09-10 20:33:35 +00:00
|
|
|
* Find webroot from config
|
|
|
|
*
|
2018-09-10 20:45:40 +00:00
|
|
|
* @throws InvalidArgumentException when invalid value for overwrite.cli.url
|
2011-04-18 12:05:21 +00:00
|
|
|
*/
|
2018-09-13 10:24:06 +00:00
|
|
|
private static function findWebRoot(SystemConfig $config): string {
|
2016-04-20 20:00:44 +00:00
|
|
|
// For CLI read the value from overwrite.cli.url
|
2018-09-10 20:33:35 +00:00
|
|
|
if (\OC::$CLI) {
|
2017-03-17 22:37:48 +00:00
|
|
|
$webRoot = $config->getValue('overwrite.cli.url', '');
|
2018-09-10 20:33:35 +00:00
|
|
|
if ($webRoot === '') {
|
2018-09-10 20:45:40 +00:00
|
|
|
throw new InvalidArgumentException('overwrite.cli.url is empty');
|
2016-04-20 20:00:44 +00:00
|
|
|
}
|
2018-09-28 20:32:19 +00:00
|
|
|
if (!filter_var($webRoot, FILTER_VALIDATE_URL)) {
|
2018-09-10 20:45:40 +00:00
|
|
|
throw new InvalidArgumentException('invalid value for overwrite.cli.url');
|
2018-01-12 22:56:25 +00:00
|
|
|
}
|
2021-10-25 12:25:49 +00:00
|
|
|
$webRoot = rtrim((parse_url($webRoot, PHP_URL_PATH) ?? ''), '/');
|
2016-04-20 20:00:44 +00:00
|
|
|
} else {
|
|
|
|
$webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';
|
2015-12-08 07:17:04 +00:00
|
|
|
}
|
2016-04-20 20:00:44 +00:00
|
|
|
|
2018-09-10 20:33:35 +00:00
|
|
|
return $webRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the correct ErrorDocument path for Apache hosts
|
|
|
|
*
|
|
|
|
* @return bool True when success, False otherwise
|
|
|
|
* @throws \OCP\AppFramework\QueryException
|
|
|
|
*/
|
2024-01-29 14:12:53 +00:00
|
|
|
public static function updateHtaccess(): bool {
|
|
|
|
$config = Server::get(SystemConfig::class);
|
2018-09-10 20:45:40 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$webRoot = self::findWebRoot($config);
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-10 20:33:35 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$setupHelper = Server::get(\OC\Setup::class);
|
2015-01-08 11:05:54 +00:00
|
|
|
|
2023-12-15 07:35:15 +00:00
|
|
|
if (!is_writable($setupHelper->pathToHtaccess())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:24:21 +00:00
|
|
|
$htaccessContent = file_get_contents($setupHelper->pathToHtaccess());
|
2016-01-20 22:05:37 +00:00
|
|
|
$content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n";
|
2016-05-12 07:43:26 +00:00
|
|
|
$htaccessContent = explode($content, $htaccessContent, 2)[0];
|
2016-03-17 16:59:28 +00:00
|
|
|
|
2016-05-12 07:43:26 +00:00
|
|
|
//custom 403 error page
|
2022-10-14 05:42:25 +00:00
|
|
|
$content .= "\nErrorDocument 403 " . $webRoot . '/index.php/error/403';
|
2015-02-11 00:10:03 +00:00
|
|
|
|
2016-05-12 07:43:26 +00:00
|
|
|
//custom 404 error page
|
2022-10-14 05:42:25 +00:00
|
|
|
$content .= "\nErrorDocument 404 " . $webRoot . '/index.php/error/404';
|
2016-04-26 11:18:18 +00:00
|
|
|
|
2016-05-12 07:43:26 +00:00
|
|
|
// Add rewrite rules if the RewriteBase is configured
|
2017-03-17 22:37:48 +00:00
|
|
|
$rewriteBase = $config->getValue('htaccess.RewriteBase', '');
|
2019-10-14 09:32:05 +00:00
|
|
|
if ($rewriteBase !== '') {
|
2016-03-17 16:59:28 +00:00
|
|
|
$content .= "\n<IfModule mod_rewrite.c>";
|
2016-05-12 07:43:26 +00:00
|
|
|
$content .= "\n Options -MultiViews";
|
|
|
|
$content .= "\n RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]";
|
|
|
|
$content .= "\n RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]";
|
2024-09-16 07:58:54 +00:00
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !\\.(css|js|mjs|svg|gif|ico|jpg|jpeg|png|webp|html|otf|ttf|woff2?|map|webm|mp4|mp3|ogg|wav|flac|wasm|tflite)$";
|
2020-09-06 14:22:47 +00:00
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !/core/ajax/update\\.php";
|
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !/core/img/(favicon\\.ico|manifest\\.json)$";
|
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !/(cron|public|remote|status)\\.php";
|
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !/ocs/v(1|2)\\.php";
|
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !/robots\\.txt";
|
2023-10-02 15:38:10 +00:00
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !/(ocs-provider|updater)/";
|
2018-02-28 13:30:18 +00:00
|
|
|
$content .= "\n RewriteCond %{REQUEST_URI} !^/\\.well-known/(acme-challenge|pki-validation)/.*";
|
2020-09-14 08:30:12 +00:00
|
|
|
$content .= "\n RewriteCond %{REQUEST_FILENAME} !/richdocumentscode(_arm64)?/proxy.php$";
|
2016-03-17 16:59:28 +00:00
|
|
|
$content .= "\n RewriteRule . index.php [PT,E=PATH_INFO:$1]";
|
2016-05-12 07:43:26 +00:00
|
|
|
$content .= "\n RewriteBase " . $rewriteBase;
|
2016-03-17 16:59:28 +00:00
|
|
|
$content .= "\n <IfModule mod_env.c>";
|
|
|
|
$content .= "\n SetEnv front_controller_active true";
|
|
|
|
$content .= "\n <IfModule mod_dir.c>";
|
|
|
|
$content .= "\n DirectorySlash off";
|
|
|
|
$content .= "\n </IfModule>";
|
2016-05-12 07:43:26 +00:00
|
|
|
$content .= "\n </IfModule>";
|
|
|
|
$content .= "\n</IfModule>";
|
|
|
|
}
|
2016-03-17 16:59:28 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
// Never write file back if disk space should be too low
|
|
|
|
if (function_exists('disk_free_space')) {
|
|
|
|
$df = disk_free_space(\OC::$SERVERROOT);
|
|
|
|
$size = strlen($content) + 10240;
|
|
|
|
if ($df !== false && $df < (float)$size) {
|
|
|
|
throw new \Exception(\OC::$SERVERROOT . ' does not have enough space for writing the htaccess file! Not writing it back!');
|
2023-11-16 15:06:44 +00:00
|
|
|
}
|
2015-06-23 13:24:21 +00:00
|
|
|
}
|
2024-01-29 14:12:53 +00:00
|
|
|
//suppress errors in case we don't have permissions for it
|
|
|
|
return (bool)@file_put_contents($setupHelper->pathToHtaccess(), $htaccessContent . $content . "\n");
|
2012-10-30 19:57:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
public static function protectDataDirectory(): void {
|
2013-11-08 09:45:35 +00:00
|
|
|
//Require all denied
|
2019-10-14 09:32:05 +00:00
|
|
|
$now = date('Y-m-d H:i:s');
|
2017-02-24 11:13:26 +00:00
|
|
|
$content = "# Generated by Nextcloud on $now\n";
|
2019-08-19 13:50:48 +00:00
|
|
|
$content .= "# Section for Apache 2.4 to 2.6\n";
|
|
|
|
$content .= "<IfModule mod_authz_core.c>\n";
|
|
|
|
$content .= " Require all denied\n";
|
|
|
|
$content .= "</IfModule>\n";
|
|
|
|
$content .= "<IfModule mod_access_compat.c>\n";
|
2019-10-31 19:15:49 +00:00
|
|
|
$content .= " Order Allow,Deny\n";
|
2019-08-19 13:50:48 +00:00
|
|
|
$content .= " Deny from all\n";
|
|
|
|
$content .= " Satisfy All\n";
|
|
|
|
$content .= "</IfModule>\n\n";
|
|
|
|
$content .= "# Section for Apache 2.2\n";
|
|
|
|
$content .= "<IfModule !mod_authz_core.c>\n";
|
|
|
|
$content .= " <IfModule !mod_access_compat.c>\n";
|
|
|
|
$content .= " <IfModule mod_authz_host.c>\n";
|
2019-10-31 19:15:49 +00:00
|
|
|
$content .= " Order Allow,Deny\n";
|
2019-08-19 13:50:48 +00:00
|
|
|
$content .= " Deny from all\n";
|
2020-01-26 14:47:04 +00:00
|
|
|
$content .= " </IfModule>\n";
|
2019-08-19 13:50:48 +00:00
|
|
|
$content .= " Satisfy All\n";
|
|
|
|
$content .= " </IfModule>\n";
|
|
|
|
$content .= "</IfModule>\n\n";
|
|
|
|
$content .= "# Section for Apache 2.2 to 2.6\n";
|
|
|
|
$content .= "<IfModule mod_autoindex.c>\n";
|
|
|
|
$content .= " IndexIgnore *\n";
|
|
|
|
$content .= '</IfModule>';
|
2015-12-02 14:56:59 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
$baseDir = Server::get(IConfig::class)->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data');
|
2015-12-02 14:56:59 +00:00
|
|
|
file_put_contents($baseDir . '/.htaccess', $content);
|
|
|
|
file_put_contents($baseDir . '/index.html', '');
|
2011-04-18 12:05:21 +00:00
|
|
|
}
|
2017-02-10 05:37:00 +00:00
|
|
|
|
2022-09-19 11:53:49 +00:00
|
|
|
private function getVendorData(): array {
|
2017-02-10 05:37:00 +00:00
|
|
|
// this should really be a JSON file
|
|
|
|
require \OC::$SERVERROOT . '/version.php';
|
2022-09-19 11:53:49 +00:00
|
|
|
/** @var mixed $vendor */
|
|
|
|
/** @var mixed $OC_Channel */
|
|
|
|
return [
|
|
|
|
'vendor' => (string)$vendor,
|
|
|
|
'channel' => (string)$OC_Channel,
|
|
|
|
];
|
2017-02-10 05:37:00 +00:00
|
|
|
}
|
2021-06-19 18:56:54 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
public function shouldRemoveCanInstallFile(): bool {
|
2024-09-17 20:38:44 +00:00
|
|
|
return Server::get(ServerVersion::class)->getChannel() !== 'git' && is_file(\OC::$configDir . '/CAN_INSTALL');
|
2021-06-19 18:56:54 +00:00
|
|
|
}
|
2021-06-19 20:06:57 +00:00
|
|
|
|
2024-01-29 14:12:53 +00:00
|
|
|
public function canInstallFileExists(): bool {
|
2021-06-19 20:06:57 +00:00
|
|
|
return is_file(\OC::$configDir . '/CAN_INSTALL');
|
|
|
|
}
|
2023-10-31 11:06:09 +00:00
|
|
|
|
|
|
|
protected function outputDebug(?IOutput $output, string $message): void {
|
|
|
|
if ($output instanceof IOutput) {
|
|
|
|
$output->debug($message);
|
|
|
|
}
|
|
|
|
}
|
2011-04-16 22:45:05 +00:00
|
|
|
}
|