From 10e4f16c815235e5c8393ed3c7629ba8f4c62f23 Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 8 Jul 2007 13:08:38 +0000 Subject: [PATCH] add DB driver architecture provide convenience loader add admin page git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@7 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- www/.htaccess | 7 ++++ www/admin.php | 9 +++++ www/inc/db.php | 59 +++++++++++++++++++++++++++++++++++ www/inc/db_mysql.php | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ www/index.php | 6 ++- www/loader.php | 9 +++++ www/loader2.php | 5 +++ 7 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 www/.htaccess create mode 100644 www/admin.php create mode 100644 www/inc/db.php create mode 100644 www/inc/db_mysql.php create mode 100644 www/loader.php create mode 100644 www/loader2.php diff --git a/www/.htaccess b/www/.htaccess new file mode 100644 index 0000000..82ada0c --- /dev/null +++ b/www/.htaccess @@ -0,0 +1,7 @@ + + Deny from all + + + Deny from all + + diff --git a/www/admin.php b/www/admin.php new file mode 100644 index 0000000..4a7e3a3 --- /dev/null +++ b/www/admin.php @@ -0,0 +1,9 @@ +canAdministrate()) + die("Administration is turned off. Sorry."); +?> +

Magic Smoke Admin Utility

+ +

Checking DB

\ No newline at end of file diff --git a/www/inc/db.php b/www/inc/db.php new file mode 100644 index 0000000..22567c5 --- /dev/null +++ b/www/inc/db.php @@ -0,0 +1,59 @@ +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 index 0000000..d3a43b0 --- /dev/null +++ b/www/inc/db_mysql.php @@ -0,0 +1,83 @@ +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 diff --git a/www/index.php b/www/index.php index 5fa4215..ba57067 100644 --- a/www/index.php +++ b/www/index.php @@ -1,6 +1,8 @@ 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 index 0000000..a22f454 --- /dev/null +++ b/www/loader2.php @@ -0,0 +1,5 @@ +canUseDb()) + die("Database is not correctly configured. Giving up."); +?> \ No newline at end of file -- 1.7.2.5