Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.28 KB | None | 0 0
  1. <?php
  2. setlocale(LC_ALL, 'nl_NL');
  3.  
  4. /**
  5.  * @author Manuel de Ruiter <manuel@alien-conquest.nl>
  6.  * @copyright Copyright (c) 2009, Manuel de Ruiter
  7.  * @category Database initialisation
  8.  * @package Alien Conquest
  9.  * @version 1.0
  10.  */
  11.  
  12. class AlienConquestInitDB {
  13.     /**
  14.      * $connection is a object for the MySQL connection.
  15.      * @access public
  16.      * @since 1.0
  17.      * @return mixed
  18.      */
  19.     public $connection;
  20.     /**
  21.      * The database object
  22.      * @access private
  23.      * @since 1.004
  24.      * @var $handle
  25.      */
  26.     private $_handle = null;
  27.  
  28.     /**
  29.      * @access private
  30.      * @since 1.0
  31.      * @var $server is the datatype + host where the server is located
  32.      * @var $username is the database username
  33.      * @var $password is the database password
  34.      * @var $data is the database in string format
  35.      */
  36.     private $server, $username, $password, $data;
  37.     /**
  38.      * Initialise database connection
  39.      * @access private
  40.      * @since 1.0
  41.      * @param string $host The database host
  42.      * @param string $username The database username
  43.      * @param string $password The database password
  44.      * @param string $data The database name
  45.      * @return boolean
  46.      */
  47.     private function __construct($host="mysql:host=127.0.0.1",$username="acgame_listen",$password="Qp",$data="dbname=game_1")
  48.     {
  49.         $this->server = $host;
  50.         $this->username = $username;
  51.         $this->password = $password;
  52.         $this->data = $data;
  53.        
  54.         $this->CreateConnection();
  55.         return;
  56.     }
  57.  
  58.     /**
  59.      * Create a connection to the database
  60.      * @access private
  61.      * @since 1.001
  62.      * @return object
  63.      */
  64.     private function CreateConnection()
  65.     {
  66.         try {
  67.             $this->_handle = new PDO($this->server . ";" . $this->data, $this->username, $this->password, array(
  68.                 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  69.                 PDO::ATTR_PERSISTENT => false
  70.             ));
  71.         } catch(PDOException $e) {
  72.             $this->_handle = null;
  73.             die(file_get_contents("/srv/www/vhosts/alien-conquest.nl/htdocs/AC-Beta-Beta/b-laag/lib/messages/server-offline.txt"));
  74.         }
  75.     }
  76.    
  77.     /**
  78.      * @access public
  79.      * @since 1.0
  80.      * @return boolean
  81.      */
  82.     public function __destruct()
  83.     {
  84.         /*
  85.             Close the connection from the database.
  86.         */
  87.         $this->_handle = null;
  88.     }
  89.    
  90.     /**
  91.      * PHP magic method
  92.      * @access public
  93.      * @since 1.001
  94.      * @return array
  95.      */
  96.     public function __sleep()
  97.     {
  98.         return array("server", "username", "password", "data");
  99.     }
  100.    
  101.     /**
  102.      * PHP magic method
  103.      * @access public
  104.      * @since 1.001
  105.      * @return array
  106.      */
  107.     public function __wakeup()
  108.     {
  109.         $this->CreateConnection();
  110.     }
  111.    
  112.     /**
  113.      * The singleton pattern
  114.      * @access public
  115.      * @since 1.004
  116.      * @return object
  117.      */
  118.     public static function get()
  119.     {
  120.         static $db = null;
  121.         if($db == null) {
  122.             $db = new AlienConquestInitDB();
  123.         }
  124.         return $db;
  125.     }
  126.    
  127.     /**
  128.      * Return the database object
  129.      * @access public
  130.      * @since 1.004
  131.      * @return object
  132.      */
  133.     public function handle()
  134.     {
  135.         return $this->_handle;
  136.     }
  137. }
  138. ?>
  139.  
  140. /** --- DBFunctions.inc.php */
  141. <?php
  142. /**
  143.  * @author Manuel de Ruiter <manuel@alien-conquest.nl>
  144.  * @copyright Copyright (c) 2009, Manuel de Ruiter
  145.  * @category Database functions
  146.  * @package Alien Conquest
  147.  * @version 1.0
  148.  */
  149. require("/srv/www/vhosts/alien-conquest.nl/htdocs/AC-Beta-Beta/d-laag/dbconnection.inc.php");
  150.  
  151. class AlienConquestBasicDatabase {
  152.     /**
  153.      * @access public
  154.      * @since 1.0
  155.      * @static
  156.      * @return object
  157.      */
  158.     public static $db;
  159.     /**
  160.      * @access public
  161.      * @since 1.0
  162.      * @return object
  163.      */
  164.     public $connect, $firebug;
  165.    
  166.     /**
  167.      * Initialise constructor
  168.      * @access public
  169.      * @since 1.0
  170.      * @return mixed
  171.      */
  172.     public function __construct()
  173.     {
  174.         $this->connect = AlienConquestInitDB::get()->handle();
  175.     }
  176.    
  177.     /**
  178.      * Export the results to XML
  179.      * @access public
  180.      * @since 1.0
  181.      * @param string $query The query results that needs to be transformed to SQL.
  182.      * @return mixed
  183.      */
  184.     public static function ExportResultsXML($query)
  185.     {
  186.         $num = $this->NumRowsStatement($query);
  187.         $xml = "<?xml version=\"1.0\" ?>\n";
  188.         $xml .= "<resultset query=\"".$this->EscapeData($query)."\" nrows=\"".($num ? $num : 0)."\">\n";
  189.        
  190.         if($this->NumRowsStatement($query)) {
  191.             $xml .= "\t<rowvalues>\n";
  192.             $result = $this->RunQueryWithResults($query);
  193.            
  194.             foreach($this->GetTableFields($query) as $a => $b) {
  195.                 $xml .= "\t\t<row name=\"" . $b . "\">" . $result[$a] . "</row>\n";
  196.             }
  197.             $xml .= "\t</rowvalues>\n";
  198.         } else {
  199.             $xml .= "\t<noresults>\n";
  200.             $xml .= "\t\t<row>No results!</row>\n";
  201.             $xml .= "\t</noresults>\n";
  202.         }
  203.        
  204.         $xml .= "\t<time>\n";
  205.         $xml .= "\t\t<year>" . date("Y", time()) . "</year>\n";
  206.         $xml .= "\t\t<month>" . date("m", time()) . "</month>\n";
  207.         $xml .= "\t\t<day>" . date("d", time()) . "</day>\n";
  208.         $xml .= "\t\t<fulldate>" . date("d-m-Y H:i:s") . "</fulldate>\n";
  209.         $xml .= "\t</time>\n";
  210.         $xml .= "</resultset>";
  211.        
  212.         return $xml;
  213.     }
  214.    
  215.     /**
  216.      * Fetch database results and put them in a array
  217.      * @access public
  218.      * @since 1.004
  219.      * @param string $query The query that must be executed
  220.      * @param array $params The parameters for a bindValue()
  221.      * @return array
  222.      */
  223.     public function QueryWithSelectField_Daemon($query, $params = array())
  224.     {
  225.         $this->connect->beginTransaction();
  226.         $sql = $this->connect->prepare($query);
  227.        
  228.         if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
  229.        
  230.         if($sql->execute() === true) {
  231.             $a = array();
  232.            
  233.             while(($row = $sql->fetch(PDO::FETCH_ASSOC)) !== false) {
  234.                 $a[] = $row;
  235.             }
  236.             $this->connect->commit();
  237.             $sql->closeCursor();
  238.             return $a;
  239.         } else {
  240.             $this->connect->rollBack();
  241.             return false;
  242.         }
  243.     }
  244.    
  245.     /**
  246.      * A database function to count the rows that are in that table.
  247.      * <code>
  248.      * <?php
  249.      *    NumRows("", "ac_user");
  250.      * ?>
  251.      * </code>
  252.      * @access public
  253.      * @since 1.0
  254.      * @param boolean|string $rows The rows that need to be counted.
  255.      * @param string $table The table where must be counted from.
  256.      * @param boolean|array The where query
  257.      * @param boolean|int $limit If there is a limit.
  258.      * @return int|boolean
  259.      */
  260.     public function NumRows($rows = false, $table, $where = false, $limit = false)
  261.     {
  262.         $rows = (!$rows || "" ? "*" : $rows);
  263.         $query = "SELECT
  264.             " . $rows . "
  265.         FROM
  266.             " . $table . "
  267.         " . ($where ? "WHERE " . $where[0] . "='" . $where[1] . "'" : "") . "
  268.             " . ($limit ? "LIMIT " . $limit : "");
  269.         $this->connect->beginTransaction();
  270.         $sql = $this->connect->prepare($query);
  271.        
  272.         if($sql->execute() === true) {
  273.             $this->connect->commit();
  274.             $sql->closeCursor();
  275.             return $sql->rowCount();
  276.         } else {
  277.             $this->connect->rollBack();
  278.             return false;
  279.         }
  280.     }
  281.    
  282.     /**
  283.      * Count the rows by using a stored procedure.
  284.      * <code>
  285.      * <?php
  286.      *  NumRowsStatement("CALL xxxxxxxxxxxx();");
  287.      * ?>
  288.      * </code>
  289.      * @access public
  290.      * @since 1.0
  291.      * @param string $query The query that needs to be executed.
  292.      * @param array $params The parameters for a bindValue
  293.      * @return string|boolean
  294.      */
  295.     public function NumRowsStatement($query, $params = array())
  296.     {
  297.         $this->connect->beginTransaction();
  298.         $sql = $this->connect->prepare($query);
  299.         if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
  300.        
  301.         if($sql->execute() === true) {
  302.             $this->connect->commit();
  303.             $sql->closeCursor();
  304.             return $sql->rowCount();
  305.         } else {
  306.             $this->connect->rollBack();
  307.             return null;
  308.         }
  309.     }
  310.    
  311.     /**
  312.      * Execute a query on the database.
  313.      * <code>
  314.      * <?php
  315.      *  RunQuery("SELECT * FROM xxxxxxxxxxxx");
  316.      * ?>
  317.      * </code>
  318.      * @access public
  319.      * @since 1.0
  320.      * @param string $query The query that needs to be executed.
  321.      * @param array $params The parameters for a bindValue
  322.      * @return boolean
  323.      */
  324.     public function RunQuery($query, $params = array())
  325.     {
  326.         $this->connect->beginTransaction();
  327.         $sql = $this->connect->prepare($query);
  328.        
  329.         if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
  330.        
  331.         if($sql->execute() === true) {
  332.             $this->connect->commit();
  333.             $sql->closeCursor();
  334.             return true;
  335.         } else {
  336.             $this->connect->rollBack();
  337.             return null;
  338.         }
  339.     }
  340.    
  341.     /**
  342.      * Fetch all tables fields that must be listed
  343.      * <code>
  344.      * <?php
  345.      *  GetTableFields($query);
  346.      * ?>
  347.      * </code>
  348.      * @access public
  349.      * @since 1.0
  350.      * @param string $query The query that needs to be executed.
  351.      * @ignore This function is only for debug use!
  352.      * @return array
  353.      */
  354.     public function GetTableFields($query)
  355.     {
  356.         $this->connect->beginTransaction();
  357.         $sql = $this->connect->prepare($query);
  358.         if($sql->execute() === true) {
  359.             while(($row = $sql->fetch(PDO::FETCH_ASSOC)) !== false) {
  360.                 $a[] = $row;
  361.             }
  362.            
  363.             foreach($a as $k => $v) {
  364.                 foreach($v as $f => $e) {
  365.                     $b[] = $f;
  366.                 }
  367.             }
  368.            
  369.             $this->connect->commit();
  370.             $sql->closeCursor();
  371.             return $b;
  372.         } else {
  373.             $this->connect->rollBack();
  374.             return null;
  375.         }
  376.     }
  377.    
  378.     /**
  379.      * Execute the query and list the new results, specially for SELECT query's.
  380.      * <code>
  381.      * <?php
  382.      *  RunQueryWithResults("SELECT *******");
  383.      * ?>
  384.      * </code>
  385.      * @access public
  386.      * @since 1.0
  387.      * @param string $query The query that needs to be executed.
  388.      * @return array
  389.      */
  390.     public function RunQueryWithResults($query, $params = array())
  391.     {
  392.         $this->connect->beginTransaction();
  393.         $sql = $this->connect->prepare($query);
  394.         if(count($params) > 0) { $this->_createParamStatement($sql, $params); }
  395.        
  396.         if($sql->execute() === true) {
  397.             $a = array(); $b = array(); $k = array(); $v = array(); $f = array(); $e = array();
  398.        
  399.             while(($row = $sql->fetch(PDO::FETCH_ASSOC)) !== false) {
  400.                 $a[] = $row;
  401.             }
  402.            
  403.             foreach($a as $k => $v) {
  404.                 foreach($v as $f => $e) {
  405.                     $b[] = $e;
  406.                 }
  407.             }
  408.            
  409.             $this->connect->commit();
  410.             $sql->closeCursor();
  411.             return $b;
  412.         } else {
  413.             $this->connect->rollBack();
  414.             return null;
  415.         }
  416.     }
  417.    
  418.     /**
  419.      * Escape all characters
  420.      * <code>
  421.      * <?php
  422.      *  EscapeData("String")
  423.      * ?>
  424.      * </code>
  425.      * @access public
  426.      * @since 1.0
  427.      * @param string $data The data that needs to be escaped.
  428.      * @return string
  429.      */
  430.     public function EscapeData($data)
  431.     {
  432.         return strip_tags(htmlentities(stripslashes(trim(sprintf("%s", $data))), ENT_QUOTES));
  433.     }
  434.    
  435.     /**
  436.      * Create a parametered query
  437.      * @access private
  438.      * @since 1.1
  439.      * @param object $type The SQL object
  440.      * @param array $params The parameters: array($var, 'type', length)
  441.      * @return null
  442.      */
  443.     private function _createParamStatement(&$type, $params = array())
  444.     {
  445.         if(count($params) > 0) {
  446.             for($i = 1; $i <= count($params); $i++) {
  447.                 $type->bindValue(
  448.                     $i,
  449.                     $params[$i-1][0],
  450.                     $this->_defineTypeParam($params[$i-1][1])
  451.                 );
  452.             }
  453.             return;
  454.         }
  455.     }
  456.    
  457.     /**
  458.      * Define the param type based on $input
  459.      * @access private
  460.      * @since 1.1
  461.      * @param string $input The input
  462.      * @return const
  463.      */
  464.     private function _defineTypeParam($input)
  465.     {
  466.         if($input == 'int') {
  467.             return PDO::PARAM_INT;
  468.         } elseif($input == 'string') {
  469.             return PDO::PARAM_STR;
  470.         } else {
  471.             return PDO::PARAM_STR;
  472.         }
  473.     }
  474.    
  475.     public function _getAffectedRows(&$holder)
  476.     {
  477.         //return $holder->rowCount();
  478.     }
  479.    
  480.     /**
  481.      * The singleton pattern
  482.      * @access public
  483.      * @since 1.0
  484.      * @final
  485.      * @return mixed
  486.      */
  487.     public static function getInstance()
  488.     {
  489.         if(!self::$db) {
  490.             self::$db = new AlienConquestBasicDatabase();
  491.             return self::$db;
  492.         }
  493.     }
  494. }
  495. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement