Advertisement
Guest User

MySQLi Driver

a guest
Feb 28th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.65 KB | None | 0 0
  1. <?php
  2. // -------------------------------------------------------------------------//
  3. //                               RUNCMS                                     //
  4. //                                                                          //
  5. //   reliable - Unique - Nocost &  Simplicity & ease off use                //
  6. //                       < https://www.runcms.org >                          //
  7. // -------------------------------------------------------------------------//
  8. // Original Author: Half-Dead
  9. // Author Website : https://www.e-xoops.com
  10. // License Type   : Proprietary: See /manual/LICENSES/E-Xoops.txt
  11. // Modified       : SVL https://www.propan-sochi.ru/ & https://www.runetcms.ru/
  12. // -------------------------------------------------------------------------//
  13. if (!defined('XOOPS_ROOT_PATH')) exit();
  14.  
  15. /**
  16.  * @todo is real need for checking of defined("SQL_LAYER")?
  17.  */
  18. if (defined("SQL_LAYER")) exit ();
  19.  
  20. define("SQL_LAYER", "mysqli");
  21.  
  22. include_once(XOOPS_ROOT_PATH."/class/database.php");
  23.  
  24. /**
  25. * MySQL datadase class
  26. *
  27. */
  28. class Database extends AbsDatabase
  29. {
  30.    var $db_connect;
  31.  
  32.    /**
  33.      * Real SQL queries loag
  34.      *
  35.      * @var Array of string
  36.      */
  37.    var $query_log;
  38.  
  39.    /**
  40.    * Background class
  41.    *
  42.    * @var String
  43.    */
  44.    var $bg = 'bg1';
  45.    var $query_result;
  46.  
  47.    var $num_queries = 0;
  48.  
  49.    /**
  50.      * Cached queries log
  51.      *
  52.      * @var  Array of string
  53.      */
  54.    var $cached_query_log = array();
  55.  
  56.    /**
  57.      * Save errors log to files
  58.      *
  59.      * @var bool
  60.      * @todo settings
  61.      */
  62.    var $save_error_logs = false;
  63.  
  64.  
  65.    var $row = array();
  66.  
  67.    var $rowset = array();
  68.  
  69.    /**
  70.     * Array for storing $set from file cache
  71.     *
  72.     * @var Array
  73.     */
  74.    var $cache = array();
  75.    var $caching = false;
  76.    var $cached = false;
  77.  
  78.    /**
  79.    * Connect to the database
  80.    *
  81.    * @param String $server
  82.    * @param String $user
  83.    * @param String $pass
  84.    * @param Number $persistent
  85.    * @return DB Connect
  86.    */
  87.    function connect($server, $user, $pass, $persistent=0)
  88.    {
  89.       $this->db_connect = mysqli_connect($server, $user, $pass);
  90.  
  91.       return $this->db_connect;
  92.    }
  93.  
  94.    /**
  95.     * Select the database
  96.     *
  97.     * @param unknown_type $db
  98.     * @return unknown
  99.     */
  100.    function select_db($db)
  101.    {
  102.       return mysqli_select_db($this->db_connect, $db);
  103.    }
  104.  
  105.    /**
  106.     * Close MySQL connection
  107.     *
  108.     */
  109.    function close()
  110.    {
  111.       if($this->query_result)
  112.       {
  113.          mysqli_free_result($this->query_result);
  114.       }
  115.  
  116.       return mysqli_close($this->db_connect);
  117.    }
  118.  
  119.    /**
  120.     *  Perform a query on the MySQL database with debug info if set also perform sql cache
  121.     *
  122.     * @param String $sql - SQL query for execute
  123.     * @param Number $limit - Limit of SQL resut
  124.     * @param Number $start - Start for SQL
  125.     * @param String/false $cache - String for name of cached SQL file
  126.     * @return Array/false
  127.     * @todo Time for storing database caching file
  128.     */
  129.    function query($sql, $limit=0, $start=0, $cache = false,$time_to_cache=false)
  130.    {
  131.       // Remove any pre-existing queries
  132.       unset($this->query_result);
  133.       // Check cache
  134.       $this->caching = false;
  135.       $this->cache = array();
  136.       $this->cached = false;
  137.       if($sql !== '' && $cache)
  138.       {
  139.          $hash = md5($sql);
  140.          if(strlen($cache))
  141.          {
  142.             $hash = $cache . $hash;
  143.          }
  144.          $filename = XOOPS_ROOT_PATH.'/cache/sql/sql_'.$hash.'.php';
  145.          if ($time_to_cache && @file_exists($filename) && (@filemtime($filename)+$time_to_cache<time())) $this->clear_cache($this->caching);
  146.          if(@file_exists($filename))
  147.          {
  148.             $set = array();
  149.             include($filename);
  150.             $this->cache = $set;
  151.             $this->cached = true;
  152.             $this->caching = false;
  153.  
  154.             if (($this->debug & 4) || ($this->debug & 8) || ($this->debug & 16))
  155.             {
  156.                $this->cached_query_log[] = $sql;
  157.                $this->trace($sql);
  158.             }
  159.  
  160.             return 'cache';
  161.          }
  162.          // missing cache file
  163.          //        echo 'cache is missing: ', $filename, '<br />';
  164.          $this->caching = $hash;
  165.  
  166.       }
  167.       // not cached
  168.       //      echo 'sql: ', htmlspecialchars($sql), '<br />';
  169.       if (($this->debug & 8) || ($this->debug & 16))
  170.       {
  171.          $this->query_log[] = $sql;
  172.       }
  173.       else
  174.       {
  175.          $this->query_log[] = 0;
  176.       }
  177.       // deprecatory
  178.       if (!empty($limit))
  179.       {
  180.          if (empty($start))
  181.          {
  182.             $start = 0;
  183.          }
  184.          $sql = $sql. " LIMIT ".intval($start).",".intval($limit)."";
  185.       }
  186.  
  187.       if ($this->debug & 16)
  188.       {
  189.          $output = '';
  190.          if (function_exists('xdebug_call_function'))
  191.          {
  192.             if (function_exists('xdebug_call_class'))
  193.             {
  194.                if ($caller_class = xdebug_call_class())
  195.                {
  196.                   $output = "<strong>".$caller_class." ::</strong> ";
  197.                }
  198.             }
  199.             $output .= "<strong>".xdebug_call_function()."()</strong><br />";
  200.          }
  201.          $this->trace($output . count($this->query_log) . ": " . $sql);
  202.       }
  203.  
  204.       if($sql != '')
  205.       {
  206.          $this->num_queries++;
  207.          $this->query_result = mysqli_query($this->db_connect, $sql);
  208.       }
  209.  
  210.       if($this->query_result)
  211.       {
  212.          unset($this->row[$this->query_result]);
  213.          unset($this->rowset[$this->query_result]);
  214.          return $this->query_result;
  215.       }
  216.       else
  217.       {
  218.          $this->log_error($sql);
  219.          return false;
  220.       }
  221.    }
  222.  
  223.  
  224.    /**
  225.     * Save the current SQL error to text log
  226.     *
  227.     * @author Michael XIII Neradkov
  228.     * @param String $sql - SQL query which have an error
  229.     * @param $error - String error, if false - get simple MySQL errror
  230.     */
  231.    function log_error($sql, $error = false)
  232.    {
  233.       $text = ($error) ? $error : $this->error();
  234.  
  235.       if (($this->debug & 4) || ($this->debug & 8) || ($this->debug & 16))
  236.       {
  237.          echo "<div class=\"rc_warning\">$text: $sql</div>";
  238.       }
  239.  
  240.       if($this->save_error_logs)
  241.       {
  242.          $content = "\n\n".date("Y m d H:i:s")."\nIP:"._REMOTE_ADDR."\n";
  243.          $content .= "GET: "._QUERY_STRING."\nLocation: "._PHP_SELF."\n";
  244.          $content .= "Query: $sql\nError$text";
  245.  
  246.          $filename = XOOPS_ROOT_PATH."/cache/sql_error-".date("Y_m_d").".log";
  247.  
  248.          if ( $file = fopen($filename, "a") )
  249.          {
  250.             fwrite($file, $content);
  251.             fclose($file);
  252.          }
  253.       }
  254.    }
  255.  
  256.    /**
  257.     * Show SQL query on page
  258.     * @author Michael XIII Neradkov
  259.     */
  260.    function trace($text)
  261.    {
  262.       if ($this->debug & 16)
  263.       {
  264.          $this->bg = ($this->bg == 'bg2') ? 'bg1' : 'bg2';
  265.  
  266.          $r = "<div class='".$this->bg;
  267.          $r .= ($this->cached) ? " cached_sql_dbg'>Cached: " : " sql_dbg'>";
  268.          $r .= $text."</div>";
  269.  
  270.          echo $r;
  271.       }
  272.    }
  273.  
  274.    /**
  275.     * Depracted, use $db->query()
  276.     * For backward compatibility only
  277.     * @deprecated
  278.     */
  279.    function queryF($sql, $limit=0, $start=0, $cache = false)
  280.    {
  281.       return $this->query($sql, $limit, $start, $cache = false);
  282.    }
  283.  
  284.    /**
  285.     * generate an ID for a new row
  286.     *
  287.     * This is for compatibility only. Will always return 0, because MySQL supports
  288.     * autoincrement for primary keys.
  289.     *
  290.     * @param string $sequence name of the sequence from which to get the next ID
  291.     * @return int always 0, because mysql has support for autoincrement
  292.     */
  293.    function genId($sequence)
  294.    {
  295.       return 0;
  296.    }
  297.  
  298.    /**
  299.     * Get number of affected rows in previous MySQL operation
  300.     *
  301.     * @return Returns the number of affected rows on success, and -1 if the last query failed.
  302.     */
  303.    function affected_rows()
  304.    {
  305.       return mysqli_affected_rows($this->db_connect);
  306.    }
  307.  
  308.    /**
  309.     * Get number of rows in result
  310.     *
  311.     * @param resource query result
  312.     * @return The number of rows in a result set on success, or FALSE on failure.
  313.     */
  314.    function num_rows($resource)
  315.    {
  316.       if($resource === 'cache' && $this->cached)
  317.       {
  318.          return count($this->cache);
  319.       }
  320.  
  321.       return mysqli_num_rows($resource);
  322.    }
  323.  
  324.    /**
  325.     * Get number of fields in result
  326.     *
  327.     * @param resource $result query result
  328.     * @return Returns the number of fields in the result set resource on success,
  329.     * or FALSE on failure.
  330.     */
  331.    function num_fields($resource)
  332.    {
  333.       return mysqli_num_fields($resource);
  334.    }
  335.  
  336.    /**
  337.     * Get the name of the specified field in a result
  338.     *
  339.     * @param resource $result query result
  340.     * @param int numerical field index
  341.     * @return The name of the specified field index on success, or FALSE on failure.
  342.     */
  343.    function field_name($resource, $index)
  344.    {
  345.       return mysqli_fetch_field_direct($resource, $index)->name;
  346.    }
  347.  
  348.    /**
  349.     * Get field type
  350.     *
  351.     * @param resource $result query result
  352.     * @param int $offset numerical field index
  353.     * @return The returned field type will be one of "int", "real", "string", "blob",
  354.     * and others as detailed in the MySQL documentation.
  355.     */
  356.    function field_type($resource, $offset)
  357.    {
  358.       return mysqli_fetch_field_direct($resource, $offset)->type;
  359.    }
  360.  
  361.    /**
  362.     * Fetch a result row as an associative array
  363.     *
  364.     * @return Returns an array that corresponds to the fetched row,
  365.     * or FALSE if there are no more rows.
  366.     */
  367.    function fetch_array($resource)
  368.    {
  369.       if($resource === 'cache' && $this->cached)
  370.       {
  371.          return count($this->cache) ? array_shift($this->cache) : false;
  372.       }
  373.  
  374.       $this->row[$resource] = mysqli_fetch_array($resource);
  375.  
  376.       if($this->caching)
  377.       {
  378.          if($this->row[$resource] === false)
  379.          {
  380.             $this->write_cache();
  381.          }
  382.          $this->cache[] = $this->row[$resource];
  383.       }
  384.       return $this->row[$resource];
  385.    }
  386.  
  387.    /**
  388.     * Fetch a result row as an object
  389.     *
  390.     * @return Returns an object with properties that correspond to the fetched row,
  391.     * or FALSE if there are no more rows.
  392.     */
  393.    function fetch_object($resource)
  394.    {
  395.       return mysqli_fetch_object($resource);
  396.    }
  397.  
  398.    /**
  399.     * Get a result row as an enumerated array
  400.     *
  401.     * @param resource $resource
  402.     * @return Returns an numerical array that corresponds to the fetched row,
  403.     * or FALSE if there are no more rows.
  404.     */
  405.    function fetch_row($resource)
  406.    {
  407.       return mysqli_fetch_row($resource);
  408.    }
  409.  
  410.  
  411.    /**
  412.     * Get the ID generated from the previous INSERT operation
  413.     *
  414.     * @return The ID generated for an AUTO_INCREMENT column by the previous INSERT query
  415.     * on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or
  416.     * FALSE if no MySQL connection was established.
  417.     */
  418.    function insert_id()
  419.    {
  420.       return mysqli_insert_id($this->db_connect);
  421.    }
  422.  
  423.  
  424.    /**
  425.     * Returns the text of the error message from previous MySQL operation
  426.     *
  427.     * @return bool Returns the error text from the last MySQL function,
  428.     * or '' (the empty string) if no error occurred.
  429.     * @return int Returns the error number from the last MySQL function,
  430.     * or 0 (zero) if no error occurred.
  431.     */
  432.    function error()
  433.    {
  434.       if ($this->debug & 1)
  435.       {
  436.          return mysqli_errno($this->db_connect) . ": " . mysqli_error($this->db_connect);
  437.       }
  438.    }
  439.  
  440.    /**
  441.     * Fetch a result row as an n-d associative array
  442.     *
  443.     * @param resource $result
  444.     * @return array set
  445.     */
  446.    function fetch_rowset($resource = 0)
  447.    {
  448.       if($resource === 'cache' && $this->cached)
  449.       {
  450.          return $this->cache;
  451.       }
  452.       while($this->rowset[$resource] = mysqli_fetch_array($resource))
  453.       {
  454.          if($this->caching)
  455.          {
  456.             if($this->row[$resource] === false)
  457.             {
  458.                $this->write_cache();
  459.             }
  460.             $this->cache[] = $this->row[$resource];
  461.          }
  462.          $result[] = $this->rowset[$resource];
  463.       }
  464.       if($this->caching)
  465.       {
  466.          $this->write_cache();
  467.       }
  468.  
  469.       return $result;
  470.    }
  471.    
  472.    /**
  473.     * Will free all memory associated with the result identifier result.
  474.     *
  475.     * @param resource query result
  476.     * @return bool TRUE on success or FALSE on failure.
  477.     */
  478.    function free_result($resource = 0)
  479.    {
  480.       if($resource === 'cache')
  481.       {
  482.          $this->caching = false;
  483.          $this->cached = false;
  484.          $this->cache = array();
  485.       }
  486.      
  487.       if($this->caching)
  488.       {
  489.          $this->write_cache();
  490.       }
  491.      
  492.       if(!$resource)
  493.       {
  494.          $resource = $this->query_result;
  495.       }
  496.  
  497.       if ($resource)
  498.       {
  499.          unset($this->row[$resource]);
  500.          unset($this->rowset[$resource]);
  501.          mysqli_free_result($resource);
  502.          return true;
  503.       }
  504.       else
  505.       {
  506.          return false;
  507.       }
  508.    }
  509.  
  510.    /**
  511.     * Fetch a result row as an associative array
  512.     *
  513.     * @return Returns an associative array that corresponds to the fetched row,
  514.     * or FALSE if there are no more rows.
  515.     */
  516.    function fetch_assoc($resource = 0)
  517.    {
  518.       if($resource === 'cache' && $this->cached)
  519.       {
  520.          return count($this->cache) ? array_shift($this->cache) : false;
  521.       }
  522.  
  523.       $this->row[$resource] = mysqli_fetch_assoc($resource);
  524.  
  525.       if($this->caching)
  526.       {
  527.          if($this->row[$resource] === false)
  528.          {
  529.             $this->write_cache();
  530.          }
  531.          $this->cache[] = $this->row[$resource];
  532.       }
  533.       return $this->row[$resource];
  534.       //      return mysqli_fetch_assoc($resource);
  535.    }
  536.  
  537.    /**
  538.     * Escapes special characters in a string for use in a SQL statement
  539.     *
  540.     * @return Returns the escaped string.
  541.     */
  542.    function escape($str)
  543.    {
  544.       return mysqli_real_escape_string($this->db_connect, $str);
  545.    }
  546.  
  547.    /**
  548.     * Get result data
  549.     *
  550.     * @return The contents of one cell from a MySQL result set on success,
  551.     * or FALSE on failure.
  552.     */
  553.    //function result($query_id = 0, $row = 0)
  554.    //{
  555.    //   return mysqli_result($query_id, $row);
  556.    //}
  557.  
  558.    function result($query_id = 0, $row = 0, $field = 0)
  559.    {
  560.       $query_id->data_seek($row);
  561.       $datarow = $query_id->fetch_array();
  562.       return $datarow[$field];
  563.    }
  564.    /**
  565.     * Write sql cache to file
  566.     *
  567.     * @return
  568.     */
  569.    function write_cache()
  570.    {
  571.       if(!$this->caching)
  572.       {
  573.          return;
  574.       }
  575.       $f = fopen(XOOPS_ROOT_PATH.'/cache/sql/sql_'.$this->caching.'.php', 'w');
  576.       $data = var_export($this->cache, true);
  577.       @flock($f,LOCK_EX);
  578.       @fputs($f, '<?php $set = ' . $data . '; ?>');
  579.       @flock($f,LOCK_UN);
  580.       @fclose($f);
  581.       @chmod(XOOPS_ROOT_PATH.'/cache/sql/sql_'.$this->caching.'.php', 0777);
  582.       $this->caching = false;
  583.       $this->cached = false;
  584.       $this->cache = array();
  585.    }
  586.  
  587.    /**
  588.     * Clear cache files
  589.     *
  590.     * @return
  591.     */
  592.    function clear_cache($prefix = '')
  593.    {
  594.       $this->caching = false;
  595.       $this->cached = false;
  596.       $this->cache = array();
  597.       $prefix = 'sql_' . $prefix;
  598.       $prefix_len = strlen($prefix);
  599.       $res = opendir(XOOPS_ROOT_PATH.'/cache/sql');
  600.       if($res)
  601.       {
  602.          while(($file = readdir($res)) !== false)
  603.          {
  604.             if(substr($file, 0, $prefix_len) === $prefix)
  605.             {
  606.                @unlink(XOOPS_ROOT_PATH.'/cache/sql/'.$file);
  607.             }
  608.          }
  609.       }
  610.       @closedir($res);
  611.    }
  612. } // END CLASS
  613.  
  614. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement