added MySQL support, user can now choose between MySQL and SQLite

This commit is contained in:
Robin 2010-03-30 22:15:45 +02:00
parent 6602a61a38
commit 91d603a48c
4 changed files with 157 additions and 21 deletions

View File

@ -46,6 +46,7 @@ $CONFIG_DATADIRECTORY=$SERVERROOT.$WEBROOT.'/data';
$CONFIG_HTTPFORCESSL=false;
$CONFIG_DATEFORMAT='j M Y G:i';
$CONFIG_DBNAME='owncloud';
$CONFIG_DBTYPE='sqlite';
// include the generated configfile
@include_once('config.php');
@ -245,20 +246,34 @@ class OC_DB {
* @return result-set
*/
static function query($cmd) {
global $DOCUMENTROOT;
global $DOCUMENTROOT;
global $DBConnection;
global $CONFIG_DBNAME;
global $CONFIG_DBHOST;
global $CONFIG_DBUSER;
global $CONFIG_DBPASSWORD;
global $CONFIG_DBTYPE;
if(!isset($DBConnection)) {
$DBConnection = @new SQLiteDatabase($DOCUMENTROOT.'/'.$CONFIG_DBNAME);
if($CONFIG_DBTYPE=='sqlite'){
$DBConnection = @new SQLiteDatabase($DOCUMENTROOT.'/'.$CONFIG_DBNAME);
}elseif($CONFIG_DBTYPE=='mysql'){
$DBConnection = @new mysqli($CONFIG_DBHOST, $CONFIG_DBUSER, $CONFIG_DBPASSWORD,$CONFIG_DBNAME);
}
if (!$DBConnection) {
@ob_end_clean();
echo('<b>can not connect to database.</center>');
echo('<b>can not connect to database, using '.$CONFIG_DBTYPE.'.</center>');
exit();
}
}
$result = @$DBConnection->query($cmd);
if (!$result) {
$entry='DB Error: "'.sqlite_error_string($DBConnection->lastError()).'"<br />';
if($CONFIG_DBTYPE=='sqlite'){
$error=sqlite_error_string($DBConnection->lastError());
}elseif($CONFIG_DBTYPE=='mysql'){
print_r($DBConnection);
$error=$DBConnection->error;
}
$entry='DB Error: "'.$error.'"<br />';
$entry.='Offending command was: '.$cmd.'<br />';
echo($entry);
}
@ -275,17 +290,31 @@ class OC_DB {
global $DOCUMENTROOT;
global $DBConnection;
global $CONFIG_DBNAME;
global $CONFIG_DBTYPE;
if(!isset($DBConnection)) {
$DBConnection = @new SQLiteDatabase($DOCUMENTROOT.'/'.$CONFIG_DBNAME);
if($CONFIG_DBTYPE=='sqlite'){
$DBConnection = @new SQLiteDatabase($DOCUMENTROOT.'/'.$CONFIG_DBNAME);
}elseif($CONFIG_DBTYPE=='mysql'){
$DBConnection = @new mysqli($CONFIG_DBHOST, $CONFIG_DBUSER, $CONFIG_DBPASSWORD,$CONFIG_DBNAME);
}
if (!$DBConnection) {
@ob_end_clean();
echo('<b>can not connect to database.</center>');
echo('<b>can not connect to database, using '.$CONFIG_DBTYPE.'.</center>');
exit();
}
}
$result = @$DBConnection->queryExec($cmd);
if($CONFIG_DBTYPE=='sqlite'){
$result = @$DBConnection->queryExec($cmd);
}elseif($CONFIG_DBTYPE=='mysql'){
$result = @$DBConnection->multi_query($cmd);
}
if (!$result) {
$entry='DB Error: "'.sqlite_error_string($DBConnection->lastError()).'"<br />';
if($CONFIG_DBTYPE=='sqlite'){
$error=sqlite_error_string($DBConnection->lastError());
}elseif($CONFIG_DBTYPE=='mysql'){
$error=$DBConnection->error;
}
$entry='DB Error: "'.$error.'"<br />';
$entry.='Offending command was: '.$cmd.'<br />';
echo($entry);
}
@ -299,6 +328,7 @@ class OC_DB {
* @return bool
*/
static function close() {
global $CONFIG_DBTYPE;
global $DBConnection;
if(isset($DBConnection)) {
return $DBConnection->close();
@ -315,7 +345,12 @@ class OC_DB {
*/
static function insertid() {
global $DBConnection;
return $DBConnectio->lastInsertRowid();
global $CONFIG_DBTYPE;
if($CONFIG_DBTYPE=='sqlite'){
return $DBConnection->lastInsertRowid();
}elseif($CONFIG_DBTYPE=='mysql'){
return(mysqli_insert_id($DBConnection));
}
}
/**
@ -326,7 +361,12 @@ class OC_DB {
*/
static function numrows($result) {
if(!isset($result) or ($result == false)) return 0;
$num= $result->numRows();
global $CONFIG_DBTYPE;
if($CONFIG_DBTYPE=='sqlite'){
$num= $result->numRows();
}elseif($CONFIG_DBTYPE=='mysql'){
$num= mysqli_num_rows($result);
}
return($num);
}
@ -337,8 +377,13 @@ class OC_DB {
*/
static function affected_rows() {
global $DBConnection;
global $CONFIG_DBTYPE;
if(!isset($DBConnection) or ($DBConnection==false)) return 0;
$num= $DBConnection->changes();
if($CONFIG_DBTYPE=='sqlite'){
$num= $DBConnection->changes();
}elseif($CONFIG_DBTYPE=='mysql'){
$num= mysqli_affected_rows($DBConnection);
}
return($num);
}
@ -351,10 +396,20 @@ class OC_DB {
* @return unknown
*/
static function result($result, $i, $field) {
$result->seek($ii);
$tmp=$result->fetch();
global $CONFIG_DBTYPE;
if($CONFIG_DBTYPE=='sqlite'){
$result->seek($i);
$tmp=$result->fetch();
}elseif($CONFIG_DBTYPE=='mysql'){
mysqli_data_seek($result,$i);
if (is_string($field))
$tmp=mysqli_fetch_array($result,MYSQLI_BOTH);
else
$tmp=mysqli_fetch_array($result,MYSQLI_NUM);
}
$tmp=$tmp[$field];
return($tmp);
return($tmp);
}
/**
@ -364,7 +419,12 @@ class OC_DB {
* @return data
*/
static function fetch_assoc($result) {
return $result->fetch(SQLITE_ASSOC);
global $CONFIG_DBTYPE;
if($CONFIG_DBTYPE=='sqlite'){
return $result->fetch(SQLITE_ASSOC);
}elseif($CONFIG_DBTYPE=='mysql'){
return mysqli_fetch_assoc($result);
}
}
@ -375,8 +435,13 @@ class OC_DB {
* @return bool
*/
static function free_result($result) {
$result = null; //No native way to do this
return true;
global $CONFIG_DBTYPE;
if($CONFIG_DBTYPE=='sqlite'){
$result = null; //No native way to do this
return true;
}elseif($CONFIG_DBTYPE=='mysql'){
return @mysqli_free_result($result);
}
}
}

View File

@ -20,6 +20,7 @@ class OC_CONFIG{
*/
public static function writeconfiglisener(){
global $DOCUMENTROOT;
global $SERVERROOT;
global $WEBROOT;
global $CONFIG_DBNAME;
if(isset($_POST['set_config'])){
@ -37,18 +38,25 @@ class OC_CONFIG{
if(!isset($_POST['adminpassword']) or empty($_POST['adminpassword']) and $FIRSTRUN) $error.='admin password not set<br />';
if(!isset($_POST['adminpassword2']) or empty($_POST['adminpassword2']) and $FIRSTRUN) $error.='retype admin password not set<br />';
if(!isset($_POST['datadirectory']) or empty($_POST['datadirectory'])) $error.='data directory not set<br />';
if(!isset($_POST['dateformat']) or empty($_POST['dateformat'])) $error.='dteformat not set<br />';
if(!isset($_POST['dateformat']) or empty($_POST['dateformat'])) $error.='dateformat not set<br />';
if(!isset($_POST['dbname']) or empty($_POST['dbname'])) $error.='databasename not set<br />';
if($_POST['adminpassword']<>$_POST['adminpassword2'] ) $error.='admin passwords are not the same<br />';
if(!isset($_POST['adminpassword']) or empty($_POST['adminpassword']) and !$FIRSTRUN){
$_POST['adminpassword']=$CONFIG_ADMINPASSWORD;
}
$dbtype=$_POST['dbtype'];
if($dbtype=='mysql'){
if(!isset($_POST['dbhost']) or empty($_POST['dbhost'])) $error.='database host not set<br />';
if(!isset($_POST['dbuser']) or empty($_POST['dbuser'])) $error.='database user not set<br />';
if($_POST['dbpassword']<>$_POST['dbpassword2'] ) $error.='database passwords are not the same<br />';
}
if(empty($error)) {
//create/fill database
$CONFIG_DBNAME=$_POST['dbname'];
if(isset($_POST['filldb'])){
self::filldatabase();
// self::filldatabase();
}
//storedata
@ -58,13 +66,19 @@ class OC_CONFIG{
$config.='$CONFIG_DATADIRECTORY=\''.$_POST['datadirectory']."';\n";
if(isset($_POST['forcessl'])) $config.='$CONFIG_HTTPFORCESSL=true'.";\n"; else $config.='$CONFIG_HTTPFORCESSL=false'.";\n";
$config.='$CONFIG_DATEFORMAT=\''.$_POST['dateformat']."';\n";
$config.='$CONFIG_DBTYPE=\''.$dbtype."';\n";
$config.='$CONFIG_DBNAME=\''.$_POST['dbname']."';\n";
if($dbtype=='mysql'){
$config.='$CONFIG_DBHOST=\''.$_POST['dbhost']."';\n";
$config.='$CONFIG_DBUSER=\''.$_POST['dbuser']."';\n";
$config.='$CONFIG_DBPASSWORD=\''.$_POST['dbpassword']."';\n";
}
$config.='?> ';
$filename=$DOCUMENTROOT.'/config/config.php';
$filename=$SERVERROOT.'/config/config.php';
file_put_contents($filename,$config);
header("Location: ".$WEBROOT."/");
header("Location: ".$WEBROOT."/");
}
return($error);

View File

@ -1,8 +1,49 @@
<?php
global $FIRSTRUN;
if(!isset($fillDB)) $fillDB=true;
if(!isset($CONFIG_DBHOST)) $CONFIG_DBHOST='localhost';
if(!isset($CONFIG_DBUSER)) $CONFIG_DBUSER='owncloud';
?>
<script type="text/javascript">
function showDBAdmin(){
var show=document.getElementById('dbcreate').checked;
document.getElementById('dbAdminUser').style.display=(show)?'table-row':'none';
document.getElementById('dbAdminPwd').style.display=(show)?'table-row':'none';
}
function dbtypechange(){
var dropdown=action=document.getElementById('dbtype');
var type=dropdown.options[dropdown.selectedIndex].value;
var inputs=Array('dbhost','dbuser','dbpass','dbpass_retype','dbcreaterow','dbAdminPwd','dbAdminUser');
var id,element;
if(type=='sqlite'){
for(i in inputs){
id=inputs[i];
element=document.getElementById(id);
if(element){
element.style.display='none';
}
}
}else if(type=='mysql'){
for(i in inputs){
id=inputs[i];
element=document.getElementById(id);
if(element){
element.style.display='table-row';
}
}
showDBAdmin()
}
}
</script>
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table cellpadding="5" cellspacing="5" border="0" class="loginform">
<?php
if(!$FIRSTRUN){?>
<tr><td>current password</td><td><input type="password" name="currentpassword" size="30" class="formstyle"></input></td></tr>
<?php
}
?>
<form method="post" enctype="multipart/form-data">
<table cellpadding="5" cellspacing="5" border="0" class="loginform">
<?php
@ -17,7 +58,23 @@ if(!isset($fillDB)) $fillDB=true;
<tr><td>data directory:</td><td><input type="text" name="datadirectory" size="30" class="formstyle" value="<?php echo($CONFIG_DATADIRECTORY);?>"></input></td></tr>
<tr><td>force ssl:</td><td><input type="checkbox" name="forcessl" size="30" class="formstyle" value='<?php echo($CONFIG_HTTPFORCESSL);?>'></input></td></tr>
<tr><td>date format:</td><td><input type="text" name="dateformat" size="30" class="formstyle" value='<?php echo($CONFIG_DATEFORMAT);?>'></input></td></tr>
<tr><td>database type:</td><td>
<select id='dbtype' name="dbtype" onchange='dbtypechange()'>
<option value="sqlite">SQLite</option>
<option value="mysql">MySQL</option>
</select>
</td></tr>
<tr id='dbhost'><td>database host:</td><td><input type="text" name="dbhost" size="30" class="formstyle" value='<?php echo($CONFIG_DBHOST);?>'></input></td></tr>
<tr><td>database name:</td><td><input type="text" name="dbname" size="30" class="formstyle" value='<?php echo($CONFIG_DBNAME);?>'></input></td></tr>
<tr id='dbuser'><td>database user:</td><td><input type="text" name="dbuser" size="30" class="formstyle" value='<?php echo($CONFIG_DBUSER);?>'></input></td></tr>
<tr id='dbpass'><td>database password:</td><td><input type="password" name="dbpassword" size="30" class="formstyle" value=''></input></td></tr>
<tr id='dbpass_retype'><td>retype database password:</td><td><input type="password" name="dbpassword2" size="30" class="formstyle" value=''></input></td></tr>
<tr id='dbcreaterow'><td>create database and user:</td><td><input id='dbcreate' type="checkbox" name="createdatabase" size="30" class="formstyle" value='1' <?php if($FIRSTRUN) echo 'checked'; ?> onchange='showDBAdmin()'></input></td></tr>
<tr id='dbAdminUser'><td>database administrative user:</td><td><input type="text" name="dbadminuser" size="30" class="formstyle" value='root'></input></td></tr>
<tr id='dbAdminPwd'><td>database administrative password:</td><td><input type="password" name="dbadminpwd" size="30" class="formstyle" value=''></input></td></tr>
<tr><td>automaticly fill initial database:</td><td><input type="checkbox" name="filldb" size="30" class="formstyle" value='1' <?php if($FIRSTRUN) echo 'checked'; ?>></input></td></tr>
<tr><td></td><td><input type="submit" name="set_config" alt="save" value="save" class="formstyle" /></td></tr>
</table></form>
<script type="text/javascript">
dbtypechange()
</script>

View File

@ -28,7 +28,7 @@ require_once('HTTP/WebDAV/Server/Filesystem.php');
ini_set('default_charset', 'UTF-8');
#ini_set('error_reporting', '');
ob_clean();
if(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['REDIRECT_REMOTE_USER'])) {
header('WWW-Authenticate: Basic realm="ownCloud"');