Add postgresql support

REVIEW: 102101
This commit is contained in:
Serge Martin 2011-08-07 21:06:53 +02:00
parent acf7916e71
commit 3e8ae8636c
4 changed files with 138 additions and 4 deletions

View File

@ -1,9 +1,14 @@
$(document).ready(function() { $(document).ready(function() {
// Hide the MySQL config div if needed : // Hide the MySQL config div if needed :
if(!$('#mysql').is(':checked') && $('#hasSQLite').val()=='true') { if(!$('#mysql').is(':checked')) {
$('#use_mysql').hide(); $('#use_mysql').hide();
} }
// Hide the PostgreSQL config div if needed:
if(!$('#pgsql').is(':checked')) {
$('#use_postgresql').hide();
}
$('#datadirField').hide(250); $('#datadirField').hide(250);
if($('#hasSQLite').val()=='true'){ if($('#hasSQLite').val()=='true'){
$('#databaseField').hide(250); $('#databaseField').hide(250);
@ -11,10 +16,17 @@ $(document).ready(function() {
$('#sqlite').click(function() { $('#sqlite').click(function() {
$('#use_mysql').slideUp(250); $('#use_mysql').slideUp(250);
$('#use_postgresql').slideUp(250);
}); });
$('#mysql').click(function() { $('#mysql').click(function() {
$('#use_mysql').slideDown(250); $('#use_mysql').slideDown(250);
$('#use_postgresql').slideUp(250);
});
$('#pgsql').click(function() {
$('#use_postgresql').slideDown(250);
$('#use_mysql').slideUp(250);
}); });
$('#showAdvanced').click(function() { $('#showAdvanced').click(function() {

View File

@ -39,7 +39,7 @@
<legend><?php echo $l->t( 'Configure the database.' ); ?></legend> <legend><?php echo $l->t( 'Configure the database.' ); ?></legend>
<?php if($_['hasSQLite']): ?> <?php if($_['hasSQLite']): ?>
<input type='hidden' id='hasSQLite' value='true'/> <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> <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" /> <input type="hidden" id="dbtype" name="dbtype" value="sqlite" />
<?php else: ?> <?php else: ?>
@ -49,11 +49,11 @@
<?php if($_['hasMySQL']): ?> <?php if($_['hasMySQL']): ?>
<input type='hidden' id='hasMySQL' value='true'/> <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> <p><?php echo $l->t( 'MySQL will be used for the database.' ); ?></p>
<input type="hidden" id="dbtype" name="dbtype" value="mysql" /> <input type="hidden" id="dbtype" name="dbtype" value="mysql" />
<?php else: ?> <?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; ?> <?php endif; ?>
<div id="use_mysql"> <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> <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> </div>
<?php endif; ?> <?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> </fieldset>
<p class="submit"><input type="submit" value="<?php echo $l->t( 'Finish setup' ); ?>" /></p> <p class="submit"><input type="submit" value="<?php echo $l->t( 'Finish setup' ); ?>" /></p>

View File

@ -237,6 +237,7 @@ class OC_DB {
public static function createDbFromStructure( $file ){ public static function createDbFromStructure( $file ){
$CONFIG_DBNAME = OC_Config::getValue( "dbname", "owncloud" ); $CONFIG_DBNAME = OC_Config::getValue( "dbname", "owncloud" );
$CONFIG_DBTABLEPREFIX = OC_Config::getValue( "dbtableprefix", "oc_" ); $CONFIG_DBTABLEPREFIX = OC_Config::getValue( "dbtableprefix", "oc_" );
$CONFIG_DBTYPE = OC_Config::getValue( "dbtype", "sqlite" );
self::connectScheme(); self::connectScheme();
@ -247,6 +248,9 @@ class OC_DB {
$file2 = tempnam( sys_get_temp_dir(), 'oc_db_scheme_' ); $file2 = tempnam( sys_get_temp_dir(), 'oc_db_scheme_' );
$content = str_replace( '*dbname*', $CONFIG_DBNAME, $content ); $content = str_replace( '*dbname*', $CONFIG_DBNAME, $content );
$content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $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 ); file_put_contents( $file2, $content );
// Try to create tables // Try to create tables

View File

@ -2,10 +2,12 @@
$hasSQLite = (is_callable('sqlite_open') or class_exists('SQLite3')); $hasSQLite = (is_callable('sqlite_open') or class_exists('SQLite3'));
$hasMySQL = is_callable('mysql_connect'); $hasMySQL = is_callable('mysql_connect');
$hasPostgreSQL = is_callable('pg_connect');
$datadir = OC_Config::getValue('datadir', $SERVERROOT.'/data'); $datadir = OC_Config::getValue('datadir', $SERVERROOT.'/data');
$opts = array( $opts = array(
'hasSQLite' => $hasSQLite, 'hasSQLite' => $hasSQLite,
'hasMySQL' => $hasMySQL, 'hasMySQL' => $hasMySQL,
'hasPostgreSQL' => $hasPostgreSQL,
'directory' => $datadir, 'directory' => $datadir,
'errors' => array(), 'errors' => array(),
); );
@ -43,6 +45,7 @@ class OC_Setup {
if(empty($options['directory'])) { if(empty($options['directory'])) {
$error[] = 'STEP 2 : data directory path is not set.'; $error[] = 'STEP 2 : data directory path is not set.';
} }
if($dbtype=='mysql') { //mysql needs more config options if($dbtype=='mysql') { //mysql needs more config options
if(empty($options['dbuser'])) { if(empty($options['dbuser'])) {
$error[] = 'STEP 3 : MySQL database user is not set.'; $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 if(count($error) == 0) { //no errors, good
$username = htmlspecialchars_decode($options['adminlogin']); $username = htmlspecialchars_decode($options['adminlogin']);
$password = htmlspecialchars_decode($options['adminpass']); $password = htmlspecialchars_decode($options['adminpass']);
@ -128,6 +149,62 @@ class OC_Setup {
mysql_close($connection); 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 { else {
//delete the old sqlite database first, might cause infinte loops otherwise //delete the old sqlite database first, might cause infinte loops otherwise
if(file_exists("$datadir/owncloud.db")){ if(file_exists("$datadir/owncloud.db")){
@ -179,6 +256,29 @@ class OC_Setup {
$result = mysql_query($query, $connection); $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 * create .htaccess files for apache hosts
*/ */