From 8f92dc8e250326bfbb1144d5cbcdf3622c378889 Mon Sep 17 00:00:00 2001
From: Robin <robin@Amaya.(none)>
Date: Thu, 8 Apr 2010 14:49:48 +0200
Subject: [PATCH] fixed automatic creation/filling of mysql databases

---
 inc/lib_base.php   |  3 ++
 inc/lib_config.php | 93 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 92 insertions(+), 4 deletions(-)

diff --git a/inc/lib_base.php b/inc/lib_base.php
index 5d02cfeaa97..a196b6d78ab 100755
--- a/inc/lib_base.php
+++ b/inc/lib_base.php
@@ -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);
diff --git a/inc/lib_config.php b/inc/lib_config.php
index b54a4f4f168..d2853e71734 100755
--- a/inc/lib_config.php
+++ b/inc/lib_config.php
@@ -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();
+   }
 }
 ?>
 
-- 
GitLab