File indexing completed on 2024-12-22 05:33:23

0001 <?php
0002 /*
0003  *   TRT GFX 3.0.1 (beta build) BackToSlash
0004  * 
0005  *   support: happy.snizzo@gmail.com
0006  *   website: http://trt-gfx.googlecode.com
0007  *   credits: Claudio Desideri
0008  *   
0009  *   This software is released under the MIT License.
0010  *   http://opensource.org/licenses/mit-license.php
0011  */ 
0012 
0013 //db related include files
0014 include_once("EDatasetter.class.php");
0015 include_once("EModel.class.php");
0016 
0017 class EDatabase {
0018   
0019     //server config
0020   private static $db_name;
0021   private static $db_host;
0022   private static $db_user;
0023   private static $db_pass;
0024   
0025   private static $db_link = 0;
0026   
0027   private static $opened = false;
0028   private static $debug = true;
0029   
0030   private static $queries = 0;
0031   private static $status = 0;
0032   
0033   /**
0034    * set temporary database information for the database class
0035    * @param string $name
0036    * @param string $host
0037    * @param string $user
0038    * @param string $pass
0039    * @return null
0040    */
0041   
0042   public static function load(){
0043     
0044     //loading vars from config file
0045     EDatabase::$db_name = EConfig::$data["database"]["name"];
0046     EDatabase::$db_host = EConfig::$data["database"]["host"];
0047     EDatabase::$db_user = EConfig::$data["database"]["user"];
0048     EDatabase::$db_pass = EConfig::$data["database"]["password"];
0049     
0050     EDatabase::open_session();
0051   }
0052   
0053   public static function open_session()
0054   {
0055     //opening session
0056     $db = mysqli_connect(EDatabase::$db_host, EDatabase::$db_user, EDatabase::$db_pass); //EDatabase::$status = 2;
0057     /* check connection */
0058     if (!mysqli_connect_errno()) {
0059       $db_select = mysqli_select_db($db, EDatabase::$db_name); //EDatabase::$status = 1;
0060       /* check if server is alive */
0061       if (!mysqli_ping($db)) {
0062         return false;
0063       }
0064     
0065     } else {
0066       return false;
0067     }
0068     EDatabase::$db_link = $db;
0069     if(EDatabase::$status==0){
0070       EDatabase::$opened = true;
0071     } else {
0072       EDatabase::$opened = false;
0073     }
0074     return EDatabase::$opened;
0075   }
0076   
0077   public static function set_db_info($name,$host,$user,$pass){
0078     EDatabase::$db_name = $name;
0079     EDatabase::$db_host = $host;
0080     EDatabase::$db_user = $user;
0081     EDatabase::$db_pass = $pass;
0082   }
0083   
0084   public static function get_db_name(){ return EDatabase::$db_name; }
0085   public static function get_db_host(){ return EDatabase::$db_host; }
0086   public static function get_db_user(){ return EDatabase::$db_user; }
0087   public static function get_db_pass(){ return EDatabase::$db_pass; }
0088   
0089   /*
0090    * This function is to assure that string or string array 
0091    * are safe to be executed as parts of SQL queries
0092    */
0093   public static function safe($s){
0094     if(is_array($s)){
0095       foreach($s as $key => $value){
0096         $s[$key] = mysqli_real_escape_string(EDatabase::$db_link, $s[$key]);
0097       }
0098       return $s;
0099     } else {
0100       $s = mysqli_real_escape_string(EDatabase::$db_link, $s);
0101       return $s;
0102     }
0103   }
0104   
0105   /**
0106    * execute query on database
0107    * @param string $q
0108    * @return null
0109    */
0110   public static function q($q){
0111     if(EDatabase::$opened==true){
0112       EDatabase::$queries += 1;
0113       $ret = mysqli_query(EDatabase::$db_link, $q);
0114       $error = mysqli_error(EDatabase::$db_link);
0115       if(empty($error)){ 
0116         $ret = $ret;
0117       } else {
0118         ELog::warning($error."<br>Query string: ".$q);
0119       }
0120       return $ret;
0121     } else {
0122       if(EDatabase::$debug==false){
0123         ELog::warning("sql session not already opened!");
0124       }
0125     }
0126   }
0127   
0128   /*TODO: What is this method supposed to do?
0129    *    Inspect.
0130    */
0131   public static function sq($q){
0132     if(EDatabase::$opened==true){
0133       EDatabase::$queries += 1;
0134       $ret = mysqli_query(EDatabase::$db_link, $q); //FIXME: error management
0135       while($row = $ret->fetch_array()){
0136         $number = $row[0];
0137       }
0138       return $number;
0139     } else {
0140       $error = " Query not executed due to mysql session not opened. Try to open one using open method. ";
0141       ELog::error($error);
0142     }
0143   }
0144   
0145   public static function table_exists($table){
0146     $r = EDatabase::q("SHOW TABLES LIKE '$table'");
0147     if(EDatabase::$opened) {
0148       $row = mysqli_fetch_row($r);
0149       if(empty($row)){
0150         return false;
0151       } else {
0152         return true;
0153       }
0154     } else {
0155       ELog::error("Database not opened!");
0156       return false;
0157     }
0158     
0159   }
0160   
0161   public static function num_rows($result){
0162     return mysqli_num_rows($result);
0163   }
0164   
0165   public static function fetch_assoc($result){
0166     die("<b>[BUG]</b> use my to implement database/fetch_assoc!");
0167     //return mysql_fetch_assoc($result);
0168   }
0169   
0170   public static function status(){
0171     return EDatabase::$status;
0172   }
0173   
0174   public static function last_insert_id(){
0175     $r = EDatabase::sq("SELECT LAST_INSERT_ID()");
0176     return $r;
0177   }
0178   
0179   public static function all_queries(){
0180     return EDatabase::$queries;
0181   }
0182   
0183   public static function ping(){
0184     echo "EDatabase::ping() called!";
0185   }
0186   
0187   public static function unload(){
0188     if(EDatabase::$opened==true){
0189       // TODO: strange behaviour under root. Inspect.
0190       // mysql_close(EDatabase::db_link);
0191       EDatabase::$db_link = 0;
0192       EDatabase::$opened = false;
0193     } else {
0194       if(EDatabase::$debug==false){
0195         ELog::error("TRT GFX ISSUE: unable to close mysql session because no one was already opened.");
0196       }
0197     }
0198   }
0199 }
0200 
0201 ?>