summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerge Martin <edb@sigluy.net>2011-08-07 21:06:53 +0200
committerSerge Martin <edb@sigluy.net>2011-08-07 21:06:53 +0200
commit3e8ae8636cad91877c727c7b1ea77436816e7c3d (patch)
tree297fb9c5ab82a27d042bad7bc304f020f957a170
parentacf7916e71f1e290aa12eb259d92aa8ab2ffcf23 (diff)
downloadnextcloud-server-3e8ae8636cad91877c727c7b1ea77436816e7c3d.tar.gz
nextcloud-server-3e8ae8636cad91877c727c7b1ea77436816e7c3d.zip
Add postgresql support
REVIEW: 102101
-rw-r--r--core/js/setup.js14
-rw-r--r--core/templates/installation.php24
-rw-r--r--lib/db.php4
-rw-r--r--lib/setup.php100
4 files changed, 138 insertions, 4 deletions
diff --git a/core/js/setup.js b/core/js/setup.js
index b4616b8b14c..3025a511c61 100644
--- a/core/js/setup.js
+++ b/core/js/setup.js
@@ -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() {
diff --git a/core/templates/installation.php b/core/templates/installation.php
index 8b36d14bbf5..e2392778bed 100644
--- a/core/templates/installation.php
+++ b/core/templates/installation.php
@@ -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: ?>
@@ -49,11 +49,11 @@
<?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>
@@ -64,6 +64,24 @@
</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>
diff --git a/lib/db.php b/lib/db.php
index 9b18bc4c97f..76d3fe5efcb 100644
--- a/lib/db.php
+++ b/lib/db.php
@@ -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
diff --git a/lib/setup.php b/lib/setup.php
index f295d69562e..f87581d7582 100644
--- a/lib/setup.php
+++ b/lib/setup.php
@@ -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
*/