Initial PostgreSQL support

This commit is contained in:
Robin Appelman 2010-06-20 17:58:39 +02:00
parent 7503ad139b
commit a9fd0c67b4
6 changed files with 101 additions and 53 deletions

View File

@ -304,18 +304,18 @@
<field> <field>
<name>user_id</name> <name>user_id</name>
<type>text</type> <type>integer</type>
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
<length>64</length> <length>4</length>
</field> </field>
<field> <field>
<name>group_id</name> <name>group_id</name>
<type>text</type> <type>integer</type>
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
<length>64</length> <length>4</length>
</field> </field>
</declaration> </declaration>

View File

@ -353,6 +353,14 @@ class OC_DB {
'hostspec' => $CONFIG_DBHOST, 'hostspec' => $CONFIG_DBHOST,
'database' => $CONFIG_DBNAME, 'database' => $CONFIG_DBNAME,
); );
}elseif($CONFIG_DBTYPE=='pgsql'){
$dsn = array(
'phptype' => 'pgsql',
'username' => $CONFIG_DBUSER,
'password' => $CONFIG_DBPASSWORD,
'hostspec' => $CONFIG_DBHOST,
'database' => $CONFIG_DBNAME,
);
} }
self::$DBConnection=&MDB2::factory($dsn,$options); self::$DBConnection=&MDB2::factory($dsn,$options);
if (@PEAR::isError(self::$DBConnection)) { if (@PEAR::isError(self::$DBConnection)) {
@ -378,6 +386,8 @@ class OC_DB {
OC_DB::connect(); OC_DB::connect();
if($CONFIG_DBTYPE=='sqlite'){//fix differences between sql versions if($CONFIG_DBTYPE=='sqlite'){//fix differences between sql versions
$cmd=str_replace('`','',$cmd); $cmd=str_replace('`','',$cmd);
}elseif($CONFIG_DBTYPE=='pgsql'){
$cmd=str_replace('`','"',$cmd);
} }
$result=self::$DBConnection->query($cmd); $result=self::$DBConnection->query($cmd);
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
@ -396,7 +406,19 @@ class OC_DB {
*/ */
static function select($cmd){ static function select($cmd){
OC_DB::connect(); OC_DB::connect();
return self::$DBConnection->queryAll($cmd); global $CONFIG_DBTYPE;
if($CONFIG_DBTYPE=='sqlite'){//fix differences between sql versions
$cmd=str_replace('`','',$cmd);
}elseif($CONFIG_DBTYPE=='pgsql'){
$cmd=str_replace('`','"',$cmd);
}
$result=self::$DBConnection->queryAll($cmd);
if (PEAR::isError($result)) {
$entry='DB Error: "'.$result->getMessage().'"<br />';
$entry.='Offending command was: '.$cmd.'<br />';
die($entry);
}
return $result;
} }
/** /**

View File

@ -184,13 +184,13 @@ class OC_CONFIG{
//create/fill database //create/fill database
$CONFIG_DBTYPE=$dbtype; $CONFIG_DBTYPE=$dbtype;
$CONFIG_DBNAME=$_POST['dbname']; $CONFIG_DBNAME=$_POST['dbname'];
if($dbtype=='mysql'){ if($dbtype!='sqlite'){
$CONFIG_DBHOST=$_POST['dbhost']; $CONFIG_DBHOST=$_POST['dbhost'];
$CONFIG_DBUSER=$_POST['dbuser']; $CONFIG_DBUSER=$_POST['dbuser'];
$CONFIG_DBPASSWORD=$_POST['dbpassword']; $CONFIG_DBPASSWORD=$_POST['dbpassword'];
} }
try{ try{
if(isset($_POST['createdatabase']) and $CONFIG_DBTYPE=='mysql'){ if(isset($_POST['createdatabase']) and $CONFIG_DBTYPE!='sqlite'){
self::createdatabase($_POST['dbadminuser'],$_POST['dbadminpwd']); self::createdatabase($_POST['dbadminuser'],$_POST['dbadminpwd']);
} }
}catch(Exception $e){ }catch(Exception $e){
@ -241,7 +241,7 @@ class OC_CONFIG{
$config.='$CONFIG_DATEFORMAT=\''.$_POST['dateformat']."';\n"; $config.='$CONFIG_DATEFORMAT=\''.$_POST['dateformat']."';\n";
$config.='$CONFIG_DBTYPE=\''.$dbtype."';\n"; $config.='$CONFIG_DBTYPE=\''.$dbtype."';\n";
$config.='$CONFIG_DBNAME=\''.$_POST['dbname']."';\n"; $config.='$CONFIG_DBNAME=\''.$_POST['dbname']."';\n";
if($dbtype=='mysql'){ if($dbtype!='sqlite'){
$config.='$CONFIG_DBHOST=\''.$_POST['dbhost']."';\n"; $config.='$CONFIG_DBHOST=\''.$_POST['dbhost']."';\n";
$config.='$CONFIG_DBUSER=\''.$_POST['dbuser']."';\n"; $config.='$CONFIG_DBUSER=\''.$_POST['dbuser']."';\n";
$config.='$CONFIG_DBPASSWORD=\''.$_POST['dbpassword']."';\n"; $config.='$CONFIG_DBPASSWORD=\''.$_POST['dbpassword']."';\n";
@ -290,7 +290,9 @@ class OC_CONFIG{
global $CONFIG_DBNAME; global $CONFIG_DBNAME;
global $CONFIG_DBUSER; global $CONFIG_DBUSER;
global $CONFIG_DBPWD; global $CONFIG_DBPWD;
global $CONFIG_DBTYPE;
//we cant user OC_BD functions here because we need to connect as the administrative user. //we cant user OC_BD functions here because we need to connect as the administrative user.
if($CONFIG_DBTYPE=='mysql'){
$connection = @new mysqli($CONFIG_DBHOST, $adminUser, $adminPwd); $connection = @new mysqli($CONFIG_DBHOST, $adminUser, $adminPwd);
if (mysqli_connect_errno()) { if (mysqli_connect_errno()) {
@ob_end_clean(); @ob_end_clean();
@ -309,8 +311,8 @@ class OC_CONFIG{
}else{ }else{
$query=''; $query='';
} }
$query.="CREATE DATABASE IF NOT EXISTS `{$_POST['dbname']}`; $query.="CREATE DATABASE IF NOT EXISTS `{$_POST['dbname']}`;";
GRANT ALL PRIVILEGES ON `{$_POST['dbname']}` . * TO '{$_POST['dbuser']}';"; $query.="GRANT ALL PRIVILEGES ON `{$_POST['dbname']}` . * TO '{$_POST['dbuser']}';";
$result = @$connection->multi_query($query); $result = @$connection->multi_query($query);
if (!$result) { if (!$result) {
$entry='DB Error: "'.$connection->error.'"<br />'; $entry='DB Error: "'.$connection->error.'"<br />';
@ -318,6 +320,19 @@ class OC_CONFIG{
echo($entry); echo($entry);
} }
$connection->close(); $connection->close();
}elseif($CONFIG_DBTYPE=='pgsql'){
$connection = pg_connect("user='$adminUser' host='$CONFIG_DBHOST' password='$adminPwd'");
$query="CREATE USER {$_POST['dbuser']} WITH PASSWORD '{$_POST['dbpassword']}' CREATEDB;";
$result = pg_exec($connection, $query);
$query="select count(*) from pg_catalog.pg_database where datname = '{$_POST['dbname']}';";
$result = pg_exec($connection, $query);
if(pg_result($result,0,0)==0){
$query="CREATE DATABASE {$_POST['dbname']};";
$result = pg_exec($connection, $query);
$query="ALTER DATABASE {$_POST['dbname']} OWNER TO {$_POST['dbuser']};";
$result = pg_exec($connection, $query);
}
}
} }
} }
?> ?>

View File

@ -48,7 +48,7 @@ class OC_LOG {
* @param message $message * @param message $message
*/ */
public static function event($user,$type,$message){ public static function event($user,$type,$message){
$result = OC_DB::query('insert into log (timestamp,user,type,message) values ("'.time().'","'.addslashes($user).'","'.addslashes($type).'","'.addslashes($message).'")'); $result = OC_DB::query('INSERT INTO `log` (`timestamp`,`user`,`type`,`message`) VALUES ('.time().',\''.addslashes($user).'\','.addslashes($type).',\''.addslashes($message).'\');');
OC_DB::free_result($result); OC_DB::free_result($result);
} }
@ -62,10 +62,10 @@ class OC_LOG {
echo('<div class="center"><table cellpadding="6" cellspacing="0" border="0" class="log">'); echo('<div class="center"><table cellpadding="6" cellspacing="0" border="0" class="log">');
if(OC_USER::ingroup($_SESSION['username_clean'],'admin')){ if(OC_USER::ingroup($_SESSION['username_clean'],'admin')){
$result = OC_DB::select('select timestamp,user,type,message from log order by timestamp desc limit 20'); $result = OC_DB::select('select `timestamp`,`user`,`type`,`message` from log order by timestamp desc limit 20');
}else{ }else{
$user=$_SESSION['username_clean']; $user=$_SESSION['username_clean'];
$result = OC_DB::select('select timestamp,user,type,message from log where user=\''.$user.'\' order by timestamp desc limit 20'); $result = OC_DB::select('select `timestamp`,`user`,`type`,`message` from log where user=\''.$user.'\' order by timestamp desc limit 20');
} }
foreach($result as $entry){ foreach($result as $entry){
echo('<tr class="browserline">'); echo('<tr class="browserline">');

View File

@ -58,11 +58,11 @@ class OC_USER {
if(OC_USER::getuserid($username)!=0){ if(OC_USER::getuserid($username)!=0){
return false; return false;
}else{ }else{
$password=sha1($password);
$usernameclean=strtolower($username); $usernameclean=strtolower($username);
$password=sha1($password);
$username=OC_DB::escape($username); $username=OC_DB::escape($username);
$usernameclean=OC_DB::escape($usernameclean); $usernameclean=OC_DB::escape($usernameclean);
$query="INSERT INTO `users` (`user_id` ,`user_name` ,`user_name_clean` ,`user_password`) VALUES (NULL , '$username', '$usernameclean', '$password')"; $query="INSERT INTO `users` (`user_name` ,`user_name_clean` ,`user_password`) VALUES ('$username', '$usernameclean', '$password')";
$result=OC_DB::query($query); $result=OC_DB::query($query);
return ($result)?true:false; return ($result)?true:false;
} }
@ -118,7 +118,7 @@ class OC_USER {
public static function creategroup($groupname){ public static function creategroup($groupname){
if(OC_USER::getgroupid($groupname)==0){ if(OC_USER::getgroupid($groupname)==0){
$groupname=OC_DB::escape($groupname); $groupname=OC_DB::escape($groupname);
$query="INSERT INTO `groups` (`group_id` ,`group_name`) VALUES (NULL , '$groupname')"; $query="INSERT INTO `groups` (`group_name`) VALUES ('$groupname')";
$result=OC_DB::query($query); $result=OC_DB::query($query);
return ($result)?true:false; return ($result)?true:false;
}else{ }else{
@ -132,7 +132,6 @@ class OC_USER {
*/ */
public static function getuserid($username){ public static function getuserid($username){
$usernameclean=strtolower($username); $usernameclean=strtolower($username);
$username=OC_DB::escape($username);
$usernameclean=OC_DB::escape($usernameclean); $usernameclean=OC_DB::escape($usernameclean);
$query="SELECT user_id FROM users WHERE user_name_clean = '$usernameclean'"; $query="SELECT user_id FROM users WHERE user_name_clean = '$usernameclean'";
$result=OC_DB::select($query); $result=OC_DB::select($query);
@ -187,7 +186,7 @@ class OC_USER {
$userid=OC_USER::getuserid($username); $userid=OC_USER::getuserid($username);
$groupid=OC_USER::getgroupid($groupname); $groupid=OC_USER::getgroupid($groupname);
if($groupid>0 and $userid>0){ if($groupid>0 and $userid>0){
$query="SELECT user_group_id FROM user_group WHERE group_id = $groupid AND user_id = $userid LIMIT 1"; $query="SELECT * FROM user_group WHERE group_id = '$groupid' AND user_id = '$userid';";
$result=OC_DB::select($query); $result=OC_DB::select($query);
if(isset($result[0]) && isset($result[0]['user_group_id'])){ if(isset($result[0]) && isset($result[0]['user_group_id'])){
return true; return true;
@ -208,7 +207,7 @@ class OC_USER {
$userid=OC_USER::getuserid($username); $userid=OC_USER::getuserid($username);
$groupid=OC_USER::getgroupid($groupname); $groupid=OC_USER::getgroupid($groupname);
if($groupid!=0 and $userid!=0){ if($groupid!=0 and $userid!=0){
$query="INSERT INTO `user_group` (`user_group_id` ,`user_id` ,`group_id`) VALUES (NULL , '$userid', '$groupid');"; $query="INSERT INTO `user_group` (`user_id` ,`group_id`) VALUES ('$userid', '$groupid');";
$result=OC_DB::query($query); $result=OC_DB::query($query);
if($result){ if($result){
return true; return true;

View File

@ -37,7 +37,7 @@ function dbtypechange(){
element.style.display='none'; element.style.display='none';
} }
} }
}else if(type=='mysql'){ }else if(type=='mysql' || type=='pgsql'){
for(i in inputs){ for(i in inputs){
id=inputs[i]; id=inputs[i];
element=document.getElementById(id); element=document.getElementById(id);
@ -80,24 +80,36 @@ if($FIRSTRUN){?>
<select id='dbtype' name="dbtype" onchange='dbtypechange()'> <select id='dbtype' name="dbtype" onchange='dbtypechange()'>
<?php <?php
global $CONFIG_DBTYPE; global $CONFIG_DBTYPE;
$dbtypes=array();
if($CONFIG_DBTYPE=='sqlite'){ if($CONFIG_DBTYPE=='sqlite'){
if(is_callable('sqlite_open')){ if(is_callable('sqlite_open')){
$dbtypes[]='SQLite'; echo "<option value='sqlite'>SQLite</option>";
} }
if(is_callable('mysql_connect')){ if(is_callable('mysql_connect')){
$dbtypes[]='MySQL'; echo "<option value='mysql'>MySQL</option>";
} }
}else{ if(is_callable('pg_connect')){
echo "<option value='pgsql'>PostgreSQL</option>";
}
}elseif($CONFIG_DBTYPE=='mysql'){
if(is_callable('mysql_connect')){ if(is_callable('mysql_connect')){
$dbtypes[]='MySQL'; echo "<option value='mysql'>MySQL</option>";
} }
if(is_callable('sqlite_open')){ if(is_callable('sqlite_open')){
$dbtypes[]='SQLite'; echo "<option value='sqlite'>SQLite</option>";
}
if(is_callable('pg_connect')){
echo "<option value='pgsql'>PostgreSQL</option>";
}
}elseif($CONFIG_DBTYPE=='pgsql'){
if(is_callable('pg_connect')){
echo "<option value='pgsql'>PostgreSQL</option>";
}
if(is_callable('mysql_connect')){
echo "<option value='mysql'>MySQL</option>";
}
if(is_callable('sqlite_open')){
echo "<option value='sqlite'>SQLite</option>";
} }
}
foreach($dbtypes as $dbtype){
echo "<option value='".strtolower($dbtype)."'>$dbtype</option>";
} }
?> ?>
</select> </select>