]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use db setup class for option validation
authorBart Visscher <bartv@thisnet.nl>
Wed, 3 Apr 2013 15:52:18 +0000 (17:52 +0200)
committerBart Visscher <bartv@thisnet.nl>
Sat, 29 Jun 2013 19:15:05 +0000 (21:15 +0200)
lib/setup.php
lib/setup/abstractdatabase.php
lib/setup/mssql.php
lib/setup/mysql.php
lib/setup/oci.php
lib/setup/postgresql.php
lib/setup/sqlite.php

index e73ba6cdf4ff04c991b804ec61464218d8eab73d..11c6cc76b66bfdc4bf7ac15c0f72fdc6c202f35f 100644 (file)
@@ -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(
index cef64ee7ebbe9a4ed009d7e15ec8baf9ef248c7e..07a679f211227b9d0c4f58eb2655e1a4318ee219 100644 (file)
@@ -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);
index a1414a9ac573fd2a6d56090fd2e613bf9e2f92b1..74ac8f294a9817c9a7667910f1c3c1e736aaa357 100644 (file)
@@ -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);
index 790b8e82257d9d6b8e1914fbdfaa006fcd1958cf..aa0344f686ccfc7628d26e63197fd39d70206c34 100644 (file)
@@ -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);
index 3bb625c557d3f426b843a04166c5809b6deb3b4b..a3e2ccc8da7cc37db04113265950c18327676b41 100644 (file)
@@ -3,6 +3,8 @@
 namespace OC\Setup;
 
 class OCI extends AbstractDatabase {
+       public $dbprettyname = 'Oracle';
+
        protected $dbtablespace;
 
        public function initialize($config) {
index 3fb1e6b878768da05cadcc5d271c139c8eb425b3..67e4f901a9191ab53abdd17d639ad7fec4921120 100644 (file)
@@ -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);
index b1785ce1b0bdce6bf7415efae05aaad1a2f03911..0b96ec6000930dab5cbc265c44c8258dcdb95774 100644 (file)
@@ -3,6 +3,11 @@
 namespace OC\Setup;
 
 class Sqlite extends AbstractDatabase {
+       public $dbprettyname = 'Sqlite';
+
+       public function validate($config) {
+       }
+
        public function initialize($config) {
        }