Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.06 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @version      database.php 2011-02-14 08:01
  5. * @package      ErCox.Framework
  6. * @copyright    Copyright (C) ErCox, Damjan Krstevski. All rights reserved.
  7. * @license      GNU/GPL, see LICENSE.php
  8. */
  9.  
  10.    
  11.     /**
  12.     * Database class
  13.     *
  14.     * @package      ErCox.Framework
  15.     */
  16.     class Database {
  17.        
  18.         /**
  19.         * MySql Connection
  20.         *
  21.         * @access public
  22.         * @var resource
  23.         */
  24.         public $connection = NULL;
  25.        
  26.        
  27.         /**
  28.         * MySql Hostname
  29.         *
  30.         * @access private
  31.         * @var string
  32.         */
  33.         private $DbHost     = NULL;
  34.        
  35.        
  36.         /**
  37.         * MySql Root Username
  38.         *
  39.         * @access private
  40.         * @var string
  41.         */
  42.         private $DbUser     = NULL;
  43.        
  44.        
  45.         /**
  46.         * MySql Root Password
  47.         *
  48.         * @access private
  49.         * @var string
  50.         */
  51.         private $DbPass     = NULL;
  52.        
  53.        
  54.         /**
  55.         * Used Database
  56.         *
  57.         * @access private
  58.         * @var string
  59.         */
  60.         private $DbData     = NULL;
  61.        
  62.        
  63.         /**
  64.         * Database TimeOut
  65.         *
  66.         * @access private
  67.         * @var int
  68.         */
  69.         private $DbTimeout  = 10;
  70.        
  71.        
  72.         /**
  73.         * Database Table Prefix
  74.         *
  75.         * @access private
  76.         * @var string
  77.         */
  78.         private $DbPrefix   = NULL;
  79.        
  80.        
  81.         /**
  82.         * MySql Error Number
  83.         *
  84.         * @access private
  85.         * @var int
  86.         */
  87.         private $DbError    = 0;
  88.        
  89.        
  90.        
  91.         /**
  92.         * Database object constructor
  93.         *
  94.         * @access public
  95.         * @param    array $params Database Information (Host, User, Pass, Data, Prefix, [TimeOut])
  96.         */
  97.         public function __construct( $params = NULL )
  98.         {
  99.             /* Validate Params */
  100.             if ( !$this->set_dbparams($params) )
  101.             {
  102.                 return;
  103.             }
  104.            
  105.             /* Validate if the MySQL functions is defined */
  106.             if( !$this->test_db_functions(true) )
  107.             {
  108.                 return;
  109.             }
  110.            
  111.             /* Connect with the MySql database */
  112.             if( !$this->connect() )
  113.             {
  114.                 return;
  115.             }
  116.            
  117.             if( !$this->select_db() )
  118.             {
  119.                 return;
  120.             }
  121.            
  122.             if( !$this->set_charset() )
  123.             {
  124.                 return;
  125.             }
  126.         }
  127.        
  128.        
  129.         /**
  130.         * Database object destructor
  131.         *
  132.         * @access   public
  133.         */
  134.         public function __destruct()
  135.         {
  136.             /* Close the connection */
  137.             $this->close();
  138.            
  139.             /* Free Memory */
  140.             $this->DbHost       = NULL;
  141.             $this->DbUser       = NULL;
  142.             $this->DbPass       = NULL;
  143.             $this->DbData       = NULL;
  144.             $this->DbPrefix     = NULL;
  145.         }
  146.        
  147.        
  148.         /**
  149.         * Method to check if the MySql functions are defined
  150.         *
  151.         * @access   public
  152.         * @param    boolean $all True to check all MySql functions that are used
  153.         * @return   boolean True if the MySql functions are defined
  154.         */
  155.         public function test_db_functions( $all = false )
  156.         {
  157.             if( !function_exists("mysql_connect") )
  158.             {
  159.                 return false;
  160.             }
  161.            
  162.             if( !function_exists("mysql_select_db") )
  163.             {
  164.                 return false;
  165.             }
  166.            
  167.             if( !function_exists("mysql_query") )
  168.             {
  169.                 return false;
  170.             }
  171.            
  172.             if( !function_exists("mysql_close") )
  173.             {
  174.                 return false;
  175.             }
  176.            
  177.             /* Validate non essential functions */
  178.             if( $all === true )
  179.             {
  180.                 if( !function_exists("mysql_free_result") )
  181.                 {
  182.                     return false;
  183.                 }
  184.                
  185.                 if( !function_exists("mysql_real_escape_string") )
  186.                 {
  187.                     return false;
  188.                 }
  189.                
  190.                 if( !function_exists("mysql_set_charset") )
  191.                 {
  192.                     return false;
  193.                 }
  194.             }
  195.            
  196.             return true;
  197.         }
  198.        
  199.        
  200.         /**
  201.         * Set database information
  202.         *
  203.         * @access   public
  204.         * @param    array $params Database Information (Host, User, Pass, Data, Prefix, [TimeOut])
  205.         * @return   boolean True if successful
  206.         */
  207.         public function set_dbparams( $params )
  208.         {
  209.             /* Validate params */
  210.             if( empty($params) ) { $this->set_dberror(1); return false; } /* Invalid params */
  211.            
  212.             /* Set the values */
  213.             $this->DbHost       = isset($params["host"])    ? $params["host"]       : "";
  214.             $this->DbUser       = isset($params["user"])    ? $params["user"]       : "";
  215.             $this->DbPass       = isset($params["pass"])    ? $params["pass"]       : "";
  216.             $this->DbData       = isset($params["data"])    ? $params["data"]       : "";
  217.             $this->DbPrefix     = isset($params["prefix"])  ? $params["prefix"]     : "";
  218.             $this->DbTimeout    = isset($params["timeout"]) ? $params["timeout"]    : $this->DbTimeout;
  219.            
  220.             return true;
  221.         }
  222.        
  223.        
  224.         /**
  225.         * Set database error
  226.         *
  227.         * @access   private
  228.         * @param    int Error number
  229.         */
  230.         private function set_dberror( $no )
  231.         {
  232.             $this->DbError = $no;
  233.         }
  234.        
  235.        
  236.         /**
  237.         * Get database error
  238.         *
  239.         * @access   public
  240.         * @return   int Error Number
  241.         */
  242.         public  function get_dberror()
  243.         {
  244.             return $this->DbError;
  245.         }
  246.        
  247.        
  248.         /**
  249.         * Set database timeout
  250.         *
  251.         * The configuration option will keep this new value during the script's execution
  252.         *
  253.         * @access   private
  254.         * @param    int $to Database timeout
  255.         */
  256.         private function set_dbtimeout( $to )
  257.         {
  258.             $this->DbTimeout = $to;
  259.         }
  260.        
  261.        
  262.         /**
  263.         * Get database timeout value
  264.         *
  265.         * @access   public
  266.         * @return   int Database timeout value
  267.         */
  268.         public function get_dbtimeout()
  269.         {
  270.             return $this->DbTimeout;
  271.         }
  272.        
  273.        
  274.         /**
  275.         * Set database hostname
  276.         *
  277.         * @access   private
  278.         * @param    string $val Database hostname
  279.         */
  280.         private function set_dbhost( $val )
  281.         {
  282.             $this->DbHost = empty($val) ? $val : "";
  283.         }
  284.        
  285.        
  286.         /**
  287.         * Get database hostname
  288.         *
  289.         * @access   public
  290.         * @return   string Database hostname
  291.         */
  292.         public  function get_dbhost()
  293.         {
  294.             return $this->DbHost;
  295.         }
  296.        
  297.        
  298.         /**
  299.         * Set database username
  300.         *
  301.         * @access   private
  302.         * @param    string $val Database root password
  303.         */
  304.         private function set_dbuser( $val )
  305.         {
  306.             $this->DbUser = empty($val) ? $val : "";
  307.         }
  308.        
  309.        
  310.         /**
  311.         * Get database username
  312.         *
  313.         * @access   public
  314.         * @return   string Database root username
  315.         */
  316.         public  function get_dbuser()
  317.         {
  318.             return $this->DbUser;
  319.         }
  320.        
  321.        
  322.         /**
  323.         * Set database timeout value
  324.         *
  325.         * @access   private
  326.         * @param    string $val Database root password
  327.         */
  328.         private function set_dbpass( $val )
  329.         {
  330.             $this->DbPass = empty($val) ? $val : "";
  331.         }
  332.         //public function get_dbpass() {} This function is not valid for safety reasons
  333.        
  334.        
  335.         /**
  336.         * Set the name of the used
  337.         *
  338.         * @access   private
  339.         * @param    string $val The name of the database
  340.         */
  341.         private function set_dbdata( $val )
  342.         {
  343.             $this->DbData = empty($val) ? $val : "";
  344.         }
  345.        
  346.        
  347.         /**
  348.         * Get the name of the database
  349.         *
  350.         * @access   public
  351.         * @return   string The name of the used database
  352.         */
  353.         public  function get_dbdata()
  354.         {
  355.             return $this->DbData;
  356.         }
  357.        
  358.        
  359.         /**
  360.         * Set table prefix
  361.         *
  362.         * @access   private
  363.         * @param    string $val The table prefix
  364.         */
  365.         private function set_dbprefix( $val )
  366.         {
  367.             $this->DbPrefix = empty($val) ? $$val : "";
  368.         }
  369.        
  370.        
  371.         /**
  372.         * Get table prefix
  373.         *
  374.         * @access   public
  375.         * @return   string The table prefix
  376.         */
  377.         public  function get_dbprefix()
  378.         {
  379.             return $this->DbPrefix;
  380.         }
  381.        
  382.        
  383.         /**
  384.         * Method to connect to a MySql database
  385.         *
  386.         * @access   public
  387.         * @return   boolean True if successful
  388.         */
  389.         public function connect()
  390.         {
  391.             /* If already connected, return */
  392.             if( $this->is_connected($this->connection) )
  393.             {
  394.                 return true;
  395.             }
  396.            
  397.             /* Validate param if is int */
  398.             if( !is_int($this->DbTimeout) )
  399.             {
  400.                 $this->DbTimeout = 10;
  401.             }
  402.            
  403.             /* Set the timeout */
  404.             @ini_set("mysql.connect_timeout", $this->DbTimeout);
  405.            
  406.             /* Connect with the MySql database */
  407.             $this->connection = @mysql_connect($this->DbHost, $this->DbUser, $this->DbPass);
  408.             return $this->is_connected($this->connection) ? true : false;
  409.         }
  410.        
  411.        
  412.         /**
  413.         * Method to determine if the object is connected to an MySql server
  414.         *
  415.         * @access   public
  416.         * @param    string $link The database connection
  417.         * @param    boolean $flag If flag is true will try to connect to the database [Default: false]
  418.         * @return   boolean True if connected
  419.         */
  420.         public function is_connected( $link, $flag = false )
  421.         {
  422.             /* Only determine if the object is connected */
  423.             if( $flag === false )
  424.             {
  425.                 return @is_resource($this->connection) ? true : false;
  426.             }
  427.             else
  428.             {
  429.                 /* If object is connected return */
  430.                 if( @is_resource($this->connection) )
  431.                 {
  432.                     return true;
  433.                 }
  434.                 else
  435.                 {
  436.                     /* Try to connect and if not sucessfully return */
  437.                     if( $this->connect() === false )
  438.                     {
  439.                         return false;
  440.                     }
  441.                 }
  442.             }
  443.            
  444.             return true;
  445.         }
  446.        
  447.        
  448.         /**
  449.         * Method for selecting the database to be used
  450.         *
  451.         * @access   public
  452.         * @return   boolean True if successful
  453.         */
  454.         public function select_db()
  455.         {
  456.             /* Determine connection, if not connected try to connect */
  457.             if( !$this->is_connected($this->connection, true) )
  458.             {
  459.                 return false;
  460.             }
  461.            
  462.             return @mysql_select_db($this->DbData) ? true : false;
  463.         }
  464.        
  465.        
  466.         /**
  467.         * Method to execute SQL command
  468.         *
  469.         * @access   public
  470.         * @param    string $sql The SQL command
  471.         * @return   boolean True if successful
  472.         */
  473.         public function query( $sql )
  474.         {
  475.             /* Validate SQL value */
  476.             if( empty($sql) )
  477.             {
  478.                 return false;
  479.             }
  480.            
  481.             /* Determine connection, if not connected try to connect */
  482.             if( !$this->is_connected($this->connection, true) )
  483.             {
  484.                 return false;
  485.             }
  486.            
  487.             return @mysql_query($sql, $this->connection) ? true : false;
  488.         }
  489.        
  490.        
  491.         /**
  492.         * Method to check whether or not the connection to the server is working.
  493.         *
  494.         * @access   public
  495.         * @return   boolean True if successful
  496.         */
  497.         public function ping_db()
  498.         {
  499.             return @mysql_ping($this->connection) ? true : false;
  500.         }
  501.        
  502.        
  503.         /**
  504.         * Method to free all memory associated with the result identifier
  505.         *
  506.         * @access   public
  507.         * @param    resource $result The result resource that is being evaluated
  508.         * @return   boolean True if successful
  509.         */
  510.         public function free_memory( $result )
  511.         {
  512.             /* Allow only resource */
  513.             if( !is_resource($result) )
  514.             {
  515.                 return false;
  516.             }
  517.            
  518.             return @mysql_free_result($this->connection) ? true : false;
  519.         }
  520.        
  521.        
  522.         /**
  523.         * Method to close the database connection
  524.         *
  525.         * @access   public
  526.         * @return   boolean True if successful
  527.         */
  528.         public function close()
  529.         {
  530.             /* If already connected, try to close database connection */
  531.             if( $this->is_connected($this->connection) )
  532.             {
  533.                 return @mysql_close($this->connection) ? true : false;
  534.             }
  535.            
  536.             return true;
  537.         }
  538.        
  539.        
  540.         /**
  541.         * Method to escapes special characters in a string for use in an SQL statement
  542.         *
  543.         * @access   public
  544.         * @param    string $str The string that is to be escaped
  545.         * @return   string True if successful
  546.         */
  547.         public function escape( $str )
  548.         {
  549.             if( empty($str) )
  550.             {
  551.                 $str = "";
  552.                 return true;
  553.             }
  554.            
  555.             return @mysql_real_escape_string($str);
  556.         }
  557.        
  558.        
  559.         /**
  560.         * Method to sets the client character set
  561.         *
  562.         * @access   public
  563.         * @param    string $charset A valid character set name [Default UTF8]
  564.         * @return   boolean True if successful
  565.         */
  566.         public function set_charset( $charset = "UTF8" )
  567.         {
  568.             if( empty($charset) )
  569.             {
  570.                 $charset = "UTF8";
  571.             }
  572.            
  573.             return @mysql_set_charset($charset) ? true : false;
  574.         }
  575.        
  576.         /* mysql_fetch_field */
  577.         /* mysql_result */
  578.     }
  579.  
  580. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement