Вход Регистрация
Файл: adultscript-2.0.3-pro/files/install/db.php
Строк: 104
<?php
defined
('_VALID') or die('Restricted Access!');
class 
VInstall_db
{
    private 
$_link;
    private 
$_query;
    private 
$_query_save;

    private 
$db_host;
    private 
$db_name;
    private 
$db_user;
    private 
$db_pass;
    private 
$db_prefix;
    
    private 
$error        false;

    private 
$_row;
    private 
$_result;
    private 
$_record;
            
    public function 
__construct($db_host$db_name$db_user$db_pass$db_prefix)
    {
        
$this->db_host        $db_host;
        
$this->db_name        $db_name;
        
$this->db_user        $db_user;
        
$this->db_pass        $db_pass;
        
$this->db_prefix    $db_prefix;
        
$this->connect();
        
$this->select();
        
$this->setUTF();
    }
    
    public function 
__destruct()
    {
        
$this->close();
    }
    
    public function 
connect()
    {
        
$this->_link    mysql_connect($this->db_host$this->db_user$this->db_pass);
        if (!
$this->_link) {
            die(
'Failed to connect to mysql! Are you sure the credentials you provided are correct!?');
        }
    }

    public function 
select()
    {
        if (!
$this->_link) {
            
$this->connect();
        }
        
        if (!
mysql_select_db($this->db_name)) {
              die(
'Failed to select mysql database! Are you sure the database name is correct!?');
        }    
    }

    private function 
setUTF()
    {
        
$this->query('SET NAMES utf8');
        
$this->query('SET CHARACTER SET utf8');
    }
    

    public function 
query($sql$replace=FALSE)
    {
        if (!
$this->_link) {
            
$this->connect();
            
$this->select();
            
$this->setUTF();
        }
        
        if (!
$this->_link) {
              die(
'Failed to connect to database server!');
        }
            
        if (
$this->_query) {
            
$this->_query NULL;
        }
        
        if (
$this->_result) {
              
$this->_result NULL;
        }
        
        if (
$replace === TRUE) {
              
$sql    str_replace('CREATE TABLE `''CREATE TABLE `'.$this->db_prefix$sql);
              
$sql    str_replace('DROP TABLE IF EXISTS `''DROP TABLE IF EXISTS `'.$this->db_prefix$sql);
              
$sql    str_replace('LOCK TABLES `''LOCK TABLES `'.$this->db_prefix$sql);
              
$sql    str_replace('INSERT INTO `''INSERT INTO `'.$this->db_prefix$sql);
            
$sql    str_replace('ALTER TABLE `''ALTER TABLE `'.$this->db_prefix$sql);
        }
        
        
$this->_query_save    str_replace('#__'$this->db_prefix$sql);
        
$this->_query         mysql_query(str_replace('#__'$this->db_prefix$sql), $this->_link);
        if (!
$this->_query) {
            die(
'Could not execute mysql query ('.$sql.')! ERROR:' .mysql_error().'!');
        }
    }

    public function 
affected_rows()
    {
        return (
$this->_link) ? mysql_affected_rows($this->_link) : NULL;
    }
    
    public function 
get_last_insert_id($table)
    {
        return 
mysql_insert_id($this->_link);
    }

    public function 
fetch_assoc()
    {
        
$this->_result = array();
        
$this->_row    mysql_fetch_assoc($this->_query);
        if ( !
$this->_row ) {
            
$this->error('Could not fetch mysql row!');
        }
        
        
$this->_result $this->_row;
        
$this->free();
    
        return 
$this->_result;
    }

    public function 
fetch_field($field)
    {
        if (!
$this->_result OR !isset($this->_result[$field])) {
            
$this->_result $this->fetch_assoc();
        }
    
        if (isset(
$this->_result[$field])) {
            return 
$this->_result[$field];
        }
    }

    public function 
fetch_rows()
    {
        
$this->_result = array();
        while (
$this->_row mysql_fetch_array($this->_query)) {
            
$this->_result[]    = $this->_row;
        }
        
$this->free();
    
        return 
$this->_result;
    }
    
    public function 
escape($sql)
    {
        return 
mysql_real_escape_string($sql$this->_link);
    }

    private function 
free()
    {
          return ( 
$this->_query ) ? mysql_free_result($this->_query) : NULL;
    }
    
    public function 
close()
    {
        return (
$this->_link && is_resource($this->_link)) ? mysql_close($this->_link) : $this->_link NULL;
    }
    
    public function 
is_error()
    {
        if (
$this->error !== false) {
            return 
true;
        }
        
        return 
false;
    }
    
    public function 
get_error()
    {
        return 
$this->error;
    }
    
    public function 
debug()
    {
        echo 
var_dump($this->_query_save).'<br />';
    }
    
    function 
load_file($file$delimiter=';')
    {
        
set_time_limit(0);
        if (
is_file($file)) {
            
$file fopen($file'r');
            if (
is_resource($file)) {
                
$query = array();
                while (!
feof($file)) {
                    
$query[] = fgets($file);
                    if (
preg_match('~' preg_quote($delimiter'~') . 's*$~iS'end($query)) === 1) {
                        
$query trim(implode(''$query));
                        
$replace = (strpos($query'nuevo__') !== false) ? false true;
                        
$this->query($query$replace);
                        if (
$this->is_error()) {
                            return 
false;
                        }
                        
                        while (
ob_get_level() > 0) {
                              
ob_end_flush();
                          }

                          
flush();
                    }
                    
                      if (
is_string($query)) {
                          
$query = array();
                      }                    
                }
                
                return 
fclose($file);
            } else {
                
$this->error 'Failed to open sql dump file! Aborting...';
            }
        } else {
            
$this->error 'Failed to find sql dump file or not a valid file! Aborting...';
        }
    }
}
Онлайн: 0
Реклама