Advertisement
Guest User

bilo.db.php

a guest
Jan 28th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.95 KB | None | 0 0
  1. <?php
  2.  
  3. // (C) 2012, All rights reversed
  4.  
  5. class ezSQLcore
  6. {
  7.     public $prefix = '';
  8.     public $trace = false;
  9.     public $debug_all = false;
  10.     public $debug_called = false;
  11.     public $vardump_called = false;
  12.     public $show_errors = true;
  13.     public $num_queries = 0;
  14.     public $last_query = null;
  15.     public $last_error = null;
  16.     public $col_info = null;
  17.     public $captured_errors = array();
  18.     public $cache_dir = false;
  19.     public $cache_queries = false;
  20.     public $cache_inserts = false;
  21.     public $use_disk_cache = false;
  22.     public $cache_timeout = 24;
  23.     public $timers = array();
  24.     public $total_query_time = 0;
  25.     public $db_connect_time = 0;
  26.     public $trace_log = array();
  27.     public $use_trace_log = false;
  28.     public $sql_log_file = false;
  29.     public $do_profile = false;
  30.     public $profile_times = array();
  31.     public $field_types = array();
  32.     public $real_escape = false;
  33.     public $debug_echo_is_on = true;
  34.  
  35.     public function ezSQLcore()
  36.     {
  37.     }
  38.  
  39.     public function register_error($err_str)
  40.     {
  41.         $this->last_error = $err_str;
  42.         $this->captured_errors[] = array('error_str' => $err_str, 'query' => $this->last_query);
  43.     }
  44.  
  45.     public function show_errors()
  46.     {
  47.         $this->show_errors = true;
  48.     }
  49.  
  50.     public function hide_errors()
  51.     {
  52.         $this->show_errors = false;
  53.     }
  54.  
  55.     public function flush()
  56.     {
  57.         $this->last_result = null;
  58.         $this->col_info = null;
  59.         $this->last_query = null;
  60.         $this->from_disk_cache = false;
  61.     }
  62.  
  63.     public function get_var($query = null, $x = 0, $y = 0)
  64.     {
  65.         $this->func_call = '$db->get_var("' . $query . '",' . $x . ',' . $y . ')';
  66.  
  67.         if ($query) {
  68.             $this->query($query);
  69.         }
  70.  
  71.         if ($this->last_result[$y]) {
  72.             $values = array_values(get_object_vars($this->last_result[$y]));
  73.         }
  74.  
  75.         return isset($values[$x]) && ($values[$x] !== '') ? $values[$x] : null;
  76.     }
  77.  
  78.     public function set_prefix($table_prefix)
  79.     {
  80.         $this->prefix = $table_prefix;
  81.     }
  82.  
  83.     public function get_row($query = null, $output = OBJECT, $y = 0)
  84.     {
  85.         $this->func_call = '$db->get_row("' . $query . '",' . $output . ',' . $y . ')';
  86.  
  87.         if ($query) {
  88.             $this->query($query);
  89.         }
  90.  
  91.         if ($output == OBJECT) {
  92.             return $this->last_result[$y] ? $this->last_result[$y] : null;
  93.         }
  94.  
  95.         if ($output == ARRAY_A) {
  96.             return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
  97.         }
  98.  
  99.         if ($output == ARRAY_N) {
  100.             return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
  101.         }
  102.  
  103.         $this->print_error(' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N');
  104.     }
  105.  
  106.     public function get_col($query = null, $x = 0)
  107.     {
  108.         $new_array = array();
  109.  
  110.         if ($query) {
  111.             $this->query($query);
  112.         }
  113.  
  114.         $i = 0;
  115.  
  116.         while ($i < count($this->last_result)) {
  117.             $new_array[$i] = $this->get_var(null, $x, $i);
  118.             ++$i;
  119.         }
  120.  
  121.         return $new_array;
  122.     }
  123.  
  124.     public function get_results($query = null, $output = OBJECT)
  125.     {
  126.         $this->func_call = '$db->get_results("' . $query . '", ' . $output . ')';
  127.  
  128.         if ($query) {
  129.             $this->query($query);
  130.         }
  131.  
  132.         if ($output == OBJECT) {
  133.             return $this->last_result;
  134.         }
  135.  
  136.         if (($output == ARRAY_A) || ($output == ARRAY_N)) {
  137.             if ($this->last_result) {
  138.                 $i = 0;
  139.  
  140.                 foreach ($this->last_result as $row) {
  141.                     $new_array[$i] = get_object_vars($row);
  142.  
  143.                     if ($output == ARRAY_N) {
  144.                         $new_array[$i] = array_values($new_array[$i]);
  145.                     }
  146.  
  147.                     ++$i;
  148.                 }
  149.  
  150.                 return $new_array;
  151.             }
  152.  
  153.             return null;
  154.         }
  155.     }
  156.  
  157.     public function get_col_info($info_type = 'name', $col_offset = -1)
  158.     {
  159.         if ($this->col_info) {
  160.             if ($col_offset == -1) {
  161.                 $i = 0;
  162.  
  163.                 foreach ($this->col_info as $col) {
  164.                     $new_array[$i] = $col->{$info_type};
  165.                     ++$i;
  166.                 }
  167.  
  168.                 return $new_array;
  169.             }
  170.  
  171.             return $this->col_info[$col_offset]->{$info_type};
  172.         }
  173.     }
  174.  
  175.     public function store_cache($query, $is_insert)
  176.     {
  177.         $cache_file = $this->cache_dir . '/' . md5($query);
  178.         if (($this->use_disk_cache && $this->cache_queries && !$is_insert) || ($this->cache_inserts && $is_insert)) {
  179.             if (!is_dir($this->cache_dir)) {
  180.                 $this->register_error('Could not open cache dir: ' . $this->cache_dir);
  181.                 $this->show_errors ? trigger_error('Could not open cache dir: ' . $this->cache_dir, E_USER_WARNING) : null;
  182.                 return NULL;
  183.             }
  184.  
  185.             $result_cache = array('col_info' => $this->col_info, 'last_result' => $this->last_result, 'num_rows' => $this->num_rows, 'return_value' => $this->num_rows);
  186.             error_log(serialize($result_cache), 3, $cache_file);
  187.         }
  188.     }
  189.  
  190.     public function get_cache($query)
  191.     {
  192.         $cache_file = $this->cache_dir . '/' . md5($query);
  193.         if ($this->use_disk_cache && file_exists($cache_file)) {
  194.             if ($this->cache_timeout < (time() - filemtime($cache_file))) {
  195.                 unlink($cache_file);
  196.                 return NULL;
  197.             }
  198.  
  199.             $result_cache = unserialize(file_get_contents($cache_file));
  200.             $this->col_info = $result_cache['col_info'];
  201.             $this->last_result = $result_cache['last_result'];
  202.             $this->num_rows = $result_cache['num_rows'];
  203.             $this->from_disk_cache = true;
  204.             $this->trace || $this->debug_all ? $this->debug() : null;
  205.             return $result_cache['return_value'];
  206.         }
  207.     }
  208.  
  209.     public function vardump($mixed = '')
  210.     {
  211.         ob_start();
  212.         echo '<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>';
  213.         echo '<pre><font face=arial>';
  214.  
  215.         if (!$this->vardump_called) {
  216.             echo '<font color=800080><b>ezSQL</b> (v' . EZSQL_VERSION . ') <b>Variable Dump..</b></font>' . "\n\n";
  217.         }
  218.  
  219.         $var_type = gettype($mixed);
  220.         print_r($mixed ? $mixed : '<font color=red>No Value / False</font>');
  221.         echo "\n\n" . '<b>Type:</b> ' . ucfirst($var_type) . "\n";
  222.         echo '<b>Last Query</b> [' . $this->num_queries . ']<b>:</b> ' . ($this->last_query ? $this->last_query : 'NULL') . "\n";
  223.         echo '<b>Last Function Call:</b> ' . ($this->func_call ? $this->func_call : 'None') . "\n";
  224.         echo '<b>Last Rows Returned:</b> ' . count($this->last_result) . "\n";
  225.         echo '</font></pre></font></blockquote></td></tr></table>' . $this->donation();
  226.         echo "\n" . '<hr size=1 noshade color=dddddd>';
  227.         $html = ob_get_contents();
  228.         ob_end_clean();
  229.  
  230.         if ($this->debug_echo_is_on) {
  231.             echo $html;
  232.         }
  233.  
  234.         $this->vardump_called = true;
  235.         return $html;
  236.     }
  237.  
  238.     public function dumpvar($mixed)
  239.     {
  240.         $this->vardump($mixed);
  241.     }
  242.  
  243.     public function debug($print_to_screen = true)
  244.     {
  245.         ob_start();
  246.         echo '<blockquote>';
  247.  
  248.         if (!$this->debug_called) {
  249.             echo '<font color=800080 face=arial size=2><b>ezSQL</b> (v' . EZSQL_VERSION . ') <b>Debug..</b></font><p>' . "\n";
  250.         }
  251.  
  252.         if ($this->last_error) {
  253.             echo '<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>' . $this->last_error . '</b></font>]<p>';
  254.         }
  255.  
  256.         if ($this->from_disk_cache) {
  257.             echo '<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>';
  258.         }
  259.  
  260.         echo '<font face=arial size=2 color=000099><b>Query</b> [' . $this->num_queries . '] <b>--</b> ';
  261.         echo '[<font color=000000><b>' . $this->last_query . '</b></font>]</font><p>';
  262.         echo '<font face=arial size=2 color=000099><b>Query Result..</b></font>';
  263.         echo '<blockquote>';
  264.  
  265.         if ($this->col_info) {
  266.             echo '<table cellpadding=5 cellspacing=1 bgcolor=555555>';
  267.             echo '<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>';
  268.             $i = 0;
  269.  
  270.             while ($i < count($this->col_info)) {
  271.                 echo '<td nowrap align=left valign=top><font size=1 color=555599 face=arial>' . $this->col_info[$i]->type . ' ' . $this->col_info[$i]->max_length . '</font><br><span style=\'font-family: arial; font-size: 10pt; font-weight: bold;\'>' . $this->col_info[$i]->name . '</span></td>';
  272.                 ++$i;
  273.             }
  274.  
  275.             echo '</tr>';
  276.  
  277.             if ($this->last_result) {
  278.                 $i = 0;
  279.  
  280.                 foreach ($this->get_results(null, ARRAY_N) as $one_row) {
  281.                     ++$i;
  282.                     echo '<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>' . $i . '</font></td>';
  283.  
  284.                     foreach ($one_row as $item) {
  285.                         echo '<td nowrap><font face=arial size=2>' . $item . '</font></td>';
  286.                     }
  287.  
  288.                     echo '</tr>';
  289.                 }
  290.             } else {
  291.                 echo '<tr bgcolor=ffffff><td colspan=' . (count($this->col_info) + 1) . '><font face=arial size=2>No Results</font></td></tr>';
  292.             }
  293.  
  294.             echo '</table>';
  295.         } else {
  296.             echo '<font face=arial size=2>No Results</font>';
  297.         }
  298.  
  299.         echo '</blockquote></blockquote>' . $this->donation() . '<hr noshade color=dddddd size=1>';
  300.         $html = ob_get_contents();
  301.         ob_end_clean();
  302.         if ($this->debug_echo_is_on && $print_to_screen) {
  303.             echo $html;
  304.         }
  305.  
  306.         $this->debug_called = true;
  307.         return $html;
  308.     }
  309.  
  310.     public function donation()
  311.     {
  312.         return '<font size=1 face=arial color=000000>If ezSQL has helped <a href="https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0" style="color: 0000CC;">make a donation!?</a> &nbsp;&nbsp;<!--[ go on! you know you want to! ]--></font>';
  313.     }
  314.  
  315.     public function timer_get_cur()
  316.     {
  317.         list($usec, $sec) = explode(' ', microtime());
  318.         return (double) $usec + (double) $sec;
  319.     }
  320.  
  321.     public function timer_start($timer_name)
  322.     {
  323.         $this->timers[$timer_name] = $this->timer_get_cur();
  324.     }
  325.  
  326.     public function timer_elapsed($timer_name)
  327.     {
  328.         return round($this->timer_get_cur() - $this->timers[$timer_name], 2);
  329.     }
  330.  
  331.     public function timer_update_global($timer_name)
  332.     {
  333.         if ($this->do_profile) {
  334.             $this->profile_times[] = array('query' => $this->last_query, 'time' => $this->timer_elapsed($timer_name));
  335.         }
  336.  
  337.         $this->total_query_time += $this->timer_elapsed($timer_name);
  338.     }
  339.  
  340.     public function get_set($parms)
  341.     {
  342.         $sql = '';
  343.  
  344.         foreach ($parms as $field => $val) {
  345.             if ($val === 'true') {
  346.                 $val = 1;
  347.             }
  348.  
  349.             if ($val === 'false') {
  350.                 $val = 0;
  351.             }
  352.  
  353.             if ($val == 'NOW()') {
  354.                 $sql .= $field . ' = ' . $this->escape($val) . ', ';
  355.             } else {
  356.                 $sql .= $field . ' = \'' . $this->escape($val) . '\', ';
  357.             }
  358.         }
  359.  
  360.         return substr($sql, 0, -2);
  361.     }
  362. }
  363.  
  364. class ezSQL_mysql extends ezSQLcore {
  365.  
  366.     public $dbuser = false;
  367.     public $dbpassword = false;
  368.     public $dbname = false;
  369.     public $dbhost = false;
  370.     public $prefix = '';
  371.     public $trace = false;
  372.     public $debug_all = false;
  373.     public $debug_called = false;
  374.     public $vardump_called = false;
  375.     public $show_errors = true;
  376.     public $num_queries = 0;
  377.     public $last_query = null;
  378.     public $last_error = null;
  379.     public $col_info = null;
  380.     public $captured_errors = array();
  381.     public $cache_dir = false;
  382.     public $cache_queries = false;
  383.     public $cache_inserts = false;
  384.     public $use_disk_cache = false;
  385.     public $cache_timeout = 24;
  386.     public $timers = array();
  387.     public $total_query_time = 0;
  388.     public $db_connect_time = 0;
  389.     public $trace_log = array();
  390.     public $use_trace_log = false;
  391.     public $sql_log_file = false;
  392.     public $do_profile = false;
  393.     public $profile_times = array();
  394.     public $field_types = array();
  395.     public $real_escape = false;
  396.     public $debug_echo_is_on = true;
  397.  
  398.     public function ezSQL_mysql($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost')
  399.     {
  400.         $this->dbuser = $dbuser;
  401.         $this->dbpassword = $dbpassword;
  402.         $this->dbname = $dbname;
  403.         $this->dbhost = $dbhost;
  404.     }
  405.  
  406.     public function quick_connect($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost')
  407.     {
  408.         $return_val = false;
  409.  
  410.         if (!$this->connect($dbuser, $dbpassword, $dbhost, true)) {
  411.         } else if (!$this->select($dbname)) {
  412.         } else {
  413.             $return_val = true;
  414.         }
  415.  
  416.         return $return_val;
  417.     }
  418.  
  419.     public function connect($dbuser = '', $dbpassword = '', $dbhost = 'localhost')
  420.     {
  421.         global $ezsql_mysql_str;
  422.         $return_val = false;
  423.  
  424.         if (!$dbuser) {
  425.             $this->register_error($ezsql_mysql_str[1] . ' in ' . 'bilo.db.php' . ' on line ' . 659);
  426.             $this->show_errors ? trigger_error($ezsql_mysql_str[1], E_USER_WARNING) : null;
  427.         } else if (!$this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true)) {
  428.             $this->register_error($ezsql_mysql_str[2] . ' in ' . 'bilo.db.php' . ' on line ' . 665);
  429.             $this->show_errors ? trigger_error($ezsql_mysql_str[2], E_USER_WARNING) : null;
  430.         } else {
  431.             $this->dbuser = $dbuser;
  432.             $this->dbpassword = $dbpassword;
  433.             $this->dbhost = $dbhost;
  434.             $return_val = true;
  435.         }
  436.  
  437.         return $return_val;
  438.     }
  439.  
  440.     public function select($dbname = '')
  441.     {
  442.         global $ezsql_mysql_str;
  443.         $return_val = false;
  444.  
  445.         if (!$dbname) {
  446.             $this->register_error($ezsql_mysql_str[3] . ' in ' . 'bilo.db.php' . ' on line ' . 690);
  447.             $this->show_errors ? trigger_error($ezsql_mysql_str[3], E_USER_WARNING) : null;
  448.         } else if (!$this->dbh) {
  449.             $this->register_error($ezsql_mysql_str[4] . ' in ' . 'bilo.db.php' . ' on line ' . 697);
  450.             $this->show_errors ? trigger_error($ezsql_mysql_str[4], E_USER_WARNING) : null;
  451.         } else if (!@mysql_select_db($dbname, $this->dbh)) {
  452.             if (!($str = @mysql_error($this->dbh))) {
  453.                 $str = $ezsql_mysql_str[5];
  454.             }
  455.  
  456.             $this->register_error($str . ' in ' . 'bilo.db.php' . ' on line ' . 708);
  457.             $this->show_errors ? trigger_error($str, E_USER_WARNING) : null;
  458.         } else {
  459.             $this->dbname = $dbname;
  460.             $return_val = true;
  461.         }
  462.  
  463.         return $return_val;
  464.     }
  465.  
  466.     public function sysdate()
  467.     {
  468.         return 'NOW()';
  469.     }
  470.  
  471.     public function query($query)
  472.     {
  473.         $return_val = 0;
  474.         $this->flush();
  475.         $query = trim($query);
  476.         $this->func_call = '$db->query("' . $query . '")';
  477.         $this->last_query = $query;
  478.         $this->num_queries++;
  479.  
  480.         if ($cache = $this->get_cache($query)) {
  481.             return $cache;
  482.         }
  483.  
  484.         if (!isset($this->dbh) || !$this->dbh) {
  485.             $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
  486.             $this->select($this->dbname);
  487.         }
  488.  
  489.         $this->result = @mysql_query($query, $this->dbh);
  490.  
  491.         if ($str = @mysql_error($this->dbh)) {
  492.             $is_insert = true;
  493.             $this->register_error($str);
  494.             $this->show_errors ? trigger_error($str, E_USER_WARNING) : null;
  495.             return false;
  496.         }
  497.  
  498.         $is_insert = false;
  499.  
  500.         if (preg_match('/^(insert|delete|update|replace)\\s+/i', $query)) {
  501.             $this->rows_affected = @mysql_affected_rows();
  502.  
  503.             if (preg_match('/^(insert|replace)\\s+/i', $query)) {
  504.                 $this->insert_id = @mysql_insert_id($this->dbh);
  505.             }
  506.  
  507.             $return_val = $this->rows_affected;
  508.         } else {
  509.             $i = 0;
  510.  
  511.             while ($i < @mysql_num_fields($this->result)) {
  512.                 $this->col_info[$i] = @mysql_fetch_field($this->result);
  513.                 ++$i;
  514.             }
  515.  
  516.             $num_rows = 0;
  517.  
  518.             while ($row = @mysql_fetch_object($this->result)) {
  519.                 $this->last_result[$num_rows] = $row;
  520.                 ++$num_rows;
  521.             }
  522.  
  523.             @mysql_free_result($this->result);
  524.             $this->num_rows = $num_rows;
  525.             $return_val = $this->num_rows;
  526.         }
  527.  
  528.         $this->store_cache($query, $is_insert);
  529.         $this->trace || $this->debug_all ? $this->debug() : null;
  530.         return $return_val;
  531.     }
  532.  
  533.     public function _weak_escape($string)
  534.     {
  535.         return addslashes($string);
  536.     }
  537.  
  538.     public function _real_escape($string)
  539.     {
  540.         if ($this->dbh && $this->real_escape) {
  541.             return mysql_real_escape_string($string, $this->dbh);
  542.         }
  543.  
  544.         return addslashes($string);
  545.     }
  546.  
  547.     public function _escape($data)
  548.     {
  549.         if (is_array($data)) {
  550.             foreach ((array) $data as $k => $v) {
  551.                 if (is_array($v)) {
  552.                     $data[$k] = $this->_escape($v);
  553.                 } else {
  554.                     $data[$k] = $this->_real_escape($v);
  555.                 }
  556.             }
  557.         } else {
  558.             $data = $this->_real_escape($data);
  559.         }
  560.  
  561.         return $data;
  562.     }
  563.  
  564.     public function escape($data)
  565.     {
  566.         if (is_array($data)) {
  567.             foreach ((array) $data as $k => $v) {
  568.                 if (is_array($v)) {
  569.                     $data[$k] = $this->escape($v);
  570.                 } else {
  571.                     $data[$k] = $this->_weak_escape($v);
  572.                 }
  573.             }
  574.         } else {
  575.             $data = $this->_weak_escape($data);
  576.         }
  577.  
  578.         return $data;
  579.     }
  580.  
  581.     public function escape_by_ref(&$string)
  582.     {
  583.         $string = $this->_real_escape($string);
  584.     }
  585.  
  586.     public function prepare($query = null)
  587.     {
  588.         if (is_null($query)) {
  589.             return NULL;
  590.         }
  591.  
  592.         $args = func_get_args();
  593.         array_shift($args);
  594.  
  595.         if (isset($args[0]) && is_array($args[0])) {
  596.             $args = $args[0];
  597.         }
  598.  
  599.         $query = str_replace('\'%s\'', '%s', $query);
  600.         $query = str_replace('"%s"', '%s', $query);
  601.         $query = preg_replace('|(?<!%)%s|', '\'%s\'', $query);
  602.         array_walk($args, array($this, 'escape_by_ref'));
  603.         return @vsprintf($query, $args);
  604.     }
  605.  
  606.     public function insert($table, $data, $format = null)
  607.     {
  608.         return $this->_insert_replace_helper($table, $data, $format, 'INSERT');
  609.     }
  610.  
  611.     public function replace($table, $data, $format = null)
  612.     {
  613.         return $this->_insert_replace_helper($table, $data, $format, 'REPLACE');
  614.     }
  615.  
  616.     public function _insert_replace_helper($table, $data, $format = null, $type = 'INSERT')
  617.     {
  618.         if (!in_array(strtoupper($type), array('REPLACE', 'INSERT'))) {
  619.             return false;
  620.         }
  621.  
  622.         $formats = $format = (array) $format;
  623.         $fields = array_keys($data);
  624.         $formatted_fields = array();
  625.  
  626.         foreach ($fields as $field) {
  627.             if (!empty($format)) {
  628.                 $form = ($form = array_shift($formats) ? $form : $format[0]);
  629.             } else if (isset($this->field_types[$field])) {
  630.                 $form = $this->field_types[$field];
  631.             } else {
  632.                 $form = '%s';
  633.             }
  634.  
  635.             $formatted_fields[] = $form;
  636.         }
  637.  
  638.         $sql = $type . ' INTO `' . $table . '` (`' . implode('`,`', $fields) . '`) VALUES (\'' . implode('\',\'', $formatted_fields) . '\')';
  639.         return $this->query($this->prepare($sql, $data));
  640.     }
  641.  
  642.     public function update($table, $data, $where, $format = null, $where_format = null)
  643.     {
  644.         if (!is_array($data) || !is_array($where)) {
  645.             return false;
  646.         }
  647.  
  648.         $formats = $format = (array) $format;
  649.         $bits = $wheres = array();
  650.  
  651.         foreach ((array) array_keys($data) as $field) {
  652.             if (!empty($format)) {
  653.                 $form = ($form = array_shift($formats) ? $form : $format[0]);
  654.             } else if (isset($this->field_types[$field])) {
  655.                 $form = $this->field_types[$field];
  656.             } else {
  657.                 $form = '%s';
  658.             }
  659.  
  660.             $bits[] = '`' . $field . '` = ' . $form;
  661.         }
  662.  
  663.         $where_formats = $where_format = (array) $where_format;
  664.  
  665.         foreach ((array) array_keys($where) as $field) {
  666.             if (!empty($where_format)) {
  667.                 $form = ($form = array_shift($where_formats) ? $form : $where_format[0]);
  668.             } else if (isset($this->field_types[$field])) {
  669.                 $form = $this->field_types[$field];
  670.             } else {
  671.                 $form = '%s';
  672.             }
  673.  
  674.             $wheres[] = '`' . $field . '` = ' . $form;
  675.         }
  676.  
  677.         $sql = 'UPDATE `' . $table . '` SET ' . implode(', ', $bits) . ' WHERE ' . implode(' AND ', $wheres);
  678.         return $this->query($this->prepare($sql, array_merge(array_values($data), array_values($where))));
  679.     }
  680.  
  681.     public function ezSQLcore()
  682.     {
  683.     }
  684.  
  685.     public function register_error($err_str)
  686.     {
  687.         $this->last_error = $err_str;
  688.         $this->captured_errors[] = array('error_str' => $err_str, 'query' => $this->last_query);
  689.     }
  690.  
  691.     public function show_errors()
  692.     {
  693.         $this->show_errors = true;
  694.     }
  695.  
  696.     public function hide_errors()
  697.     {
  698.         $this->show_errors = false;
  699.     }
  700.  
  701.     public function flush()
  702.     {
  703.         $this->last_result = null;
  704.         $this->col_info = null;
  705.         $this->last_query = null;
  706.         $this->from_disk_cache = false;
  707.     }
  708.  
  709.     public function get_var($query = null, $x = 0, $y = 0)
  710.     {
  711.         $this->func_call = '$db->get_var("' . $query . '",' . $x . ',' . $y . ')';
  712.  
  713.         if ($query) {
  714.             $this->query($query);
  715.         }
  716.  
  717.         if ($this->last_result[$y]) {
  718.             $values = array_values(get_object_vars($this->last_result[$y]));
  719.         }
  720.  
  721.         return isset($values[$x]) && ($values[$x] !== '') ? $values[$x] : null;
  722.     }
  723.  
  724.     public function set_prefix($table_prefix)
  725.     {
  726.         $this->prefix = $table_prefix;
  727.     }
  728.  
  729.     public function get_row($query = null, $output = OBJECT, $y = 0)
  730.     {
  731.         $this->func_call = '$db->get_row("' . $query . '",' . $output . ',' . $y . ')';
  732.  
  733.         if ($query) {
  734.             $this->query($query);
  735.         }
  736.  
  737.         if ($output == OBJECT) {
  738.             return $this->last_result[$y] ? $this->last_result[$y] : null;
  739.         }
  740.  
  741.         if ($output == ARRAY_A) {
  742.             return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
  743.         }
  744.  
  745.         if ($output == ARRAY_N) {
  746.             return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
  747.         }
  748.  
  749.         $this->print_error(' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N');
  750.     }
  751.  
  752.     public function get_col($query = null, $x = 0)
  753.     {
  754.         $new_array = array();
  755.  
  756.         if ($query) {
  757.             $this->query($query);
  758.         }
  759.  
  760.         $i = 0;
  761.  
  762.         while ($i < count($this->last_result)) {
  763.             $new_array[$i] = $this->get_var(null, $x, $i);
  764.             ++$i;
  765.         }
  766.  
  767.         return $new_array;
  768.     }
  769.  
  770.     public function get_results($query = null, $output = OBJECT)
  771.     {
  772.         $this->func_call = '$db->get_results("' . $query . '", ' . $output . ')';
  773.  
  774.         if ($query) {
  775.             $this->query($query);
  776.         }
  777.  
  778.         if ($output == OBJECT) {
  779.             return $this->last_result;
  780.         }
  781.  
  782.         if (($output == ARRAY_A) || ($output == ARRAY_N)) {
  783.             if ($this->last_result) {
  784.                 $i = 0;
  785.  
  786.                 foreach ($this->last_result as $row) {
  787.                     $new_array[$i] = get_object_vars($row);
  788.  
  789.                     if ($output == ARRAY_N) {
  790.                         $new_array[$i] = array_values($new_array[$i]);
  791.                     }
  792.  
  793.                     ++$i;
  794.                 }
  795.  
  796.                 return $new_array;
  797.             }
  798.  
  799.             return null;
  800.         }
  801.     }
  802.  
  803.     public function get_col_info($info_type = 'name', $col_offset = -1)
  804.     {
  805.         if ($this->col_info) {
  806.             if ($col_offset == -1) {
  807.                 $i = 0;
  808.  
  809.                 foreach ($this->col_info as $col) {
  810.                     $new_array[$i] = $col->{$info_type};
  811.                     ++$i;
  812.                 }
  813.  
  814.                 return $new_array;
  815.             }
  816.  
  817.             return $this->col_info[$col_offset]->{$info_type};
  818.         }
  819.     }
  820.  
  821.     public function store_cache($query, $is_insert)
  822.     {
  823.         $cache_file = $this->cache_dir . '/' . md5($query);
  824.         if (($this->use_disk_cache && $this->cache_queries && !$is_insert) || ($this->cache_inserts && $is_insert)) {
  825.             if (!is_dir($this->cache_dir)) {
  826.                 $this->register_error('Could not open cache dir: ' . $this->cache_dir);
  827.                 $this->show_errors ? trigger_error('Could not open cache dir: ' . $this->cache_dir, E_USER_WARNING) : null;
  828.                 return NULL;
  829.             }
  830.  
  831.             $result_cache = array('col_info' => $this->col_info, 'last_result' => $this->last_result, 'num_rows' => $this->num_rows, 'return_value' => $this->num_rows);
  832.             error_log(serialize($result_cache), 3, $cache_file);
  833.         }
  834.     }
  835.  
  836.     public function get_cache($query)
  837.     {
  838.         $cache_file = $this->cache_dir . '/' . md5($query);
  839.         if ($this->use_disk_cache && file_exists($cache_file)) {
  840.             if ($this->cache_timeout < (time() - filemtime($cache_file))) {
  841.                 unlink($cache_file);
  842.                 return NULL;
  843.             }
  844.  
  845.             $result_cache = unserialize(file_get_contents($cache_file));
  846.             $this->col_info = $result_cache['col_info'];
  847.             $this->last_result = $result_cache['last_result'];
  848.             $this->num_rows = $result_cache['num_rows'];
  849.             $this->from_disk_cache = true;
  850.             $this->trace || $this->debug_all ? $this->debug() : null;
  851.             return $result_cache['return_value'];
  852.         }
  853.     }
  854.  
  855.     public function vardump($mixed = '')
  856.     {
  857.         ob_start();
  858.         echo '<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>';
  859.         echo '<pre><font face=arial>';
  860.  
  861.         if (!$this->vardump_called) {
  862.             echo '<font color=800080><b>ezSQL</b> (v' . EZSQL_VERSION . ') <b>Variable Dump..</b></font>' . "\n\n";
  863.         }
  864.  
  865.         $var_type = gettype($mixed);
  866.         print_r($mixed ? $mixed : '<font color=red>No Value / False</font>');
  867.         echo "\n\n" . '<b>Type:</b> ' . ucfirst($var_type) . "\n";
  868.         echo '<b>Last Query</b> [' . $this->num_queries . ']<b>:</b> ' . ($this->last_query ? $this->last_query : 'NULL') . "\n";
  869.         echo '<b>Last Function Call:</b> ' . ($this->func_call ? $this->func_call : 'None') . "\n";
  870.         echo '<b>Last Rows Returned:</b> ' . count($this->last_result) . "\n";
  871.         echo '</font></pre></font></blockquote></td></tr></table>' . $this->donation();
  872.         echo "\n" . '<hr size=1 noshade color=dddddd>';
  873.         $html = ob_get_contents();
  874.         ob_end_clean();
  875.  
  876.         if ($this->debug_echo_is_on) {
  877.             echo $html;
  878.         }
  879.  
  880.         $this->vardump_called = true;
  881.         return $html;
  882.     }
  883.  
  884.     public function dumpvar($mixed)
  885.     {
  886.         $this->vardump($mixed);
  887.     }
  888.  
  889.     public function debug($print_to_screen = true)
  890.     {
  891.         ob_start();
  892.         echo '<blockquote>';
  893.  
  894.         if (!$this->debug_called) {
  895.             echo '<font color=800080 face=arial size=2><b>ezSQL</b> (v' . EZSQL_VERSION . ') <b>Debug..</b></font><p>' . "\n";
  896.         }
  897.  
  898.         if ($this->last_error) {
  899.             echo '<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>' . $this->last_error . '</b></font>]<p>';
  900.         }
  901.  
  902.         if ($this->from_disk_cache) {
  903.             echo '<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>';
  904.         }
  905.  
  906.         echo '<font face=arial size=2 color=000099><b>Query</b> [' . $this->num_queries . '] <b>--</b> ';
  907.         echo '[<font color=000000><b>' . $this->last_query . '</b></font>]</font><p>';
  908.         echo '<font face=arial size=2 color=000099><b>Query Result..</b></font>';
  909.         echo '<blockquote>';
  910.  
  911.         if ($this->col_info) {
  912.             echo '<table cellpadding=5 cellspacing=1 bgcolor=555555>';
  913.             echo '<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>';
  914.             $i = 0;
  915.  
  916.             while ($i < count($this->col_info)) {
  917.                 echo '<td nowrap align=left valign=top><font size=1 color=555599 face=arial>' . $this->col_info[$i]->type . ' ' . $this->col_info[$i]->max_length . '</font><br><span style=\'font-family: arial; font-size: 10pt; font-weight: bold;\'>' . $this->col_info[$i]->name . '</span></td>';
  918.                 ++$i;
  919.             }
  920.  
  921.             echo '</tr>';
  922.  
  923.             if ($this->last_result) {
  924.                 $i = 0;
  925.  
  926.                 foreach ($this->get_results(null, ARRAY_N) as $one_row) {
  927.                     ++$i;
  928.                     echo '<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>' . $i . '</font></td>';
  929.  
  930.                     foreach ($one_row as $item) {
  931.                         echo '<td nowrap><font face=arial size=2>' . $item . '</font></td>';
  932.                     }
  933.  
  934.                     echo '</tr>';
  935.                 }
  936.             } else {
  937.                 echo '<tr bgcolor=ffffff><td colspan=' . (count($this->col_info) + 1) . '><font face=arial size=2>No Results</font></td></tr>';
  938.             }
  939.  
  940.             echo '</table>';
  941.         } else {
  942.             echo '<font face=arial size=2>No Results</font>';
  943.         }
  944.  
  945.         echo '</blockquote></blockquote>' . $this->donation() . '<hr noshade color=dddddd size=1>';
  946.         $html = ob_get_contents();
  947.         ob_end_clean();
  948.         if ($this->debug_echo_is_on && $print_to_screen) {
  949.             echo $html;
  950.         }
  951.  
  952.         $this->debug_called = true;
  953.         return $html;
  954.     }
  955.  
  956.     public function donation()
  957.     {
  958.         return '<font size=1 face=arial color=000000>If ezSQL has helped <a href="https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0" style="color: 0000CC;">make a donation!?</a> &nbsp;&nbsp;<!--[ go on! you know you want to! ]--></font>';
  959.     }
  960.  
  961.     public function timer_get_cur()
  962.     {
  963.         list($usec, $sec) = explode(' ', microtime());
  964.         return (double) $usec + (double) $sec;
  965.     }
  966.  
  967.     public function timer_start($timer_name)
  968.     {
  969.         $this->timers[$timer_name] = $this->timer_get_cur();
  970.     }
  971.  
  972.     public function timer_elapsed($timer_name)
  973.     {
  974.         return round($this->timer_get_cur() - $this->timers[$timer_name], 2);
  975.     }
  976.  
  977.     public function timer_update_global($timer_name)
  978.     {
  979.         if ($this->do_profile) {
  980.             $this->profile_times[] = array('query' => $this->last_query, 'time' => $this->timer_elapsed($timer_name));
  981.         }
  982.  
  983.         $this->total_query_time += $this->timer_elapsed($timer_name);
  984.     }
  985.  
  986.     public function get_set($parms)
  987.     {
  988.         $sql = '';
  989.  
  990.         foreach ($parms as $field => $val) {
  991.             if ($val === 'true') {
  992.                 $val = 1;
  993.             }
  994.  
  995.             if ($val === 'false') {
  996.                 $val = 0;
  997.             }
  998.  
  999.             if ($val == 'NOW()') {
  1000.                 $sql .= $field . ' = ' . $this->escape($val) . ', ';
  1001.             } else {
  1002.                 $sql .= $field . ' = \'' . $this->escape($val) . '\', ';
  1003.             }
  1004.         }
  1005.  
  1006.         return substr($sql, 0, -2);
  1007.     }
  1008. }
  1009.  
  1010. define('EZSQL_VERSION', '2.12');
  1011. define('OBJECT', 'OBJECT', true);
  1012. define('ARRAY_A', 'ARRAY_A', true);
  1013. define('ARRAY_N', 'ARRAY_N', true);
  1014. $ezsql_mysql_str = array(1 => 'Require $dbuser and $dbpassword to connect to a database server', 2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?', 3 => 'Require $dbname to select a database', 4 => 'mySQL database connection is not active', 5 => 'Unexpected error while trying to select database');
  1015.  
  1016. if (!function_exists('mysql_connect')) {
  1017.     exit('<b>Fatal Error:</b> ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine');
  1018. }
  1019.  
  1020. if (!class_exists('ezSQLcore')) {
  1021.     exit('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  1022. }
  1023.  
  1024. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement