Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- setlocale(LC_ALL, 'nl_NL');
- /**
- * @author Manuel de Ruiter <manuel@alien-conquest.nl>
- * @copyright Copyright (c) 2009, Manuel de Ruiter
- * @category Database initialisation
- * @package Alien Conquest
- * @version 1.0
- */
- class AlienConquestInitDB {
- /**
- * $connection is a object for the MySQL connection.
- * @access public
- * @since 1.0
- * @return mixed
- */
- public $connection;
- /**
- * The database object
- * @access private
- * @since 1.004
- * @var $handle
- */
- private $_handle = null;
- /**
- * @access private
- * @since 1.0
- * @var $server is the datatype + host where the server is located
- * @var $username is the database username
- * @var $password is the database password
- * @var $data is the database in string format
- */
- private $server, $username, $password, $data;
- /**
- * Initialise database connection
- * @access private
- * @since 1.0
- * @param string $host The database host
- * @param string $username The database username
- * @param string $password The database password
- * @param string $data The database name
- * @return boolean
- */
- private function __construct($host="mysql:host=127.0.0.1",$username="acgame_listen",$password="Qp",$data="dbname=game_1")
- {
- $this->server = $host;
- $this->username = $username;
- $this->password = $password;
- $this->data = $data;
- $this->CreateConnection();
- return;
- }
- /**
- * Create a connection to the database
- * @access private
- * @since 1.001
- * @return object
- */
- private function CreateConnection()
- {
- try {
- $this->_handle = new PDO($this->server . ";" . $this->data, $this->username, $this->password, array(
- PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
- PDO::ATTR_PERSISTENT => false
- ));
- } catch(PDOException $e) {
- $this->_handle = null;
- die(file_get_contents("/srv/www/vhosts/alien-conquest.nl/htdocs/AC-Beta-Beta/b-laag/lib/messages/server-offline.txt"));
- }
- }
- /**
- * @access public
- * @since 1.0
- * @return boolean
- */
- public function __destruct()
- {
- /*
- Close the connection from the database.
- */
- $this->_handle = null;
- }
- /**
- * PHP magic method
- * @access public
- * @since 1.001
- * @return array
- */
- public function __sleep()
- {
- return array("server", "username", "password", "data");
- }
- /**
- * PHP magic method
- * @access public
- * @since 1.001
- * @return array
- */
- public function __wakeup()
- {
- $this->CreateConnection();
- }
- /**
- * The singleton pattern
- * @access public
- * @since 1.004
- * @return object
- */
- public static function get()
- {
- static $db = null;
- if($db == null) {
- $db = new AlienConquestInitDB();
- }
- return $db;
- }
- /**
- * Return the database object
- * @access public
- * @since 1.004
- * @return object
- */
- public function handle()
- {
- return $this->_handle;
- }
- }
- ?>
- /** --- DBFunctions.inc.php */
- <?php
- /**
- * @author Manuel de Ruiter <manuel@alien-conquest.nl>
- * @copyright Copyright (c) 2009, Manuel de Ruiter
- * @category Database functions
- * @package Alien Conquest
- * @version 1.0
- */
- require("/srv/www/vhosts/alien-conquest.nl/htdocs/AC-Beta-Beta/d-laag/dbconnection.inc.php");
- class AlienConquestBasicDatabase {
- /**
- * @access public
- * @since 1.0
- * @static
- * @return object
- */
- public static $db;
- /**
- * @access public
- * @since 1.0
- * @return object
- */
- public $connect, $firebug;
- /**
- * Initialise constructor
- * @access public
- * @since 1.0
- * @return mixed
- */
- public function __construct()
- {
- $this->connect = AlienConquestInitDB::get()->handle();
- }
- /**
- * Export the results to XML
- * @access public
- * @since 1.0
- * @param string $query The query results that needs to be transformed to SQL.
- * @return mixed
- */
- public static function ExportResultsXML($query)
- {
- $num = $this->NumRowsStatement($query);
- $xml = "<?xml version=\"1.0\" ?>\n";
- $xml .= "<resultset query=\"".$this->EscapeData($query)."\" nrows=\"".($num ? $num : 0)."\">\n";
- if($this->NumRowsStatement($query)) {
- $xml .= "\t<rowvalues>\n";
- $result = $this->RunQueryWithResults($query);
- foreach($this->GetTableFields($query) as $a => $b) {
- $xml .= "\t\t<row name=\"" . $b . "\">" . $result[$a] . "</row>\n";
- }
- $xml .= "\t</rowvalues>\n";
- } else {
- $xml .= "\t<noresults>\n";
- $xml .= "\t\t<row>No results!</row>\n";
- $xml .= "\t</noresults>\n";
- }
- $xml .= "\t<time>\n";
- $xml .= "\t\t<year>" . date("Y", time()) . "</year>\n";
- $xml .= "\t\t<month>" . date("m", time()) . "</month>\n";
- $xml .= "\t\t<day>" . date("d", time()) . "</day>\n";
- $xml .= "\t\t<fulldate>" . date("d-m-Y H:i:s") . "</fulldate>\n";
- $xml .= "\t</time>\n";
- $xml .= "</resultset>";
- return $xml;
- }
- /**
- * Fetch database results and put them in a array
- * @access public
- * @since 1.004
- * @param string $query The query that must be executed
- * @param array $params The parameters for a bindValue()
- * @return array
- */
- public function QueryWithSelectField_Daemon($query, $params = array())
- {
- $this->connect->beginTransaction();
- $sql = $this->connect->prepare($query);
- if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
- if($sql->execute() === true) {
- $a = array();
- while(($row = $sql->fetch(PDO::FETCH_ASSOC)) !== false) {
- $a[] = $row;
- }
- $this->connect->commit();
- $sql->closeCursor();
- return $a;
- } else {
- $this->connect->rollBack();
- return false;
- }
- }
- /**
- * A database function to count the rows that are in that table.
- * <code>
- * <?php
- * NumRows("", "ac_user");
- * ?>
- * </code>
- * @access public
- * @since 1.0
- * @param boolean|string $rows The rows that need to be counted.
- * @param string $table The table where must be counted from.
- * @param boolean|array The where query
- * @param boolean|int $limit If there is a limit.
- * @return int|boolean
- */
- public function NumRows($rows = false, $table, $where = false, $limit = false)
- {
- $rows = (!$rows || "" ? "*" : $rows);
- $query = "SELECT
- " . $rows . "
- FROM
- " . $table . "
- " . ($where ? "WHERE " . $where[0] . "='" . $where[1] . "'" : "") . "
- " . ($limit ? "LIMIT " . $limit : "");
- $this->connect->beginTransaction();
- $sql = $this->connect->prepare($query);
- if($sql->execute() === true) {
- $this->connect->commit();
- $sql->closeCursor();
- return $sql->rowCount();
- } else {
- $this->connect->rollBack();
- return false;
- }
- }
- /**
- * Count the rows by using a stored procedure.
- * <code>
- * <?php
- * NumRowsStatement("CALL xxxxxxxxxxxx();");
- * ?>
- * </code>
- * @access public
- * @since 1.0
- * @param string $query The query that needs to be executed.
- * @param array $params The parameters for a bindValue
- * @return string|boolean
- */
- public function NumRowsStatement($query, $params = array())
- {
- $this->connect->beginTransaction();
- $sql = $this->connect->prepare($query);
- if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
- if($sql->execute() === true) {
- $this->connect->commit();
- $sql->closeCursor();
- return $sql->rowCount();
- } else {
- $this->connect->rollBack();
- return null;
- }
- }
- /**
- * Execute a query on the database.
- * <code>
- * <?php
- * RunQuery("SELECT * FROM xxxxxxxxxxxx");
- * ?>
- * </code>
- * @access public
- * @since 1.0
- * @param string $query The query that needs to be executed.
- * @param array $params The parameters for a bindValue
- * @return boolean
- */
- public function RunQuery($query, $params = array())
- {
- $this->connect->beginTransaction();
- $sql = $this->connect->prepare($query);
- if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
- if($sql->execute() === true) {
- $this->connect->commit();
- $sql->closeCursor();
- return true;
- } else {
- $this->connect->rollBack();
- return null;
- }
- }
- /**
- * Fetch all tables fields that must be listed
- * <code>
- * <?php
- * GetTableFields($query);
- * ?>
- * </code>
- * @access public
- * @since 1.0
- * @param string $query The query that needs to be executed.
- * @ignore This function is only for debug use!
- * @return array
- */
- public function GetTableFields($query)
- {
- $this->connect->beginTransaction();
- $sql = $this->connect->prepare($query);
- if($sql->execute() === true) {
- while(($row = $sql->fetch(PDO::FETCH_ASSOC)) !== false) {
- $a[] = $row;
- }
- foreach($a as $k => $v) {
- foreach($v as $f => $e) {
- $b[] = $f;
- }
- }
- $this->connect->commit();
- $sql->closeCursor();
- return $b;
- } else {
- $this->connect->rollBack();
- return null;
- }
- }
- /**
- * Execute the query and list the new results, specially for SELECT query's.
- * <code>
- * <?php
- * RunQueryWithResults("SELECT *******");
- * ?>
- * </code>
- * @access public
- * @since 1.0
- * @param string $query The query that needs to be executed.
- * @return array
- */
- public function RunQueryWithResults($query, $params = array())
- {
- $this->connect->beginTransaction();
- $sql = $this->connect->prepare($query);
- if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
- if($sql->execute() === true) {
- $a = array(); $b = array(); $k = array(); $v = array(); $f = array(); $e = array();
- while(($row = $sql->fetch(PDO::FETCH_ASSOC)) !== false) {
- $a[] = $row;
- }
- foreach($a as $k => $v) {
- foreach($v as $f => $e) {
- $b[] = $e;
- }
- }
- $this->connect->commit();
- $sql->closeCursor();
- return $b;
- } else {
- $this->connect->rollBack();
- return null;
- }
- }
- /**
- * Escape all characters
- * <code>
- * <?php
- * EscapeData("String")
- * ?>
- * </code>
- * @access public
- * @since 1.0
- * @param string $data The data that needs to be escaped.
- * @return string
- */
- public function EscapeData($data)
- {
- return strip_tags(htmlentities(stripslashes(trim(sprintf("%s", $data))), ENT_QUOTES));
- }
- /**
- * Create a parametered query
- * @access private
- * @since 1.1
- * @param object $type The SQL object
- * @param array $params The parameters: array($var, 'type', length)
- * @return null
- */
- private function _createParamStatement(&$type, $params = array())
- {
- if(count($params) > 0) {
- for($i = 1; $i <= count($params); $i++) {
- $type->bindValue(
- $i,
- $params[$i-1][0],
- $this->_defineTypeParam($params[$i-1][1])
- );
- }
- return;
- }
- }
- /**
- * Define the param type based on $input
- * @access private
- * @since 1.1
- * @param string $input The input
- * @return const
- */
- private function _defineTypeParam($input)
- {
- if($input == 'int') {
- return PDO::PARAM_INT;
- } elseif($input == 'string') {
- return PDO::PARAM_STR;
- } else {
- return PDO::PARAM_STR;
- }
- }
- public function _getAffectedRows(&$holder)
- {
- //return $holder->rowCount();
- }
- /**
- * The singleton pattern
- * @access public
- * @since 1.0
- * @final
- * @return mixed
- */
- public static function getInstance()
- {
- if(!self::$db) {
- self::$db = new AlienConquestBasicDatabase();
- return self::$db;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement