Guest User

Untitled

a guest
Apr 15th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.65 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package Walmo.tv
  4. * @version 0.0.1
  5. * @author @_mvader
  6. * @copyright Walmo.tv 2010-11
  7. */
  8.  
  9. /*
  10.  * Database abstraction layer
  11.  * Abstract class to connect and interact with Walmo's database
  12.  */
  13.  
  14. abstract class Database
  15. {
  16.     private static $dbHost = 'localhost';
  17.     private static $dbUser = 'root';
  18.     private static $dbPass = 'root';
  19.     private static $dbName = 'walmo';
  20.     protected $query;
  21.     protected $rows = array();
  22.     private $conn;
  23.    
  24.     abstract public function get();
  25.     abstract public function set();
  26.     abstract public function update();
  27.     abstract public function delete();
  28.    
  29.     /**
  30.      * openConnection
  31.      * Establishes database connection
  32.      * @params: none
  33.      */
  34.    
  35.     private function openConnection()
  36.     {
  37.         $this->conn = mysql_connect(self::$dbHost, self::$dbUser, self::$dbPass) or die("Error establishing database connection");
  38.         $selected_db = mysql_select_db(self::$dbName, $this->conn) or die("Can't find the database " . $this->dbName);
  39.     }
  40.    
  41.     /**
  42.      * closeConnection
  43.      * Closes the database connection
  44.      * @params: none
  45.      */
  46.  
  47.     private function closeConnection()
  48.     {
  49.         mysql_close($this->conn);
  50.     }
  51.    
  52.     /**
  53.      * query
  54.      * This function executes a query without returning anything
  55.      * @params: $q [string]
  56.      */
  57.    
  58.     protected function query($q)
  59.     {
  60.         $this->openConnection();
  61.         mysql_query($q, $this->conn) or die(mysql_error($this->conn));
  62.         $this->closeConnection();
  63.     }
  64.    
  65.     /**
  66.      * getQuery
  67.      * This function returns an associative array with the result received from a query
  68.      * @params: $q [string]
  69.      */
  70.  
  71.     protected function getQuery($q)
  72.     {
  73.         $this->openConnection();
  74.         $result = mysql_query($q, $this->conn) or die("Query error");
  75.         $this->closeConnection();
  76.         return $this->fetch($result);
  77.     }
  78.    
  79.     /**
  80.      * fetch
  81.      * Fetches into an array the data received from a query
  82.      * @params: $f [object]
  83.      */
  84.    
  85.     public function fetch($f)
  86.     {
  87.         return mysql_fetch_assoc($f);
  88.     }
  89.    
  90.     /**
  91.      * buildQuery
  92.      * Builds a query with the given array
  93.      * @params: $array [array], $mode [string]
  94.      * Array structure:
  95.      * $array = array(
  96.      *      ['TABLE']       => '',
  97.      *      ['FIELDS']      => array(),
  98.      *      ['VALUES']      => array(), // $array['VALUES'][i] = $array['FIELDS'][i] || Optional
  99.      *      ['WHERE']       => '', // optional
  100.      *      ['MISC']        => '', // optional
  101.      * );
  102.      */
  103.    
  104.     public function buildQuery(array $array, $mode)
  105.     {
  106.         if(is_array($array) && !empty($array) && !empty($mode))
  107.         {
  108.             switch ($mode)
  109.             {
  110.                 /*
  111.                  * Build an update query
  112.                  */
  113.                  
  114.                 case 'update':
  115.                     $query = 'UPDATE ';
  116.                     foreach($array as $key => $value)
  117.                     {
  118.                         if($key == 'TABLE')
  119.                         {
  120.                             $query .= ' ' . $value . ' SET ';
  121.                         }
  122.                        
  123.                         if($key == 'FIELDS')
  124.                         {
  125.                             if(is_array($value))
  126.                             {
  127.                                 $first = true;
  128.                                 foreach($value as $field)
  129.                                 {
  130.                                     if($first)
  131.                                     {
  132.                                         $query .= $field;
  133.                                     }
  134.                                     else
  135.                                     {
  136.                                         $query .= ', ' . $field;
  137.                                     }
  138.                                     $first = false;
  139.                                 }
  140.                             }
  141.                             else
  142.                             {
  143.                                 $query .= $value;
  144.                             }
  145.                         }
  146.                        
  147.                         if($key == 'WHERE')
  148.                         {
  149.                             $query .= ' WHERE ' . $value;
  150.                         }
  151.                        
  152.                         if($key == 'MISC')
  153.                         {
  154.                             $query .= ' ' . $value;
  155.                         }
  156.                     }
  157.                 break;
  158.                
  159.                 /*
  160.                  * Build an insert query
  161.                  */
  162.                
  163.                 case 'insert':
  164.                     $query = 'INSERT INTO ';
  165.                     foreach($array as $key => $value)
  166.                     {
  167.                         if($key == 'TABLE')
  168.                         {
  169.                             $query .= $value . ' ';
  170.                         }
  171.                        
  172.                         if($key == 'FIELDS')
  173.                         {
  174.                             if(is_array($value))
  175.                             {
  176.                                 $first = true;
  177.                                 foreach($value as $field)
  178.                                 {
  179.                                     if($first)
  180.                                     {
  181.                                         $query .= "(" . $field;
  182.                                     }
  183.                                     else
  184.                                     {
  185.                                         $query .= ", " . $field;
  186.                                     }
  187.                                     $first = false;
  188.                                 }
  189.                             }
  190.                             else
  191.                             {
  192.                                 $query .= $value . ' ';
  193.                             }
  194.                         }
  195.                        
  196.                         if($key == 'VALUES')
  197.                         {
  198.                             if(is_array($value))
  199.                             {
  200.                                 $first = true;
  201.                                 foreach($value as $field)
  202.                                 {
  203.                                     if($first)
  204.                                     {
  205.                                         $query .= ") VALUES ('" . $field;
  206.                                     }
  207.                                     else
  208.                                     {
  209.                                         $query .= "', '" . $field;
  210.                                     }
  211.                                     $first = false;
  212.                                 }
  213.                                 $query .= "')";
  214.                             }
  215.                             else
  216.                             {
  217.                                 $query .= ") VALUES ('" . $value . "')";
  218.                             }
  219.                         }
  220.                     }
  221.                 break;
  222.                
  223.                 /*
  224.                  * Build a select query
  225.                  */
  226.                  
  227.                 case 'select':
  228.                     $query = 'SELECT ';
  229.                     foreach($array as $key => $value)
  230.                     {
  231.                         if($key == 'TABLE')
  232.                         {
  233.                             $query .= ' FROM ' . $value . ' ';
  234.                         }
  235.                        
  236.                         if($key == 'FIELDS')
  237.                         {
  238.                             if(is_array($value))
  239.                             {
  240.                                 $first = true;
  241.                                 foreach($value as $field)
  242.                                 {
  243.                                     if($first)
  244.                                     {
  245.                                         $query .= $field;
  246.                                     }
  247.                                     else
  248.                                     {
  249.                                         $query .= ', ' . $field;
  250.                                     }
  251.                                     $first = false;
  252.                                 }
  253.                             }
  254.                             else
  255.                             {
  256.                                 $query .= $value . ' ';
  257.                             }
  258.                         }
  259.                        
  260.                         if($key == 'WHERE')
  261.                         {
  262.                             $query .= ' WHERE ' . $value;
  263.                         }
  264.                        
  265.                         if($key == 'MISC')
  266.                         {
  267.                             $query .= ' ' . $value;
  268.                         }
  269.                     }
  270.                 break;
  271.                
  272.                 /*
  273.                  * Build a delete query
  274.                  */
  275.                
  276.                 case 'delete':
  277.                     $query = 'DELETE FROM ';
  278.                     foreach($array as $key => $value)
  279.                     {
  280.                         if($key == 'TABLE')
  281.                         {
  282.                             $query .= $value;
  283.                         }
  284.                        
  285.                         if($key == 'WHERE')
  286.                         {
  287.                             $query .= ' WHERE ' . $value;
  288.                         }
  289.                     }
  290.                 break;
  291.                
  292.                 default:
  293.                     trigger_error('Error @ build_query() => Mode not specified.');
  294.             }
  295.            
  296.             return $query;
  297.         }
  298.         else
  299.         {
  300.             trigger_error('Error @ build_query() => Data error.');
  301.         }
  302.     }
  303.    
  304.     /**
  305.      * cleanString
  306.      * Cleans a string and removes illegal characters
  307.      * @params: $var [string]
  308.      */
  309.    
  310.     public function cleanString($var)
  311.     {
  312.         $var = preg_replace("[^A-Za-z0-9]", "", $var);
  313.         $var = str_replace(" ", "-", $var);
  314.         $var = strtolower($var);
  315.         return $var;
  316.     }
  317.    
  318.     /**
  319.      * affectedRows
  320.      * Returns the number of affected rows by the previous query executed
  321.      * @params: none
  322.      */
  323.    
  324.     public function affectedRows()
  325.     {
  326.         return ($this->conn) ? @mysql_affected_rows($this->conn) : false;
  327.     }
  328.    
  329.     /**
  330.      * freeResult
  331.      * Releases the memory used by a query previously executed
  332.      * @params: $query [object]
  333.      */
  334.    
  335.     public function freeResult($query)
  336.     {
  337.         mysql_free_result($query);
  338.     }
  339.    
  340.     /**
  341.      * sanitize
  342.      * Cleans a variable before storing it in the databse
  343.      * @params: $var [string]
  344.      * Note: use $var = (int) $var; for integer values
  345.      */
  346.    
  347.     public function sanitize($var)
  348.     {
  349.         $this->openConnection();
  350.         $var = mysql_real_escape_string($var,$this->conn);
  351.         $this->closeConnection();
  352.         return $var;
  353.     }
  354. }
Add Comment
Please, Sign In to add comment