Advertisement
Guest User

MySQL driver

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