cvs: pear /Config/Config/Container db.php
| From: | Bertrand Mansion | Date: | Tue, 23 Jul 2002 16:00:12 +0000 |
| Subject: | cvs: pear /Config/Config/Container db.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5307@lists.php.net to get a copy of this message | ||
mansion Tue Jul 23 12:00:12 2002 EDT
Modified files:
/pear/Config/Config/Container db.php
Log:
Some modifications to make it more compliant with PEAR coding standards and more secure.
Index: pear/Config/Config/Container/db.php diff -u pear/Config/Config/Container/db.php:1.2 pear/Config/Config/Container/db.php:1.3 --- pear/Config/Config/Container/db.php:1.2 Tue Dec 11 14:45:37 2001 +++ pear/Config/Config/Container/db.php Tue Jul 23 12:00:12 2002 @@ -15,161 +15,194 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.2 2001/12/11 19:45:37 sebastian Exp $ +// $Id: db.php,v 1.3 2002/07/23 16:00:12 mansion Exp $ -require_once( "Config/Container.php" ) ; +require_once('Config/Container.php'); +require_once('DB.php'); /** * Config-API-Implemtentation for DB-Ini-Entries * * This class implements the Config-API based on ConfigDefault -* -* The Table structure should be as follows: -* +* The Table structure should be as follows by default: * CREATE TABLE config ( * datasrc varchar(50) NOT NULL, * block varchar(50) NOT NULL, * name varchar(50) NOT NULL, * value varchar(50) * ); -* -* You can name the Table other than config, but you have to supply then -* this name in the $feature->array +* You can name the table and fields as you prefer, but you have to supply +* these values in the $options array as parameter for the constructor. * * @author Christian Stocker <chregu@phant.ch> * @access public -* @version $Id: db.php,v 1.2 2001/12/11 19:45:37 sebastian Exp $ +* @version $Id: db.php,v 1.3 2002/07/23 16:00:12 mansion Exp $ * @package Config */ - class Config_Container_db extends Config_Container { - /** - * contains the features given by parseInput + * Contains the features given by parseInput * @var array * @see parseInput() */ - var $feature = array("Table" => "config") ; + var $feature = array( + 'table' => 'config', + 'blockcol' => 'block', + 'namecol' => 'name', + 'valuecol' => 'value', + 'datasrccol' => 'datasrc', + 'dsn' => '' + ); /** - * Constructor of the class - * + * DB object + * @var object + */ + var $db = null; + + /** + * Constructor * Connects to the DB via the PEAR::DB-class * - * @param $dsn string with PEAR::DB "data source name" or object DB object + * @param mixed $options can be a DSN string or an array of options */ - - function Config_Container_db ($dsn ) { - - //if dsn is a string, then we have to connect to the db - if (is_string($dsn)) - { - include_once ("DB.php"); - $this->db = DB::Connect($dsn); - if (DB::isError($db)) - { - print "The given dsn was not valid in file ".__FILE__." at line ".__LINE__."<br>\n"; - return new DB_Error($db->code,PEAR_ERROR_DIE); + function Config_Container_db ($feature) + { + if (is_array($feature)) { + $this->setFeatures($feature, array('datasrccol', 'table', + 'valuecol','namecol','blockcol', 'dsn')); + if ($this->feature['dsn'] == '') { + return new DB_Error('No connection parameters specified.'); } - + } else { + $this->feature['dsn'] = $feature; } - - //if it's an object, it could be a db_object. - elseif (is_object($dsn) && DB::isError($dsn)) - { - print "The given param was not valid in file ".__FILE__." at line ".__LINE__."<br>\n"; - return new DB_Error($dsn->code,PEAR_ERROR_DIE); - } - - // if parent class is db_common, then it's already a connected identifier - elseif (get_parent_class($dsn) == "db_common") - { + } // end constructor + + /** + * Connect to database by using the given DSN string or DB object + * + * @access private + * @param mixed DSN string or DB object + * @return mixed Object on error, otherwise bool + */ + function _connect($dsn) + { + if (is_string($dsn)) { + $this->db = DB::Connect($dsn); + } elseif (get_parent_class($dsn) == 'db_common') { $this->db = $dsn; + } elseif (is_object($dsn) && DB::isError($dsn)) { + return new DB_Error($dsn->code, PEAR_ERROR_DIE); + } else { + return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__, + 41, + PEAR_ERROR_RETURN, + null, + null + ); + } + if (DB::isError($this->db)) { + return new DB_Error($this->db->code, PEAR_ERROR_DIE); + } else { + return true; } + } // end func _connect - else { - return new PEAR_Error( "The given dsn ($dsn) was not valid in file ".__FILE__." at line ".__LINE__, 41, PEAR_ERROR_RETURN, null, null ); + /** + * Prepare query to the database + * + * This function checks if we have already opened a connection to + * the database. If that's not the case, a new connection is opened. + * After that the query is passed to the database. + * + * @access public + * @param string Query string + * @return True or DB_Error + */ + function query($query) + { + if (!DB::isConnection($this->db)) { + $this->_connect($this->feature['dsn']); } - - - } + return $this->db->query($query); + } // end func query /** - * parses the input of the given data source + * Parses the input of the given data source * * The Data Source can be a string with field-name of the datasrc field in the db * - * * @access public * @param string $datasrc Name of the datasource to parse * @param array $feature Contains a hash of features - * @return mixed returns a DB_ERROR, if error occurs + * @return mixed returns a DB_ERROR, if error occurs */ - - function parseInput( $datasrc = "", $feature = array( ) ) + function parseInput($datasrc = '', $feature = array()) { - $this->setFeatures($feature, array_merge($this->allowed_options,array("Table"))); + $this->setFeatures($feature, array('datasrccol', 'table', + 'valuecol','namecol','blockcol')); - $query = "SELECT block,name,value from ".$this->feature["Table"]." where datasrc = '$datasrc'"; - $res = $this->db->query($query); - if (DB::isError($res)) - { - return new DB_Error($dsn->code,PEAR_ERROR_DIE); - } - else - { - while ($entry = $res->FetchRow(DB_FETCHMODE_ASSOC)) - { - $this->data[$entry[block]][$entry[name]] = $entry[value]; + $query = sprintf("SELECT %s FROM %s WHERE %s = '%s'", + $this->feature['blockcol'].", ". + $this->feature['namecol'].", ". + $this->feature['valuecol'], + $this->feature['table'], + $this->feature['datasrccol'], + $datasrc + ); + + $res = $this->query($query); + if (DB::isError($res)) { + return new DB_Error($res->code, PEAR_ERROR_DIE); + } else { + while ($entry = $res->fetchRow()) { + $this->data[$entry[0]][$entry[1]] = $entry[2]; } } - - - } - + } // end func parseInput /** - * writes the data to the given data source or if not given to the datasource of parseInput - * If $datasrc was a array, the last file will used. + * Writes the data to the given data source or if not given to the datasource of parseInput * - * See parseInput for $datasrc. If the second argument $preserve is true, the implementation - * should try to preserve the original format and data of the source except changed or added values. - * This mean to preserve for example comments in files or so. + * Preserve is not supported in DB container * * @access public - * @param string $datasrc Name of the datasource to parse + * @param string $datasrc Name of the datasource to parse * @param boolean $preserve preserving behavior - * @return mixed returns a DB_ERROR, if error occurs - * @see parseInput() + * @return mixed returns a DB_ERROR, if error occurs */ - - function writeInput( $datasrc = "", $preserve = True ) + function writeInput($datasrc = '', $preserve = false) { - $query = "delete from ".$this->feature["Table"]." where datasrc = '$datasrc'"; - $res = $this->db->query($query); - if (DB::isError($res)) - { - return new DB_Error($dsn->code,PEAR_ERROR_DIE); - } - - - foreach ($this->data as $block => $blockarray) - { - - foreach ($blockarray as $name => $value) - { - $query = "insert into ".$this->feature["Table"]." (datasrc,block,name,value) VALUES - ('".addslashes($datasrc)."','".addslashes($block)."','".addslashes($name)."','".addslashes($value)."')"; - - $res = $this->db->query($query); - if (DB::isError($res)) - { - return new DB_Error($dsn->code,PEAR_ERROR_DIE); + $query = sprintf("DELETE FROM %s WHERE %s = '%s'", + $this->feature['table'], + $this->feature['datasrccol'], + $datasrc + ); + $res = $this->query($query); + if (DB::isError($res)) { + return new DB_Error($res->code, PEAR_ERROR_DIE); + } + foreach ($this->data as $block => $blockarray) { + foreach ($blockarray as $name => $value) { + $query = sprintf("INSERT INTO %s (%s, %s, %s, %s) VALUES ('%s', '%s', '%s', '%s')", + $this->feature['table'], + $this->feature['datasrccol'], + $this->feature['blockcol'], + $this->feature['namecol'], + $this->feature['valuecol'], + $datasrc, + $block, + $name, + $value + ); + $res = $this->query($query); + if (DB::isError($res)) { + return new DB_Error($res->code, PEAR_ERROR_DIE); } - } } - } -} + } // end func writeInput +} // end class Config_Container_db ?>
Index: pear/Config/Config/Container/db.php diff -u pear/Config/Config/Container/db.php:1.2 pear/Config/Config/Container/db.php:1.3 --- pear/Config/Config/Container/db.php:1.2 Tue Dec 11 14:45:37 2001 +++ pear/Config/Config/Container/db.php Tue Jul 23 12:00:12 2002 @@ -15,161 +15,194 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.2 2001/12/11 19:45:37 sebastian Exp $ +// $Id: db.php,v 1.3 2002/07/23 16:00:12 mansion Exp $ -require_once( "Config/Container.php" ) ; +require_once('Config/Container.php'); +require_once('DB.php'); /** * Config-API-Implemtentation for DB-Ini-Entries * * This class implements the Config-API based on ConfigDefault -* -* The Table structure should be as follows: -* +* The Table structure should be as follows by default: * CREATE TABLE config ( * datasrc varchar(50) NOT NULL, * block varchar(50) NOT NULL, * name varchar(50) NOT NULL, * value varchar(50) * ); -* -* You can name the Table other than config, but you have to supply then -* this name in the $feature->array +* You can name the table and fields as you prefer, but you have to supply +* these values in the $options array as parameter for the constructor. * * @author Christian Stocker <chregu@phant.ch> * @access public -* @version $Id: db.php,v 1.2 2001/12/11 19:45:37 sebastian Exp $ +* @version $Id: db.php,v 1.3 2002/07/23 16:00:12 mansion Exp $ * @package Config */ - class Config_Container_db extends Config_Container { - /** - * contains the features given by parseInput + * Contains the features given by parseInput * @var array * @see parseInput() */ - var $feature = array("Table" => "config") ; + var $feature = array( + 'table' => 'config', + 'blockcol' => 'block', + 'namecol' => 'name', + 'valuecol' => 'value', + 'datasrccol' => 'datasrc', + 'dsn' => '' + ); /** - * Constructor of the class - * + * DB object + * @var object + */ + var $db = null; + + /** + * Constructor * Connects to the DB via the PEAR::DB-class * - * @param $dsn string with PEAR::DB "data source name" or object DB object + * @param mixed $options can be a DSN string or an array of options */ - - function Config_Container_db ($dsn ) { - - //if dsn is a string, then we have to connect to the db - if (is_string($dsn)) - { - include_once ("DB.php"); - $this->db = DB::Connect($dsn); - if (DB::isError($db)) - { - print "The given dsn was not valid in file ".__FILE__." at line ".__LINE__."<br>\n"; - return new DB_Error($db->code,PEAR_ERROR_DIE); + function Config_Container_db ($feature) + { + if (is_array($feature)) { + $this->setFeatures($feature, array('datasrccol', 'table', + 'valuecol','namecol','blockcol', 'dsn')); + if ($this->feature['dsn'] == '') { + return new DB_Error('No connection parameters specified.'); } - + } else { + $this->feature['dsn'] = $feature; } - - //if it's an object, it could be a db_object. - elseif (is_object($dsn) && DB::isError($dsn)) - { - print "The given param was not valid in file ".__FILE__." at line ".__LINE__."<br>\n"; - return new DB_Error($dsn->code,PEAR_ERROR_DIE); - } - - // if parent class is db_common, then it's already a connected identifier - elseif (get_parent_class($dsn) == "db_common") - { + } // end constructor + + /** + * Connect to database by using the given DSN string or DB object + * + * @access private + * @param mixed DSN string or DB object + * @return mixed Object on error, otherwise bool + */ + function _connect($dsn) + { + if (is_string($dsn)) { + $this->db = DB::Connect($dsn); + } elseif (get_parent_class($dsn) == 'db_common') { $this->db = $dsn; + } elseif (is_object($dsn) && DB::isError($dsn)) { + return new DB_Error($dsn->code, PEAR_ERROR_DIE); + } else { + return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__, + 41, + PEAR_ERROR_RETURN, + null, + null + ); + } + if (DB::isError($this->db)) { + return new DB_Error($this->db->code, PEAR_ERROR_DIE); + } else { + return true; } + } // end func _connect - else { - return new PEAR_Error( "The given dsn ($dsn) was not valid in file ".__FILE__." at line ".__LINE__, 41, PEAR_ERROR_RETURN, null, null ); + /** + * Prepare query to the database + * + * This function checks if we have already opened a connection to + * the database. If that's not the case, a new connection is opened. + * After that the query is passed to the database. + * + * @access public + * @param string Query string + * @return True or DB_Error + */ + function query($query) + { + if (!DB::isConnection($this->db)) { + $this->_connect($this->feature['dsn']); } - - - } + return $this->db->query($query); + } // end func query /** - * parses the input of the given data source + * Parses the input of the given data source * * The Data Source can be a string with field-name of the datasrc field in the db * - * * @access public * @param string $datasrc Name of the datasource to parse * @param array $feature Contains a hash of features - * @return mixed returns a DB_ERROR, if error occurs + * @return mixed returns a DB_ERROR, if error occurs */ - - function parseInput( $datasrc = "", $feature = array( ) ) + function parseInput($datasrc = '', $feature = array()) { - $this->setFeatures($feature, array_merge($this->allowed_options,array("Table"))); + $this->setFeatures($feature, array('datasrccol', 'table', + 'valuecol','namecol','blockcol')); - $query = "SELECT block,name,value from ".$this->feature["Table"]." where datasrc = '$datasrc'"; - $res = $this->db->query($query); - if (DB::isError($res)) - { - return new DB_Error($dsn->code,PEAR_ERROR_DIE); - } - else - { - while ($entry = $res->FetchRow(DB_FETCHMODE_ASSOC)) - { - $this->data[$entry[block]][$entry[name]] = $entry[value]; + $query = sprintf("SELECT %s FROM %s WHERE %s = '%s'", + $this->feature['blockcol'].", ". + $this->feature['namecol'].", ". + $this->feature['valuecol'], + $this->feature['table'], + $this->feature['datasrccol'], + $datasrc + ); + + $res = $this->query($query); + if (DB::isError($res)) { + return new DB_Error($res->code, PEAR_ERROR_DIE); + } else { + while ($entry = $res->fetchRow()) { + $this->data[$entry[0]][$entry[1]] = $entry[2]; } } - - - } - + } // end func parseInput /** - * writes the data to the given data source or if not given to the datasource of parseInput - * If $datasrc was a array, the last file will used. + * Writes the data to the given data source or if not given to the datasource of parseInput * - * See parseInput for $datasrc. If the second argument $preserve is true, the implementation - * should try to preserve the original format and data of the source except changed or added values. - * This mean to preserve for example comments in files or so. + * Preserve is not supported in DB container * * @access public - * @param string $datasrc Name of the datasource to parse + * @param string $datasrc Name of the datasource to parse * @param boolean $preserve preserving behavior - * @return mixed returns a DB_ERROR, if error occurs - * @see parseInput() + * @return mixed returns a DB_ERROR, if error occurs */ - - function writeInput( $datasrc = "", $preserve = True ) + function writeInput($datasrc = '', $preserve = false) { - $query = "delete from ".$this->feature["Table"]." where datasrc = '$datasrc'"; - $res = $this->db->query($query); - if (DB::isError($res)) - { - return new DB_Error($dsn->code,PEAR_ERROR_DIE); - } - - - foreach ($this->data as $block => $blockarray) - { - - foreach ($blockarray as $name => $value) - { - $query = "insert into ".$this->feature["Table"]." (datasrc,block,name,value) VALUES - ('".addslashes($datasrc)."','".addslashes($block)."','".addslashes($name)."','".addslashes($value)."')"; - - $res = $this->db->query($query); - if (DB::isError($res)) - { - return new DB_Error($dsn->code,PEAR_ERROR_DIE); + $query = sprintf("DELETE FROM %s WHERE %s = '%s'", + $this->feature['table'], + $this->feature['datasrccol'], + $datasrc + ); + $res = $this->query($query); + if (DB::isError($res)) { + return new DB_Error($res->code, PEAR_ERROR_DIE); + } + foreach ($this->data as $block => $blockarray) { + foreach ($blockarray as $name => $value) { + $query = sprintf("INSERT INTO %s (%s, %s, %s, %s) VALUES ('%s', '%s', '%s', '%s')", + $this->feature['table'], + $this->feature['datasrccol'], + $this->feature['blockcol'], + $this->feature['namecol'], + $this->feature['valuecol'], + $datasrc, + $block, + $name, + $value + ); + $res = $this->query($query); + if (DB::isError($res)) { + return new DB_Error($res->code, PEAR_ERROR_DIE); } - } } - } -} + } // end func writeInput +} // end class Config_Container_db ?>