fixed automatic creation/filling of mysql databases

This commit is contained in:
Robin 2010-04-08 14:49:48 +02:00
parent 91d603a48c
commit 8f92dc8e25
2 changed files with 92 additions and 4 deletions

View File

@ -291,6 +291,9 @@ class OC_DB {
global $DBConnection;
global $CONFIG_DBNAME;
global $CONFIG_DBTYPE;
global $CONFIG_DBHOST;
global $CONFIG_DBUSER;
global $CONFIG_DBPASSWORD;
if(!isset($DBConnection)) {
if($CONFIG_DBTYPE=='sqlite'){
$DBConnection = @new SQLiteDatabase($DOCUMENTROOT.'/'.$CONFIG_DBNAME);

View File

@ -22,12 +22,16 @@ class OC_CONFIG{
global $DOCUMENTROOT;
global $SERVERROOT;
global $WEBROOT;
global $CONFIG_DBHOST;
global $CONFIG_DBNAME;
global $CONFIG_DBUSER;
global $CONFIG_DBPASSWORD;
global $CONFIG_DBTYPE;
if(isset($_POST['set_config'])){
//checkdata
$error='';
$FIRSTRUN=isset($CONFIG_ADMINLOGIN);
$FIRSTRUN=!isset($CONFIG_ADMINLOGIN);
if(!$FIRSTRUN){
if($_POST['currentpassword']!=$CONFIG_ADMINPASSWORD){
$error.='wrong password';
@ -54,9 +58,18 @@ class OC_CONFIG{
}
if(empty($error)) {
//create/fill database
$CONFIG_DBTYPE=$dbtype;
$CONFIG_DBNAME=$_POST['dbname'];
if($dbtype=='mysql'){
$CONFIG_DBHOST=$_POST['dbhost'];
$CONFIG_DBUSER=$_POST['dbuser'];
$CONFIG_DBPASSWORD=$_POST['dbpassword'];
}
if(isset($_POST['createdatabase']) and $CONFIG_DBTYPE=='mysql'){
self::createdatabase($_POST['dbadminuser'],$_POST['dbadminpwd']);
}
if(isset($_POST['filldb'])){
// self::filldatabase();
self::filldatabase();
}
//storedata
@ -77,7 +90,6 @@ class OC_CONFIG{
$filename=$SERVERROOT.'/config/config.php';
file_put_contents($filename,$config);
header("Location: ".$WEBROOT."/");
}
@ -94,7 +106,9 @@ class OC_CONFIG{
* "rowid"
*/
private static function filldatabase(){
$query="CREATE TABLE 'locks' (
global $CONFIG_DBTYPE;
if($CONFIG_DBTYPE=='sqlite'){
$query="CREATE TABLE 'locks' (
'token' VARCHAR(255) NOT NULL DEFAULT '',
'path' varchar(200) NOT NULL DEFAULT '',
'expires' int(11) NOT NULL DEFAULT '0',
@ -121,8 +135,79 @@ CREATE TABLE 'properties' (
'value' text,
PRIMARY KEY ('path','name','ns')
);";
}elseif($CONFIG_DBTYPE=='mysql'){
$query="SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";
CREATE TABLE IF NOT EXISTS `locks` (
`token` varchar(255) NOT NULL DEFAULT '',
`path` varchar(200) NOT NULL DEFAULT '',
`expires` int(11) NOT NULL DEFAULT '0',
`owner` varchar(200) DEFAULT NULL,
`recursive` int(11) DEFAULT '0',
`writelock` int(11) DEFAULT '0',
`exclusivelock` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`token`),
UNIQUE KEY `token` (`token`),
KEY `path` (`path`),
KEY `path_2` (`path`),
KEY `path_3` (`path`,`token`),
KEY `expires` (`expires`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`timestamp` int(11) NOT NULL,
`user` varchar(250) NOT NULL,
`type` int(11) NOT NULL,
`message` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
CREATE TABLE IF NOT EXISTS `properties` (
`path` varchar(255) NOT NULL DEFAULT '',
`name` varchar(120) NOT NULL DEFAULT '',
`ns` varchar(120) NOT NULL DEFAULT 'DAV:',
`value` text,
PRIMARY KEY (`path`,`name`,`ns`),
KEY `path` (`path`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
";
}
OC_DB::multiquery($query);
}
/**
* Create the database and user
* @param string adminUser
* @param string adminPwd
*
*/
private static function createdatabase($adminUser,$adminPwd){
global $CONFIG_DBHOST;
global $CONFIG_DBNAME;
global $CONFIG_DBUSER;
global $CONFIG_DBPWD;
//we cant user OC_BD functions here because we need to connect as the administrative user.
$connection = @new mysqli($CONFIG_DBHOST, $adminUser, $adminPwd);
if (mysqli_connect_errno()) {
@ob_end_clean();
echo('<html><head></head><body bgcolor="#F0F0F0"><br /><br /><center><b>can not connect to database as administrative user.</center></body></html>');
exit();
}
$query="CREATE USER '{$_POST['dbuser']}' IDENTIFIED BY '{$_POST['dbpassword']}';
CREATE DATABASE IF NOT EXISTS `{$_POST['dbname']}` ;
GRANT ALL PRIVILEGES ON `{$_POST['dbname']}` . * TO '{$_POST['dbuser']}';";
$result = @$connection->multi_query($query);
if (!$result) {
$entry='DB Error: "'.$connection->error.'"<br />';
$entry.='Offending command was: '.$query.'<br />';
echo($entry);
}
$connection->close();
}
}
?>