Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * MySQL class to work with the MySQL database.
  5.  */
  6.  
  7. class MySQL
  8. {
  9.     const Server = MySQLSettings::Server;
  10.     const User = MySQLSettings::User;
  11.     const Password = MySQLSettings::Password;
  12.     const Database = MySQLSettings::Database;
  13.    
  14.     public static $Connected = false;
  15.     public static $ConnectionError = '';
  16.     public static $Errors = array();
  17.     public static $ErrorSQL = array();
  18.    
  19.     public static function Connect()
  20.     {
  21.         if(self::$Connected)
  22.         {
  23.             return true;
  24.         }
  25.  
  26.         if(!mysql_connect(self::Server, self::User, self::Password))
  27.         {
  28.             self::$ConnectionError = mysql_error();
  29.             return false;
  30.         }
  31.        
  32.         if(!mysql_select_db(self::Database))
  33.         {
  34.             self::$ConnectionError = mysql_error();
  35.             return false;
  36.         }
  37.  
  38.         return true;
  39.     }
  40.  
  41.     public static function CheckConnection()
  42.     {
  43.         if(!self::Connect())
  44.         {
  45.             die(self::$ConnectionError);
  46.             return false;
  47.         }
  48.         else
  49.         {
  50.             return true;
  51.         }
  52.     }
  53.  
  54.     public static function Query($sql)
  55.     {
  56.         $result = mysql_query($sql);
  57.  
  58.         if($result)
  59.         {
  60.             return $result;
  61.         }
  62.         else
  63.         {
  64.             $error = 'MySQL Error: ' . mysql_error() . "\n" . "SQL: $sql\n" . "-------\n";
  65.  
  66.             if(!file_exists(Config::ErrorLogFile))
  67.             {
  68.                 file_put_contents(Config::ErrorLogFile, '');
  69.             }
  70.  
  71.             file_put_contents(Config::ErrorLogFile, $error, FILE_APPEND);
  72.  
  73.             throw new Exception('MySQL Error: '.mysql_error());
  74.             return false;
  75.         }
  76.     }
  77.  
  78.     private static function NumRows($result)
  79.     {
  80.         if(!$result)
  81.         {
  82.             return 0;
  83.         }
  84.         else
  85.         {
  86.             return mysql_num_rows($result);
  87.         }
  88.     }
  89.  
  90.     private static function FetchRow($result)
  91.     {
  92.         if(!$result)
  93.         {
  94.             return false;
  95.         }
  96.         else
  97.         {
  98.             return mysql_fetch_assoc($result);
  99.         }
  100.     }
  101.    
  102.     public static function Sanitize($str)
  103.     {
  104.         if(function_exists('mysql_real_escape_string'))
  105.             $str = mysql_real_escape_string($str);
  106.         else
  107.             die('Please upgrade your version of PHP to at least 4.3.0');
  108.        
  109.         return $str;
  110.     }
  111.  
  112.     /*
  113.      * The following function are interfaces for accessing the MySQL database
  114.      * without having SQL scattered all throughout the code.
  115.      */
  116.  
  117.     private static $Cache = array();
  118.  
  119.     private static function GetInfo($table, $id, &$cls) // cls is the class that corresponds to each row
  120.     {
  121.         self::CheckConnection();
  122.  
  123.         $table = self::Sanitize($table);
  124.         $id = self::Sanitize($id);
  125.  
  126.         if(!isset(self::$Cache[$table]))
  127.         {
  128.             self::$Cache[$table] = array();
  129.         }
  130.  
  131.         if(isset(self::$Cache[$table][$id]))
  132.         {
  133.             $row = self::$Cache[$table][$id];
  134.         }
  135.         else
  136.         {
  137.             $result = self::Query("SELECT * FROM $table WHERE `id` = '$id' LIMIT 1");
  138.  
  139.             if(self::NumRows($result))
  140.             {
  141.                 return null;
  142.             }
  143.  
  144.             $row = self::FetchRow($result);
  145.             self::$Cache[$table][$id] = $row;
  146.         }
  147.  
  148.         $cls->FromSQLRow($row);
  149.  
  150.         return $cls;
  151.     }
  152.  
  153.     private static function SetInfo($table, $id, $cls)
  154.     {
  155.         self::CheckConnection();
  156.  
  157.         $table = self::Sanitize($table);
  158.         $id = self::Sanitize($id);
  159.  
  160.         if(!isset(self::$Cache[$table]))
  161.         {
  162.             self::$Cache[$table] = array();
  163.         }
  164.  
  165.         $row = $cls->ToSQLRow();
  166.         $cached = (isset(self::$Cache[$table][$id]) ? self::$Cache[$table][$id] : array());
  167.         $sqlParts = array();
  168.  
  169.         foreach($row as $key=>$val)
  170.         {
  171.             if($val == null) // Skip null values
  172.                 continue;
  173.  
  174.             if(!isset($cached[$key]) || $val != $cached[$key]) // Only set if it is different
  175.             {
  176.                 $sqlParts[] = "$key='".self::Sanitize($val)."'";
  177.             }
  178.         }
  179.  
  180.         if(count($sqlParts) == 0) // Nothing to update
  181.         {
  182.             return true;
  183.         }
  184.  
  185.         $sql = implode(',', $sqlParts);
  186.  
  187.         if(self::Query("UPDATE $table SET $sql WHERE `id`='$id' LIMIT 1"))
  188.         {
  189.             self::$Cache[$table][$id] = $row; // Update the cache
  190.             return true;
  191.         }
  192.         else
  193.         {
  194.             return false;
  195.         }
  196.     }
  197.  
  198.     public static function GetImageInfo($id)
  199.     {
  200.         return self::GetInfo('images', $id, new Image());
  201.     }
  202.  
  203.     public static function SetImageInfo($id, $image)
  204.     {
  205.         return self::SetInfo('images', $id, $image);
  206.     }
  207. }
  208.  
  209. // FIXME: Only call Connect when needed
  210. if(!MySQL::Connect())
  211. {
  212.     die(MySQL::$ConnectionError);
  213. }
  214.  
  215. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement