Current path: home/webdevt/www/schtroumpf.fr/modules/ets_superspeed/classes/
?? Go up: /home/webdevt/www/schtroumpf.fr/modules/ets_superspeed
<?php
/**
* Copyright ETS Software Technology Co., Ltd
*
* NOTICE OF LICENSE
*
* This file is not open source! Each license that you purchased is only available for 1 website only.
* If you want to use this file on more websites (or projects), you need to purchase additional licenses.
* You are not allowed to redistribute, resell, lease, license, sub-license or offer our resources to any third party.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future.
*
* @author ETS Software Technology Co., Ltd
* @copyright ETS Software Technology Co., Ltd
* @license Valid for 1 website (or project) for each purchase of license
*/
if (!defined('_PS_VERSION_')) { exit; }
class Ets_superspeed_cache_page extends ObjectModel
{
public $page;
public $id_object;
public $id_product_attribute;
public $ip;
public $file_cache;
public $request_uri;
public $id_shop;
public $id_lang;
public $id_currency;
public $id_country;
public $file_size;
public $user_agent;
public $has_customer;
public $has_cart;
public $date_add;
public $date_expired;
public $_dir;
public $click;
public static $definition = array(
'table' => 'ets_superspeed_cache_page',
'primary' => 'id_cache_page',
'multilang' => false,
'fields' => array(
'page' => array('type' => self::TYPE_STRING),
'id_object' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'id_product_attribute' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'ip' => array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'),
'file_cache' => array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'),
'request_uri' => array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'),
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'id_country' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'has_customer' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'has_cart' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'click' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'file_size' => array('type' => self::TYPE_FLOAT),
'user_agent' => array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'),
'date_add' => array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'),
'date_expired' =>array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'),
)
);
public static function getFolderStatic($file_name,$length = 6)
{
$file_name = Tools::substr($file_name,0,$length);
$folders = str_split((string) $file_name,1);
return implode('/', $folders) . '/';
}
public function __construct($id_item = null, $id_lang = null, $id_shop = null)
{
parent::__construct($id_item, $id_lang, $id_shop);
$this->_dir = _ETS_SPEED_CACHE_DIR_.(int)$this->id_shop.'/'.self::getFolderStatic($this->file_cache);
}
public function add($auto_date = true,$null_value=false)
{
if(parent::add($auto_date,$null_value))
{
$this->_dir = _ETS_SPEED_CACHE_DIR_.(int)$this->id_shop.'/'.self::getFolderStatic($this->file_cache);
return true;
}
}
public function setFileCache($value)
{
if(!$this->id)
return false;
if(!is_dir($this->_dir))
@mkdir($this->_dir,0777,true);
$fileName = $this->file_cache.($this->date_expired ? strtotime($this->date_expired):'');
if(Configuration::get('ETS_SPEED_COMPRESS_CACHE_FIIE') && class_exists('ZipArchive'))
{
$cache_file = $this->_dir.$fileName.'.zip';
$zip = new ZipArchive();
if(!file_exists($cache_file))
{
if($zip->open($cache_file, ZipArchive::CREATE | ZipArchive::CHECKCONS)===true)
{
$zip->addFromString($fileName, $value);
}
}
else
{
if($zip->open($cache_file))
{
$zip->addFromString($fileName, $value);
}
}
$zip->close();
}
else
{
$cache_file = $this->_dir.$fileName.'.html';
file_put_contents($cache_file, $value);
}
return Tools::ps_round(@filesize($cache_file)/1024,2);
}
public static function getCacheContent($file_name)
{
$dir = _ETS_SPEED_CACHE_DIR_.(int)Context::getContext()->shop->id.'/'.self::getFolderStatic($file_name);
if(Configuration::get('ETS_SPEED_COMPRESS_CACHE_FIIE') && class_exists('ZipArchive'))
{
if(is_dir($dir))
{
foreach (glob($dir .$file_name.'*.zip' ) as $filename)
{
if(($time = str_replace('.html','',Tools::substr(basename($filename),32))))
{
if($time >= time() && ($zip = new ZipArchive()) && $zip->open($filename))
{
return array(
'date_add' => Ets_ss_class_cache::displayDate(Date('Y-m-d H:i:s',filemtime($filename)),true),
'cache_content' => $zip->getFromName(str_replace('.zip','',basename($filename))),
);
}
else
{
@unlink($filename);
return false;
}
}
elseif(($zip = new ZipArchive()) && $zip->open($filename))
{
return array(
'date_add' => Ets_ss_class_cache::displayDate(Date('Y-m-d H:i:s',filemtime($filename)),true),
'cache_content' =>$zip->getFromName(str_replace('.zip','',basename($filename))),
);
}
}
}
}
else
{
if(is_dir($dir))
{
foreach (glob($dir .$file_name.'*.html' ) as $filename)
{
if(($time = str_replace('.html','',Tools::substr(basename($filename),32))))
{
if($time >= time())
{
return array(
'date_add' => Ets_ss_class_cache::displayDate(Date('Y-m-d H:i:s',filemtime($filename)),true),
'cache_content' =>Tools::file_get_contents($filename),
);
}
else
{
@unlink($filename);
return false;
}
}
return array(
'date_add' => Ets_ss_class_cache::displayDate(Date('Y-m-d H:i:s',filemtime($filename)),true),
'cache_content' =>Tools::file_get_contents($filename),
);
}
}
}
return false;
}
public function delete()
{
if(parent::delete()) {
Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'ets_superspeed_cache_page_hook` WHERE id_cache_page = ' . (int)$this->id);
if (is_dir($this->_dir))
{
foreach(glob($this->_dir .$this->file_cache.'*.*' ) as $filename)
{
@unlink($filename);
}
}
self::deleteFolder($this->file_cache,$this->id_shop);
return true;
}
return false;
}
public static function deleteById($id,$id_shop)
{
$pageCache = new Ets_superspeed_cache_page($id,$id_shop);
return $pageCache->delete();
}
public static function deleteByWhere($where='')
{
return Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'ets_superspeed_cache_page` WHERE 1 '.($where ? (string)$where :''));
}
public static function deleteFolder($file_cache,$id_shop,$length = 6)
{
$dir = _ETS_SPEED_CACHE_DIR_.(int)$id_shop.'/'.self::getFolderStatic($file_cache,$length);
if (is_dir($dir)) {
$deleteFolder = true;
foreach (scandir($dir, SCANDIR_SORT_NONE) as $file) {
if (($file != '.' && $file != '..')) {
$deleteFolder = false;
break;
}
}
}
if (isset($deleteFolder) && $deleteFolder && @rmdir($dir) && $length >1) {
$length = $length-1;
self::deleteFolder($file_cache,$id_shop,$length);
}
return true;
}
public static function getTotalPageCaches($filter='')
{
$sql ='SELECT COUNT(cache.id_cache_page) FROM `' . _DB_PREFIX_ . 'ets_superspeed_cache_page` cache';
if($filter)
{
$sql .=' LEFT JOIN `'._DB_PREFIX_.'currency` currency ON (cache.id_currency = currency.id_currency)
LEFT JOIN `'._DB_PREFIX_.'country` country ON (country.id_country=cache.id_country)
LEFT JOIN `'._DB_PREFIX_.'country_lang` country_lang ON (country_lang.id_country = country.id_country AND country_lang.id_lang="'.(int)Context::getContext()->language->id.'")
LEFT JOIN `'._DB_PREFIX_.'lang` lang ON (lang.id_lang=cache.id_lang)';
}
$sql .=' WHERE cache.id_shop=' . (int)Context::getContext()->shop->id.($filter ? (string)$filter:'');
return (int)Db::getInstance()->getValue($sql);
}
public static function getListPageCaches($start=0,$limit=20,$sql_sort='',$filter='')
{
$sql ='SELECT cache.*,currency.iso_code,country_lang.name as country_name,lang.name as lang_name FROM `' . _DB_PREFIX_ . 'ets_superspeed_cache_page` cache
LEFT JOIN `'._DB_PREFIX_.'currency` currency ON (cache.id_currency = currency.id_currency)
LEFT JOIN `'._DB_PREFIX_.'country` country ON (country.id_country=cache.id_country)
LEFT JOIN `'._DB_PREFIX_.'country_lang` country_lang ON (country_lang.id_country = country.id_country AND country_lang.id_lang="'.(int)Context::getContext()->language->id.'")
LEFT JOIN `'._DB_PREFIX_.'lang` lang ON (lang.id_lang=cache.id_lang)
WHERE cache.id_shop="' . (int)Context::getContext()->shop->id . '"'.($filter ? (string)$filter:'') .($sql_sort ? ' ORDER BY '.bqSQL($sql_sort) : '').' LIMIT ' . (int)$start . ',' . (int)$limit;
return Db::getInstance()->executeS($sql);
}
public static function getListFileCache($limit = 10,$filter='',$sort= '')
{
return Db::getInstance()->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'ets_superspeed_cache_page` WHERE 1 '.($filter ? (string)$filter:' AND id_shop="' . (int)Context::getContext()->shop->id . '"').' ORDER BY '.($sort ? bqSQL($sort) : 'date_add desc').($limit ? ' LIMIT 0,'.(int)$limit:''));
}
public static function getRowCache()
{
return Db::getInstance()->getRow('SELECT SUM(file_size) as total_cache,COUNT(file_cache) as total_file FROM `'._DB_PREFIX_.'ets_superspeed_cache_page` WHERE id_shop='.(int)Context::getContext()->shop->id);
}
public static function getTotalHomeSlider()
{
return Db::getInstance()->getValue('SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'homeslider_slides` hs
INNER JOIN `' . _DB_PREFIX_ . 'homeslider` h ON (hs.id_homeslider_slides = h.id_homeslider_slides)
WHERE hs.active=1 AND h.id_shop=' . (int)Context::getContext()->shop->id);
}
public static function getTotalPoints()
{
return (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'ets_superspeed_hook_time` pht
INNER JOIN `' . _DB_PREFIX_ . 'hook` h ON (pht.hook_name = h.name)
INNER JOIN `' . _DB_PREFIX_ . 'hook_module` hm ON (hm.id_hook=h.id_hook AND hm.id_module=pht.id_module)
WHERE hm.id_shop="' . (int)Context::getContext()->shop->id . '" AND pht.time >1');
}
public static function getDynamicHookModule($id_module, $hook_name)
{
$context = Context::getContext();
if ($id_module && ($id_module == Module::getModuleIdByName('blockcart') || $id_module == Module::getModuleIdByName('ps_shoppingcart') || $id_module == Module::getModuleIdByName('tdshoppingcart')) && $hook_name != 'header' && $hook_name != 'displayHeader' && isset($context->cookie->id_cart) && $context->cookie->id_cart) {
return array(
'empty_content' => 0,
);
}
if ($id_module && ($id_module == Module::getModuleIdByName('ps_customersignin') || $id_module == Module::getModuleIdByName('blockuserinfo')) && $hook_name != 'header' && $hook_name != 'displayHeader' && isset($context->customer->id) && $context->customer->id && $context->customer->logged) {
return array(
'empty_content' => 1,
);
}
if(($hooks = Ets_superspeed_defines::getInstance()->getFieldConfig('_dynamic_hooks')) && ($hooks = array_map('strtolower',$hooks)) && in_array(Tools::strtolower($hook_name),$hooks))
{
$sql = 'SELECT d.* FROM `' . _DB_PREFIX_ . 'ets_superspeed_dynamic` d
LEFT JOIN `' . _DB_PREFIX_ . 'hook` h ON (h.name=d.hook_name)
LEFT JOIN `' . _DB_PREFIX_ . 'hook_alias` ha ON (d.hook_name=ha.name OR d.hook_name = ha.alias)
WHERE (h.name="' . pSQL($hook_name) . '" OR ha.alias ="' . pSQL($hook_name) . '" OR ha.name="'.pSQL($hook_name).'") AND d.id_module="' . (int)$id_module . '"';
return Db::getInstance()->getRow($sql);
}
}
public static function updateDynamicHook($add,$action,$id_module,$hook_name,$empty_content)
{
if ($add || $action == 'update_dynamic_modules') {
if (!Db::getInstance()->getRow('SELECT * FROM `' . _DB_PREFIX_ . 'ets_superspeed_dynamic` WHERE id_module="' . (int)$id_module . '" AND hook_name="' . pSQL($hook_name) . '"')) {
Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'ets_superspeed_dynamic` (id_module,hook_name,empty_content) VALUES("' . (int)$id_module . '","' . pSQL($hook_name) . '","' . (int)$empty_content . '")');
} else {
Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'ets_superspeed_dynamic` SET empty_content="' . (int)$empty_content . '" WHERE id_module="' . (int)$id_module . '" AND hook_name="' . pSQL($hook_name) . '"');
}
}
else
{
if(Validate::isHookName($hook_name))
Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'ets_superspeed_dynamic` WHERE id_module="' . (int)$id_module . '" AND hook_name="' . pSQL($hook_name) . '"');
}
return true;
}
public static function submitTimeSpeed($request_time){
$id_shop = Context::getContext()->shop->id;
if($request_time)
{
$request_time = Tools::ps_round($request_time/1000,2);
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'ets_superspeed_time`(id_shop,`date`,`time`) VALUES("'.(int)$id_shop.'","'.pSQL(date('Y-m-d H:i:s')).'","'.(float)$request_time.'")');
$count= Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'ets_superspeed_time` WHERE id_shop="'.(int)$id_shop.'"');
if($count > 150)
{
$mintime= Db::getInstance()->getValue('SELECT MIN(`date`) FROM `'._DB_PREFIX_.'ets_superspeed_time` WHERE id_shop="'.(int)$id_shop.'"');
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'ets_superspeed_time` WHERE id_shop="'.(int)$id_shop.'" AND `date` ="'.pSQL($mintime).'"');
}
}
}
public static function getTimeSpeed()
{
return Db::getInstance()->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'ets_superspeed_time` WHERE id_shop="' . (int)Context::getContext()->shop->id . '" AND `date`<="' . pSQL(date('Y-m-d H:i:s')) . '" ORDER BY `date` DESC LIMIT 0,150');
}
public static function deleteAllCache()
{
Db::getInstance()->execute('TRUNCATE TABLE `'._DB_PREFIX_.'ets_superspeed_cache_page`');
Db::getInstance()->execute('TRUNCATE TABLE `'._DB_PREFIX_.'ets_superspeed_cache_page_hook`');
Ets_ss_class_cache::getInstance()->rmDir(_ETS_SPEED_CACHE_DIR_);
return true;
}
}