@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | $appName = $input->getArgument('app'); |
| 107 | 107 | $version = $input->getArgument('version'); |
| 108 | 108 | |
| 109 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 109 | + if (!preg_match('/^\d{1,16}$/', $version)) { |
|
| 110 | 110 | $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
| 111 | 111 | return 1; |
| 112 | 112 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
| 115 | 115 | |
| 116 | 116 | $date = date('YmdHis'); |
| 117 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 117 | + $path = $this->generateMigration($ms, 'Version'.$version.'Date'.$date); |
|
| 118 | 118 | |
| 119 | 119 | $output->writeln("New migration class has been generated to <info>$path</info>"); |
| 120 | 120 | return 0; |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
| 130 | 130 | if ($schemaBody === '') { |
| 131 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 131 | + $schemaBody = "\t\t".'return null;'; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | $dir = $ms->getMigrationsDirectory(); |
| 147 | 147 | |
| 148 | 148 | $this->ensureMigrationDirExists($dir); |
| 149 | - $path = $dir . '/' . $className . '.php'; |
|
| 149 | + $path = $dir.'/'.$className.'.php'; |
|
| 150 | 150 | |
| 151 | 151 | if (file_put_contents($path, $code) === false) { |
| 152 | 152 | throw new RuntimeException('Failed to generate new migration step.'); |
@@ -36,8 +36,8 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | class GenerateCommand extends Command { |
| 38 | 38 | |
| 39 | - protected static $_templateSimple = |
|
| 40 | - '<?php |
|
| 39 | + protected static $_templateSimple = |
|
| 40 | + '<?php |
|
| 41 | 41 | namespace {{<namespace}}; |
| 42 | 42 | |
| 43 | 43 | use Doctrine\DBAL\Schema\Schema; |
@@ -80,94 +80,94 @@ discard block |
||
| 80 | 80 | } |
| 81 | 81 | '; |
| 82 | 82 | |
| 83 | - /** @var IDBConnection */ |
|
| 84 | - protected $connection; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @param IDBConnection $connection |
|
| 88 | - */ |
|
| 89 | - public function __construct(IDBConnection $connection) { |
|
| 90 | - $this->connection = $connection; |
|
| 91 | - |
|
| 92 | - parent::__construct(); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - protected function configure() { |
|
| 96 | - $this |
|
| 97 | - ->setName('migrations:generate') |
|
| 98 | - ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 99 | - ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 100 | - ; |
|
| 101 | - |
|
| 102 | - parent::configure(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
| 106 | - $appName = $input->getArgument('app'); |
|
| 107 | - $version = $input->getArgument('version'); |
|
| 108 | - |
|
| 109 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 110 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 111 | - return 1; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 115 | - |
|
| 116 | - $date = date('YmdHis'); |
|
| 117 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 118 | - |
|
| 119 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 120 | - return 0; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param MigrationService $ms |
|
| 125 | - * @param string $className |
|
| 126 | - * @param string $schemaBody |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 130 | - if ($schemaBody === '') { |
|
| 131 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - $placeHolders = [ |
|
| 136 | - '{{namespace}}', |
|
| 137 | - '{{classname}}', |
|
| 138 | - '{{schemabody}}', |
|
| 139 | - ]; |
|
| 140 | - $replacements = [ |
|
| 141 | - $ms->getMigrationsNamespace(), |
|
| 142 | - $className, |
|
| 143 | - $schemaBody, |
|
| 144 | - ]; |
|
| 145 | - $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 146 | - $dir = $ms->getMigrationsDirectory(); |
|
| 147 | - |
|
| 148 | - $this->ensureMigrationDirExists($dir); |
|
| 149 | - $path = $dir . '/' . $className . '.php'; |
|
| 150 | - |
|
| 151 | - if (file_put_contents($path, $code) === false) { |
|
| 152 | - throw new RuntimeException('Failed to generate new migration step.'); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $path; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - protected function ensureMigrationDirExists($directory) { |
|
| 159 | - if (file_exists($directory) && is_dir($directory)) { |
|
| 160 | - return; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - if (file_exists($directory)) { |
|
| 164 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $this->ensureMigrationDirExists(dirname($directory)); |
|
| 168 | - |
|
| 169 | - if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 170 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 171 | - } |
|
| 172 | - } |
|
| 83 | + /** @var IDBConnection */ |
|
| 84 | + protected $connection; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @param IDBConnection $connection |
|
| 88 | + */ |
|
| 89 | + public function __construct(IDBConnection $connection) { |
|
| 90 | + $this->connection = $connection; |
|
| 91 | + |
|
| 92 | + parent::__construct(); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + protected function configure() { |
|
| 96 | + $this |
|
| 97 | + ->setName('migrations:generate') |
|
| 98 | + ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 99 | + ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 100 | + ; |
|
| 101 | + |
|
| 102 | + parent::configure(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
| 106 | + $appName = $input->getArgument('app'); |
|
| 107 | + $version = $input->getArgument('version'); |
|
| 108 | + |
|
| 109 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 110 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 111 | + return 1; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 115 | + |
|
| 116 | + $date = date('YmdHis'); |
|
| 117 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 118 | + |
|
| 119 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 120 | + return 0; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param MigrationService $ms |
|
| 125 | + * @param string $className |
|
| 126 | + * @param string $schemaBody |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 130 | + if ($schemaBody === '') { |
|
| 131 | + $schemaBody = "\t\t" . 'return null;'; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + $placeHolders = [ |
|
| 136 | + '{{namespace}}', |
|
| 137 | + '{{classname}}', |
|
| 138 | + '{{schemabody}}', |
|
| 139 | + ]; |
|
| 140 | + $replacements = [ |
|
| 141 | + $ms->getMigrationsNamespace(), |
|
| 142 | + $className, |
|
| 143 | + $schemaBody, |
|
| 144 | + ]; |
|
| 145 | + $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 146 | + $dir = $ms->getMigrationsDirectory(); |
|
| 147 | + |
|
| 148 | + $this->ensureMigrationDirExists($dir); |
|
| 149 | + $path = $dir . '/' . $className . '.php'; |
|
| 150 | + |
|
| 151 | + if (file_put_contents($path, $code) === false) { |
|
| 152 | + throw new RuntimeException('Failed to generate new migration step.'); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $path; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + protected function ensureMigrationDirExists($directory) { |
|
| 159 | + if (file_exists($directory) && is_dir($directory)) { |
|
| 160 | + return; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + if (file_exists($directory)) { |
|
| 164 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $this->ensureMigrationDirExists(dirname($directory)); |
|
| 168 | + |
|
| 169 | + if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 170 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | 173 | } |
@@ -48,486 +48,486 @@ |
||
| 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, $this->config, $this->logger, $this->random); |
|
| 286 | - $error = array_merge($error, $dbSetup->validate($options)); |
|
| 287 | - |
|
| 288 | - // validate the data directory |
|
| 289 | - if ( |
|
| 290 | - (!is_dir($dataDir) and !mkdir($dataDir)) or |
|
| 291 | - !is_writable($dataDir) |
|
| 292 | - ) { |
|
| 293 | - $error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - if(count($error) != 0) { |
|
| 297 | - return $error; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $request = \OC::$server->getRequest(); |
|
| 301 | - |
|
| 302 | - //no errors, good |
|
| 303 | - if(isset($options['trusted_domains']) |
|
| 304 | - && is_array($options['trusted_domains'])) { |
|
| 305 | - $trustedDomains = $options['trusted_domains']; |
|
| 306 | - } else { |
|
| 307 | - $trustedDomains = [$request->getInsecureServerHost()]; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - //use sqlite3 when available, otherwise sqlite2 will be used. |
|
| 311 | - if($dbType=='sqlite' and class_exists('SQLite3')) { |
|
| 312 | - $dbType='sqlite3'; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - //generate a random salt that is used to salt the local user passwords |
|
| 316 | - $salt = $this->random->generate(30); |
|
| 317 | - // generate a secret |
|
| 318 | - $secret = $this->random->generate(48); |
|
| 319 | - |
|
| 320 | - //write the config file |
|
| 321 | - $this->config->setValues([ |
|
| 322 | - 'passwordsalt' => $salt, |
|
| 323 | - 'secret' => $secret, |
|
| 324 | - 'trusted_domains' => $trustedDomains, |
|
| 325 | - 'datadirectory' => $dataDir, |
|
| 326 | - 'overwrite.cli.url' => $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT, |
|
| 327 | - 'dbtype' => $dbType, |
|
| 328 | - 'version' => implode('.', \OCP\Util::getVersion()), |
|
| 329 | - ]); |
|
| 330 | - |
|
| 331 | - try { |
|
| 332 | - $dbSetup->initialize($options); |
|
| 333 | - $dbSetup->setupDatabase($username); |
|
| 334 | - // apply necessary migrations |
|
| 335 | - $dbSetup->runMigrations(); |
|
| 336 | - } catch (\OC\DatabaseSetupException $e) { |
|
| 337 | - $error[] = array( |
|
| 338 | - 'error' => $e->getMessage(), |
|
| 339 | - 'hint' => $e->getHint() |
|
| 340 | - ); |
|
| 341 | - return($error); |
|
| 342 | - } catch (Exception $e) { |
|
| 343 | - $error[] = array( |
|
| 344 | - 'error' => 'Error while trying to create admin user: ' . $e->getMessage(), |
|
| 345 | - 'hint' => '' |
|
| 346 | - ); |
|
| 347 | - return($error); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - //create the user and group |
|
| 351 | - $user = null; |
|
| 352 | - try { |
|
| 353 | - $user = \OC::$server->getUserManager()->createUser($username, $password); |
|
| 354 | - if (!$user) { |
|
| 355 | - $error[] = "User <$username> could not be created."; |
|
| 356 | - } |
|
| 357 | - } catch(Exception $exception) { |
|
| 358 | - $error[] = $exception->getMessage(); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - if(count($error) == 0) { |
|
| 362 | - $config = \OC::$server->getConfig(); |
|
| 363 | - $config->setAppValue('core', 'installedat', microtime(true)); |
|
| 364 | - $config->setAppValue('core', 'lastupdatedat', microtime(true)); |
|
| 365 | - $config->setAppValue('core', 'vendor', $this->getVendor()); |
|
| 366 | - |
|
| 367 | - $group =\OC::$server->getGroupManager()->createGroup('admin'); |
|
| 368 | - $group->addUser($user); |
|
| 369 | - |
|
| 370 | - // Install shipped apps and specified app bundles |
|
| 371 | - Installer::installShippedApps(); |
|
| 372 | - $installer = new Installer( |
|
| 373 | - \OC::$server->getAppFetcher(), |
|
| 374 | - \OC::$server->getHTTPClientService(), |
|
| 375 | - \OC::$server->getTempManager(), |
|
| 376 | - \OC::$server->getLogger(), |
|
| 377 | - \OC::$server->getConfig() |
|
| 378 | - ); |
|
| 379 | - $bundleFetcher = new BundleFetcher(\OC::$server->getL10N('lib')); |
|
| 380 | - $defaultInstallationBundles = $bundleFetcher->getDefaultInstallationBundle(); |
|
| 381 | - foreach($defaultInstallationBundles as $bundle) { |
|
| 382 | - try { |
|
| 383 | - $installer->installAppBundle($bundle); |
|
| 384 | - } catch (Exception $e) {} |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // create empty file in data dir, so we can later find |
|
| 388 | - // out that this is indeed an ownCloud data directory |
|
| 389 | - file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', ''); |
|
| 390 | - |
|
| 391 | - // Update .htaccess files |
|
| 392 | - Setup::updateHtaccess(); |
|
| 393 | - Setup::protectDataDirectory(); |
|
| 394 | - |
|
| 395 | - self::installBackgroundJobs(); |
|
| 396 | - |
|
| 397 | - //and we are done |
|
| 398 | - $config->setSystemValue('installed', true); |
|
| 399 | - |
|
| 400 | - // Create a session token for the newly created user |
|
| 401 | - // The token provider requires a working db, so it's not injected on setup |
|
| 402 | - /* @var $userSession User\Session */ |
|
| 403 | - $userSession = \OC::$server->getUserSession(); |
|
| 404 | - $defaultTokenProvider = \OC::$server->query('OC\Authentication\Token\DefaultTokenProvider'); |
|
| 405 | - $userSession->setTokenProvider($defaultTokenProvider); |
|
| 406 | - $userSession->login($username, $password); |
|
| 407 | - $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - return $error; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - public static function installBackgroundJobs() { |
|
| 414 | - \OC::$server->getJobList()->add('\OC\Authentication\Token\DefaultTokenCleanupJob'); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * @return string Absolute path to htaccess |
|
| 419 | - */ |
|
| 420 | - private function pathToHtaccess() { |
|
| 421 | - return \OC::$SERVERROOT.'/.htaccess'; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Append the correct ErrorDocument path for Apache hosts |
|
| 426 | - * @return bool True when success, False otherwise |
|
| 427 | - */ |
|
| 428 | - public static function updateHtaccess() { |
|
| 429 | - $config = \OC::$server->getSystemConfig(); |
|
| 430 | - |
|
| 431 | - // For CLI read the value from overwrite.cli.url |
|
| 432 | - if(\OC::$CLI) { |
|
| 433 | - $webRoot = $config->getValue('overwrite.cli.url', ''); |
|
| 434 | - if($webRoot === '') { |
|
| 435 | - return false; |
|
| 436 | - } |
|
| 437 | - $webRoot = parse_url($webRoot, PHP_URL_PATH); |
|
| 438 | - $webRoot = rtrim($webRoot, '/'); |
|
| 439 | - } else { |
|
| 440 | - $webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/'; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - $setupHelper = new \OC\Setup($config, \OC::$server->getIniWrapper(), |
|
| 444 | - \OC::$server->getL10N('lib'), \OC::$server->query(Defaults::class), \OC::$server->getLogger(), |
|
| 445 | - \OC::$server->getSecureRandom()); |
|
| 446 | - |
|
| 447 | - $htaccessContent = file_get_contents($setupHelper->pathToHtaccess()); |
|
| 448 | - $content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n"; |
|
| 449 | - $htaccessContent = explode($content, $htaccessContent, 2)[0]; |
|
| 450 | - |
|
| 451 | - //custom 403 error page |
|
| 452 | - $content.= "\nErrorDocument 403 ".$webRoot."/core/templates/403.php"; |
|
| 453 | - |
|
| 454 | - //custom 404 error page |
|
| 455 | - $content.= "\nErrorDocument 404 ".$webRoot."/core/templates/404.php"; |
|
| 456 | - |
|
| 457 | - // Add rewrite rules if the RewriteBase is configured |
|
| 458 | - $rewriteBase = $config->getValue('htaccess.RewriteBase', ''); |
|
| 459 | - if($rewriteBase !== '') { |
|
| 460 | - $content .= "\n<IfModule mod_rewrite.c>"; |
|
| 461 | - $content .= "\n Options -MultiViews"; |
|
| 462 | - $content .= "\n RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]"; |
|
| 463 | - $content .= "\n RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]"; |
|
| 464 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !\\.(css|js|svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$"; |
|
| 465 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !core/img/favicon.ico$"; |
|
| 466 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !core/img/manifest.json$"; |
|
| 467 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/remote.php"; |
|
| 468 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/public.php"; |
|
| 469 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/cron.php"; |
|
| 470 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/core/ajax/update.php"; |
|
| 471 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/status.php"; |
|
| 472 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/ocs/v1.php"; |
|
| 473 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/ocs/v2.php"; |
|
| 474 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/robots.txt"; |
|
| 475 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/updater/"; |
|
| 476 | - $content .= "\n RewriteCond %{REQUEST_FILENAME} !/ocs-provider/"; |
|
| 477 | - $content .= "\n RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/.*"; |
|
| 478 | - $content .= "\n RewriteRule . index.php [PT,E=PATH_INFO:$1]"; |
|
| 479 | - $content .= "\n RewriteBase " . $rewriteBase; |
|
| 480 | - $content .= "\n <IfModule mod_env.c>"; |
|
| 481 | - $content .= "\n SetEnv front_controller_active true"; |
|
| 482 | - $content .= "\n <IfModule mod_dir.c>"; |
|
| 483 | - $content .= "\n DirectorySlash off"; |
|
| 484 | - $content .= "\n </IfModule>"; |
|
| 485 | - $content .= "\n </IfModule>"; |
|
| 486 | - $content .= "\n</IfModule>"; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - if ($content !== '') { |
|
| 490 | - //suppress errors in case we don't have permissions for it |
|
| 491 | - return (bool) @file_put_contents($setupHelper->pathToHtaccess(), $htaccessContent.$content . "\n"); |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - return false; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - public static function protectDataDirectory() { |
|
| 498 | - //Require all denied |
|
| 499 | - $now = date('Y-m-d H:i:s'); |
|
| 500 | - $content = "# Generated by Nextcloud on $now\n"; |
|
| 501 | - $content.= "# line below if for Apache 2.4\n"; |
|
| 502 | - $content.= "<ifModule mod_authz_core.c>\n"; |
|
| 503 | - $content.= "Require all denied\n"; |
|
| 504 | - $content.= "</ifModule>\n\n"; |
|
| 505 | - $content.= "# line below if for Apache 2.2\n"; |
|
| 506 | - $content.= "<ifModule !mod_authz_core.c>\n"; |
|
| 507 | - $content.= "deny from all\n"; |
|
| 508 | - $content.= "Satisfy All\n"; |
|
| 509 | - $content.= "</ifModule>\n\n"; |
|
| 510 | - $content.= "# section for Apache 2.2 and 2.4\n"; |
|
| 511 | - $content.= "<ifModule mod_autoindex.c>\n"; |
|
| 512 | - $content.= "IndexIgnore *\n"; |
|
| 513 | - $content.= "</ifModule>\n"; |
|
| 514 | - |
|
| 515 | - $baseDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data'); |
|
| 516 | - file_put_contents($baseDir . '/.htaccess', $content); |
|
| 517 | - file_put_contents($baseDir . '/index.html', ''); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Return vendor from which this version was published |
|
| 522 | - * |
|
| 523 | - * @return string Get the vendor |
|
| 524 | - * |
|
| 525 | - * Copy of \OC\Updater::getVendor() |
|
| 526 | - */ |
|
| 527 | - private function getVendor() { |
|
| 528 | - // this should really be a JSON file |
|
| 529 | - require \OC::$SERVERROOT . '/version.php'; |
|
| 530 | - /** @var string $vendor */ |
|
| 531 | - return (string) $vendor; |
|
| 532 | - } |
|
| 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, $this->config, $this->logger, $this->random); |
|
| 286 | + $error = array_merge($error, $dbSetup->validate($options)); |
|
| 287 | + |
|
| 288 | + // validate the data directory |
|
| 289 | + if ( |
|
| 290 | + (!is_dir($dataDir) and !mkdir($dataDir)) or |
|
| 291 | + !is_writable($dataDir) |
|
| 292 | + ) { |
|
| 293 | + $error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + if(count($error) != 0) { |
|
| 297 | + return $error; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $request = \OC::$server->getRequest(); |
|
| 301 | + |
|
| 302 | + //no errors, good |
|
| 303 | + if(isset($options['trusted_domains']) |
|
| 304 | + && is_array($options['trusted_domains'])) { |
|
| 305 | + $trustedDomains = $options['trusted_domains']; |
|
| 306 | + } else { |
|
| 307 | + $trustedDomains = [$request->getInsecureServerHost()]; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + //use sqlite3 when available, otherwise sqlite2 will be used. |
|
| 311 | + if($dbType=='sqlite' and class_exists('SQLite3')) { |
|
| 312 | + $dbType='sqlite3'; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + //generate a random salt that is used to salt the local user passwords |
|
| 316 | + $salt = $this->random->generate(30); |
|
| 317 | + // generate a secret |
|
| 318 | + $secret = $this->random->generate(48); |
|
| 319 | + |
|
| 320 | + //write the config file |
|
| 321 | + $this->config->setValues([ |
|
| 322 | + 'passwordsalt' => $salt, |
|
| 323 | + 'secret' => $secret, |
|
| 324 | + 'trusted_domains' => $trustedDomains, |
|
| 325 | + 'datadirectory' => $dataDir, |
|
| 326 | + 'overwrite.cli.url' => $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT, |
|
| 327 | + 'dbtype' => $dbType, |
|
| 328 | + 'version' => implode('.', \OCP\Util::getVersion()), |
|
| 329 | + ]); |
|
| 330 | + |
|
| 331 | + try { |
|
| 332 | + $dbSetup->initialize($options); |
|
| 333 | + $dbSetup->setupDatabase($username); |
|
| 334 | + // apply necessary migrations |
|
| 335 | + $dbSetup->runMigrations(); |
|
| 336 | + } catch (\OC\DatabaseSetupException $e) { |
|
| 337 | + $error[] = array( |
|
| 338 | + 'error' => $e->getMessage(), |
|
| 339 | + 'hint' => $e->getHint() |
|
| 340 | + ); |
|
| 341 | + return($error); |
|
| 342 | + } catch (Exception $e) { |
|
| 343 | + $error[] = array( |
|
| 344 | + 'error' => 'Error while trying to create admin user: ' . $e->getMessage(), |
|
| 345 | + 'hint' => '' |
|
| 346 | + ); |
|
| 347 | + return($error); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + //create the user and group |
|
| 351 | + $user = null; |
|
| 352 | + try { |
|
| 353 | + $user = \OC::$server->getUserManager()->createUser($username, $password); |
|
| 354 | + if (!$user) { |
|
| 355 | + $error[] = "User <$username> could not be created."; |
|
| 356 | + } |
|
| 357 | + } catch(Exception $exception) { |
|
| 358 | + $error[] = $exception->getMessage(); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + if(count($error) == 0) { |
|
| 362 | + $config = \OC::$server->getConfig(); |
|
| 363 | + $config->setAppValue('core', 'installedat', microtime(true)); |
|
| 364 | + $config->setAppValue('core', 'lastupdatedat', microtime(true)); |
|
| 365 | + $config->setAppValue('core', 'vendor', $this->getVendor()); |
|
| 366 | + |
|
| 367 | + $group =\OC::$server->getGroupManager()->createGroup('admin'); |
|
| 368 | + $group->addUser($user); |
|
| 369 | + |
|
| 370 | + // Install shipped apps and specified app bundles |
|
| 371 | + Installer::installShippedApps(); |
|
| 372 | + $installer = new Installer( |
|
| 373 | + \OC::$server->getAppFetcher(), |
|
| 374 | + \OC::$server->getHTTPClientService(), |
|
| 375 | + \OC::$server->getTempManager(), |
|
| 376 | + \OC::$server->getLogger(), |
|
| 377 | + \OC::$server->getConfig() |
|
| 378 | + ); |
|
| 379 | + $bundleFetcher = new BundleFetcher(\OC::$server->getL10N('lib')); |
|
| 380 | + $defaultInstallationBundles = $bundleFetcher->getDefaultInstallationBundle(); |
|
| 381 | + foreach($defaultInstallationBundles as $bundle) { |
|
| 382 | + try { |
|
| 383 | + $installer->installAppBundle($bundle); |
|
| 384 | + } catch (Exception $e) {} |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // create empty file in data dir, so we can later find |
|
| 388 | + // out that this is indeed an ownCloud data directory |
|
| 389 | + file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', ''); |
|
| 390 | + |
|
| 391 | + // Update .htaccess files |
|
| 392 | + Setup::updateHtaccess(); |
|
| 393 | + Setup::protectDataDirectory(); |
|
| 394 | + |
|
| 395 | + self::installBackgroundJobs(); |
|
| 396 | + |
|
| 397 | + //and we are done |
|
| 398 | + $config->setSystemValue('installed', true); |
|
| 399 | + |
|
| 400 | + // Create a session token for the newly created user |
|
| 401 | + // The token provider requires a working db, so it's not injected on setup |
|
| 402 | + /* @var $userSession User\Session */ |
|
| 403 | + $userSession = \OC::$server->getUserSession(); |
|
| 404 | + $defaultTokenProvider = \OC::$server->query('OC\Authentication\Token\DefaultTokenProvider'); |
|
| 405 | + $userSession->setTokenProvider($defaultTokenProvider); |
|
| 406 | + $userSession->login($username, $password); |
|
| 407 | + $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + return $error; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + public static function installBackgroundJobs() { |
|
| 414 | + \OC::$server->getJobList()->add('\OC\Authentication\Token\DefaultTokenCleanupJob'); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * @return string Absolute path to htaccess |
|
| 419 | + */ |
|
| 420 | + private function pathToHtaccess() { |
|
| 421 | + return \OC::$SERVERROOT.'/.htaccess'; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Append the correct ErrorDocument path for Apache hosts |
|
| 426 | + * @return bool True when success, False otherwise |
|
| 427 | + */ |
|
| 428 | + public static function updateHtaccess() { |
|
| 429 | + $config = \OC::$server->getSystemConfig(); |
|
| 430 | + |
|
| 431 | + // For CLI read the value from overwrite.cli.url |
|
| 432 | + if(\OC::$CLI) { |
|
| 433 | + $webRoot = $config->getValue('overwrite.cli.url', ''); |
|
| 434 | + if($webRoot === '') { |
|
| 435 | + return false; |
|
| 436 | + } |
|
| 437 | + $webRoot = parse_url($webRoot, PHP_URL_PATH); |
|
| 438 | + $webRoot = rtrim($webRoot, '/'); |
|
| 439 | + } else { |
|
| 440 | + $webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/'; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + $setupHelper = new \OC\Setup($config, \OC::$server->getIniWrapper(), |
|
| 444 | + \OC::$server->getL10N('lib'), \OC::$server->query(Defaults::class), \OC::$server->getLogger(), |
|
| 445 | + \OC::$server->getSecureRandom()); |
|
| 446 | + |
|
| 447 | + $htaccessContent = file_get_contents($setupHelper->pathToHtaccess()); |
|
| 448 | + $content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n"; |
|
| 449 | + $htaccessContent = explode($content, $htaccessContent, 2)[0]; |
|
| 450 | + |
|
| 451 | + //custom 403 error page |
|
| 452 | + $content.= "\nErrorDocument 403 ".$webRoot."/core/templates/403.php"; |
|
| 453 | + |
|
| 454 | + //custom 404 error page |
|
| 455 | + $content.= "\nErrorDocument 404 ".$webRoot."/core/templates/404.php"; |
|
| 456 | + |
|
| 457 | + // Add rewrite rules if the RewriteBase is configured |
|
| 458 | + $rewriteBase = $config->getValue('htaccess.RewriteBase', ''); |
|
| 459 | + if($rewriteBase !== '') { |
|
| 460 | + $content .= "\n<IfModule mod_rewrite.c>"; |
|
| 461 | + $content .= "\n Options -MultiViews"; |
|
| 462 | + $content .= "\n RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]"; |
|
| 463 | + $content .= "\n RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]"; |
|
| 464 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !\\.(css|js|svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$"; |
|
| 465 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !core/img/favicon.ico$"; |
|
| 466 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !core/img/manifest.json$"; |
|
| 467 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/remote.php"; |
|
| 468 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/public.php"; |
|
| 469 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/cron.php"; |
|
| 470 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/core/ajax/update.php"; |
|
| 471 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/status.php"; |
|
| 472 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/ocs/v1.php"; |
|
| 473 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/ocs/v2.php"; |
|
| 474 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/robots.txt"; |
|
| 475 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/updater/"; |
|
| 476 | + $content .= "\n RewriteCond %{REQUEST_FILENAME} !/ocs-provider/"; |
|
| 477 | + $content .= "\n RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/.*"; |
|
| 478 | + $content .= "\n RewriteRule . index.php [PT,E=PATH_INFO:$1]"; |
|
| 479 | + $content .= "\n RewriteBase " . $rewriteBase; |
|
| 480 | + $content .= "\n <IfModule mod_env.c>"; |
|
| 481 | + $content .= "\n SetEnv front_controller_active true"; |
|
| 482 | + $content .= "\n <IfModule mod_dir.c>"; |
|
| 483 | + $content .= "\n DirectorySlash off"; |
|
| 484 | + $content .= "\n </IfModule>"; |
|
| 485 | + $content .= "\n </IfModule>"; |
|
| 486 | + $content .= "\n</IfModule>"; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + if ($content !== '') { |
|
| 490 | + //suppress errors in case we don't have permissions for it |
|
| 491 | + return (bool) @file_put_contents($setupHelper->pathToHtaccess(), $htaccessContent.$content . "\n"); |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + return false; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + public static function protectDataDirectory() { |
|
| 498 | + //Require all denied |
|
| 499 | + $now = date('Y-m-d H:i:s'); |
|
| 500 | + $content = "# Generated by Nextcloud on $now\n"; |
|
| 501 | + $content.= "# line below if for Apache 2.4\n"; |
|
| 502 | + $content.= "<ifModule mod_authz_core.c>\n"; |
|
| 503 | + $content.= "Require all denied\n"; |
|
| 504 | + $content.= "</ifModule>\n\n"; |
|
| 505 | + $content.= "# line below if for Apache 2.2\n"; |
|
| 506 | + $content.= "<ifModule !mod_authz_core.c>\n"; |
|
| 507 | + $content.= "deny from all\n"; |
|
| 508 | + $content.= "Satisfy All\n"; |
|
| 509 | + $content.= "</ifModule>\n\n"; |
|
| 510 | + $content.= "# section for Apache 2.2 and 2.4\n"; |
|
| 511 | + $content.= "<ifModule mod_autoindex.c>\n"; |
|
| 512 | + $content.= "IndexIgnore *\n"; |
|
| 513 | + $content.= "</ifModule>\n"; |
|
| 514 | + |
|
| 515 | + $baseDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data'); |
|
| 516 | + file_put_contents($baseDir . '/.htaccess', $content); |
|
| 517 | + file_put_contents($baseDir . '/index.html', ''); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Return vendor from which this version was published |
|
| 522 | + * |
|
| 523 | + * @return string Get the vendor |
|
| 524 | + * |
|
| 525 | + * Copy of \OC\Updater::getVendor() |
|
| 526 | + */ |
|
| 527 | + private function getVendor() { |
|
| 528 | + // this should really be a JSON file |
|
| 529 | + require \OC::$SERVERROOT . '/version.php'; |
|
| 530 | + /** @var string $vendor */ |
|
| 531 | + return (string) $vendor; |
|
| 532 | + } |
|
| 533 | 533 | } |
@@ -31,233 +31,233 @@ |
||
| 31 | 31 | namespace OC\Setup; |
| 32 | 32 | |
| 33 | 33 | class OCI extends AbstractDatabase { |
| 34 | - public $dbprettyname = 'Oracle'; |
|
| 34 | + public $dbprettyname = 'Oracle'; |
|
| 35 | 35 | |
| 36 | - protected $dbtablespace; |
|
| 36 | + protected $dbtablespace; |
|
| 37 | 37 | |
| 38 | - public function initialize($config) { |
|
| 39 | - parent::initialize($config); |
|
| 40 | - if (array_key_exists('dbtablespace', $config)) { |
|
| 41 | - $this->dbtablespace = $config['dbtablespace']; |
|
| 42 | - } else { |
|
| 43 | - $this->dbtablespace = 'USERS'; |
|
| 44 | - } |
|
| 45 | - // allow empty hostname for oracle |
|
| 46 | - $this->dbHost = $config['dbhost']; |
|
| 38 | + public function initialize($config) { |
|
| 39 | + parent::initialize($config); |
|
| 40 | + if (array_key_exists('dbtablespace', $config)) { |
|
| 41 | + $this->dbtablespace = $config['dbtablespace']; |
|
| 42 | + } else { |
|
| 43 | + $this->dbtablespace = 'USERS'; |
|
| 44 | + } |
|
| 45 | + // allow empty hostname for oracle |
|
| 46 | + $this->dbHost = $config['dbhost']; |
|
| 47 | 47 | |
| 48 | - $this->config->setValues([ |
|
| 49 | - 'dbhost' => $this->dbHost, |
|
| 50 | - 'dbtablespace' => $this->dbtablespace, |
|
| 51 | - ]); |
|
| 52 | - } |
|
| 48 | + $this->config->setValues([ |
|
| 49 | + 'dbhost' => $this->dbHost, |
|
| 50 | + 'dbtablespace' => $this->dbtablespace, |
|
| 51 | + ]); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function validate($config) { |
|
| 55 | - $errors = array(); |
|
| 56 | - if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 57 | - $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
|
| 58 | - } else if(empty($config['dbuser'])) { |
|
| 59 | - $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
|
| 60 | - } else if(empty($config['dbname'])) { |
|
| 61 | - $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
|
| 62 | - } |
|
| 63 | - return $errors; |
|
| 64 | - } |
|
| 54 | + public function validate($config) { |
|
| 55 | + $errors = array(); |
|
| 56 | + if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 57 | + $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
|
| 58 | + } else if(empty($config['dbuser'])) { |
|
| 59 | + $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
|
| 60 | + } else if(empty($config['dbname'])) { |
|
| 61 | + $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
|
| 62 | + } |
|
| 63 | + return $errors; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public function setupDatabase($username) { |
|
| 67 | - $e_host = addslashes($this->dbHost); |
|
| 68 | - // casting to int to avoid malicious input |
|
| 69 | - $e_port = (int)$this->dbPort; |
|
| 70 | - $e_dbname = addslashes($this->dbName); |
|
| 71 | - //check if the database user has admin right |
|
| 72 | - if ($e_host == '') { |
|
| 73 | - $easy_connect_string = $e_dbname; // use dbname as easy connect name |
|
| 74 | - } else { |
|
| 75 | - $easy_connect_string = '//'.$e_host.(!empty($e_port) ? ":{$e_port}" : "").'/'.$e_dbname; |
|
| 76 | - } |
|
| 77 | - $this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']); |
|
| 78 | - $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
|
| 79 | - if(!$connection) { |
|
| 80 | - $errorMessage = $this->getLastError(); |
|
| 81 | - if ($errorMessage) { |
|
| 82 | - throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'), |
|
| 83 | - $errorMessage.' Check environment: ORACLE_HOME='.getenv('ORACLE_HOME') |
|
| 84 | - .' ORACLE_SID='.getenv('ORACLE_SID') |
|
| 85 | - .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
|
| 86 | - .' NLS_LANG='.getenv('NLS_LANG') |
|
| 87 | - .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
|
| 88 | - } |
|
| 89 | - throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
|
| 90 | - 'Check environment: ORACLE_HOME='.getenv('ORACLE_HOME') |
|
| 91 | - .' ORACLE_SID='.getenv('ORACLE_SID') |
|
| 92 | - .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
|
| 93 | - .' NLS_LANG='.getenv('NLS_LANG') |
|
| 94 | - .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
|
| 95 | - } |
|
| 96 | - //check for roles creation rights in oracle |
|
| 66 | + public function setupDatabase($username) { |
|
| 67 | + $e_host = addslashes($this->dbHost); |
|
| 68 | + // casting to int to avoid malicious input |
|
| 69 | + $e_port = (int)$this->dbPort; |
|
| 70 | + $e_dbname = addslashes($this->dbName); |
|
| 71 | + //check if the database user has admin right |
|
| 72 | + if ($e_host == '') { |
|
| 73 | + $easy_connect_string = $e_dbname; // use dbname as easy connect name |
|
| 74 | + } else { |
|
| 75 | + $easy_connect_string = '//'.$e_host.(!empty($e_port) ? ":{$e_port}" : "").'/'.$e_dbname; |
|
| 76 | + } |
|
| 77 | + $this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']); |
|
| 78 | + $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
|
| 79 | + if(!$connection) { |
|
| 80 | + $errorMessage = $this->getLastError(); |
|
| 81 | + if ($errorMessage) { |
|
| 82 | + throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'), |
|
| 83 | + $errorMessage.' Check environment: ORACLE_HOME='.getenv('ORACLE_HOME') |
|
| 84 | + .' ORACLE_SID='.getenv('ORACLE_SID') |
|
| 85 | + .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
|
| 86 | + .' NLS_LANG='.getenv('NLS_LANG') |
|
| 87 | + .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
|
| 88 | + } |
|
| 89 | + throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
|
| 90 | + 'Check environment: ORACLE_HOME='.getenv('ORACLE_HOME') |
|
| 91 | + .' ORACLE_SID='.getenv('ORACLE_SID') |
|
| 92 | + .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
|
| 93 | + .' NLS_LANG='.getenv('NLS_LANG') |
|
| 94 | + .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
|
| 95 | + } |
|
| 96 | + //check for roles creation rights in oracle |
|
| 97 | 97 | |
| 98 | - $query='SELECT count(*) FROM user_role_privs, role_sys_privs' |
|
| 99 | - ." WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'"; |
|
| 100 | - $stmt = oci_parse($connection, $query); |
|
| 101 | - if (!$stmt) { |
|
| 102 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 103 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 104 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 105 | - } |
|
| 106 | - $result = oci_execute($stmt); |
|
| 107 | - if($result) { |
|
| 108 | - $row = oci_fetch_row($stmt); |
|
| 98 | + $query='SELECT count(*) FROM user_role_privs, role_sys_privs' |
|
| 99 | + ." WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'"; |
|
| 100 | + $stmt = oci_parse($connection, $query); |
|
| 101 | + if (!$stmt) { |
|
| 102 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 103 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 104 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 105 | + } |
|
| 106 | + $result = oci_execute($stmt); |
|
| 107 | + if($result) { |
|
| 108 | + $row = oci_fetch_row($stmt); |
|
| 109 | 109 | |
| 110 | - if ($row[0] > 0) { |
|
| 111 | - //use the admin login data for the new database user |
|
| 110 | + if ($row[0] > 0) { |
|
| 111 | + //use the admin login data for the new database user |
|
| 112 | 112 | |
| 113 | - //add prefix to the oracle user name to prevent collisions |
|
| 114 | - $this->dbUser='oc_'.$username; |
|
| 115 | - //create a new password so we don't need to store the admin config in the config file |
|
| 116 | - $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 113 | + //add prefix to the oracle user name to prevent collisions |
|
| 114 | + $this->dbUser='oc_'.$username; |
|
| 115 | + //create a new password so we don't need to store the admin config in the config file |
|
| 116 | + $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 117 | 117 | |
| 118 | - //oracle passwords are treated as identifiers: |
|
| 119 | - // must start with alphanumeric char |
|
| 120 | - // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. |
|
| 121 | - $this->dbPassword=substr($this->dbPassword, 0, 30); |
|
| 118 | + //oracle passwords are treated as identifiers: |
|
| 119 | + // must start with alphanumeric char |
|
| 120 | + // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. |
|
| 121 | + $this->dbPassword=substr($this->dbPassword, 0, 30); |
|
| 122 | 122 | |
| 123 | - $this->createDBUser($connection); |
|
| 124 | - } |
|
| 125 | - } |
|
| 123 | + $this->createDBUser($connection); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - $this->config->setValues([ |
|
| 128 | - 'dbuser' => $this->dbUser, |
|
| 129 | - 'dbname' => $this->dbName, |
|
| 130 | - 'dbpassword' => $this->dbPassword, |
|
| 131 | - ]); |
|
| 127 | + $this->config->setValues([ |
|
| 128 | + 'dbuser' => $this->dbUser, |
|
| 129 | + 'dbname' => $this->dbName, |
|
| 130 | + 'dbpassword' => $this->dbPassword, |
|
| 131 | + ]); |
|
| 132 | 132 | |
| 133 | - //create the database not necessary, oracle implies user = schema |
|
| 134 | - //$this->createDatabase($this->dbname, $this->dbuser, $connection); |
|
| 133 | + //create the database not necessary, oracle implies user = schema |
|
| 134 | + //$this->createDatabase($this->dbname, $this->dbuser, $connection); |
|
| 135 | 135 | |
| 136 | - //FIXME check tablespace exists: select * from user_tablespaces |
|
| 136 | + //FIXME check tablespace exists: select * from user_tablespaces |
|
| 137 | 137 | |
| 138 | - // the connection to dbname=oracle is not needed anymore |
|
| 139 | - oci_close($connection); |
|
| 138 | + // the connection to dbname=oracle is not needed anymore |
|
| 139 | + oci_close($connection); |
|
| 140 | 140 | |
| 141 | - // connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled |
|
| 142 | - $this->dbUser = $this->config->getValue('dbuser'); |
|
| 143 | - //$this->dbname = \OC_Config::getValue('dbname'); |
|
| 144 | - $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 141 | + // connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled |
|
| 142 | + $this->dbUser = $this->config->getValue('dbuser'); |
|
| 143 | + //$this->dbname = \OC_Config::getValue('dbname'); |
|
| 144 | + $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 145 | 145 | |
| 146 | - $e_host = addslashes($this->dbHost); |
|
| 147 | - $e_dbname = addslashes($this->dbName); |
|
| 146 | + $e_host = addslashes($this->dbHost); |
|
| 147 | + $e_dbname = addslashes($this->dbName); |
|
| 148 | 148 | |
| 149 | - if ($e_host == '') { |
|
| 150 | - $easy_connect_string = $e_dbname; // use dbname as easy connect name |
|
| 151 | - } else { |
|
| 152 | - $easy_connect_string = '//' . $e_host . (!empty($e_port) ? ":{$e_port}" : "") . '/' . $e_dbname; |
|
| 153 | - } |
|
| 154 | - $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
|
| 155 | - if(!$connection) { |
|
| 156 | - throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
|
| 157 | - $this->trans->t('You need to enter details of an existing account.')); |
|
| 158 | - } |
|
| 159 | - $query = "SELECT count(*) FROM user_tables WHERE table_name = :un"; |
|
| 160 | - $stmt = oci_parse($connection, $query); |
|
| 161 | - $un = $this->tablePrefix.'users'; |
|
| 162 | - oci_bind_by_name($stmt, ':un', $un); |
|
| 163 | - if (!$stmt) { |
|
| 164 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 165 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 166 | - $this->logger->warning( $entry, ['app' => 'setup.oci']); |
|
| 167 | - } |
|
| 168 | - oci_execute($stmt); |
|
| 169 | - } |
|
| 149 | + if ($e_host == '') { |
|
| 150 | + $easy_connect_string = $e_dbname; // use dbname as easy connect name |
|
| 151 | + } else { |
|
| 152 | + $easy_connect_string = '//' . $e_host . (!empty($e_port) ? ":{$e_port}" : "") . '/' . $e_dbname; |
|
| 153 | + } |
|
| 154 | + $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
|
| 155 | + if(!$connection) { |
|
| 156 | + throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
|
| 157 | + $this->trans->t('You need to enter details of an existing account.')); |
|
| 158 | + } |
|
| 159 | + $query = "SELECT count(*) FROM user_tables WHERE table_name = :un"; |
|
| 160 | + $stmt = oci_parse($connection, $query); |
|
| 161 | + $un = $this->tablePrefix.'users'; |
|
| 162 | + oci_bind_by_name($stmt, ':un', $un); |
|
| 163 | + if (!$stmt) { |
|
| 164 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 165 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 166 | + $this->logger->warning( $entry, ['app' => 'setup.oci']); |
|
| 167 | + } |
|
| 168 | + oci_execute($stmt); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * @param resource $connection |
|
| 173 | - */ |
|
| 174 | - private function createDBUser($connection) { |
|
| 175 | - $name = $this->dbUser; |
|
| 176 | - $password = $this->dbPassword; |
|
| 177 | - $query = "SELECT * FROM all_users WHERE USERNAME = :un"; |
|
| 178 | - $stmt = oci_parse($connection, $query); |
|
| 179 | - if (!$stmt) { |
|
| 180 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 181 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 182 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 183 | - } |
|
| 184 | - oci_bind_by_name($stmt, ':un', $name); |
|
| 185 | - $result = oci_execute($stmt); |
|
| 186 | - if(!$result) { |
|
| 187 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 188 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 189 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 190 | - } |
|
| 171 | + /** |
|
| 172 | + * @param resource $connection |
|
| 173 | + */ |
|
| 174 | + private function createDBUser($connection) { |
|
| 175 | + $name = $this->dbUser; |
|
| 176 | + $password = $this->dbPassword; |
|
| 177 | + $query = "SELECT * FROM all_users WHERE USERNAME = :un"; |
|
| 178 | + $stmt = oci_parse($connection, $query); |
|
| 179 | + if (!$stmt) { |
|
| 180 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 181 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 182 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 183 | + } |
|
| 184 | + oci_bind_by_name($stmt, ':un', $name); |
|
| 185 | + $result = oci_execute($stmt); |
|
| 186 | + if(!$result) { |
|
| 187 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 188 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 189 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - if(! oci_fetch_row($stmt)) { |
|
| 193 | - //user does not exists let's create it :) |
|
| 194 | - //password must start with alphabetic character in oracle |
|
| 195 | - $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$this->dbtablespace; |
|
| 196 | - $stmt = oci_parse($connection, $query); |
|
| 197 | - if (!$stmt) { |
|
| 198 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 199 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 200 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 192 | + if(! oci_fetch_row($stmt)) { |
|
| 193 | + //user does not exists let's create it :) |
|
| 194 | + //password must start with alphabetic character in oracle |
|
| 195 | + $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$this->dbtablespace; |
|
| 196 | + $stmt = oci_parse($connection, $query); |
|
| 197 | + if (!$stmt) { |
|
| 198 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 199 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 200 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 201 | 201 | |
| 202 | - } |
|
| 203 | - //oci_bind_by_name($stmt, ':un', $name); |
|
| 204 | - $result = oci_execute($stmt); |
|
| 205 | - if(!$result) { |
|
| 206 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 207 | - $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s', |
|
| 208 | - array($query, $name, $password)) . '<br />'; |
|
| 209 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 202 | + } |
|
| 203 | + //oci_bind_by_name($stmt, ':un', $name); |
|
| 204 | + $result = oci_execute($stmt); |
|
| 205 | + if(!$result) { |
|
| 206 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 207 | + $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s', |
|
| 208 | + array($query, $name, $password)) . '<br />'; |
|
| 209 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 210 | 210 | |
| 211 | - } |
|
| 212 | - } else { // change password of the existing role |
|
| 213 | - $query = "ALTER USER :un IDENTIFIED BY :pw"; |
|
| 214 | - $stmt = oci_parse($connection, $query); |
|
| 215 | - if (!$stmt) { |
|
| 216 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 217 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 218 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 219 | - } |
|
| 220 | - oci_bind_by_name($stmt, ':un', $name); |
|
| 221 | - oci_bind_by_name($stmt, ':pw', $password); |
|
| 222 | - $result = oci_execute($stmt); |
|
| 223 | - if(!$result) { |
|
| 224 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 225 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 226 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - // grant necessary roles |
|
| 230 | - $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name; |
|
| 231 | - $stmt = oci_parse($connection, $query); |
|
| 232 | - if (!$stmt) { |
|
| 233 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 234 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 235 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 236 | - } |
|
| 237 | - $result = oci_execute($stmt); |
|
| 238 | - if(!$result) { |
|
| 239 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 240 | - $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s', |
|
| 241 | - array($query, $name, $password)) . '<br />'; |
|
| 242 | - $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 243 | - } |
|
| 244 | - } |
|
| 211 | + } |
|
| 212 | + } else { // change password of the existing role |
|
| 213 | + $query = "ALTER USER :un IDENTIFIED BY :pw"; |
|
| 214 | + $stmt = oci_parse($connection, $query); |
|
| 215 | + if (!$stmt) { |
|
| 216 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 217 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 218 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 219 | + } |
|
| 220 | + oci_bind_by_name($stmt, ':un', $name); |
|
| 221 | + oci_bind_by_name($stmt, ':pw', $password); |
|
| 222 | + $result = oci_execute($stmt); |
|
| 223 | + if(!$result) { |
|
| 224 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 225 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 226 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + // grant necessary roles |
|
| 230 | + $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name; |
|
| 231 | + $stmt = oci_parse($connection, $query); |
|
| 232 | + if (!$stmt) { |
|
| 233 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 234 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 235 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 236 | + } |
|
| 237 | + $result = oci_execute($stmt); |
|
| 238 | + if(!$result) { |
|
| 239 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 240 | + $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s', |
|
| 241 | + array($query, $name, $password)) . '<br />'; |
|
| 242 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * @param resource $connection |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - protected function getLastError($connection = null) { |
|
| 251 | - if ($connection) { |
|
| 252 | - $error = oci_error($connection); |
|
| 253 | - } else { |
|
| 254 | - $error = oci_error(); |
|
| 255 | - } |
|
| 256 | - foreach (array('message', 'code') as $key) { |
|
| 257 | - if (isset($error[$key])) { |
|
| 258 | - return $error[$key]; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - return ''; |
|
| 262 | - } |
|
| 246 | + /** |
|
| 247 | + * @param resource $connection |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + protected function getLastError($connection = null) { |
|
| 251 | + if ($connection) { |
|
| 252 | + $error = oci_error($connection); |
|
| 253 | + } else { |
|
| 254 | + $error = oci_error(); |
|
| 255 | + } |
|
| 256 | + foreach (array('message', 'code') as $key) { |
|
| 257 | + if (isset($error[$key])) { |
|
| 258 | + return $error[$key]; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + return ''; |
|
| 262 | + } |
|
| 263 | 263 | } |
@@ -53,11 +53,11 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | public function validate($config) { |
| 55 | 55 | $errors = array(); |
| 56 | - if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 56 | + if (empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 57 | 57 | $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
| 58 | - } else if(empty($config['dbuser'])) { |
|
| 58 | + } else if (empty($config['dbuser'])) { |
|
| 59 | 59 | $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
| 60 | - } else if(empty($config['dbname'])) { |
|
| 60 | + } else if (empty($config['dbname'])) { |
|
| 61 | 61 | $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
| 62 | 62 | } |
| 63 | 63 | return $errors; |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | public function setupDatabase($username) { |
| 67 | 67 | $e_host = addslashes($this->dbHost); |
| 68 | 68 | // casting to int to avoid malicious input |
| 69 | - $e_port = (int)$this->dbPort; |
|
| 69 | + $e_port = (int) $this->dbPort; |
|
| 70 | 70 | $e_dbname = addslashes($this->dbName); |
| 71 | 71 | //check if the database user has admin right |
| 72 | 72 | if ($e_host == '') { |
@@ -74,9 +74,9 @@ discard block |
||
| 74 | 74 | } else { |
| 75 | 75 | $easy_connect_string = '//'.$e_host.(!empty($e_port) ? ":{$e_port}" : "").'/'.$e_dbname; |
| 76 | 76 | } |
| 77 | - $this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']); |
|
| 77 | + $this->logger->debug('connect string: '.$easy_connect_string, ['app' => 'setup.oci']); |
|
| 78 | 78 | $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
| 79 | - if(!$connection) { |
|
| 79 | + if (!$connection) { |
|
| 80 | 80 | $errorMessage = $this->getLastError(); |
| 81 | 81 | if ($errorMessage) { |
| 82 | 82 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'), |
@@ -84,41 +84,41 @@ discard block |
||
| 84 | 84 | .' ORACLE_SID='.getenv('ORACLE_SID') |
| 85 | 85 | .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
| 86 | 86 | .' NLS_LANG='.getenv('NLS_LANG') |
| 87 | - .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
|
| 87 | + .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora') ? '' : 'not ').'readable'); |
|
| 88 | 88 | } |
| 89 | 89 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
| 90 | 90 | 'Check environment: ORACLE_HOME='.getenv('ORACLE_HOME') |
| 91 | 91 | .' ORACLE_SID='.getenv('ORACLE_SID') |
| 92 | 92 | .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
| 93 | 93 | .' NLS_LANG='.getenv('NLS_LANG') |
| 94 | - .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
|
| 94 | + .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora') ? '' : 'not ').'readable'); |
|
| 95 | 95 | } |
| 96 | 96 | //check for roles creation rights in oracle |
| 97 | 97 | |
| 98 | - $query='SELECT count(*) FROM user_role_privs, role_sys_privs' |
|
| 98 | + $query = 'SELECT count(*) FROM user_role_privs, role_sys_privs' |
|
| 99 | 99 | ." WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'"; |
| 100 | 100 | $stmt = oci_parse($connection, $query); |
| 101 | 101 | if (!$stmt) { |
| 102 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 103 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 102 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 103 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 104 | 104 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 105 | 105 | } |
| 106 | 106 | $result = oci_execute($stmt); |
| 107 | - if($result) { |
|
| 107 | + if ($result) { |
|
| 108 | 108 | $row = oci_fetch_row($stmt); |
| 109 | 109 | |
| 110 | 110 | if ($row[0] > 0) { |
| 111 | 111 | //use the admin login data for the new database user |
| 112 | 112 | |
| 113 | 113 | //add prefix to the oracle user name to prevent collisions |
| 114 | - $this->dbUser='oc_'.$username; |
|
| 114 | + $this->dbUser = 'oc_'.$username; |
|
| 115 | 115 | //create a new password so we don't need to store the admin config in the config file |
| 116 | 116 | $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
| 117 | 117 | |
| 118 | 118 | //oracle passwords are treated as identifiers: |
| 119 | 119 | // must start with alphanumeric char |
| 120 | 120 | // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. |
| 121 | - $this->dbPassword=substr($this->dbPassword, 0, 30); |
|
| 121 | + $this->dbPassword = substr($this->dbPassword, 0, 30); |
|
| 122 | 122 | |
| 123 | 123 | $this->createDBUser($connection); |
| 124 | 124 | } |
@@ -149,10 +149,10 @@ discard block |
||
| 149 | 149 | if ($e_host == '') { |
| 150 | 150 | $easy_connect_string = $e_dbname; // use dbname as easy connect name |
| 151 | 151 | } else { |
| 152 | - $easy_connect_string = '//' . $e_host . (!empty($e_port) ? ":{$e_port}" : "") . '/' . $e_dbname; |
|
| 152 | + $easy_connect_string = '//'.$e_host.(!empty($e_port) ? ":{$e_port}" : "").'/'.$e_dbname; |
|
| 153 | 153 | } |
| 154 | 154 | $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
| 155 | - if(!$connection) { |
|
| 155 | + if (!$connection) { |
|
| 156 | 156 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
| 157 | 157 | $this->trans->t('You need to enter details of an existing account.')); |
| 158 | 158 | } |
@@ -161,9 +161,9 @@ discard block |
||
| 161 | 161 | $un = $this->tablePrefix.'users'; |
| 162 | 162 | oci_bind_by_name($stmt, ':un', $un); |
| 163 | 163 | if (!$stmt) { |
| 164 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 165 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 166 | - $this->logger->warning( $entry, ['app' => 'setup.oci']); |
|
| 164 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 165 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 166 | + $this->logger->warning($entry, ['app' => 'setup.oci']); |
|
| 167 | 167 | } |
| 168 | 168 | oci_execute($stmt); |
| 169 | 169 | } |
@@ -177,35 +177,35 @@ discard block |
||
| 177 | 177 | $query = "SELECT * FROM all_users WHERE USERNAME = :un"; |
| 178 | 178 | $stmt = oci_parse($connection, $query); |
| 179 | 179 | if (!$stmt) { |
| 180 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 181 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 180 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 181 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 182 | 182 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 183 | 183 | } |
| 184 | 184 | oci_bind_by_name($stmt, ':un', $name); |
| 185 | 185 | $result = oci_execute($stmt); |
| 186 | - if(!$result) { |
|
| 187 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 188 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 186 | + if (!$result) { |
|
| 187 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 188 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 189 | 189 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - if(! oci_fetch_row($stmt)) { |
|
| 192 | + if (!oci_fetch_row($stmt)) { |
|
| 193 | 193 | //user does not exists let's create it :) |
| 194 | 194 | //password must start with alphabetic character in oracle |
| 195 | 195 | $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$this->dbtablespace; |
| 196 | 196 | $stmt = oci_parse($connection, $query); |
| 197 | 197 | if (!$stmt) { |
| 198 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 199 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 198 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 199 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 200 | 200 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 201 | 201 | |
| 202 | 202 | } |
| 203 | 203 | //oci_bind_by_name($stmt, ':un', $name); |
| 204 | 204 | $result = oci_execute($stmt); |
| 205 | - if(!$result) { |
|
| 206 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 205 | + if (!$result) { |
|
| 206 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 207 | 207 | $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s', |
| 208 | - array($query, $name, $password)) . '<br />'; |
|
| 208 | + array($query, $name, $password)).'<br />'; |
|
| 209 | 209 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 210 | 210 | |
| 211 | 211 | } |
@@ -213,16 +213,16 @@ discard block |
||
| 213 | 213 | $query = "ALTER USER :un IDENTIFIED BY :pw"; |
| 214 | 214 | $stmt = oci_parse($connection, $query); |
| 215 | 215 | if (!$stmt) { |
| 216 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 217 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 216 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 217 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 218 | 218 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 219 | 219 | } |
| 220 | 220 | oci_bind_by_name($stmt, ':un', $name); |
| 221 | 221 | oci_bind_by_name($stmt, ':pw', $password); |
| 222 | 222 | $result = oci_execute($stmt); |
| 223 | - if(!$result) { |
|
| 224 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 225 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 223 | + if (!$result) { |
|
| 224 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 225 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 226 | 226 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 227 | 227 | } |
| 228 | 228 | } |
@@ -230,15 +230,15 @@ discard block |
||
| 230 | 230 | $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name; |
| 231 | 231 | $stmt = oci_parse($connection, $query); |
| 232 | 232 | if (!$stmt) { |
| 233 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 234 | - $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
|
| 233 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 234 | + $entry .= $this->trans->t('Offending command was: "%s"', array($query)).'<br />'; |
|
| 235 | 235 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 236 | 236 | } |
| 237 | 237 | $result = oci_execute($stmt); |
| 238 | - if(!$result) { |
|
| 239 | - $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
|
| 238 | + if (!$result) { |
|
| 239 | + $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))).'<br />'; |
|
| 240 | 240 | $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s', |
| 241 | - array($query, $name, $password)) . '<br />'; |
|
| 241 | + array($query, $name, $password)).'<br />'; |
|
| 242 | 242 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
| 243 | 243 | } |
| 244 | 244 | } |
@@ -23,23 +23,23 @@ |
||
| 23 | 23 | namespace OC\Setup; |
| 24 | 24 | |
| 25 | 25 | class Sqlite extends AbstractDatabase { |
| 26 | - public $dbprettyname = 'Sqlite'; |
|
| 26 | + public $dbprettyname = 'Sqlite'; |
|
| 27 | 27 | |
| 28 | - public function validate($config) { |
|
| 29 | - return array(); |
|
| 30 | - } |
|
| 28 | + public function validate($config) { |
|
| 29 | + return array(); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - public function initialize($config) { |
|
| 33 | - } |
|
| 32 | + public function initialize($config) { |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - public function setupDatabase($username) { |
|
| 36 | - $datadir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); |
|
| 35 | + public function setupDatabase($username) { |
|
| 36 | + $datadir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); |
|
| 37 | 37 | |
| 38 | - //delete the old sqlite database first, might cause infinte loops otherwise |
|
| 39 | - if(file_exists("$datadir/owncloud.db")) { |
|
| 40 | - unlink("$datadir/owncloud.db"); |
|
| 41 | - } |
|
| 42 | - //in case of sqlite, we can always fill the database |
|
| 43 | - error_log("creating sqlite db"); |
|
| 44 | - } |
|
| 38 | + //delete the old sqlite database first, might cause infinte loops otherwise |
|
| 39 | + if(file_exists("$datadir/owncloud.db")) { |
|
| 40 | + unlink("$datadir/owncloud.db"); |
|
| 41 | + } |
|
| 42 | + //in case of sqlite, we can always fill the database |
|
| 43 | + error_log("creating sqlite db"); |
|
| 44 | + } |
|
| 45 | 45 | } |
@@ -31,139 +31,139 @@ |
||
| 31 | 31 | use OCP\IDBConnection; |
| 32 | 32 | |
| 33 | 33 | class MySQL extends AbstractDatabase { |
| 34 | - public $dbprettyname = 'MySQL/MariaDB'; |
|
| 35 | - |
|
| 36 | - public function setupDatabase($username) { |
|
| 37 | - //check if the database user has admin right |
|
| 38 | - $connection = $this->connect(['dbname' => null]); |
|
| 39 | - |
|
| 40 | - // detect mb4 |
|
| 41 | - $tools = new MySqlTools(); |
|
| 42 | - if ($tools->supports4ByteCharset($connection)) { |
|
| 43 | - $this->config->setValue('mysql.utf8mb4', true); |
|
| 44 | - $connection = $this->connect(['dbname' => null]); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $this->createSpecificUser($username, $connection); |
|
| 48 | - |
|
| 49 | - //create the database |
|
| 50 | - $this->createDatabase($connection); |
|
| 51 | - |
|
| 52 | - //fill the database if needed |
|
| 53 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 54 | - $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param \OC\DB\Connection $connection |
|
| 59 | - */ |
|
| 60 | - private function createDatabase($connection) { |
|
| 61 | - try{ |
|
| 62 | - $name = $this->dbName; |
|
| 63 | - $user = $this->dbUser; |
|
| 64 | - //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 65 | - $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 66 | - $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 67 | - $connection->executeUpdate($query); |
|
| 68 | - } catch (\Exception $ex) { |
|
| 69 | - $this->logger->error('Database creation failed: {error}', [ |
|
| 70 | - 'app' => 'mysql.setup', |
|
| 71 | - 'error' => $ex->getMessage() |
|
| 72 | - ]); |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - try { |
|
| 77 | - //this query will fail if there aren't the right permissions, ignore the error |
|
| 78 | - $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 79 | - $connection->executeUpdate($query); |
|
| 80 | - } catch (\Exception $ex) { |
|
| 81 | - $this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges: {error}', [ |
|
| 82 | - 'app' => 'mysql.setup', |
|
| 83 | - 'error' => $ex->getMessage() |
|
| 84 | - ]); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param IDBConnection $connection |
|
| 90 | - * @throws \OC\DatabaseSetupException |
|
| 91 | - */ |
|
| 92 | - private function createDBUser($connection) { |
|
| 93 | - try{ |
|
| 94 | - $name = $this->dbUser; |
|
| 95 | - $password = $this->dbPassword; |
|
| 96 | - // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 97 | - // the anonymous user would take precedence when there is one. |
|
| 98 | - $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 99 | - $connection->executeUpdate($query); |
|
| 100 | - $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 101 | - $connection->executeUpdate($query); |
|
| 102 | - } |
|
| 103 | - catch (\Exception $ex){ |
|
| 104 | - $this->logger->error('Database User creation failed: {error}', [ |
|
| 34 | + public $dbprettyname = 'MySQL/MariaDB'; |
|
| 35 | + |
|
| 36 | + public function setupDatabase($username) { |
|
| 37 | + //check if the database user has admin right |
|
| 38 | + $connection = $this->connect(['dbname' => null]); |
|
| 39 | + |
|
| 40 | + // detect mb4 |
|
| 41 | + $tools = new MySqlTools(); |
|
| 42 | + if ($tools->supports4ByteCharset($connection)) { |
|
| 43 | + $this->config->setValue('mysql.utf8mb4', true); |
|
| 44 | + $connection = $this->connect(['dbname' => null]); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $this->createSpecificUser($username, $connection); |
|
| 48 | + |
|
| 49 | + //create the database |
|
| 50 | + $this->createDatabase($connection); |
|
| 51 | + |
|
| 52 | + //fill the database if needed |
|
| 53 | + $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 54 | + $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param \OC\DB\Connection $connection |
|
| 59 | + */ |
|
| 60 | + private function createDatabase($connection) { |
|
| 61 | + try{ |
|
| 62 | + $name = $this->dbName; |
|
| 63 | + $user = $this->dbUser; |
|
| 64 | + //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 65 | + $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 66 | + $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 67 | + $connection->executeUpdate($query); |
|
| 68 | + } catch (\Exception $ex) { |
|
| 69 | + $this->logger->error('Database creation failed: {error}', [ |
|
| 70 | + 'app' => 'mysql.setup', |
|
| 71 | + 'error' => $ex->getMessage() |
|
| 72 | + ]); |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + try { |
|
| 77 | + //this query will fail if there aren't the right permissions, ignore the error |
|
| 78 | + $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 79 | + $connection->executeUpdate($query); |
|
| 80 | + } catch (\Exception $ex) { |
|
| 81 | + $this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges: {error}', [ |
|
| 82 | + 'app' => 'mysql.setup', |
|
| 83 | + 'error' => $ex->getMessage() |
|
| 84 | + ]); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param IDBConnection $connection |
|
| 90 | + * @throws \OC\DatabaseSetupException |
|
| 91 | + */ |
|
| 92 | + private function createDBUser($connection) { |
|
| 93 | + try{ |
|
| 94 | + $name = $this->dbUser; |
|
| 95 | + $password = $this->dbPassword; |
|
| 96 | + // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 97 | + // the anonymous user would take precedence when there is one. |
|
| 98 | + $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 99 | + $connection->executeUpdate($query); |
|
| 100 | + $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 101 | + $connection->executeUpdate($query); |
|
| 102 | + } |
|
| 103 | + catch (\Exception $ex){ |
|
| 104 | + $this->logger->error('Database User creation failed: {error}', [ |
|
| 105 | 105 | 'app' => 'mysql.setup', |
| 106 | 106 | 'error' => $ex->getMessage() |
| 107 | 107 | ]); |
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param $username |
|
| 113 | - * @param IDBConnection $connection |
|
| 114 | - * @return array |
|
| 115 | - */ |
|
| 116 | - private function createSpecificUser($username, $connection) { |
|
| 117 | - try { |
|
| 118 | - //user already specified in config |
|
| 119 | - $oldUser = $this->config->getValue('dbuser', false); |
|
| 120 | - |
|
| 121 | - //we don't have a dbuser specified in config |
|
| 122 | - if ($this->dbUser !== $oldUser) { |
|
| 123 | - //add prefix to the admin username to prevent collisions |
|
| 124 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 125 | - |
|
| 126 | - $i = 1; |
|
| 127 | - while (true) { |
|
| 128 | - //this should be enough to check for admin rights in mysql |
|
| 129 | - $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 130 | - $result = $connection->executeQuery($query, [$adminUser]); |
|
| 131 | - |
|
| 132 | - //current dbuser has admin rights |
|
| 133 | - if ($result) { |
|
| 134 | - $data = $result->fetchAll(); |
|
| 135 | - //new dbuser does not exist |
|
| 136 | - if (count($data) === 0) { |
|
| 137 | - //use the admin login data for the new database user |
|
| 138 | - $this->dbUser = $adminUser; |
|
| 139 | - |
|
| 140 | - //create a random password so we don't need to store the admin password in the config file |
|
| 141 | - $this->dbPassword = $this->random->generate(30); |
|
| 142 | - |
|
| 143 | - $this->createDBUser($connection); |
|
| 144 | - |
|
| 145 | - break; |
|
| 146 | - } else { |
|
| 147 | - //repeat with different username |
|
| 148 | - $length = strlen((string)$i); |
|
| 149 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 150 | - $i++; |
|
| 151 | - } |
|
| 152 | - } else { |
|
| 153 | - break; |
|
| 154 | - } |
|
| 155 | - }; |
|
| 156 | - } |
|
| 157 | - } catch (\Exception $ex) { |
|
| 158 | - $this->logger->info('Can not create a new MySQL user, will continue with the provided user: {error}', [ |
|
| 159 | - 'app' => 'mysql.setup', |
|
| 160 | - 'error' => $ex->getMessage() |
|
| 161 | - ]); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $this->config->setValues([ |
|
| 165 | - 'dbuser' => $this->dbUser, |
|
| 166 | - 'dbpassword' => $this->dbPassword, |
|
| 167 | - ]); |
|
| 168 | - } |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param $username |
|
| 113 | + * @param IDBConnection $connection |
|
| 114 | + * @return array |
|
| 115 | + */ |
|
| 116 | + private function createSpecificUser($username, $connection) { |
|
| 117 | + try { |
|
| 118 | + //user already specified in config |
|
| 119 | + $oldUser = $this->config->getValue('dbuser', false); |
|
| 120 | + |
|
| 121 | + //we don't have a dbuser specified in config |
|
| 122 | + if ($this->dbUser !== $oldUser) { |
|
| 123 | + //add prefix to the admin username to prevent collisions |
|
| 124 | + $adminUser = substr('oc_' . $username, 0, 16); |
|
| 125 | + |
|
| 126 | + $i = 1; |
|
| 127 | + while (true) { |
|
| 128 | + //this should be enough to check for admin rights in mysql |
|
| 129 | + $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 130 | + $result = $connection->executeQuery($query, [$adminUser]); |
|
| 131 | + |
|
| 132 | + //current dbuser has admin rights |
|
| 133 | + if ($result) { |
|
| 134 | + $data = $result->fetchAll(); |
|
| 135 | + //new dbuser does not exist |
|
| 136 | + if (count($data) === 0) { |
|
| 137 | + //use the admin login data for the new database user |
|
| 138 | + $this->dbUser = $adminUser; |
|
| 139 | + |
|
| 140 | + //create a random password so we don't need to store the admin password in the config file |
|
| 141 | + $this->dbPassword = $this->random->generate(30); |
|
| 142 | + |
|
| 143 | + $this->createDBUser($connection); |
|
| 144 | + |
|
| 145 | + break; |
|
| 146 | + } else { |
|
| 147 | + //repeat with different username |
|
| 148 | + $length = strlen((string)$i); |
|
| 149 | + $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 150 | + $i++; |
|
| 151 | + } |
|
| 152 | + } else { |
|
| 153 | + break; |
|
| 154 | + } |
|
| 155 | + }; |
|
| 156 | + } |
|
| 157 | + } catch (\Exception $ex) { |
|
| 158 | + $this->logger->info('Can not create a new MySQL user, will continue with the provided user: {error}', [ |
|
| 159 | + 'app' => 'mysql.setup', |
|
| 160 | + 'error' => $ex->getMessage() |
|
| 161 | + ]); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $this->config->setValues([ |
|
| 165 | + 'dbuser' => $this->dbUser, |
|
| 166 | + 'dbpassword' => $this->dbPassword, |
|
| 167 | + ]); |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | $this->createDatabase($connection); |
| 51 | 51 | |
| 52 | 52 | //fill the database if needed |
| 53 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 53 | + $query = 'select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 54 | 54 | $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
| 55 | 55 | } |
| 56 | 56 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | * @param \OC\DB\Connection $connection |
| 59 | 59 | */ |
| 60 | 60 | private function createDatabase($connection) { |
| 61 | - try{ |
|
| 61 | + try { |
|
| 62 | 62 | $name = $this->dbName; |
| 63 | 63 | $user = $this->dbUser; |
| 64 | 64 | //we can't use OC_DB functions here because we need to connect as the administrative user. |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | |
| 76 | 76 | try { |
| 77 | 77 | //this query will fail if there aren't the right permissions, ignore the error |
| 78 | - $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 78 | + $query = "GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 79 | 79 | $connection->executeUpdate($query); |
| 80 | 80 | } catch (\Exception $ex) { |
| 81 | 81 | $this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges: {error}', [ |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | * @throws \OC\DatabaseSetupException |
| 91 | 91 | */ |
| 92 | 92 | private function createDBUser($connection) { |
| 93 | - try{ |
|
| 93 | + try { |
|
| 94 | 94 | $name = $this->dbUser; |
| 95 | 95 | $password = $this->dbPassword; |
| 96 | 96 | // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
| 101 | 101 | $connection->executeUpdate($query); |
| 102 | 102 | } |
| 103 | - catch (\Exception $ex){ |
|
| 103 | + catch (\Exception $ex) { |
|
| 104 | 104 | $this->logger->error('Database User creation failed: {error}', [ |
| 105 | 105 | 'app' => 'mysql.setup', |
| 106 | 106 | 'error' => $ex->getMessage() |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | //we don't have a dbuser specified in config |
| 122 | 122 | if ($this->dbUser !== $oldUser) { |
| 123 | 123 | //add prefix to the admin username to prevent collisions |
| 124 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 124 | + $adminUser = substr('oc_'.$username, 0, 16); |
|
| 125 | 125 | |
| 126 | 126 | $i = 1; |
| 127 | 127 | while (true) { |
@@ -138,15 +138,15 @@ discard block |
||
| 138 | 138 | $this->dbUser = $adminUser; |
| 139 | 139 | |
| 140 | 140 | //create a random password so we don't need to store the admin password in the config file |
| 141 | - $this->dbPassword = $this->random->generate(30); |
|
| 141 | + $this->dbPassword = $this->random->generate(30); |
|
| 142 | 142 | |
| 143 | 143 | $this->createDBUser($connection); |
| 144 | 144 | |
| 145 | 145 | break; |
| 146 | 146 | } else { |
| 147 | 147 | //repeat with different username |
| 148 | - $length = strlen((string)$i); |
|
| 149 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 148 | + $length = strlen((string) $i); |
|
| 149 | + $adminUser = substr('oc_'.$username, 0, 16 - $length).$i; |
|
| 150 | 150 | $i++; |
| 151 | 151 | } |
| 152 | 152 | } else { |
@@ -35,118 +35,118 @@ |
||
| 35 | 35 | |
| 36 | 36 | abstract class AbstractDatabase { |
| 37 | 37 | |
| 38 | - /** @var IL10N */ |
|
| 39 | - protected $trans; |
|
| 40 | - /** @var string */ |
|
| 41 | - protected $dbUser; |
|
| 42 | - /** @var string */ |
|
| 43 | - protected $dbPassword; |
|
| 44 | - /** @var string */ |
|
| 45 | - protected $dbName; |
|
| 46 | - /** @var string */ |
|
| 47 | - protected $dbHost; |
|
| 48 | - /** @var string */ |
|
| 49 | - protected $dbPort; |
|
| 50 | - /** @var string */ |
|
| 51 | - protected $tablePrefix; |
|
| 52 | - /** @var SystemConfig */ |
|
| 53 | - protected $config; |
|
| 54 | - /** @var ILogger */ |
|
| 55 | - protected $logger; |
|
| 56 | - /** @var ISecureRandom */ |
|
| 57 | - protected $random; |
|
| 38 | + /** @var IL10N */ |
|
| 39 | + protected $trans; |
|
| 40 | + /** @var string */ |
|
| 41 | + protected $dbUser; |
|
| 42 | + /** @var string */ |
|
| 43 | + protected $dbPassword; |
|
| 44 | + /** @var string */ |
|
| 45 | + protected $dbName; |
|
| 46 | + /** @var string */ |
|
| 47 | + protected $dbHost; |
|
| 48 | + /** @var string */ |
|
| 49 | + protected $dbPort; |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $tablePrefix; |
|
| 52 | + /** @var SystemConfig */ |
|
| 53 | + protected $config; |
|
| 54 | + /** @var ILogger */ |
|
| 55 | + protected $logger; |
|
| 56 | + /** @var ISecureRandom */ |
|
| 57 | + protected $random; |
|
| 58 | 58 | |
| 59 | - public function __construct(IL10N $trans, SystemConfig $config, ILogger $logger, ISecureRandom $random) { |
|
| 60 | - $this->trans = $trans; |
|
| 61 | - $this->config = $config; |
|
| 62 | - $this->logger = $logger; |
|
| 63 | - $this->random = $random; |
|
| 64 | - } |
|
| 59 | + public function __construct(IL10N $trans, SystemConfig $config, ILogger $logger, ISecureRandom $random) { |
|
| 60 | + $this->trans = $trans; |
|
| 61 | + $this->config = $config; |
|
| 62 | + $this->logger = $logger; |
|
| 63 | + $this->random = $random; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public function validate($config) { |
|
| 67 | - $errors = array(); |
|
| 68 | - if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 69 | - $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
|
| 70 | - } else if(empty($config['dbuser'])) { |
|
| 71 | - $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
|
| 72 | - } else if(empty($config['dbname'])) { |
|
| 73 | - $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
|
| 74 | - } |
|
| 75 | - if(substr_count($config['dbname'], '.') >= 1) { |
|
| 76 | - $errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname)); |
|
| 77 | - } |
|
| 78 | - return $errors; |
|
| 79 | - } |
|
| 66 | + public function validate($config) { |
|
| 67 | + $errors = array(); |
|
| 68 | + if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 69 | + $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
|
| 70 | + } else if(empty($config['dbuser'])) { |
|
| 71 | + $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
|
| 72 | + } else if(empty($config['dbname'])) { |
|
| 73 | + $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
|
| 74 | + } |
|
| 75 | + if(substr_count($config['dbname'], '.') >= 1) { |
|
| 76 | + $errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname)); |
|
| 77 | + } |
|
| 78 | + return $errors; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - public function initialize($config) { |
|
| 82 | - $dbUser = $config['dbuser']; |
|
| 83 | - $dbPass = $config['dbpass']; |
|
| 84 | - $dbName = $config['dbname']; |
|
| 85 | - $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost'; |
|
| 86 | - $dbPort = !empty($config['dbport']) ? $config['dbport'] : ''; |
|
| 87 | - $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; |
|
| 81 | + public function initialize($config) { |
|
| 82 | + $dbUser = $config['dbuser']; |
|
| 83 | + $dbPass = $config['dbpass']; |
|
| 84 | + $dbName = $config['dbname']; |
|
| 85 | + $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost'; |
|
| 86 | + $dbPort = !empty($config['dbport']) ? $config['dbport'] : ''; |
|
| 87 | + $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; |
|
| 88 | 88 | |
| 89 | - $this->config->setValues([ |
|
| 90 | - 'dbname' => $dbName, |
|
| 91 | - 'dbhost' => $dbHost, |
|
| 92 | - 'dbport' => $dbPort, |
|
| 93 | - 'dbtableprefix' => $dbTablePrefix, |
|
| 94 | - ]); |
|
| 89 | + $this->config->setValues([ |
|
| 90 | + 'dbname' => $dbName, |
|
| 91 | + 'dbhost' => $dbHost, |
|
| 92 | + 'dbport' => $dbPort, |
|
| 93 | + 'dbtableprefix' => $dbTablePrefix, |
|
| 94 | + ]); |
|
| 95 | 95 | |
| 96 | - $this->dbUser = $dbUser; |
|
| 97 | - $this->dbPassword = $dbPass; |
|
| 98 | - $this->dbName = $dbName; |
|
| 99 | - $this->dbHost = $dbHost; |
|
| 100 | - $this->dbPort = $dbPort; |
|
| 101 | - $this->tablePrefix = $dbTablePrefix; |
|
| 102 | - } |
|
| 96 | + $this->dbUser = $dbUser; |
|
| 97 | + $this->dbPassword = $dbPass; |
|
| 98 | + $this->dbName = $dbName; |
|
| 99 | + $this->dbHost = $dbHost; |
|
| 100 | + $this->dbPort = $dbPort; |
|
| 101 | + $this->tablePrefix = $dbTablePrefix; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * @param array $configOverwrite |
|
| 106 | - * @return \OC\DB\Connection |
|
| 107 | - */ |
|
| 108 | - protected function connect(array $configOverwrite = []) { |
|
| 109 | - $connectionParams = array( |
|
| 110 | - 'host' => $this->dbHost, |
|
| 111 | - 'user' => $this->dbUser, |
|
| 112 | - 'password' => $this->dbPassword, |
|
| 113 | - 'tablePrefix' => $this->tablePrefix, |
|
| 114 | - 'dbname' => $this->dbName |
|
| 115 | - ); |
|
| 104 | + /** |
|
| 105 | + * @param array $configOverwrite |
|
| 106 | + * @return \OC\DB\Connection |
|
| 107 | + */ |
|
| 108 | + protected function connect(array $configOverwrite = []) { |
|
| 109 | + $connectionParams = array( |
|
| 110 | + 'host' => $this->dbHost, |
|
| 111 | + 'user' => $this->dbUser, |
|
| 112 | + 'password' => $this->dbPassword, |
|
| 113 | + 'tablePrefix' => $this->tablePrefix, |
|
| 114 | + 'dbname' => $this->dbName |
|
| 115 | + ); |
|
| 116 | 116 | |
| 117 | - // adding port support through installer |
|
| 118 | - if (!empty($this->dbPort)) { |
|
| 119 | - if (ctype_digit($this->dbPort)) { |
|
| 120 | - $connectionParams['port'] = $this->dbPort; |
|
| 121 | - } else { |
|
| 122 | - $connectionParams['unix_socket'] = $this->dbPort; |
|
| 123 | - } |
|
| 124 | - } else if (strpos($this->dbHost, ':')) { |
|
| 125 | - // Host variable may carry a port or socket. |
|
| 126 | - list($host, $portOrSocket) = explode(':', $this->dbHost, 2); |
|
| 127 | - if (ctype_digit($portOrSocket)) { |
|
| 128 | - $connectionParams['port'] = $portOrSocket; |
|
| 129 | - } else { |
|
| 130 | - $connectionParams['unix_socket'] = $portOrSocket; |
|
| 131 | - } |
|
| 132 | - $connectionParams['host'] = $host; |
|
| 133 | - } |
|
| 117 | + // adding port support through installer |
|
| 118 | + if (!empty($this->dbPort)) { |
|
| 119 | + if (ctype_digit($this->dbPort)) { |
|
| 120 | + $connectionParams['port'] = $this->dbPort; |
|
| 121 | + } else { |
|
| 122 | + $connectionParams['unix_socket'] = $this->dbPort; |
|
| 123 | + } |
|
| 124 | + } else if (strpos($this->dbHost, ':')) { |
|
| 125 | + // Host variable may carry a port or socket. |
|
| 126 | + list($host, $portOrSocket) = explode(':', $this->dbHost, 2); |
|
| 127 | + if (ctype_digit($portOrSocket)) { |
|
| 128 | + $connectionParams['port'] = $portOrSocket; |
|
| 129 | + } else { |
|
| 130 | + $connectionParams['unix_socket'] = $portOrSocket; |
|
| 131 | + } |
|
| 132 | + $connectionParams['host'] = $host; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - $connectionParams = array_merge($connectionParams, $configOverwrite); |
|
| 136 | - $cf = new ConnectionFactory($this->config); |
|
| 137 | - return $cf->getConnection($this->config->getValue('dbtype', 'sqlite'), $connectionParams); |
|
| 138 | - } |
|
| 135 | + $connectionParams = array_merge($connectionParams, $configOverwrite); |
|
| 136 | + $cf = new ConnectionFactory($this->config); |
|
| 137 | + return $cf->getConnection($this->config->getValue('dbtype', 'sqlite'), $connectionParams); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * @param string $userName |
|
| 142 | - */ |
|
| 143 | - abstract public function setupDatabase($userName); |
|
| 140 | + /** |
|
| 141 | + * @param string $userName |
|
| 142 | + */ |
|
| 143 | + abstract public function setupDatabase($userName); |
|
| 144 | 144 | |
| 145 | - public function runMigrations() { |
|
| 146 | - if (!is_dir(\OC::$SERVERROOT."/core/Migrations")) { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - $ms = new MigrationService('core', \OC::$server->getDatabaseConnection()); |
|
| 150 | - $ms->migrate(); |
|
| 151 | - } |
|
| 145 | + public function runMigrations() { |
|
| 146 | + if (!is_dir(\OC::$SERVERROOT."/core/Migrations")) { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + $ms = new MigrationService('core', \OC::$server->getDatabaseConnection()); |
|
| 150 | + $ms->migrate(); |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -271,7 +271,7 @@ |
||
| 271 | 271 | * Return the explicit version for the aliases; current, next, prev, latest |
| 272 | 272 | * |
| 273 | 273 | * @param string $alias |
| 274 | - * @return mixed|null|string |
|
| 274 | + * @return string |
|
| 275 | 275 | */ |
| 276 | 276 | public function getMigration($alias) { |
| 277 | 277 | switch($alias) { |
@@ -38,382 +38,382 @@ |
||
| 38 | 38 | |
| 39 | 39 | class MigrationService { |
| 40 | 40 | |
| 41 | - /** @var boolean */ |
|
| 42 | - private $migrationTableCreated; |
|
| 43 | - /** @var array */ |
|
| 44 | - private $migrations; |
|
| 45 | - /** @var IOutput */ |
|
| 46 | - private $output; |
|
| 47 | - /** @var Connection */ |
|
| 48 | - private $connection; |
|
| 49 | - /** @var string */ |
|
| 50 | - private $appName; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * MigrationService constructor. |
|
| 54 | - * |
|
| 55 | - * @param $appName |
|
| 56 | - * @param IDBConnection $connection |
|
| 57 | - * @param AppLocator $appLocator |
|
| 58 | - * @param IOutput|null $output |
|
| 59 | - * @throws \Exception |
|
| 60 | - */ |
|
| 61 | - public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
| 62 | - $this->appName = $appName; |
|
| 63 | - $this->connection = $connection; |
|
| 64 | - $this->output = $output; |
|
| 65 | - if (null === $this->output) { |
|
| 66 | - $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - if ($appName === 'core') { |
|
| 70 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
| 71 | - $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
| 72 | - } else { |
|
| 73 | - if (null === $appLocator) { |
|
| 74 | - $appLocator = new AppLocator(); |
|
| 75 | - } |
|
| 76 | - $appPath = $appLocator->getAppPath($appName); |
|
| 77 | - $namespace = App::buildAppNamespace($appName); |
|
| 78 | - $this->migrationsPath = "$appPath/lib/Migration"; |
|
| 79 | - $this->migrationsNamespace = $namespace . '\\Migration'; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Returns the name of the app for which this migration is executed |
|
| 85 | - * |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function getApp() { |
|
| 89 | - return $this->appName; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return bool |
|
| 94 | - * @codeCoverageIgnore - this will implicitly tested on installation |
|
| 95 | - */ |
|
| 96 | - private function createMigrationTable() { |
|
| 97 | - if ($this->migrationTableCreated) { |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if ($this->connection->tableExists('migrations')) { |
|
| 102 | - $this->migrationTableCreated = true; |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $tableName = $this->connection->getPrefix() . 'migrations'; |
|
| 107 | - $tableName = $this->connection->getDatabasePlatform()->quoteIdentifier($tableName); |
|
| 108 | - |
|
| 109 | - $columns = [ |
|
| 110 | - 'app' => new Column($this->connection->getDatabasePlatform()->quoteIdentifier('app'), Type::getType('string'), ['length' => 255]), |
|
| 111 | - 'version' => new Column($this->connection->getDatabasePlatform()->quoteIdentifier('version'), Type::getType('string'), ['length' => 255]), |
|
| 112 | - ]; |
|
| 113 | - $table = new Table($tableName, $columns); |
|
| 114 | - $table->setPrimaryKey([ |
|
| 115 | - $this->connection->getDatabasePlatform()->quoteIdentifier('app'), |
|
| 116 | - $this->connection->getDatabasePlatform()->quoteIdentifier('version')]); |
|
| 117 | - $this->connection->getSchemaManager()->createTable($table); |
|
| 118 | - |
|
| 119 | - $this->migrationTableCreated = true; |
|
| 120 | - |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Returns all versions which have already been applied |
|
| 126 | - * |
|
| 127 | - * @return string[] |
|
| 128 | - * @codeCoverageIgnore - no need to test this |
|
| 129 | - */ |
|
| 130 | - public function getMigratedVersions() { |
|
| 131 | - $this->createMigrationTable(); |
|
| 132 | - $qb = $this->connection->getQueryBuilder(); |
|
| 133 | - |
|
| 134 | - $qb->select('version') |
|
| 135 | - ->from('migrations') |
|
| 136 | - ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
| 137 | - ->orderBy('version'); |
|
| 138 | - |
|
| 139 | - $result = $qb->execute(); |
|
| 140 | - $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
| 141 | - $result->closeCursor(); |
|
| 142 | - |
|
| 143 | - return $rows; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Returns all versions which are available in the migration folder |
|
| 148 | - * |
|
| 149 | - * @return array |
|
| 150 | - */ |
|
| 151 | - public function getAvailableVersions() { |
|
| 152 | - $this->ensureMigrationsAreLoaded(); |
|
| 153 | - return array_map('strval', array_keys($this->migrations)); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - protected function findMigrations() { |
|
| 157 | - $directory = realpath($this->migrationsPath); |
|
| 158 | - if (!file_exists($directory) || !is_dir($directory)) { |
|
| 159 | - return []; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $iterator = new \RegexIterator( |
|
| 163 | - new \RecursiveIteratorIterator( |
|
| 164 | - new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
| 165 | - \RecursiveIteratorIterator::LEAVES_ONLY |
|
| 166 | - ), |
|
| 167 | - '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
| 168 | - \RegexIterator::GET_MATCH); |
|
| 169 | - |
|
| 170 | - $files = array_keys(iterator_to_array($iterator)); |
|
| 171 | - uasort($files, function ($a, $b) { |
|
| 172 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
| 173 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
| 174 | - if (!empty($matchA) && !empty($matchB)) { |
|
| 175 | - if ($matchA[1] !== $matchB[1]) { |
|
| 176 | - return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
| 177 | - } |
|
| 178 | - return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
| 179 | - } |
|
| 180 | - return (basename($a) < basename($b)) ? -1 : 1; |
|
| 181 | - }); |
|
| 182 | - |
|
| 183 | - $migrations = []; |
|
| 184 | - |
|
| 185 | - foreach ($files as $file) { |
|
| 186 | - $className = basename($file, '.php'); |
|
| 187 | - $version = (string) substr($className, 7); |
|
| 188 | - if ($version === '0') { |
|
| 189 | - throw new \InvalidArgumentException( |
|
| 190 | - "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
| 191 | - ); |
|
| 192 | - } |
|
| 193 | - $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $migrations; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @param string $to |
|
| 201 | - * @return string[] |
|
| 202 | - */ |
|
| 203 | - private function getMigrationsToExecute($to) { |
|
| 204 | - $knownMigrations = $this->getMigratedVersions(); |
|
| 205 | - $availableMigrations = $this->getAvailableVersions(); |
|
| 206 | - |
|
| 207 | - $toBeExecuted = []; |
|
| 208 | - foreach ($availableMigrations as $v) { |
|
| 209 | - if ($to !== 'latest' && $v > $to) { |
|
| 210 | - continue; |
|
| 211 | - } |
|
| 212 | - if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
| 213 | - $toBeExecuted[] = $v; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - return $toBeExecuted; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param string $m |
|
| 222 | - * @param string[] $knownMigrations |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 225 | - private function shallBeExecuted($m, $knownMigrations) { |
|
| 226 | - if (in_array($m, $knownMigrations)) { |
|
| 227 | - return false; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return true; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @param string $version |
|
| 235 | - */ |
|
| 236 | - private function markAsExecuted($version) { |
|
| 237 | - $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
| 238 | - 'app' => $this->appName, |
|
| 239 | - 'version' => $version |
|
| 240 | - ]); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Returns the name of the table which holds the already applied versions |
|
| 245 | - * |
|
| 246 | - * @return string |
|
| 247 | - */ |
|
| 248 | - public function getMigrationsTableName() { |
|
| 249 | - return $this->connection->getPrefix() . 'migrations'; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Returns the namespace of the version classes |
|
| 254 | - * |
|
| 255 | - * @return string |
|
| 256 | - */ |
|
| 257 | - public function getMigrationsNamespace() { |
|
| 258 | - return $this->migrationsNamespace; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Returns the directory which holds the versions |
|
| 263 | - * |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - public function getMigrationsDirectory() { |
|
| 267 | - return $this->migrationsPath; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Return the explicit version for the aliases; current, next, prev, latest |
|
| 272 | - * |
|
| 273 | - * @param string $alias |
|
| 274 | - * @return mixed|null|string |
|
| 275 | - */ |
|
| 276 | - public function getMigration($alias) { |
|
| 277 | - switch($alias) { |
|
| 278 | - case 'current': |
|
| 279 | - return $this->getCurrentVersion(); |
|
| 280 | - case 'next': |
|
| 281 | - return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
| 282 | - case 'prev': |
|
| 283 | - return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
| 284 | - case 'latest': |
|
| 285 | - $this->ensureMigrationsAreLoaded(); |
|
| 286 | - |
|
| 287 | - $migrations = $this->getAvailableVersions(); |
|
| 288 | - return @end($migrations); |
|
| 289 | - } |
|
| 290 | - return '0'; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param string $version |
|
| 295 | - * @param int $delta |
|
| 296 | - * @return null|string |
|
| 297 | - */ |
|
| 298 | - private function getRelativeVersion($version, $delta) { |
|
| 299 | - $this->ensureMigrationsAreLoaded(); |
|
| 300 | - |
|
| 301 | - $versions = $this->getAvailableVersions(); |
|
| 302 | - array_unshift($versions, 0); |
|
| 303 | - $offset = array_search($version, $versions, true); |
|
| 304 | - if ($offset === false || !isset($versions[$offset + $delta])) { |
|
| 305 | - // Unknown version or delta out of bounds. |
|
| 306 | - return null; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - return (string) $versions[$offset + $delta]; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @return string |
|
| 314 | - */ |
|
| 315 | - private function getCurrentVersion() { |
|
| 316 | - $m = $this->getMigratedVersions(); |
|
| 317 | - if (count($m) === 0) { |
|
| 318 | - return '0'; |
|
| 319 | - } |
|
| 320 | - $migrations = array_values($m); |
|
| 321 | - return @end($migrations); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * @param string $version |
|
| 326 | - * @return string |
|
| 327 | - * @throws \InvalidArgumentException |
|
| 328 | - */ |
|
| 329 | - private function getClass($version) { |
|
| 330 | - $this->ensureMigrationsAreLoaded(); |
|
| 331 | - |
|
| 332 | - if (isset($this->migrations[$version])) { |
|
| 333 | - return $this->migrations[$version]; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - throw new \InvalidArgumentException("Version $version is unknown."); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Allows to set an IOutput implementation which is used for logging progress and messages |
|
| 341 | - * |
|
| 342 | - * @param IOutput $output |
|
| 343 | - */ |
|
| 344 | - public function setOutput(IOutput $output) { |
|
| 345 | - $this->output = $output; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Applies all not yet applied versions up to $to |
|
| 350 | - * |
|
| 351 | - * @param string $to |
|
| 352 | - * @throws \InvalidArgumentException |
|
| 353 | - */ |
|
| 354 | - public function migrate($to = 'latest') { |
|
| 355 | - // read known migrations |
|
| 356 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
| 357 | - foreach ($toBeExecuted as $version) { |
|
| 358 | - $this->executeStep($version); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * @param string $version |
|
| 364 | - * @return mixed |
|
| 365 | - * @throws \InvalidArgumentException |
|
| 366 | - */ |
|
| 367 | - protected function createInstance($version) { |
|
| 368 | - $class = $this->getClass($version); |
|
| 369 | - try { |
|
| 370 | - $s = \OC::$server->query($class); |
|
| 371 | - } catch (QueryException $e) { |
|
| 372 | - if (class_exists($class)) { |
|
| 373 | - $s = new $class(); |
|
| 374 | - } else { |
|
| 375 | - throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return $s; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Executes one explicit version |
|
| 384 | - * |
|
| 385 | - * @param string $version |
|
| 386 | - * @throws \InvalidArgumentException |
|
| 387 | - */ |
|
| 388 | - public function executeStep($version) { |
|
| 389 | - $instance = $this->createInstance($version); |
|
| 390 | - if (!$instance instanceof IMigrationStep) { |
|
| 391 | - throw new \InvalidArgumentException('Not a valid migration'); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - $instance->preSchemaChange($this->output, function() { |
|
| 395 | - return $this->connection->createSchema(); |
|
| 396 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
| 397 | - |
|
| 398 | - $toSchema = $instance->changeSchema($this->output, function() { |
|
| 399 | - return new SchemaWrapper($this->connection); |
|
| 400 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
| 401 | - |
|
| 402 | - if ($toSchema instanceof SchemaWrapper) { |
|
| 403 | - $this->connection->migrateToSchema($toSchema->getWrappedSchema()); |
|
| 404 | - $toSchema->performDropTableCalls(); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - $instance->postSchemaChange($this->output, function() { |
|
| 408 | - return $this->connection->createSchema(); |
|
| 409 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
| 410 | - |
|
| 411 | - $this->markAsExecuted($version); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - private function ensureMigrationsAreLoaded() { |
|
| 415 | - if (empty($this->migrations)) { |
|
| 416 | - $this->migrations = $this->findMigrations(); |
|
| 417 | - } |
|
| 418 | - } |
|
| 41 | + /** @var boolean */ |
|
| 42 | + private $migrationTableCreated; |
|
| 43 | + /** @var array */ |
|
| 44 | + private $migrations; |
|
| 45 | + /** @var IOutput */ |
|
| 46 | + private $output; |
|
| 47 | + /** @var Connection */ |
|
| 48 | + private $connection; |
|
| 49 | + /** @var string */ |
|
| 50 | + private $appName; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * MigrationService constructor. |
|
| 54 | + * |
|
| 55 | + * @param $appName |
|
| 56 | + * @param IDBConnection $connection |
|
| 57 | + * @param AppLocator $appLocator |
|
| 58 | + * @param IOutput|null $output |
|
| 59 | + * @throws \Exception |
|
| 60 | + */ |
|
| 61 | + public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
| 62 | + $this->appName = $appName; |
|
| 63 | + $this->connection = $connection; |
|
| 64 | + $this->output = $output; |
|
| 65 | + if (null === $this->output) { |
|
| 66 | + $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if ($appName === 'core') { |
|
| 70 | + $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
| 71 | + $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
| 72 | + } else { |
|
| 73 | + if (null === $appLocator) { |
|
| 74 | + $appLocator = new AppLocator(); |
|
| 75 | + } |
|
| 76 | + $appPath = $appLocator->getAppPath($appName); |
|
| 77 | + $namespace = App::buildAppNamespace($appName); |
|
| 78 | + $this->migrationsPath = "$appPath/lib/Migration"; |
|
| 79 | + $this->migrationsNamespace = $namespace . '\\Migration'; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Returns the name of the app for which this migration is executed |
|
| 85 | + * |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function getApp() { |
|
| 89 | + return $this->appName; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return bool |
|
| 94 | + * @codeCoverageIgnore - this will implicitly tested on installation |
|
| 95 | + */ |
|
| 96 | + private function createMigrationTable() { |
|
| 97 | + if ($this->migrationTableCreated) { |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if ($this->connection->tableExists('migrations')) { |
|
| 102 | + $this->migrationTableCreated = true; |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $tableName = $this->connection->getPrefix() . 'migrations'; |
|
| 107 | + $tableName = $this->connection->getDatabasePlatform()->quoteIdentifier($tableName); |
|
| 108 | + |
|
| 109 | + $columns = [ |
|
| 110 | + 'app' => new Column($this->connection->getDatabasePlatform()->quoteIdentifier('app'), Type::getType('string'), ['length' => 255]), |
|
| 111 | + 'version' => new Column($this->connection->getDatabasePlatform()->quoteIdentifier('version'), Type::getType('string'), ['length' => 255]), |
|
| 112 | + ]; |
|
| 113 | + $table = new Table($tableName, $columns); |
|
| 114 | + $table->setPrimaryKey([ |
|
| 115 | + $this->connection->getDatabasePlatform()->quoteIdentifier('app'), |
|
| 116 | + $this->connection->getDatabasePlatform()->quoteIdentifier('version')]); |
|
| 117 | + $this->connection->getSchemaManager()->createTable($table); |
|
| 118 | + |
|
| 119 | + $this->migrationTableCreated = true; |
|
| 120 | + |
|
| 121 | + return true; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Returns all versions which have already been applied |
|
| 126 | + * |
|
| 127 | + * @return string[] |
|
| 128 | + * @codeCoverageIgnore - no need to test this |
|
| 129 | + */ |
|
| 130 | + public function getMigratedVersions() { |
|
| 131 | + $this->createMigrationTable(); |
|
| 132 | + $qb = $this->connection->getQueryBuilder(); |
|
| 133 | + |
|
| 134 | + $qb->select('version') |
|
| 135 | + ->from('migrations') |
|
| 136 | + ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
| 137 | + ->orderBy('version'); |
|
| 138 | + |
|
| 139 | + $result = $qb->execute(); |
|
| 140 | + $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
| 141 | + $result->closeCursor(); |
|
| 142 | + |
|
| 143 | + return $rows; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Returns all versions which are available in the migration folder |
|
| 148 | + * |
|
| 149 | + * @return array |
|
| 150 | + */ |
|
| 151 | + public function getAvailableVersions() { |
|
| 152 | + $this->ensureMigrationsAreLoaded(); |
|
| 153 | + return array_map('strval', array_keys($this->migrations)); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + protected function findMigrations() { |
|
| 157 | + $directory = realpath($this->migrationsPath); |
|
| 158 | + if (!file_exists($directory) || !is_dir($directory)) { |
|
| 159 | + return []; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $iterator = new \RegexIterator( |
|
| 163 | + new \RecursiveIteratorIterator( |
|
| 164 | + new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
| 165 | + \RecursiveIteratorIterator::LEAVES_ONLY |
|
| 166 | + ), |
|
| 167 | + '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
| 168 | + \RegexIterator::GET_MATCH); |
|
| 169 | + |
|
| 170 | + $files = array_keys(iterator_to_array($iterator)); |
|
| 171 | + uasort($files, function ($a, $b) { |
|
| 172 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
| 173 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
| 174 | + if (!empty($matchA) && !empty($matchB)) { |
|
| 175 | + if ($matchA[1] !== $matchB[1]) { |
|
| 176 | + return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
| 177 | + } |
|
| 178 | + return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
| 179 | + } |
|
| 180 | + return (basename($a) < basename($b)) ? -1 : 1; |
|
| 181 | + }); |
|
| 182 | + |
|
| 183 | + $migrations = []; |
|
| 184 | + |
|
| 185 | + foreach ($files as $file) { |
|
| 186 | + $className = basename($file, '.php'); |
|
| 187 | + $version = (string) substr($className, 7); |
|
| 188 | + if ($version === '0') { |
|
| 189 | + throw new \InvalidArgumentException( |
|
| 190 | + "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
| 191 | + ); |
|
| 192 | + } |
|
| 193 | + $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $migrations; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @param string $to |
|
| 201 | + * @return string[] |
|
| 202 | + */ |
|
| 203 | + private function getMigrationsToExecute($to) { |
|
| 204 | + $knownMigrations = $this->getMigratedVersions(); |
|
| 205 | + $availableMigrations = $this->getAvailableVersions(); |
|
| 206 | + |
|
| 207 | + $toBeExecuted = []; |
|
| 208 | + foreach ($availableMigrations as $v) { |
|
| 209 | + if ($to !== 'latest' && $v > $to) { |
|
| 210 | + continue; |
|
| 211 | + } |
|
| 212 | + if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
| 213 | + $toBeExecuted[] = $v; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + return $toBeExecuted; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param string $m |
|
| 222 | + * @param string[] $knownMigrations |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | + private function shallBeExecuted($m, $knownMigrations) { |
|
| 226 | + if (in_array($m, $knownMigrations)) { |
|
| 227 | + return false; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return true; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @param string $version |
|
| 235 | + */ |
|
| 236 | + private function markAsExecuted($version) { |
|
| 237 | + $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
| 238 | + 'app' => $this->appName, |
|
| 239 | + 'version' => $version |
|
| 240 | + ]); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Returns the name of the table which holds the already applied versions |
|
| 245 | + * |
|
| 246 | + * @return string |
|
| 247 | + */ |
|
| 248 | + public function getMigrationsTableName() { |
|
| 249 | + return $this->connection->getPrefix() . 'migrations'; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Returns the namespace of the version classes |
|
| 254 | + * |
|
| 255 | + * @return string |
|
| 256 | + */ |
|
| 257 | + public function getMigrationsNamespace() { |
|
| 258 | + return $this->migrationsNamespace; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Returns the directory which holds the versions |
|
| 263 | + * |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + public function getMigrationsDirectory() { |
|
| 267 | + return $this->migrationsPath; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Return the explicit version for the aliases; current, next, prev, latest |
|
| 272 | + * |
|
| 273 | + * @param string $alias |
|
| 274 | + * @return mixed|null|string |
|
| 275 | + */ |
|
| 276 | + public function getMigration($alias) { |
|
| 277 | + switch($alias) { |
|
| 278 | + case 'current': |
|
| 279 | + return $this->getCurrentVersion(); |
|
| 280 | + case 'next': |
|
| 281 | + return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
| 282 | + case 'prev': |
|
| 283 | + return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
| 284 | + case 'latest': |
|
| 285 | + $this->ensureMigrationsAreLoaded(); |
|
| 286 | + |
|
| 287 | + $migrations = $this->getAvailableVersions(); |
|
| 288 | + return @end($migrations); |
|
| 289 | + } |
|
| 290 | + return '0'; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param string $version |
|
| 295 | + * @param int $delta |
|
| 296 | + * @return null|string |
|
| 297 | + */ |
|
| 298 | + private function getRelativeVersion($version, $delta) { |
|
| 299 | + $this->ensureMigrationsAreLoaded(); |
|
| 300 | + |
|
| 301 | + $versions = $this->getAvailableVersions(); |
|
| 302 | + array_unshift($versions, 0); |
|
| 303 | + $offset = array_search($version, $versions, true); |
|
| 304 | + if ($offset === false || !isset($versions[$offset + $delta])) { |
|
| 305 | + // Unknown version or delta out of bounds. |
|
| 306 | + return null; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + return (string) $versions[$offset + $delta]; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @return string |
|
| 314 | + */ |
|
| 315 | + private function getCurrentVersion() { |
|
| 316 | + $m = $this->getMigratedVersions(); |
|
| 317 | + if (count($m) === 0) { |
|
| 318 | + return '0'; |
|
| 319 | + } |
|
| 320 | + $migrations = array_values($m); |
|
| 321 | + return @end($migrations); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * @param string $version |
|
| 326 | + * @return string |
|
| 327 | + * @throws \InvalidArgumentException |
|
| 328 | + */ |
|
| 329 | + private function getClass($version) { |
|
| 330 | + $this->ensureMigrationsAreLoaded(); |
|
| 331 | + |
|
| 332 | + if (isset($this->migrations[$version])) { |
|
| 333 | + return $this->migrations[$version]; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + throw new \InvalidArgumentException("Version $version is unknown."); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Allows to set an IOutput implementation which is used for logging progress and messages |
|
| 341 | + * |
|
| 342 | + * @param IOutput $output |
|
| 343 | + */ |
|
| 344 | + public function setOutput(IOutput $output) { |
|
| 345 | + $this->output = $output; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Applies all not yet applied versions up to $to |
|
| 350 | + * |
|
| 351 | + * @param string $to |
|
| 352 | + * @throws \InvalidArgumentException |
|
| 353 | + */ |
|
| 354 | + public function migrate($to = 'latest') { |
|
| 355 | + // read known migrations |
|
| 356 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
| 357 | + foreach ($toBeExecuted as $version) { |
|
| 358 | + $this->executeStep($version); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * @param string $version |
|
| 364 | + * @return mixed |
|
| 365 | + * @throws \InvalidArgumentException |
|
| 366 | + */ |
|
| 367 | + protected function createInstance($version) { |
|
| 368 | + $class = $this->getClass($version); |
|
| 369 | + try { |
|
| 370 | + $s = \OC::$server->query($class); |
|
| 371 | + } catch (QueryException $e) { |
|
| 372 | + if (class_exists($class)) { |
|
| 373 | + $s = new $class(); |
|
| 374 | + } else { |
|
| 375 | + throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return $s; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Executes one explicit version |
|
| 384 | + * |
|
| 385 | + * @param string $version |
|
| 386 | + * @throws \InvalidArgumentException |
|
| 387 | + */ |
|
| 388 | + public function executeStep($version) { |
|
| 389 | + $instance = $this->createInstance($version); |
|
| 390 | + if (!$instance instanceof IMigrationStep) { |
|
| 391 | + throw new \InvalidArgumentException('Not a valid migration'); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + $instance->preSchemaChange($this->output, function() { |
|
| 395 | + return $this->connection->createSchema(); |
|
| 396 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
| 397 | + |
|
| 398 | + $toSchema = $instance->changeSchema($this->output, function() { |
|
| 399 | + return new SchemaWrapper($this->connection); |
|
| 400 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
| 401 | + |
|
| 402 | + if ($toSchema instanceof SchemaWrapper) { |
|
| 403 | + $this->connection->migrateToSchema($toSchema->getWrappedSchema()); |
|
| 404 | + $toSchema->performDropTableCalls(); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + $instance->postSchemaChange($this->output, function() { |
|
| 408 | + return $this->connection->createSchema(); |
|
| 409 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
| 410 | + |
|
| 411 | + $this->markAsExecuted($version); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + private function ensureMigrationsAreLoaded() { |
|
| 415 | + if (empty($this->migrations)) { |
|
| 416 | + $this->migrations = $this->findMigrations(); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | 419 | } |
@@ -40,120 +40,120 @@ |
||
| 40 | 40 | $application->add(new OC\Core\Command\App\CheckCode($infoParser)); |
| 41 | 41 | $application->add(new OC\Core\Command\L10n\CreateJs()); |
| 42 | 42 | $application->add(new \OC\Core\Command\Integrity\SignApp( |
| 43 | - \OC::$server->getIntegrityCodeChecker(), |
|
| 44 | - new \OC\IntegrityCheck\Helpers\FileAccessHelper(), |
|
| 45 | - \OC::$server->getURLGenerator() |
|
| 43 | + \OC::$server->getIntegrityCodeChecker(), |
|
| 44 | + new \OC\IntegrityCheck\Helpers\FileAccessHelper(), |
|
| 45 | + \OC::$server->getURLGenerator() |
|
| 46 | 46 | )); |
| 47 | 47 | $application->add(new \OC\Core\Command\Integrity\SignCore( |
| 48 | - \OC::$server->getIntegrityCodeChecker(), |
|
| 49 | - new \OC\IntegrityCheck\Helpers\FileAccessHelper() |
|
| 48 | + \OC::$server->getIntegrityCodeChecker(), |
|
| 49 | + new \OC\IntegrityCheck\Helpers\FileAccessHelper() |
|
| 50 | 50 | )); |
| 51 | 51 | $application->add(new \OC\Core\Command\Integrity\CheckApp( |
| 52 | - \OC::$server->getIntegrityCodeChecker() |
|
| 52 | + \OC::$server->getIntegrityCodeChecker() |
|
| 53 | 53 | )); |
| 54 | 54 | $application->add(new \OC\Core\Command\Integrity\CheckCore( |
| 55 | - \OC::$server->getIntegrityCodeChecker() |
|
| 55 | + \OC::$server->getIntegrityCodeChecker() |
|
| 56 | 56 | )); |
| 57 | 57 | |
| 58 | 58 | |
| 59 | 59 | if (\OC::$server->getConfig()->getSystemValue('installed', false)) { |
| 60 | - $application->add(new OC\Core\Command\App\Disable(\OC::$server->getAppManager())); |
|
| 61 | - $application->add(new OC\Core\Command\App\Enable(\OC::$server->getAppManager())); |
|
| 62 | - $application->add(new OC\Core\Command\App\GetPath()); |
|
| 63 | - $application->add(new OC\Core\Command\App\ListApps(\OC::$server->getAppManager())); |
|
| 60 | + $application->add(new OC\Core\Command\App\Disable(\OC::$server->getAppManager())); |
|
| 61 | + $application->add(new OC\Core\Command\App\Enable(\OC::$server->getAppManager())); |
|
| 62 | + $application->add(new OC\Core\Command\App\GetPath()); |
|
| 63 | + $application->add(new OC\Core\Command\App\ListApps(\OC::$server->getAppManager())); |
|
| 64 | 64 | |
| 65 | - $application->add(new OC\Core\Command\TwoFactorAuth\Enable( |
|
| 66 | - \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
| 67 | - )); |
|
| 68 | - $application->add(new OC\Core\Command\TwoFactorAuth\Disable( |
|
| 69 | - \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
| 70 | - )); |
|
| 65 | + $application->add(new OC\Core\Command\TwoFactorAuth\Enable( |
|
| 66 | + \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
| 67 | + )); |
|
| 68 | + $application->add(new OC\Core\Command\TwoFactorAuth\Disable( |
|
| 69 | + \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
| 70 | + )); |
|
| 71 | 71 | |
| 72 | - $application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig())); |
|
| 73 | - $application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig())); |
|
| 74 | - $application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig())); |
|
| 72 | + $application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig())); |
|
| 73 | + $application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig())); |
|
| 74 | + $application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig())); |
|
| 75 | 75 | |
| 76 | - $application->add(new OC\Core\Command\Config\App\DeleteConfig(\OC::$server->getConfig())); |
|
| 77 | - $application->add(new OC\Core\Command\Config\App\GetConfig(\OC::$server->getConfig())); |
|
| 78 | - $application->add(new OC\Core\Command\Config\App\SetConfig(\OC::$server->getConfig())); |
|
| 79 | - $application->add(new OC\Core\Command\Config\Import(\OC::$server->getConfig())); |
|
| 80 | - $application->add(new OC\Core\Command\Config\ListConfigs(\OC::$server->getSystemConfig(), \OC::$server->getAppConfig())); |
|
| 81 | - $application->add(new OC\Core\Command\Config\System\DeleteConfig(\OC::$server->getSystemConfig())); |
|
| 82 | - $application->add(new OC\Core\Command\Config\System\GetConfig(\OC::$server->getSystemConfig())); |
|
| 83 | - $application->add(new OC\Core\Command\Config\System\SetConfig(\OC::$server->getSystemConfig())); |
|
| 76 | + $application->add(new OC\Core\Command\Config\App\DeleteConfig(\OC::$server->getConfig())); |
|
| 77 | + $application->add(new OC\Core\Command\Config\App\GetConfig(\OC::$server->getConfig())); |
|
| 78 | + $application->add(new OC\Core\Command\Config\App\SetConfig(\OC::$server->getConfig())); |
|
| 79 | + $application->add(new OC\Core\Command\Config\Import(\OC::$server->getConfig())); |
|
| 80 | + $application->add(new OC\Core\Command\Config\ListConfigs(\OC::$server->getSystemConfig(), \OC::$server->getAppConfig())); |
|
| 81 | + $application->add(new OC\Core\Command\Config\System\DeleteConfig(\OC::$server->getSystemConfig())); |
|
| 82 | + $application->add(new OC\Core\Command\Config\System\GetConfig(\OC::$server->getSystemConfig())); |
|
| 83 | + $application->add(new OC\Core\Command\Config\System\SetConfig(\OC::$server->getSystemConfig())); |
|
| 84 | 84 | |
| 85 | - $application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig()))); |
|
| 86 | - $application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->getLogger())); |
|
| 87 | - $application->add(new OC\Core\Command\Db\Migrations\StatusCommand(\OC::$server->getDatabaseConnection())); |
|
| 88 | - $application->add(new OC\Core\Command\Db\Migrations\MigrateCommand(\OC::$server->getDatabaseConnection())); |
|
| 89 | - $application->add(new OC\Core\Command\Db\Migrations\GenerateCommand(\OC::$server->getDatabaseConnection())); |
|
| 90 | - $application->add(new OC\Core\Command\Db\Migrations\GenerateFromSchemaFileCommand(\OC::$server->getConfig(), \OC::$server->getAppManager(), \OC::$server->getDatabaseConnection())); |
|
| 91 | - $application->add(new OC\Core\Command\Db\Migrations\ExecuteCommand(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig())); |
|
| 85 | + $application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig()))); |
|
| 86 | + $application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->getLogger())); |
|
| 87 | + $application->add(new OC\Core\Command\Db\Migrations\StatusCommand(\OC::$server->getDatabaseConnection())); |
|
| 88 | + $application->add(new OC\Core\Command\Db\Migrations\MigrateCommand(\OC::$server->getDatabaseConnection())); |
|
| 89 | + $application->add(new OC\Core\Command\Db\Migrations\GenerateCommand(\OC::$server->getDatabaseConnection())); |
|
| 90 | + $application->add(new OC\Core\Command\Db\Migrations\GenerateFromSchemaFileCommand(\OC::$server->getConfig(), \OC::$server->getAppManager(), \OC::$server->getDatabaseConnection())); |
|
| 91 | + $application->add(new OC\Core\Command\Db\Migrations\ExecuteCommand(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig())); |
|
| 92 | 92 | |
| 93 | - $application->add(new OC\Core\Command\Encryption\Disable(\OC::$server->getConfig())); |
|
| 94 | - $application->add(new OC\Core\Command\Encryption\Enable(\OC::$server->getConfig(), \OC::$server->getEncryptionManager())); |
|
| 95 | - $application->add(new OC\Core\Command\Encryption\ListModules(\OC::$server->getEncryptionManager())); |
|
| 96 | - $application->add(new OC\Core\Command\Encryption\SetDefaultModule(\OC::$server->getEncryptionManager())); |
|
| 97 | - $application->add(new OC\Core\Command\Encryption\Status(\OC::$server->getEncryptionManager())); |
|
| 98 | - $application->add(new OC\Core\Command\Encryption\EncryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getAppManager(), \OC::$server->getConfig(), new \Symfony\Component\Console\Helper\QuestionHelper())); |
|
| 99 | - $application->add(new OC\Core\Command\Encryption\DecryptAll( |
|
| 100 | - \OC::$server->getEncryptionManager(), |
|
| 101 | - \OC::$server->getAppManager(), |
|
| 102 | - \OC::$server->getConfig(), |
|
| 103 | - new \OC\Encryption\DecryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getUserManager(), new \OC\Files\View()), |
|
| 104 | - new \Symfony\Component\Console\Helper\QuestionHelper()) |
|
| 105 | - ); |
|
| 93 | + $application->add(new OC\Core\Command\Encryption\Disable(\OC::$server->getConfig())); |
|
| 94 | + $application->add(new OC\Core\Command\Encryption\Enable(\OC::$server->getConfig(), \OC::$server->getEncryptionManager())); |
|
| 95 | + $application->add(new OC\Core\Command\Encryption\ListModules(\OC::$server->getEncryptionManager())); |
|
| 96 | + $application->add(new OC\Core\Command\Encryption\SetDefaultModule(\OC::$server->getEncryptionManager())); |
|
| 97 | + $application->add(new OC\Core\Command\Encryption\Status(\OC::$server->getEncryptionManager())); |
|
| 98 | + $application->add(new OC\Core\Command\Encryption\EncryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getAppManager(), \OC::$server->getConfig(), new \Symfony\Component\Console\Helper\QuestionHelper())); |
|
| 99 | + $application->add(new OC\Core\Command\Encryption\DecryptAll( |
|
| 100 | + \OC::$server->getEncryptionManager(), |
|
| 101 | + \OC::$server->getAppManager(), |
|
| 102 | + \OC::$server->getConfig(), |
|
| 103 | + new \OC\Encryption\DecryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getUserManager(), new \OC\Files\View()), |
|
| 104 | + new \Symfony\Component\Console\Helper\QuestionHelper()) |
|
| 105 | + ); |
|
| 106 | 106 | |
| 107 | - $application->add(new OC\Core\Command\Log\Manage(\OC::$server->getConfig())); |
|
| 108 | - $application->add(new OC\Core\Command\Log\File(\OC::$server->getConfig())); |
|
| 107 | + $application->add(new OC\Core\Command\Log\Manage(\OC::$server->getConfig())); |
|
| 108 | + $application->add(new OC\Core\Command\Log\File(\OC::$server->getConfig())); |
|
| 109 | 109 | |
| 110 | - $view = new \OC\Files\View(); |
|
| 111 | - $util = new \OC\Encryption\Util( |
|
| 112 | - $view, |
|
| 113 | - \OC::$server->getUserManager(), |
|
| 114 | - \OC::$server->getGroupManager(), |
|
| 115 | - \OC::$server->getConfig() |
|
| 116 | - ); |
|
| 117 | - $application->add(new OC\Core\Command\Encryption\ChangeKeyStorageRoot( |
|
| 118 | - $view, |
|
| 119 | - \OC::$server->getUserManager(), |
|
| 120 | - \OC::$server->getConfig(), |
|
| 121 | - $util, |
|
| 122 | - new \Symfony\Component\Console\Helper\QuestionHelper() |
|
| 123 | - ) |
|
| 124 | - ); |
|
| 125 | - $application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util)); |
|
| 110 | + $view = new \OC\Files\View(); |
|
| 111 | + $util = new \OC\Encryption\Util( |
|
| 112 | + $view, |
|
| 113 | + \OC::$server->getUserManager(), |
|
| 114 | + \OC::$server->getGroupManager(), |
|
| 115 | + \OC::$server->getConfig() |
|
| 116 | + ); |
|
| 117 | + $application->add(new OC\Core\Command\Encryption\ChangeKeyStorageRoot( |
|
| 118 | + $view, |
|
| 119 | + \OC::$server->getUserManager(), |
|
| 120 | + \OC::$server->getConfig(), |
|
| 121 | + $util, |
|
| 122 | + new \Symfony\Component\Console\Helper\QuestionHelper() |
|
| 123 | + ) |
|
| 124 | + ); |
|
| 125 | + $application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util)); |
|
| 126 | 126 | |
| 127 | - $application->add(new OC\Core\Command\Maintenance\DataFingerprint(\OC::$server->getConfig(), new \OC\AppFramework\Utility\TimeFactory())); |
|
| 128 | - $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader())); |
|
| 129 | - $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); |
|
| 130 | - $application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig())); |
|
| 131 | - $application->add(new OC\Core\Command\Maintenance\UpdateHtaccess()); |
|
| 132 | - $application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->getMimeTypeDetector(), \OC::$server->getMemCacheFactory())); |
|
| 127 | + $application->add(new OC\Core\Command\Maintenance\DataFingerprint(\OC::$server->getConfig(), new \OC\AppFramework\Utility\TimeFactory())); |
|
| 128 | + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader())); |
|
| 129 | + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); |
|
| 130 | + $application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig())); |
|
| 131 | + $application->add(new OC\Core\Command\Maintenance\UpdateHtaccess()); |
|
| 132 | + $application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->getMimeTypeDetector(), \OC::$server->getMemCacheFactory())); |
|
| 133 | 133 | |
| 134 | - $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger())); |
|
| 135 | - $application->add(new OC\Core\Command\Maintenance\Repair( |
|
| 136 | - new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(), |
|
| 137 | - \OC::$server->getEventDispatcher())); |
|
| 134 | + $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger())); |
|
| 135 | + $application->add(new OC\Core\Command\Maintenance\Repair( |
|
| 136 | + new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(), |
|
| 137 | + \OC::$server->getEventDispatcher())); |
|
| 138 | 138 | |
| 139 | - $application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 140 | - $application->add(new OC\Core\Command\User\Delete(\OC::$server->getUserManager())); |
|
| 141 | - $application->add(new OC\Core\Command\User\Disable(\OC::$server->getUserManager())); |
|
| 142 | - $application->add(new OC\Core\Command\User\Enable(\OC::$server->getUserManager())); |
|
| 143 | - $application->add(new OC\Core\Command\User\LastSeen(\OC::$server->getUserManager())); |
|
| 144 | - $application->add(new OC\Core\Command\User\Report(\OC::$server->getUserManager())); |
|
| 145 | - $application->add(new OC\Core\Command\User\ResetPassword(\OC::$server->getUserManager())); |
|
| 146 | - $application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig(), \OC::$server->getDatabaseConnection())); |
|
| 147 | - $application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager())); |
|
| 148 | - $application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 139 | + $application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 140 | + $application->add(new OC\Core\Command\User\Delete(\OC::$server->getUserManager())); |
|
| 141 | + $application->add(new OC\Core\Command\User\Disable(\OC::$server->getUserManager())); |
|
| 142 | + $application->add(new OC\Core\Command\User\Enable(\OC::$server->getUserManager())); |
|
| 143 | + $application->add(new OC\Core\Command\User\LastSeen(\OC::$server->getUserManager())); |
|
| 144 | + $application->add(new OC\Core\Command\User\Report(\OC::$server->getUserManager())); |
|
| 145 | + $application->add(new OC\Core\Command\User\ResetPassword(\OC::$server->getUserManager())); |
|
| 146 | + $application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig(), \OC::$server->getDatabaseConnection())); |
|
| 147 | + $application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager())); |
|
| 148 | + $application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 149 | 149 | |
| 150 | - $application->add(new OC\Core\Command\Group\ListCommand(\OC::$server->getGroupManager())); |
|
| 151 | - $application->add(new OC\Core\Command\Group\AddUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 152 | - $application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 150 | + $application->add(new OC\Core\Command\Group\ListCommand(\OC::$server->getGroupManager())); |
|
| 151 | + $application->add(new OC\Core\Command\Group\AddUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 152 | + $application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
| 153 | 153 | |
| 154 | - $application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(null), \OC::$server->getL10N('core'))); |
|
| 155 | - $application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager(null))); |
|
| 156 | - $application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager(null))); |
|
| 154 | + $application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(null), \OC::$server->getL10N('core'))); |
|
| 155 | + $application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager(null))); |
|
| 156 | + $application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager(null))); |
|
| 157 | 157 | } else { |
| 158 | - $application->add(new OC\Core\Command\Maintenance\Install(\OC::$server->getSystemConfig())); |
|
| 158 | + $application->add(new OC\Core\Command\Maintenance\Install(\OC::$server->getSystemConfig())); |
|
| 159 | 159 | } |
@@ -37,163 +37,163 @@ |
||
| 37 | 37 | |
| 38 | 38 | class GenerateFromSchemaFileCommand extends GenerateCommand { |
| 39 | 39 | |
| 40 | - /** @var IConfig */ |
|
| 41 | - protected $config; |
|
| 40 | + /** @var IConfig */ |
|
| 41 | + protected $config; |
|
| 42 | 42 | |
| 43 | - /** @var IAppManager */ |
|
| 44 | - protected $appManager; |
|
| 43 | + /** @var IAppManager */ |
|
| 44 | + protected $appManager; |
|
| 45 | 45 | |
| 46 | - public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
| 47 | - parent::__construct($connection); |
|
| 48 | - $this->config = $config; |
|
| 49 | - $this->appManager = $appManager; |
|
| 50 | - } |
|
| 46 | + public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
| 47 | + parent::__construct($connection); |
|
| 48 | + $this->config = $config; |
|
| 49 | + $this->appManager = $appManager; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | |
| 53 | - protected function configure() { |
|
| 54 | - parent::configure(); |
|
| 53 | + protected function configure() { |
|
| 54 | + parent::configure(); |
|
| 55 | 55 | |
| 56 | - $this->setName('migrations:generate-from-schema'); |
|
| 57 | - } |
|
| 56 | + $this->setName('migrations:generate-from-schema'); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
| 60 | - $appName = $input->getArgument('app'); |
|
| 61 | - $version = $input->getArgument('version'); |
|
| 59 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
| 60 | + $appName = $input->getArgument('app'); |
|
| 61 | + $version = $input->getArgument('version'); |
|
| 62 | 62 | |
| 63 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 64 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 65 | - return 1; |
|
| 66 | - } |
|
| 63 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 64 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 65 | + return 1; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
| 69 | - if (!file_exists($schemaFile)) { |
|
| 70 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
| 71 | - return 2; |
|
| 72 | - } |
|
| 68 | + $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
| 69 | + if (!file_exists($schemaFile)) { |
|
| 70 | + $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
| 71 | + return 2; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
| 75 | - $schema = new Schema(); |
|
| 76 | - $reader->loadSchemaFromFile($schemaFile, $schema); |
|
| 74 | + $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
| 75 | + $schema = new Schema(); |
|
| 76 | + $reader->loadSchemaFromFile($schemaFile, $schema); |
|
| 77 | 77 | |
| 78 | - $schemaBody = $this->schemaToMigration($schema); |
|
| 78 | + $schemaBody = $this->schemaToMigration($schema); |
|
| 79 | 79 | |
| 80 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 80 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 81 | 81 | |
| 82 | - $date = date('YmdHis'); |
|
| 83 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
| 82 | + $date = date('YmdHis'); |
|
| 83 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
| 84 | 84 | |
| 85 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 86 | - return 0; |
|
| 87 | - } |
|
| 85 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 86 | + return 0; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @param Schema $schema |
|
| 91 | - * @return string |
|
| 92 | - */ |
|
| 93 | - protected function schemaToMigration(Schema $schema) { |
|
| 94 | - $content = <<<'EOT' |
|
| 89 | + /** |
|
| 90 | + * @param Schema $schema |
|
| 91 | + * @return string |
|
| 92 | + */ |
|
| 93 | + protected function schemaToMigration(Schema $schema) { |
|
| 94 | + $content = <<<'EOT' |
|
| 95 | 95 | /** @var Schema $schema */ |
| 96 | 96 | $schema = $schemaClosure(); |
| 97 | 97 | |
| 98 | 98 | EOT; |
| 99 | 99 | |
| 100 | - foreach ($schema->getTables() as $table) { |
|
| 101 | - $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
| 100 | + foreach ($schema->getTables() as $table) { |
|
| 101 | + $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
| 102 | 102 | |
| 103 | 103 | if (!$schema->hasTable('{{table-name}}')) { |
| 104 | 104 | $table = $schema->createTable('{{table-name}}'); |
| 105 | 105 | |
| 106 | 106 | EOT |
| 107 | - ); |
|
| 107 | + ); |
|
| 108 | 108 | |
| 109 | - foreach ($table->getColumns() as $column) { |
|
| 110 | - $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
| 109 | + foreach ($table->getColumns() as $column) { |
|
| 110 | + $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
| 111 | 111 | $table->addColumn('{{name}}', '{{type}}', [ |
| 112 | 112 | |
| 113 | 113 | EOT |
| 114 | - ); |
|
| 115 | - if ($column->getAutoincrement()) { |
|
| 116 | - $content .= <<<'EOT' |
|
| 114 | + ); |
|
| 115 | + if ($column->getAutoincrement()) { |
|
| 116 | + $content .= <<<'EOT' |
|
| 117 | 117 | 'autoincrement' => true, |
| 118 | 118 | |
| 119 | 119 | EOT; |
| 120 | - } |
|
| 121 | - $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
| 120 | + } |
|
| 121 | + $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
| 122 | 122 | 'notnull' => {{notnull}}, |
| 123 | 123 | |
| 124 | 124 | EOT |
| 125 | - ); |
|
| 126 | - if ($column->getLength() !== null) { |
|
| 127 | - $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
| 125 | + ); |
|
| 126 | + if ($column->getLength() !== null) { |
|
| 127 | + $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
| 128 | 128 | 'length' => {{length}}, |
| 129 | 129 | |
| 130 | 130 | EOT |
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - $default = $column->getDefault(); |
|
| 134 | - if ($default !== null) { |
|
| 135 | - $default = is_numeric($default) ? $default : "'$default'"; |
|
| 136 | - $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + $default = $column->getDefault(); |
|
| 134 | + if ($default !== null) { |
|
| 135 | + $default = is_numeric($default) ? $default : "'$default'"; |
|
| 136 | + $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
| 137 | 137 | 'default' => {{default}}, |
| 138 | 138 | |
| 139 | 139 | EOT |
| 140 | - ); |
|
| 141 | - } |
|
| 142 | - $content .= <<<'EOT' |
|
| 140 | + ); |
|
| 141 | + } |
|
| 142 | + $content .= <<<'EOT' |
|
| 143 | 143 | ]); |
| 144 | 144 | |
| 145 | 145 | EOT; |
| 146 | - } |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - $content .= <<<'EOT' |
|
| 148 | + $content .= <<<'EOT' |
|
| 149 | 149 | |
| 150 | 150 | EOT; |
| 151 | 151 | |
| 152 | - $primaryKey = $table->getPrimaryKey(); |
|
| 153 | - if ($primaryKey !== null) { |
|
| 154 | - $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
| 152 | + $primaryKey = $table->getPrimaryKey(); |
|
| 153 | + if ($primaryKey !== null) { |
|
| 154 | + $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
| 155 | 155 | $table->setPrimaryKey(['{{columns}}']); |
| 156 | 156 | |
| 157 | 157 | EOT |
| 158 | - ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - foreach ($table->getIndexes() as $index) { |
|
| 162 | - if ($index->isPrimary()) { |
|
| 163 | - continue; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if ($index->isUnique()) { |
|
| 167 | - $content .= str_replace( |
|
| 168 | - ['{{columns}}', '{{name}}'], |
|
| 169 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 170 | - <<<'EOT' |
|
| 158 | + ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + foreach ($table->getIndexes() as $index) { |
|
| 162 | + if ($index->isPrimary()) { |
|
| 163 | + continue; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if ($index->isUnique()) { |
|
| 167 | + $content .= str_replace( |
|
| 168 | + ['{{columns}}', '{{name}}'], |
|
| 169 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 170 | + <<<'EOT' |
|
| 171 | 171 | $table->addUniqueIndex(['{{columns}}'], '{{name}}'); |
| 172 | 172 | |
| 173 | 173 | EOT |
| 174 | - ); |
|
| 175 | - } else { |
|
| 176 | - $content .= str_replace( |
|
| 177 | - ['{{columns}}', '{{name}}'], |
|
| 178 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 179 | - <<<'EOT' |
|
| 174 | + ); |
|
| 175 | + } else { |
|
| 176 | + $content .= str_replace( |
|
| 177 | + ['{{columns}}', '{{name}}'], |
|
| 178 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
| 179 | + <<<'EOT' |
|
| 180 | 180 | $table->addIndex(['{{columns}}'], '{{name}}'); |
| 181 | 181 | |
| 182 | 182 | EOT |
| 183 | - ); |
|
| 184 | - } |
|
| 185 | - } |
|
| 183 | + ); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - $content .= <<<'EOT' |
|
| 187 | + $content .= <<<'EOT' |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | EOT; |
| 191 | - } |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - $content .= <<<'EOT' |
|
| 193 | + $content .= <<<'EOT' |
|
| 194 | 194 | return $schema; |
| 195 | 195 | EOT; |
| 196 | 196 | |
| 197 | - return $content; |
|
| 198 | - } |
|
| 197 | + return $content; |
|
| 198 | + } |
|
| 199 | 199 | } |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | $appName = $input->getArgument('app'); |
| 61 | 61 | $version = $input->getArgument('version'); |
| 62 | 62 | |
| 63 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 63 | + if (!preg_match('/^\d{1,16}$/', $version)) { |
|
| 64 | 64 | $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
| 65 | 65 | return 1; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
| 68 | + $schemaFile = $this->appManager->getAppPath($appName).'/appinfo/database.xml'; |
|
| 69 | 69 | if (!file_exists($schemaFile)) { |
| 70 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
| 70 | + $output->writeln('<error>App '.$appName.' does not have a database.xml file</error>'); |
|
| 71 | 71 | return 2; |
| 72 | 72 | } |
| 73 | 73 | |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
| 81 | 81 | |
| 82 | 82 | $date = date('YmdHis'); |
| 83 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
| 83 | + $path = $this->generateMigration($ms, 'Version'.$version.'Date'.$date, $schemaBody); |
|
| 84 | 84 | |
| 85 | 85 | $output->writeln("New migration class has been generated to <info>$path</info>"); |
| 86 | 86 | return 0; |