aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2014-10-27 12:51:26 +0100
committerLukas Reschke <lukas@owncloud.com>2014-10-27 22:27:57 +0100
commit9b908c3d058ad28d14787bd4b72978d0e7aa81d2 (patch)
tree2e4001a03da93373e5bed1a7aa7f62499f265b8f
parentdbfe5ef28acfc54c4f025b7fa1427113c961f5cf (diff)
downloadnextcloud-server-9b908c3d058ad28d14787bd4b72978d0e7aa81d2.tar.gz
nextcloud-server-9b908c3d058ad28d14787bd4b72978d0e7aa81d2.zip
Make supported DBs configurable within config.php
This commit will make the supported DBs for installation configurable within config.php. By default the following databases are tested: "sqlite", "mysql", "pgsql". The reason behind this is that there might be instances where we want to prevent SQLite to be used by mistake. To test this play around with the new configuration parameter "supportedDatabases". Conflicts: lib/private/util.php
-rw-r--r--config/config.sample.php17
-rw-r--r--core/setup/controller.php49
-rw-r--r--lib/base.php2
-rw-r--r--lib/private/setup.php103
-rw-r--r--lib/private/util.php8
-rw-r--r--tests/lib/setup.php103
6 files changed, 248 insertions, 34 deletions
diff --git a/config/config.sample.php b/config/config.sample.php
index 9b70ede8047..54958ce73c6 100644
--- a/config/config.sample.php
+++ b/config/config.sample.php
@@ -797,6 +797,23 @@ $CONFIG = array(
),
/**
+ * Database types that are supported for installation
+ * Available:
+ * - sqlite (SQLite3)
+ * - mysql (MySQL)
+ * - pgsql (PostgreSQL)
+ * - oci (Oracle)
+ * - mssql (Microsoft SQL Server)
+ */
+'supportedDatabases' => array(
+ 'sqlite',
+ 'mysql',
+ 'pgsql',
+ 'oci',
+ 'mssql'
+),
+
+/**
* Custom CSP policy, changing this will overwrite the standard policy
*/
'custom_csp_policy' =>
diff --git a/core/setup/controller.php b/core/setup/controller.php
index 353271902e6..ffd95d8b373 100644
--- a/core/setup/controller.php
+++ b/core/setup/controller.php
@@ -1,6 +1,7 @@
<?php
/**
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
+ * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
@@ -8,7 +9,19 @@
namespace OC\Core\Setup;
+use OCP\IConfig;
+
class Controller {
+ /** @var \OCP\IConfig */
+ protected $config;
+
+ /**
+ * @param IConfig $config
+ */
+ function __construct(IConfig $config) {
+ $this->config = $config;
+ }
+
public function run($post) {
// Check for autosetup:
$post = $this->loadAutoConfig($post);
@@ -87,28 +100,10 @@ class Controller {
* in case of errors/warnings
*/
public function getSystemInfo() {
- $hasSQLite = class_exists('SQLite3');
- $hasMySQL = is_callable('mysql_connect');
- $hasPostgreSQL = is_callable('pg_connect');
- $hasOracle = is_callable('oci_connect');
- $hasMSSQL = is_callable('sqlsrv_connect');
- $databases = array();
- if ($hasSQLite) {
- $databases['sqlite'] = 'SQLite';
- }
- if ($hasMySQL) {
- $databases['mysql'] = 'MySQL/MariaDB';
- }
- if ($hasPostgreSQL) {
- $databases['pgsql'] = 'PostgreSQL';
- }
- if ($hasOracle) {
- $databases['oci'] = 'Oracle';
- }
- if ($hasMSSQL) {
- $databases['mssql'] = 'MS SQL';
- }
- $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT.'/data');
+ $setup = new \OC_Setup($this->config);
+ $databases = $setup->getSupportedDatabases();
+
+ $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data');
$vulnerableToNullByte = false;
if(@file_exists(__FILE__."\0Nullbyte")) { // Check if the used PHP version is vulnerable to the NULL Byte attack (CVE-2006-7243)
$vulnerableToNullByte = true;
@@ -150,11 +145,11 @@ class Controller {
}
return array(
- 'hasSQLite' => $hasSQLite,
- 'hasMySQL' => $hasMySQL,
- 'hasPostgreSQL' => $hasPostgreSQL,
- 'hasOracle' => $hasOracle,
- 'hasMSSQL' => $hasMSSQL,
+ 'hasSQLite' => isset($databases['sqlite']),
+ 'hasMySQL' => isset($databases['mysql']),
+ 'hasPostgreSQL' => isset($databases['postgre']),
+ 'hasOracle' => isset($databases['oci']),
+ 'hasMSSQL' => isset($databases['mssql']),
'databases' => $databases,
'directory' => $datadir,
'secureRNG' => \OC_Util::secureRNGAvailable(),
diff --git a/lib/base.php b/lib/base.php
index 7335be037b8..5337fa9c9f7 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -681,7 +681,7 @@ class OC {
// Check if ownCloud is installed or in maintenance (update) mode
if (!OC_Config::getValue('installed', false)) {
- $controller = new OC\Core\Setup\Controller();
+ $controller = new OC\Core\Setup\Controller(\OC::$server->getConfig());
$controller->run($_POST);
exit();
}
diff --git a/lib/private/setup.php b/lib/private/setup.php
index 2ad569cb483..1a3ae584abb 100644
--- a/lib/private/setup.php
+++ b/lib/private/setup.php
@@ -1,9 +1,27 @@
<?php
+/**
+ * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+use OCP\IConfig;
class DatabaseSetupException extends \OC\HintException {
}
class OC_Setup {
+ /** @var IConfig */
+ protected $config;
+
+ /**
+ * @param IConfig $config
+ */
+ function __construct(IConfig $config) {
+ $this->config = $config;
+ }
+
static $dbSetupClasses = array(
'mysql' => '\OC\Setup\MySQL',
'pgsql' => '\OC\Setup\PostgreSQL',
@@ -13,10 +31,93 @@ class OC_Setup {
'sqlite3' => '\OC\Setup\Sqlite',
);
+ /**
+ * @return OC_L10N
+ */
public static function getTrans(){
return OC_L10N::get('lib');
}
+ /**
+ * Wrapper around the "class_exists" PHP function to be able to mock it
+ * @param string $name
+ * @return bool
+ */
+ public function class_exists($name) {
+ return class_exists($name);
+ }
+
+ /**
+ * Wrapper around the "is_callable" PHP function to be able to mock it
+ * @param string $name
+ * @return bool
+ */
+ public function is_callable($name) {
+ return is_callable($name);
+ }
+
+ /**
+ * Get the available and supported databases of this instance
+ *
+ * @throws Exception
+ * @return array
+ */
+ public function getSupportedDatabases() {
+ $availableDatabases = array(
+ 'sqlite' => array(
+ 'type' => 'class',
+ 'call' => 'SQLite3',
+ 'name' => 'SQLite'
+ ),
+ 'mysql' => array(
+ 'type' => 'function',
+ 'call' => 'mysql_connect',
+ 'name' => 'MySQL/MariaDB'
+ ),
+ 'pgsql' => array(
+ 'type' => 'function',
+ 'call' => 'oci_connect',
+ 'name' => 'PostgreSQL'
+ ),
+ 'oci' => array(
+ 'type' => 'function',
+ 'call' => 'oci_connect',
+ 'name' => 'Oracle'
+ ),
+ 'mssql' => array(
+ 'type' => 'function',
+ 'call' => 'sqlsrv_connect',
+ 'name' => 'MS SQL'
+ )
+ );
+ $configuredDatabases = $this->config->getSystemValue('supportedDatabases', array('sqlite', 'mysql', 'pgsql', 'oci', 'mssql'));
+ if(!is_array($configuredDatabases)) {
+ throw new Exception('Supported databases are not properly configured.');
+ }
+
+ $supportedDatabases = array();
+
+ foreach($configuredDatabases as $database) {
+ if(array_key_exists($database, $availableDatabases)) {
+ $working = false;
+ if($availableDatabases[$database]['type'] === 'class') {
+ $working = $this->class_exists($availableDatabases[$database]['call']);
+ } elseif ($availableDatabases[$database]['type'] === 'function') {
+ $working = $this->is_callable($availableDatabases[$database]['call']);
+ }
+ if($working) {
+ $supportedDatabases[$database] = $availableDatabases[$database]['name'];
+ }
+ }
+ }
+
+ return $supportedDatabases;
+ }
+
+ /**
+ * @param $options
+ * @return array
+ */
public static function install($options) {
$l = self::getTrans();
@@ -59,7 +160,7 @@ class OC_Setup {
}
//no errors, good
- if( isset($options['trusted_domains'])
+ if(isset($options['trusted_domains'])
&& is_array($options['trusted_domains'])) {
$trustedDomains = $options['trusted_domains'];
} else {
diff --git a/lib/private/util.php b/lib/private/util.php
index 502cf8bc0ac..08de46bbfea 100644
--- a/lib/private/util.php
+++ b/lib/private/util.php
@@ -416,11 +416,9 @@ class OC_Util {
}
$webServerRestart = false;
- //check for database drivers
- if(!(is_callable('sqlite_open') or class_exists('SQLite3'))
- and !is_callable('mysql_connect')
- and !is_callable('pg_connect')
- and !is_callable('oci_connect')) {
+ $setup = new OC_Setup($config);
+ $availableDatabases = $setup->getSupportedDatabases();
+ if (empty($availableDatabases)) {
$errors[] = array(
'error'=> $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'),
'hint'=>'' //TODO: sane hint
diff --git a/tests/lib/setup.php b/tests/lib/setup.php
new file mode 100644
index 00000000000..2c1569dd800
--- /dev/null
+++ b/tests/lib/setup.php
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+use OCP\IConfig;
+
+class Test_OC_Setup extends PHPUnit_Framework_TestCase {
+
+ /** @var IConfig */
+ protected $config;
+ /** @var \OC_Setup */
+ protected $setupClass;
+
+ public function setUp() {
+ $this->config = $this->getMock('\OCP\IConfig');
+ $this->setupClass = $this->getMock('\OC_Setup', array('class_exists', 'is_callable'), array($this->config));
+ }
+
+ public function testGetSupportedDatabasesWithOneWorking() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->will($this->returnValue(
+ array('sqlite', 'mysql', 'oci')
+ ));
+ $this->setupClass
+ ->expects($this->once())
+ ->method('class_exists')
+ ->will($this->returnValue(true));
+ $this->setupClass
+ ->expects($this->exactly(2))
+ ->method('is_callable')
+ ->will($this->returnValue(false));
+ $result = $this->setupClass->getSupportedDatabases();
+ $expectedResult = array(
+ 'sqlite' => 'SQLite'
+ );
+
+ $this->assertSame($expectedResult, $result);
+ }
+
+ public function testGetSupportedDatabasesWithNoWorking() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->will($this->returnValue(
+ array('sqlite', 'mysql', 'oci', 'pgsql')
+ ));
+ $this->setupClass
+ ->expects($this->once())
+ ->method('class_exists')
+ ->will($this->returnValue(false));
+ $this->setupClass
+ ->expects($this->exactly(3))
+ ->method('is_callable')
+ ->will($this->returnValue(false));
+ $result = $this->setupClass->getSupportedDatabases();
+
+ $this->assertSame(array(), $result);
+ }
+
+ public function testGetSupportedDatabasesWitAllWorking() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->will($this->returnValue(
+ array('sqlite', 'mysql', 'pgsql', 'oci', 'mssql')
+ ));
+ $this->setupClass
+ ->expects($this->once())
+ ->method('class_exists')
+ ->will($this->returnValue(true));
+ $this->setupClass
+ ->expects($this->exactly(4))
+ ->method('is_callable')
+ ->will($this->returnValue(true));
+ $result = $this->setupClass->getSupportedDatabases();
+ $expectedResult = array(
+ 'sqlite' => 'SQLite',
+ 'mysql' => 'MySQL/MariaDB',
+ 'pgsql' => 'PostgreSQL',
+ 'oci' => 'Oracle',
+ 'mssql' => 'MS SQL'
+ );
+ $this->assertSame($expectedResult, $result);
+ }
+
+ /**
+ * @expectedException \Exception
+ * @expectedExceptionMessage Supported databases are not properly configured.
+ */
+ public function testGetSupportedDatabaseException() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->will($this->returnValue('NotAnArray'));
+ $this->setupClass->getSupportedDatabases();
+ }
+} \ No newline at end of file