Advertisement
Guest User

Untitled

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