<?/****************************************************************** * -db.abstract.php: Database control, An easier way to define/change behavior later * on.. or port the entire mess to another type of database altogether, etc.. * * Abstract, base toolkit definitions... for later extension/implementation * * (C) 2008 and beyond, Vanguard Technical & Media Services (http://www.vtmsonline.com/) * - Author: Bryan Steimel 3/22/08 ******************************************************************/abstract class DBa { const PREP_HOP = 1; // escape and quote everything, all demensions.. in place - default behavior edit things in place, leave structure untouched const PREP_JOIN = 2; // this will flatten all arrays > 2 demensional, formatting and inserting subarrays as they are encountered const PREP_COLLAPSE = 4; // destroy subarrays, provide '' null as a placeholder const PREP_FLATTEN = 8; // completely flattens internal structure, by default.. array(array(array()), array()) each first level subarray will be handled individually, to aid in packaging multiple INSERTS and the like at once.. when this flag is provided, all data will be merged into a single regardless of depth or seperation protected $host, $username, $password; protected $error = null; public $handle, $persistant, $flags; public $query; public $result = null; private $connected = false; abstract public function connect(); abstract public function close(); abstract public function error(); abstract public function query($query=''); abstract public function sanitize($string); final public function __construct($connect=false, $host=null, $user=null, $pass=null, $database=null, $persist=false, $new=false) { list($this->host, $this->username, $this->password, $this->persistant, $this->database) = array($host, $user, $pass, $database, $database, $persist); if($connect) $this->connect($host, $user, $pass, $database, $persist, $new); } public function &prep(&$data, $callback, $op = self::PREP_HOP) { if($op & self::PREP_FLATTEN) { $op -= self::PREP_FLATTEN; $data = array($data); } foreach($data as &$v) { if(is_array($v)) { if($op & self::PREP_COLLAPSE) $v = array_map('_collapse', $v); $v = (($op & self::PREP_JOIN) ? join(',', self::prep($v, $callback, $op)) : self::prep($v, $callback, $op) ); } else $v = "'" . $callback($v) . "'"; } return $data; } public function connected() { return $this->connected; } public function exec($query) { /* This will NOT return OR assign a result object */ return ($this->query($query) !== false) ? $this->affected() : null; } } ?><?/****************************************************************** * -db.class.php: Database control, An easier way to define/change behavior later * on.. or port the entire mess to another type of database altogether, etc.. * * In atleast this instance, employ exceptions as opposed to indiscriminate default * error messages.. to avoid releasing any sensitive information they might contain, * and to provide to infinite control over the actions taken in such an event. * further force desired options, as to not permit assumptions to be made by * the functions called or php itself * * (C) 2008 and beyond, Vanguard Technical & Media Services (http://www.vtmsonline.com/) * - Author: Bryan Steimel 3/22/08 ******************************************************************/class db_result { public $resource; private $data = array(); private function __construct($result) { $this->resource = $result; } public function next($list=false) { $this->data = @mysql_fetch_array($this->resource, $list ? MYSQL_NUM : MYSQL_ASSOC); } public function rows() { return @mysql_num_rows($this->resource); } public function free() { @mysql_free_result($this->resource); } private function __set($indice, $value) { return $this->data[$indice] = $value; } private function __get($indice) { return isset($this->data[$indice]) ? $this->data[$incide] : null; } private function __destruct() { $this->free(); } } /* should probably be generic/abstract definition/interface, to aid in forcing further implementation, * but for these specific purposes I dont see the point in putting forth * the additional trouble - besides, theres far too many issues with PHP's implementation * of interface */class DB extends DBa { public function connect($host=null, $user=null, $pass=null, $database=null, $persist=false, $new=false) { parent::__construct(false, $host, $user, $pass, $database, $persist, $new); if($this->connected = ( ($this->handle = $this->persistant ? @mysql_pconnect($this->host, $this->username, $this->password, $new, $this->flags) : @mysql_connect($this->host, $this->username, $this->password, $new, $this->flags) ) !== false) ? true : false) { $this->select_db(); return $this->handle; } throw(new DBException('Failed to establish a connection to the requested host on port ' . $port . '.')); } public function close() { return @mysql_close($this->handle); } public function select_db($db=null) { if(!is_null($db)) $this->database = $db; if($r = @mysql_select_db($this->database, $this->handle)) return $r; throw(new DBException('Failed to select database \'' . $this->database . '\'.')); } public function query($query = null) { if(!is_null($query)) $this->query = $query; /* re-execute LAST query if null */ if(($result = @mysql_query($this->query, $this->handle)) !== false) return is_resource($result) ? ($this->result = new db_result($result)) : $result; throw(new DBException('Query failed.')); } public function exec($query) { /* This will NOT return OR assign a result object */ if(is_object($result = parent::exec($query))) throw(new DBException('exec(): INSERT/DELETE/UPDATE only')); return $result; } public function insert($table, $values = array(), $data = array()) { } public function affected() { return mysql_affected_rows($this->resource); } public function sanitize($string) { return mysql_real_escape_string($string); } public function prep(&$list, $flags=parent::PREP_HOP) { return parent::prep(&$list, 'sanitize', $flags); } public function error() { return $this->error = mysql_error(); } public function __destruct() { } } function sanitize($l) { return db::sanitize($l); }?>