]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add postgresql support
authorSerge Martin <edb@sigluy.net>
Sun, 7 Aug 2011 19:06:53 +0000 (21:06 +0200)
committerSerge Martin <edb@sigluy.net>
Sun, 7 Aug 2011 19:06:53 +0000 (21:06 +0200)
REVIEW: 102101

core/js/setup.js
core/templates/installation.php
lib/db.php
lib/setup.php

index b4616b8b14cf56756c9d4c8ec1753a214406b8b7..3025a511c61cc5234a67b809e1c9be03db228e49 100644 (file)
@@ -1,8 +1,13 @@
 $(document).ready(function() {
        // Hide the MySQL config div if needed :
-       if(!$('#mysql').is(':checked') && $('#hasSQLite').val()=='true') {
+       if(!$('#mysql').is(':checked')) {
                $('#use_mysql').hide();
        }
+       
+       // Hide the PostgreSQL config div if needed:
+       if(!$('#pgsql').is(':checked')) {
+               $('#use_postgresql').hide();
+       }
 
        $('#datadirField').hide(250);
        if($('#hasSQLite').val()=='true'){
@@ -11,10 +16,17 @@ $(document).ready(function() {
 
        $('#sqlite').click(function() {
                $('#use_mysql').slideUp(250);
+               $('#use_postgresql').slideUp(250);
        });
 
        $('#mysql').click(function() {
                $('#use_mysql').slideDown(250);
+               $('#use_postgresql').slideUp(250);
+       });
+       
+       $('#pgsql').click(function() {
+               $('#use_postgresql').slideDown(250);
+               $('#use_mysql').slideUp(250);
        });
 
        $('#showAdvanced').click(function() {
index 8b36d14bbf5da942887db2a27bd54704102970ee..e2392778bedcab2da364714e18a44d977c7c911c 100644 (file)
@@ -39,7 +39,7 @@
                        <legend><?php echo $l->t( 'Configure the database.' ); ?></legend>
                        <?php if($_['hasSQLite']): ?>
                        <input type='hidden' id='hasSQLite' value='true'/>
-                       <?php if(!$_['hasMySQL']): ?>
+                       <?php if(!$_['hasMySQL'] and !$_['hasPostgreSQL']): ?>
                        <p><?php echo $l->t( 'SQLite will be used for the database. You have nothing to do.' ); ?></p>
                        <input type="hidden" id="dbtype" name="dbtype" value="sqlite" />
                        <?php else: ?>
 
                        <?php if($_['hasMySQL']): ?>
                        <input type='hidden' id='hasMySQL' value='true'/>
-                       <?php if(!$_['hasSQLite']): ?>
+                       <?php if(!$_['hasSQLite'] and !$_['hasPostgreSQL']): ?>
                        <p><?php echo $l->t( 'MySQL will be used for the database.' ); ?></p>
                        <input type="hidden" id="dbtype" name="dbtype" value="mysql" />
                        <?php else: ?>
-                       <p><label class="mysql" for="mysql">MySQL </label><input type="radio" name="dbtype" value='mysql' id="mysql" <?php OC_Helper::init_radio('dbtype', 'mysql', 'sqlite'); ?>/></p>
+                       <p><label class="mysql" for="mysql">MySQL </label><input type="radio" name="dbtype" value='mysql' id="mysql" <?php OC_Helper::init_radio('dbtype','pgsql', 'mysql', 'sqlite'); ?>/></p>
                        <?php endif; ?>
                        <div id="use_mysql">
                                <p><label for="dbuser"><?php echo $l->t( 'MySQL username:' ); ?></label><input type="text" name="dbuser" id="dbuser" value="<?php print OC_Helper::init_var('dbuser'); ?>" /></p>
                                
                        </div>
                        <?php endif; ?>
+
+                       <?php if($_['hasPostgreSQL']): ?>
+                       <input type='hidden' id='hasPostgreSQL' value='true'/>
+                       <?php if(!$_['hasSQLite'] and !$_['hasSQLite']): ?>
+                       <p><?php echo $l->t( 'PostgreSQL will be used for the database.' ); ?></p>
+                       <input type="hidden" id="dbtype" name="dbtype" value="pgsql" />
+                       <?php else: ?>
+                       <p><label class="pgsql" for="pgsql">PostgreSQL </label><input type="radio" name="dbtype" value='pgsql' id="pgsql" <?php OC_Helper::init_radio('dbtype','pgsql', 'mysql', 'sqlite'); ?>/></p>
+                       <?php endif; ?>
+                       <div id="use_postgresql">
+                               <p><label for="pg_dbuser"><?php echo $l->t( 'PostgreSQL username:' ); ?></label><input type="text" name="pg_dbuser" id="pg_dbuser" value="<?php print OC_Helper::init_var('dbuser'); ?>" /></p>
+                               <p><label for="pg_dbpass"><?php echo $l->t( 'PostgreSQL password:' ); ?></label><input type="password" name="pg_dbpass" id="pg_dbpass" value="<?php print OC_Helper::init_var('dbpass'); ?>" /></p>
+                               <p><label for="pg_dbname"><?php echo $l->t( 'Database name:' ); ?></label><input type="text" name="pg_dbname" id="pg_dbname" value="<?php print OC_Helper::init_var('dbname'); ?>" /></p>
+                               <p><label for="pg_dbhost"><?php echo $l->t( 'Host:' ); ?></label><input type="text" name="pg_dbhost" id="pg_dbhost" value="<?php print OC_Helper::init_var('dbhost', 'localhost'); ?>" /></p>
+                               <p><label for="pg_dbtableprefix"><?php echo $l->t( 'Table prefix:' ); ?></label><input type="text" name="pg_dbtableprefix" id="pg_dbtableprefix" value="<?php print OC_Helper::init_var('dbtableprefix', 'oc_'); ?>" /></p>
+                               
+                       </div>
+                       <?php endif; ?>
                </fieldset>
 
                <p class="submit"><input type="submit" value="<?php echo $l->t( 'Finish setup' ); ?>" /></p>
index 9b18bc4c97f8e61b119afbe28b872088cfeb83c0..76d3fe5efcb2079afed0455f7670d0654018938a 100644 (file)
@@ -237,6 +237,7 @@ class OC_DB {
        public static function createDbFromStructure( $file ){
                $CONFIG_DBNAME  = OC_Config::getValue( "dbname", "owncloud" );
                $CONFIG_DBTABLEPREFIX = OC_Config::getValue( "dbtableprefix", "oc_" );
+               $CONFIG_DBTYPE = OC_Config::getValue( "dbtype", "sqlite" );
 
                self::connectScheme();
 
@@ -247,6 +248,9 @@ class OC_DB {
                $file2 = tempnam( sys_get_temp_dir(), 'oc_db_scheme_' );
                $content = str_replace( '*dbname*', $CONFIG_DBNAME, $content );
                $content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content );
+               if( $CONFIG_DBTYPE == 'pgsql' ){ //mysql support it too but sqlite don't
+                       $content = str_replace( '<default>0000-00-00 00:00:00</default>', '<default>CURRENT_TIMESTAMP</default>', $content );
+               }
                file_put_contents( $file2, $content );
 
                // Try to create tables
index f295d69562ed0d3f1eb0bcadde5d4e340d3c4be7..f87581d75827cda77cb54ad5fae321175b087584 100644 (file)
@@ -2,10 +2,12 @@
 
 $hasSQLite = (is_callable('sqlite_open') or class_exists('SQLite3'));
 $hasMySQL = is_callable('mysql_connect');
+$hasPostgreSQL = is_callable('pg_connect');
 $datadir = OC_Config::getValue('datadir', $SERVERROOT.'/data');
 $opts = array(
        'hasSQLite' => $hasSQLite,
        'hasMySQL' => $hasMySQL,
+       'hasPostgreSQL' => $hasPostgreSQL,
        'directory' => $datadir,
        'errors' => array(),
 );
@@ -43,6 +45,7 @@ class OC_Setup {
                if(empty($options['directory'])) {
                        $error[] = 'STEP 2 : data directory path is not set.';
                }
+
                if($dbtype=='mysql') { //mysql needs more config options
                        if(empty($options['dbuser'])) {
                                $error[] = 'STEP 3 : MySQL database user is not set.';
@@ -61,6 +64,24 @@ class OC_Setup {
                        }
                }
 
+               if($dbtype=='pgsql') { //postgresql needs more config options
+                       if(empty($options['pg_dbuser'])) {
+                               $error[] = 'STEP 3 : PostgreSQL database user is not set.';
+                       }
+                       if(empty($options['pg_dbpass'])) {
+                               $error[] = 'STEP 3 : PostgreSQL database password is not set.';
+                       }
+                       if(empty($options['pg_dbname'])) {
+                               $error[] = 'STEP 3 : PostgreSQL database name is not set.';
+                       }
+                       if(empty($options['pg_dbhost'])) {
+                               $error[] = 'STEP 3 : PostgreSQL database host is not set.';
+                       }
+                       if(!isset($options['pg_dbtableprefix'])) {
+                               $error[] = 'STEP 3 : PostgreSQL database table prefix is not set.';
+                       }
+               }
+
                if(count($error) == 0) { //no errors, good
                        $username = htmlspecialchars_decode($options['adminlogin']);
                        $password = htmlspecialchars_decode($options['adminpass']);
@@ -128,6 +149,62 @@ class OC_Setup {
                                        mysql_close($connection);
                                }
                        }
+                       elseif($dbtype == 'pgsql') {
+                               $dbuser = $options['pg_dbuser'];
+                               $dbpass = $options['pg_dbpass'];
+                               $dbname = $options['pg_dbname'];
+                               $dbhost = $options['pg_dbhost'];
+                               $dbtableprefix = $options['pg_dbtableprefix'];
+                               OC_CONFIG::setValue('dbname', $dbname);
+                               OC_CONFIG::setValue('dbhost', $dbhost);
+                               OC_CONFIG::setValue('dbtableprefix', $dbtableprefix);
+
+                               //check if the database user has admin right
+                               $connection_string = "host=$dbhost dbname=postgres user=$dbuser password=$dbpass";
+                               $connection = @pg_connect($connection_string);
+                               if(!$connection) {
+                                       $error[] = array(
+                                               'error' => 'postgresql username and/or password not valid',
+                                               'hint' => 'you need to enter either an existing account, or the administrative account if you wish to create a new user for ownCloud'
+                                       );
+                               }
+                               else {
+                                       //check for roles creation rights in postgresql
+                                       $query="SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='$dbuser'";
+                                       $result = pg_query($connection, $query);
+                                       if($result and pg_num_rows($result) > 0) {
+                                               //use the admin login data for the new database user
+
+                                               //add prefix to the postgresql user name to prevent collissions
+                                               $dbusername='oc_'.$username;
+                                               //hash the password so we don't need to store the admin config in the config file
+                                               $dbpassword=md5(time().$password);
+                                               
+                                               self::pg_createDBUser($dbusername, $dbpassword, $connection);
+                                               
+                                               OC_CONFIG::setValue('dbuser', $dbusername);
+                                               OC_CONFIG::setValue('dbpassword', $dbpassword);
+
+                                               //create the database
+                                               self::pg_createDatabase($dbname, $dbusername, $connection);
+                                       }
+                                       else {
+                                               OC_CONFIG::setValue('dbuser', $dbuser);
+                                               OC_CONFIG::setValue('dbpassword', $dbpass);
+
+                                               //create the database
+                                               self::pg_createDatabase($dbname, $dbuser, $connection);
+                                       }
+
+                                       //fill the database if needed
+                                       $query="SELECT * FROM {$dbtableprefix}users";
+                                       $result = pg_query($connection, $query);
+                                       if(!$result) {
+                                               OC_DB::createDbFromStructure('db_structure.xml');
+                                       }
+                                       pg_close($connection);
+                               }
+                       }
                        else {
                                //delete the old sqlite database first, might cause infinte loops otherwise
                                if(file_exists("$datadir/owncloud.db")){
@@ -179,6 +256,29 @@ class OC_Setup {
                $result = mysql_query($query, $connection);
        }
 
+       public static function pg_createDatabase($name,$user,$connection) {
+               //we cant user OC_BD functions here because we need to connect as the administrative user.
+               $query = "CREATE DATABASE $name OWNER $user";
+               $result = pg_query($connection, $query);
+               if(!$result) {
+                       $entry='DB Error: "'.pg_last_error($connection).'"<br />';
+                       $entry.='Offending command was: '.$query.'<br />';
+                       echo($entry);
+               }
+               $query = "REVOKE ALL PRIVILEGES ON DATABASE $name FROM PUBLIC";
+               $result = pg_query($connection, $query);                
+       }
+
+       private static function pg_createDBUser($name,$password,$connection) {
+               $query = "CREATE USER $name CREATEDB PASSWORD '$password';";
+               $result = pg_query($connection, $query);
+               if(!$result) {
+                       $entry='DB Error: "'.pg_last_error($connection).'"<br />';
+                       $entry.='Offending command was: '.$query.'<br />';
+                       echo($entry);
+               }
+       }
+
        /**
         * create .htaccess files for apache hosts
         */