Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.18 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Extends ezSQL so that it can handle active_record.
  4.  * @author: droope <pedro@worcel.com>
  5.  */
  6.  
  7. include_once "ez_sql_core.php";
  8. include_once "ez_sql_mysql.php";
  9.  
  10. class ezSQL_active_records extends ezSQL_mysql {
  11.    
  12.     public $exceptions = array('NOW()', 'NULL', 'CURRENT_TIMESTAMP');
  13.    
  14.     function ezSQL_active_records($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost') {
  15.         $this->dbuser = $dbuser;
  16.         $this->dbpassword = $dbpassword;
  17.         $this->dbname = $dbname;
  18.         $this->dbhost = $dbhost;
  19.     }
  20.    
  21.     /**
  22.      * @see $this->generate_insert()
  23.      */
  24.     function insert($table, $insertArray) {
  25.         if($this->query ( $this->generate_insert ( $table, $insertArray ) )) {
  26.             return true;
  27.         } else {
  28.             return false;
  29.         }
  30.     }
  31.    
  32.     /**
  33.      * Generates insert queries. Values are automatically escaped.
  34.      *
  35.      * @param $table table name
  36.      * @param $insertArray = array(
  37.      *              'column_name' => 'value',
  38.      *              'column_name2' => 'value2',
  39.      *          );
  40.      * @return string
  41.      */
  42.     function generate_insert($table, $insertArray) {
  43.         if(!$this->dbh) {
  44.             $this->autoconnect();
  45.         }
  46.         if (is_array ( $insertArray )) {
  47.            
  48.             $count = 1;
  49.             foreach ( $insertArray as $column => $value ) {
  50.            
  51.                 $value = $this->escape ( $value );
  52.                 if ($this->should_quote($value)) {
  53.                     @$valueString .= "'" . $value . "'";
  54.                 } else {
  55.                     @$valueString .= $value;
  56.                 }
  57.                 @$columnString .= $this->escape($column);
  58.                 if ($count != count ( $insertArray )) {
  59.                     $valueString .= ", ";
  60.                     $columnString .= ", ";
  61.                 }
  62.            
  63.                 $count ++;
  64.             }
  65.         } else {
  66.             $this->register_error ( "Active records error: \$insertArray must be an array in " . __FILE__ . " function " . __FUNCTION__ );
  67.             return false;
  68.         }
  69.         $sql = "INSERT INTO $table ($columnString) VALUES ($valueString);";
  70.         return $sql;
  71.     }
  72.    
  73.     /*
  74.      * @see $this->generate_update();
  75.     */ 
  76.     function update($table, $updateArray, $whereArray) {
  77.         $this->query ( $this->generate_update ( $table, $updateArray, $whereArray ) );
  78.         if(!mysql_error()) {
  79.             return 1;
  80.         }
  81.         return 0;
  82.     }
  83.    
  84.     /*
  85.      * Generates update queries. Values are automatically escaped
  86.      * @param string $table name of table to update
  87.      * @param array $updateArray = array(
  88.      *                      'column_name' => 'value',
  89.      *                      'column_name2' => 'value2',
  90.      *                  );
  91.      * @param array $whereArray = array(
  92.      *                      'col_name' => 'condition'
  93.      *                  );
  94.      * @return string SQL
  95.     */
  96.     function generate_update($table, $updateArray, $whereArray) {
  97.         if(!$this->dbh) {
  98.             $this->autoconnect();
  99.         }
  100.         /* Generate SET */
  101.         if (is_array ( $updateArray )) {
  102.             $count = 1;
  103.             foreach ( $updateArray as $column => $value ) {
  104.  
  105.                 if ($this->should_quote($value)) {
  106.                     $value = "'" . $this->escape($value) . "'";
  107.                 } else {
  108.                     $value = $this->escape($value);
  109.                 }
  110.                 if(!@$beenHere) {
  111.                     $beenHere = true;
  112.                     $setString = "SET ";
  113.                 }
  114.                 $setString .= "{$this->escape($column)} = $value";
  115.                 if($count != count( $updateArray )) {
  116.                     $setString .= ", ";
  117.                 }
  118.                
  119.                 $count ++;
  120.             }
  121.         } else {
  122.             $this->register_error ( "Active records error: \$updateArray must be an array in " . __FILE__ . " function " . __FUNCTION__ );
  123.             return false;
  124.         }
  125.         /* Generate WHERE*/
  126.         if(is_array($whereArray)) {
  127.             $whereCount = 1;
  128.             foreach($whereArray as $column => $condition) {
  129.                 if(!@$beenWhere) {
  130.                     $beenWhere = true;
  131.                     $whereString = "WHERE ";
  132.                 }
  133.                 if( $this->should_quote($condition)) {
  134.                     $condition = "'".$this->escape($condition)."'";
  135.                 } else {
  136.                     $condition = $this->escape($condition);
  137.                 }
  138.                 $whereString .= "{$this->escape($column)} = $condition";
  139.                 if($whereCount != count($whereArray)) {
  140.                     $whereString .= " AND ";
  141.                 }
  142.                 $whereCount++;
  143.             }
  144.         } else {
  145.             $this->register_error ( "Active records error: \$whereArray must be an array in " . __FILE__ . " function " . __FUNCTION__ );
  146.             return false;
  147.         }
  148.        
  149.         $sql = "UPDATE $table $setString $whereString;";
  150.        
  151.         return $sql;
  152.     }
  153.        
  154.     /*
  155.      * @param string $table mysql table
  156.      * @param array $fcv_array {("field", "comparator", "value"), ("field", "comparator", "value"), ...}
  157.      * @param strin $limit limit portion of the query
  158.      * @return string the query
  159.     */
  160.     function generate_select_where($table, $fcv_array, $limit = false) {
  161.         if(!$this->dbh) {
  162.             $this->autoconnect();
  163.         }
  164.         $query = "SELECT * FROM $table WHERE";
  165.        
  166.         $x = 0;
  167.         foreach($fcv_array as $fcv) {
  168.             $x++;
  169.             $fcv = $this->escape_array($fcv);
  170.             $fcv[2] = $this->should_quote($fcv[2]) ? "'{$fcv[2]}'" : $fcv[2];
  171.             $query .= " {$fcv[0]} {$fcv[1]} {$fcv[2]}";
  172.             if(count($fcv_array) > $x) {
  173.                 $query .= " AND";
  174.             }
  175.         }
  176.        
  177.         if($limit) {
  178.             $limit = $this->escape(trim($limit));
  179.             $query .= " LIMIT $limit";
  180.         }      
  181.        
  182.         $query .= ";";
  183.         return $query;
  184.    
  185.     }
  186.    
  187.     /*
  188.      * returns the number of rows to be found with the given parameters
  189.      * @param string $table mysql table
  190.      * @param array $fcv_array {("field", "comparator", "value"), ("field", "comparator", "value"), ...}
  191.      * @return int total rows
  192.     */
  193.     function count_where($table, $fcv_array) {
  194.         $query = $this->generate_select_where($table, $fcv_array);
  195.         $query = preg_replace("/^SELECT (\*)/i", "SELECT count(*)", $query);
  196.         return (int) $this->get_var($query);
  197.     }
  198.    
  199.     /*
  200.      * @see $this->generate_select_where
  201.      * @return array set of obtained objects
  202.     */
  203.     function select_where($table, $fcv_array, $limit = false) {
  204.         return $this->get_results($this->generate_select_where($table, $fcv_array, $limit));
  205.     }
  206.    
  207.     /*
  208.      * determine if $string should be wrapped in simple quotes
  209.      * @param string $string the string to be checked
  210.      * @return bool true if should quote, false if should not quote
  211.     */
  212.     function should_quote($string) {
  213.    
  214.         if(is_numeric($string) || in_array($string, $this->exceptions)) {
  215.             return false;
  216.         }
  217.        
  218.         return true;
  219.        
  220.     }
  221.    
  222.     /*
  223.      * @return array|bool escaped array or false on failure
  224.     */
  225.     function escape_array($array) {
  226.        
  227.         if(is_array($array)) {
  228.             foreach($array as $key => $a) {
  229.                 if(is_array($a)) {
  230.                     $array[$key] = $this->escape_array($a);
  231.                 } else {
  232.                     $array[$key] = $this->escape($a);
  233.                 }
  234.             }
  235.            
  236.             return $array;
  237.         }
  238.        
  239.         return false;
  240.     }
  241.        
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement