Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @version database.php 2011-02-14 08:01
- * @package ErCox.Framework
- * @copyright Copyright (C) ErCox, Damjan Krstevski. All rights reserved.
- * @license GNU/GPL, see LICENSE.php
- */
- /**
- * Database class
- *
- * @package ErCox.Framework
- */
- class Database {
- /**
- * MySql Connection
- *
- * @access public
- * @var resource
- */
- public $connection = NULL;
- /**
- * MySql Hostname
- *
- * @access private
- * @var string
- */
- private $DbHost = NULL;
- /**
- * MySql Root Username
- *
- * @access private
- * @var string
- */
- private $DbUser = NULL;
- /**
- * MySql Root Password
- *
- * @access private
- * @var string
- */
- private $DbPass = NULL;
- /**
- * Used Database
- *
- * @access private
- * @var string
- */
- private $DbData = NULL;
- /**
- * Database TimeOut
- *
- * @access private
- * @var int
- */
- private $DbTimeout = 10;
- /**
- * Database Table Prefix
- *
- * @access private
- * @var string
- */
- private $DbPrefix = NULL;
- /**
- * MySql Error Number
- *
- * @access private
- * @var int
- */
- private $DbError = 0;
- /**
- * Database object constructor
- *
- * @access public
- * @param array $params Database Information (Host, User, Pass, Data, Prefix, [TimeOut])
- */
- public function __construct( $params = NULL )
- {
- /* Validate Params */
- if ( !$this->set_dbparams($params) )
- {
- return;
- }
- /* Validate if the MySQL functions is defined */
- if( !$this->test_db_functions(true) )
- {
- return;
- }
- /* Connect with the MySql database */
- if( !$this->connect() )
- {
- return;
- }
- if( !$this->select_db() )
- {
- return;
- }
- if( !$this->set_charset() )
- {
- return;
- }
- }
- /**
- * Database object destructor
- *
- * @access public
- */
- public function __destruct()
- {
- /* Close the connection */
- $this->close();
- /* Free Memory */
- $this->DbHost = NULL;
- $this->DbUser = NULL;
- $this->DbPass = NULL;
- $this->DbData = NULL;
- $this->DbPrefix = NULL;
- }
- /**
- * Method to check if the MySql functions are defined
- *
- * @access public
- * @param boolean $all True to check all MySql functions that are used
- * @return boolean True if the MySql functions are defined
- */
- public function test_db_functions( $all = false )
- {
- if( !function_exists("mysql_connect") )
- {
- return false;
- }
- if( !function_exists("mysql_select_db") )
- {
- return false;
- }
- if( !function_exists("mysql_query") )
- {
- return false;
- }
- if( !function_exists("mysql_close") )
- {
- return false;
- }
- /* Validate non essential functions */
- if( $all === true )
- {
- if( !function_exists("mysql_free_result") )
- {
- return false;
- }
- if( !function_exists("mysql_real_escape_string") )
- {
- return false;
- }
- if( !function_exists("mysql_set_charset") )
- {
- return false;
- }
- }
- return true;
- }
- /**
- * Set database information
- *
- * @access public
- * @param array $params Database Information (Host, User, Pass, Data, Prefix, [TimeOut])
- * @return boolean True if successful
- */
- public function set_dbparams( $params )
- {
- /* Validate params */
- if( empty($params) ) { $this->set_dberror(1); return false; } /* Invalid params */
- /* Set the values */
- $this->DbHost = isset($params["host"]) ? $params["host"] : "";
- $this->DbUser = isset($params["user"]) ? $params["user"] : "";
- $this->DbPass = isset($params["pass"]) ? $params["pass"] : "";
- $this->DbData = isset($params["data"]) ? $params["data"] : "";
- $this->DbPrefix = isset($params["prefix"]) ? $params["prefix"] : "";
- $this->DbTimeout = isset($params["timeout"]) ? $params["timeout"] : $this->DbTimeout;
- return true;
- }
- /**
- * Set database error
- *
- * @access private
- * @param int Error number
- */
- private function set_dberror( $no )
- {
- $this->DbError = $no;
- }
- /**
- * Get database error
- *
- * @access public
- * @return int Error Number
- */
- public function get_dberror()
- {
- return $this->DbError;
- }
- /**
- * Set database timeout
- *
- * The configuration option will keep this new value during the script's execution
- *
- * @access private
- * @param int $to Database timeout
- */
- private function set_dbtimeout( $to )
- {
- $this->DbTimeout = $to;
- }
- /**
- * Get database timeout value
- *
- * @access public
- * @return int Database timeout value
- */
- public function get_dbtimeout()
- {
- return $this->DbTimeout;
- }
- /**
- * Set database hostname
- *
- * @access private
- * @param string $val Database hostname
- */
- private function set_dbhost( $val )
- {
- $this->DbHost = empty($val) ? $val : "";
- }
- /**
- * Get database hostname
- *
- * @access public
- * @return string Database hostname
- */
- public function get_dbhost()
- {
- return $this->DbHost;
- }
- /**
- * Set database username
- *
- * @access private
- * @param string $val Database root password
- */
- private function set_dbuser( $val )
- {
- $this->DbUser = empty($val) ? $val : "";
- }
- /**
- * Get database username
- *
- * @access public
- * @return string Database root username
- */
- public function get_dbuser()
- {
- return $this->DbUser;
- }
- /**
- * Set database timeout value
- *
- * @access private
- * @param string $val Database root password
- */
- private function set_dbpass( $val )
- {
- $this->DbPass = empty($val) ? $val : "";
- }
- //public function get_dbpass() {} This function is not valid for safety reasons
- /**
- * Set the name of the used
- *
- * @access private
- * @param string $val The name of the database
- */
- private function set_dbdata( $val )
- {
- $this->DbData = empty($val) ? $val : "";
- }
- /**
- * Get the name of the database
- *
- * @access public
- * @return string The name of the used database
- */
- public function get_dbdata()
- {
- return $this->DbData;
- }
- /**
- * Set table prefix
- *
- * @access private
- * @param string $val The table prefix
- */
- private function set_dbprefix( $val )
- {
- $this->DbPrefix = empty($val) ? $$val : "";
- }
- /**
- * Get table prefix
- *
- * @access public
- * @return string The table prefix
- */
- public function get_dbprefix()
- {
- return $this->DbPrefix;
- }
- /**
- * Method to connect to a MySql database
- *
- * @access public
- * @return boolean True if successful
- */
- public function connect()
- {
- /* If already connected, return */
- if( $this->is_connected($this->connection) )
- {
- return true;
- }
- /* Validate param if is int */
- if( !is_int($this->DbTimeout) )
- {
- $this->DbTimeout = 10;
- }
- /* Set the timeout */
- @ini_set("mysql.connect_timeout", $this->DbTimeout);
- /* Connect with the MySql database */
- $this->connection = @mysql_connect($this->DbHost, $this->DbUser, $this->DbPass);
- return $this->is_connected($this->connection) ? true : false;
- }
- /**
- * Method to determine if the object is connected to an MySql server
- *
- * @access public
- * @param string $link The database connection
- * @param boolean $flag If flag is true will try to connect to the database [Default: false]
- * @return boolean True if connected
- */
- public function is_connected( $link, $flag = false )
- {
- /* Only determine if the object is connected */
- if( $flag === false )
- {
- return @is_resource($this->connection) ? true : false;
- }
- else
- {
- /* If object is connected return */
- if( @is_resource($this->connection) )
- {
- return true;
- }
- else
- {
- /* Try to connect and if not sucessfully return */
- if( $this->connect() === false )
- {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * Method for selecting the database to be used
- *
- * @access public
- * @return boolean True if successful
- */
- public function select_db()
- {
- /* Determine connection, if not connected try to connect */
- if( !$this->is_connected($this->connection, true) )
- {
- return false;
- }
- return @mysql_select_db($this->DbData) ? true : false;
- }
- /**
- * Method to execute SQL command
- *
- * @access public
- * @param string $sql The SQL command
- * @return boolean True if successful
- */
- public function query( $sql )
- {
- /* Validate SQL value */
- if( empty($sql) )
- {
- return false;
- }
- /* Determine connection, if not connected try to connect */
- if( !$this->is_connected($this->connection, true) )
- {
- return false;
- }
- return @mysql_query($sql, $this->connection) ? true : false;
- }
- /**
- * Method to check whether or not the connection to the server is working.
- *
- * @access public
- * @return boolean True if successful
- */
- public function ping_db()
- {
- return @mysql_ping($this->connection) ? true : false;
- }
- /**
- * Method to free all memory associated with the result identifier
- *
- * @access public
- * @param resource $result The result resource that is being evaluated
- * @return boolean True if successful
- */
- public function free_memory( $result )
- {
- /* Allow only resource */
- if( !is_resource($result) )
- {
- return false;
- }
- return @mysql_free_result($this->connection) ? true : false;
- }
- /**
- * Method to close the database connection
- *
- * @access public
- * @return boolean True if successful
- */
- public function close()
- {
- /* If already connected, try to close database connection */
- if( $this->is_connected($this->connection) )
- {
- return @mysql_close($this->connection) ? true : false;
- }
- return true;
- }
- /**
- * Method to escapes special characters in a string for use in an SQL statement
- *
- * @access public
- * @param string $str The string that is to be escaped
- * @return string True if successful
- */
- public function escape( $str )
- {
- if( empty($str) )
- {
- $str = "";
- return true;
- }
- return @mysql_real_escape_string($str);
- }
- /**
- * Method to sets the client character set
- *
- * @access public
- * @param string $charset A valid character set name [Default UTF8]
- * @return boolean True if successful
- */
- public function set_charset( $charset = "UTF8" )
- {
- if( empty($charset) )
- {
- $charset = "UTF8";
- }
- return @mysql_set_charset($charset) ? true : false;
- }
- /* mysql_fetch_field */
- /* mysql_result */
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement