From 6831b78597ae38ed03343e274448dfb210af817b Mon Sep 17 00:00:00 2001
From: Bart Visscher <bartv@thisnet.nl>
Date: Wed, 3 Apr 2013 17:52:18 +0200
Subject: [PATCH] Use db setup class for option validation

---
 lib/setup.php                  | 48 +++++++++++-----------------------
 lib/setup/abstractdatabase.php | 17 ++++++++++--
 lib/setup/mssql.php            |  2 ++
 lib/setup/mysql.php            |  2 ++
 lib/setup/oci.php              |  2 ++
 lib/setup/postgresql.php       |  2 ++
 lib/setup/sqlite.php           |  5 ++++
 7 files changed, 43 insertions(+), 35 deletions(-)

diff --git a/lib/setup.php b/lib/setup.php
index e73ba6cdf4..11c6cc76b6 100644
--- a/lib/setup.php
+++ b/lib/setup.php
@@ -19,6 +19,14 @@ class DatabaseSetupException extends Exception
 }
 
 class OC_Setup {
+	static $db_setup_classes = array(
+		'mysql' => '\OC\Setup\MySQL',
+		'pgsql' => '\OC\Setup\PostgreSQL',
+		'oci'   => '\OC\Setup\OCI',
+		'mssql' => '\OC\Setup\MSSQL',
+		'sqlite' => '\OC\Setup\Sqlite',
+		'sqlite3' => '\OC\Setup\Sqlite',
+	);
 
 	public static function getTrans(){
 		return OC_L10N::get('lib');
@@ -40,31 +48,14 @@ class OC_Setup {
 			$options['directory'] = OC::$SERVERROOT."/data";
 		}
 
-		if($dbtype == 'mysql' or $dbtype == 'pgsql' or $dbtype == 'oci' or $dbtype == 'mssql') { // these needs more config options
-			if($dbtype == 'mysql')
-				$dbprettyname = 'MySQL';
-			else if($dbtype == 'pgsql')
-				$dbprettyname = 'PostgreSQL';
-			else if ($dbtype == 'mssql')
-				$dbprettyname = 'MS SQL Server';
-			else
-				$dbprettyname = 'Oracle';
-
-
-			if(empty($options['dbuser'])) {
-				$error[] = $l->t("%s enter the database username.", array($dbprettyname));
-			}
-			if(empty($options['dbname'])) {
-				$error[] = $l->t("%s enter the database name.", array($dbprettyname));
-			}
-			if(substr_count($options['dbname'], '.') >= 1) {
-				$error[] = $l->t("%s you may not use dots in the database name", array($dbprettyname));
-			}
-			if($dbtype != 'oci' && empty($options['dbhost'])) {
-				$options['dbhost'] = 'localhost';
-			}
+		if (!isset(self::$db_setup_classes[$dbtype])) {
+			$dbtype = 'sqlite';
 		}
 
+		$class = self::$db_setup_classes[$dbtype];
+		$db_setup = new $class(self::getTrans());
+		$error = array_merge($error, $db_setup->validate($options));
+
 		if(count($error) != 0) {
 			return $error;
 		}
@@ -91,17 +82,8 @@ class OC_Setup {
 		OC_Config::setValue('datadirectory', $datadir);
 		OC_Config::setValue('dbtype', $dbtype);
 		OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
-		$db_setup_classes = array(
-			'mysql' => '\OC\Setup\MySQL',
-			'pgsql' => '\OC\Setup\PostgreSQL',
-			'oci'   => '\OC\Setup\OCI',
-			'mssql' => '\OC\Setup\MSSQL',
-			'sqlite' => '\OC\Setup\Sqlite',
-			'sqlite3' => '\OC\Setup\Sqlite',
-		);
 		try {
-			$class = $db_setup_classes[$dbtype];
-			$db_setup = new $class(self::getTrans(), $options);
+			$db_setup->initialize($options);
 			$db_setup->setupDatabase($username);
 		} catch (DatabaseSetupException $e) {
 			$error[] = array(
diff --git a/lib/setup/abstractdatabase.php b/lib/setup/abstractdatabase.php
index cef64ee7eb..07a679f211 100644
--- a/lib/setup/abstractdatabase.php
+++ b/lib/setup/abstractdatabase.php
@@ -12,14 +12,27 @@ abstract class AbstractDatabase {
 
 	public function __construct($trans, $config) {
 		$this->trans = $trans;
-		$this->initialize($config);
+	}
+
+	public function validate($config) {
+		$errors = array();
+		if(empty($config['dbuser'])) {
+			$errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname));
+		}
+		if(empty($config['dbname'])) {
+			$errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname));
+		}
+		if(substr_count($config['dbname'], '.') >= 1) {
+			$errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname));
+		}
+		return $errors;
 	}
 
 	public function initialize($config) {
 		$dbuser = $config['dbuser'];
 		$dbpass = $config['dbpass'];
 		$dbname = $config['dbname'];
-		$dbhost = isset($config['dbhost']) ? $config['dbhost'] : ''; // dbhost contents is checked earlier
+		$dbhost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
 		$dbtableprefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
 
 		\OC_Config::setValue('dbname', $dbname);
diff --git a/lib/setup/mssql.php b/lib/setup/mssql.php
index a1414a9ac5..74ac8f294a 100644
--- a/lib/setup/mssql.php
+++ b/lib/setup/mssql.php
@@ -3,6 +3,8 @@
 namespace OC\Setup;
 
 class MSSQL extends AbstractDatabase {
+	public $dbprettyname = 'MS SQL Server';
+
 	public function setupDatabase() {
 		//check if the database user has admin right
 		$masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword);
diff --git a/lib/setup/mysql.php b/lib/setup/mysql.php
index 790b8e8225..aa0344f686 100644
--- a/lib/setup/mysql.php
+++ b/lib/setup/mysql.php
@@ -3,6 +3,8 @@
 namespace OC\Setup;
 
 class MySQL extends AbstractDatabase {
+	public $dbprettyname = 'MySQL';
+
 	public function setupDatabase($username) {
 		//check if the database user has admin right
 		$connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword);
diff --git a/lib/setup/oci.php b/lib/setup/oci.php
index 3bb625c557..a3e2ccc8da 100644
--- a/lib/setup/oci.php
+++ b/lib/setup/oci.php
@@ -3,6 +3,8 @@
 namespace OC\Setup;
 
 class OCI extends AbstractDatabase {
+	public $dbprettyname = 'Oracle';
+
 	protected $dbtablespace;
 
 	public function initialize($config) {
diff --git a/lib/setup/postgresql.php b/lib/setup/postgresql.php
index 3fb1e6b878..67e4f901a9 100644
--- a/lib/setup/postgresql.php
+++ b/lib/setup/postgresql.php
@@ -3,6 +3,8 @@
 namespace OC\Setup;
 
 class PostgreSQL extends AbstractDatabase {
+	public $dbprettyname = 'PostgreSQL';
+
 	public function setupDatabase($username) {
 		$e_host = addslashes($this->dbhost);
 		$e_user = addslashes($this->dbuser);
diff --git a/lib/setup/sqlite.php b/lib/setup/sqlite.php
index b1785ce1b0..0b96ec6000 100644
--- a/lib/setup/sqlite.php
+++ b/lib/setup/sqlite.php
@@ -3,6 +3,11 @@
 namespace OC\Setup;
 
 class Sqlite extends AbstractDatabase {
+	public $dbprettyname = 'Sqlite';
+
+	public function validate($config) {
+	}
+
 	public function initialize($config) {
 	}
 
-- 
GitLab