add DB driver architecture
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 8 Jul 2007 13:08:38 +0000 (13:08 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 8 Jul 2007 13:08:38 +0000 (13:08 +0000)
provide convenience loader
add admin page

git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@7 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

www/.htaccess [new file with mode: 0644]
www/admin.php [new file with mode: 0644]
www/inc/db.php [new file with mode: 0644]
www/inc/db_mysql.php [new file with mode: 0644]
www/index.php
www/loader.php [new file with mode: 0644]
www/loader2.php [new file with mode: 0644]

diff --git a/www/.htaccess b/www/.htaccess
new file mode 100644 (file)
index 0000000..82ada0c
--- /dev/null
@@ -0,0 +1,7 @@
+<Files loader*.php>
+ Deny from all
+</Files>
+<Files config.php*>
+ Deny from all
+</Files>
+
diff --git a/www/admin.php b/www/admin.php
new file mode 100644 (file)
index 0000000..4a7e3a3
--- /dev/null
@@ -0,0 +1,9 @@
+<?
+include('loader.php');
+
+if(!$db->canAdministrate())
+       die("Administration is turned off. Sorry.");
+?>
+<h1>Magic Smoke Admin Utility</h1>
+
+<h2>Checking DB</h2>
\ No newline at end of file
diff --git a/www/inc/db.php b/www/inc/db.php
new file mode 100644 (file)
index 0000000..22567c5
--- /dev/null
@@ -0,0 +1,59 @@
+<?
+/**Abstract base class for database engines*/
+abstract class DbEngine
+{
+       /**passcode for admin.php*/
+       private $adminpass=false;
+       
+       //public function __construct();
+       
+       /**connect to the database, must be overwritten by DB driver*/
+       public abstract function tryConnect();
+       
+       /**set the admin passcode*/
+       public function setAdminPassCode($p)
+       {
+               $this->adminpass=$p;
+       }
+       
+       /**returns whether a passcode is known and admin.php may be used*/
+       public function canAdministrate()
+       {
+               return $this->adminpass!==false;
+       }
+       
+       /**returns the version of the DB layout that is required by this version of Magic Smoke*/
+       public function needVersion(){return "00.01";}
+       
+       /**returns whether the table exists; must be implemented by driver*/
+       protected abstract function haveTable($tablename);
+       
+       /**begins a transaction; must be implemented by driver*/
+       protected abstract function beginTransaction();
+       
+       /**ends a transaction successfully; must be implemented by driver; returns true on success*/
+       protected abstract function commitTransaction();
+       
+       /**ends a transaction with a rollback; must be implemented by driver; returns true on success*/
+       protected abstract function rollbackTransaction();
+       
+       /**gets some data from the database; $table is the name of the table, $cols is the list of columns to return or "*" for all, $where is the where clause of the SQL-statement; returns array of rows, which are in *_fetch_array format; returns false on error*/
+       protected abstract function select($table,$cols,$where);
+       
+       /**returns a configuration setting*/
+       public function getConfig($key)
+       {
+               $mar=$this->select("config","cval","ckey='".addslashes($key)."'");
+               if(count($mar)>0)return $mar[0][0];
+       }
+       
+       /**tries to find out whether the connected DB version is usable*/
+       public function canUseDb()
+       {
+               if(!$this->haveTable("config"))
+                       return false;
+               return getConfig("MagicSmokeVersion")==$this->needVersion();
+       }
+};
+
+?>
\ No newline at end of file
diff --git a/www/inc/db_mysql.php b/www/inc/db_mysql.php
new file mode 100644 (file)
index 0000000..d3a43b0
--- /dev/null
@@ -0,0 +1,83 @@
+<?
+class MysqlEngine extends DbEngine
+{
+       /**prefix for table names*/
+       protected $prefix="";
+
+       private $user;
+       private $server;
+       private $pass;
+       private $dbhdl=false;
+       private $dbname="";
+       private $engine="InnoDB";
+       
+       /**initialize driver*/
+       public function __construct($server,$user,$pass)
+       {
+               $this->user=$user;
+               $this->server=$server;
+               $this->pass=$pass;
+       }
+       
+       /**set a table-name prefix for the database*/
+       public function setPrefix($pre)
+       {
+               $this->prefix=$pre;
+       }
+       
+       /**set the name of the database to be used*/
+       public function setDbName($dbn)
+       {
+               $this->dbname=$dbn;
+       }
+       
+       /**set the name of the storage engine to be used on DB creation*/
+       public function setStorageEngine($e)
+       {
+               $this->engine=$e;
+       }
+       
+       public function tryConnect()
+       {
+               $this->dbhdl=mysql_connect($this->server,$this->user,$this->pass);
+               if($this->dbhdl===false)
+                       die("Unable to connect to database system. Giving up.");
+               if(mysql_query("use $this->dbname",$this->dbhdl)===false)
+                       die("Unable to select database \"$this->dbname\". Giving up.");
+       }
+       
+       protected function haveTable($tnm)
+       {
+               $res=mysql_query("select * from ".$this->prefix.$tnm." where 1=2",$this->dbhdl);
+               if($res===false)return false;
+               mysql_free_result($res);
+               return true;
+       }
+       protected function beginTransaction()
+       {
+               mysql_query("begin transaction");
+       }
+       
+       protected function commitTransaction()
+       {
+               return mysql_query("commit transaction");
+       }
+       
+       protected function rollbackTransaction()
+       {
+               return mysql_query("rollback transaction");
+       }
+       
+       protected function select($table,$cols,$where)
+       {
+               $res=mysql_query("select $cols from ".$this->prefix.$table." where ".$where);
+               if($res===false)return false;
+               $nr=mysql_num_rows($res);
+               $ret=array();
+               for($i=0;$i<$max;$i++){
+                       $ret[]=mysql_fetch_array($res,MYSQL_BOTH);
+               }
+               mysql_free_result($res);
+               return $ret;
+       }
+};
\ No newline at end of file
index 5fa4215..ba57067 100644 (file)
@@ -1,6 +1,8 @@
 <? 
-//include other scripts
-include('config.php');
+//basics
+include('loader.php');
+include('loader2.php');
+//include display scripts
 include('inc/listing.php');
 include('inc/parser.php');
 
diff --git a/www/loader.php b/www/loader.php
new file mode 100644 (file)
index 0000000..cd67ba4
--- /dev/null
@@ -0,0 +1,9 @@
+<?
+//load DB drivers
+include('inc/db.php');
+include('inc/db_mysql.php');
+include('config.php');
+//try to connect
+$db->tryConnect();
+//move on in loader2.php (or admin.php)
+?>
\ No newline at end of file
diff --git a/www/loader2.php b/www/loader2.php
new file mode 100644 (file)
index 0000000..a22f454
--- /dev/null
@@ -0,0 +1,5 @@
+<?
+//check database
+if(!$db->canUseDb())
+       die("Database is not correctly configured. Giving up.");
+?>
\ No newline at end of file