<?php
final class MySQL {
private $link;
public function __construct($hostname, $username, $password, $database) {
if (!$this->link = ($GLOBALS["___mysqli_ston"] = mysqli_connect($hostname, $username, $password))) {
trigger_error('Error: Could not make a database link using ' . $username . '@' . $hostname);
}
if (!((bool)mysqli_query( $this->link, "USE $database"))) {
trigger_error('Error: Could not connect to database ' . $database);
}
mysqli_query( $this->link, "SET NAMES 'utf8'");
mysqli_query( $this->link, "SET CHARACTER SET utf8");
mysqli_query( $this->link, "SET CHARACTER_SET_CONNECTION=utf8");
mysqli_query( $this->link, "SET SQL_MODE = ''");
}
public function query($sql) {
if ($this->link) {
if(!defined('IS_ADMIN')){
$limit = 7;
$expire_default = 3600;
$expire_main = 3600 * 24 * 30;
$expire_product = 3600 * 7;
$tag_cache = array(
'main' => array(
'expire' => false,
),
'Currency::__construct' => array(
'expire' => $expire_main,
'tag' => 'main',
),
'Weight::__construct' => array(
'expire' => $expire_main,
'tag' => 'main',
),
'Length::__construct'=> array(
'expire' => $expire_main,
'tag' => 'main',
),
'ModelDesignLayout::getLayout' => array(
'expire' => $expire_main,
'tag' => 'main',
),
'ModelSettingExtension::getExtensions' => array(
'expire' => $expire_main,
'tag' => 'main',
),
'ModelDesignBanner::getBanner' => array(
'expire' => $expire_main,
'tag' => 'main',
),
'ModelCatalogProduct::getTotalProducts' => array(
'expire' => $expire_product,
'tag' => 'product',
),
'ControllerCommonSeoUrl::rewrite' => array(
'expire' => $expire_product,
'tag' => 'product',
),
'ControllerCommonSeoUrl::rewrite' => array(
'expire' => $expire_product,
'tag' => 'product',
),
'ModelCatalogCategory::getCategories'=> array(
'expire' => $expire_product,
'tag' => 'product',
),
);
if(!Staticstore::get('expire_cache')){
mysqli_query($GLOBALS["___mysqli_ston"], "DELETE FROM " . DB_PREFIX . "cache WHERE expire < '" . time(true) . "' LIMIT $limit");
Staticstore::set('expire_cache', true);
}
$caller = debug_backtrace();
$hash = md5($sql);
$method = (!empty($caller[2]['class']) ? $caller[2]['class'].'::'.$caller[2]['function'] : 'main');
$expire = (isset($tag_cache[$method])) ? (($tag_cache[$method]['expire']) ? time(true) + $tag_cache[$method]['expire'] : false) : time(true) + $expire_default;
$tag = (isset($tag_cache[$method]['tag'])) ? $tag_cache[$method]['tag'] : $method;
if($expire){
if(isset($tag_cache[$method])){
if($return = Staticstore::get($tag)){
if (isset($return[$hash])){
return unserialize($return[$hash]);
}
}else{
$resource = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT hash, data FROM " . DB_PREFIX . "cache WHERE tag = '" . $tag . "'");
if($resource){
$return = array();
while ($result = mysqli_fetch_assoc($resource)) {
$return[$result['hash']] = $result['data'];
}
Staticstore::set($tag, $return);
if(($return = Staticstore::get($tag)) && isset($return[$hash]))
return unserialize($return[$hash]);
}
}
}
$resource = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT data FROM " . DB_PREFIX . "cache WHERE hash = '" . $hash . "'");
if (is_resource($resource)) {
$result = mysqli_fetch_assoc($resource);
if($result){
return unserialize($result['data']);
}
}
}
}
$resource = mysqli_query( $this->link, $sql);
if ($resource) {
if (is_resource($resource)) {
$i = 0;
$data = array();
while ($result = mysqli_fetch_assoc($resource)) {
$data[$i] = $result;
$i++;
}
((mysqli_free_result($resource) || (is_object($resource) && (get_class($resource) == "mysqli_result"))) ? true : false);
$query = new stdClass();
$query->row = isset($data[0]) ? $data[0] : array();
$query->rows = $data;
$query->num_rows = $i;
unset($data);
if(!defined('IS_ADMIN'))
$r = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO " . DB_PREFIX . "cache SET hash = '{$hash}', data = '" . $this->escape(serialize($query)) . "', tag = '{$tag}', expire = '{$expire}' ");
return $query;
} else {
return true;
}
} else {
trigger_error('Error: ' . ((is_object($this->link)) ? mysqli_error($this->link) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '<br />Error No: ' . ((is_object($this->link)) ? mysqli_errno($this->link) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)) . '<br />' . $sql);
exit();
}
}
}
public function escape($value) {
if ($this->link) {
return mysqli_real_escape_string( $this->link, $value);
}
}
public function countAffected() {
if ($this->link) {
return mysqli_affected_rows($this->link);
}
}
public function getLastId() {
if ($this->link) {
return ((is_null($___mysqli_res = mysqli_insert_id($this->link))) ? false : $___mysqli_res);
}
}
public function __destruct() {
if ($this->link) {
((is_null($___mysqli_res = mysqli_close($this->link))) ? false : $___mysqli_res);
}
}
}
?>
Что не так?