Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.70 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Smarty Plugin CacheResource Mysql
  5. *
  6. * Implements MYSQL as resource for the HTML cache
  7. * Store as file cacheresource.mysql.php in plugins folder:
  8. *
  9. *CREATE TABLE `SMARTY_CACHE` (
  10. *  `Id` int(12) NOT NULL,
  11. *  `CacheContents` mediumtext NOT NULL,
  12. *  `Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  13. *  `CacheId` varchar(100) NOT NULL,
  14. *  `CompileId` varchar(100) NOT NULL,
  15. *  `ResourceName` varchar(100) NOT NULL,
  16. *  UNIQUE KEY `NewIndex_1` (`Id`)
  17. *) ENGINE=MyISAM DEFAULT CHARSET=latin1*
  18. * @package Smarty
  19. * @subpackage Plugins
  20. * @author Uwe Tews
  21. */
  22.  
  23. /**
  24. * This class does contain all necessary methods for the HTML cache on MYSQL system
  25. */
  26. class Smarty_CacheResource_Mysql {
  27.     // set db host, user and pass here
  28.     public $db_host = 'localhost';
  29.     public $db_user = 'root';
  30.     public $db_pass = '';
  31.     public $db_name = 'SMARTY';
  32.  
  33.     function __construct($smarty)
  34.     {
  35.         $this->smarty = $smarty;
  36.         if (!$this->link = mysql_pconnect($this->db_host, $this->db_user, $this->db_pass)) {
  37.         throw new Exception("Cache resource unable to connect to MYSQL");
  38.         }
  39.         mysql_select_db($this->db_name, $this->link);
  40.     }
  41.     /**
  42.     * Returns the filepath of the cached template output
  43.     *
  44.     * @param object $template current template
  45.     * @return string the cache filepath
  46.     */
  47.     public function getCachedFilepath($template)
  48.     {
  49.         return $this->buildCachedFilepath ($template->resource_name, $template->cache_id, $template->compile_id);
  50.     }
  51.  
  52.     /**
  53.     * Returns the timpestamp of the cached template output
  54.     *
  55.     * @param object $template current template
  56.     * @return integer |booelan the template timestamp or false if the file does not exist
  57.     */
  58.     public function getCachedTimestamp($template)
  59.     {
  60.         $Id = $template->getCachedFilepath();
  61.         // read cache from database
  62.         $results = mysql_query("select UNIX_TIMESTAMP(Timestamp) from SMARTY_CACHE where Id='$Id'", $this->link);
  63.         if (!$results) {
  64.             $this->mysqlError();
  65.         }
  66.         $row = mysql_fetch_row($results);
  67.         return (int)$row[0];
  68.     }
  69.  
  70.     /**
  71.     * Returns the cached template output
  72.     *
  73.     * @param object $template current template
  74.     * @return string |booelan the template content or false if the file does not exist
  75.     */
  76.     public function getCachedContents($template)
  77.     {
  78.         $Id = $template->getCachedFilepath();
  79.         // read cache from database
  80.         $results = mysql_query("select CacheContents from SMARTY_CACHE where Id='$Id'", $this->link);
  81.         if (!$results) {
  82.             $this->mysqlError();
  83.         }
  84.         $row = mysql_fetch_row($results);
  85.  
  86.             $cache_content = $row[0];
  87.  
  88.         $_smarty_tpl = $template;
  89.         ob_start();
  90.         eval("?>" . $cache_content);
  91.         return ob_get_clean();
  92.     }
  93.  
  94.     /**
  95.     * Writes the rendered template output to cache file
  96.     *
  97.     * @param object $template current template
  98.     * @return boolean status
  99.     */
  100.     public function writeCachedContent($template, $content)
  101.     {
  102.         if (!$template->resource_object->isEvaluated) {
  103.             $_cache_id = isset($template->cache_id) ? preg_replace('![^\w\|]+!', '_', $template->cache_id) : null;
  104.             $_compile_id = isset($template->compile_id) ? preg_replace('![^\w\|]+!', '_', $template->compile_id) : null;
  105.             // save cache to database
  106.             $Id = $template->getCachedFilepath();
  107.             $results = mysql_query("replace into SMARTY_CACHE set Id = $Id, CacheContents = '" . addslashes($content) . "',
  108.            CacheId = '$_cache_id',
  109.            CompileId = '$_compile_id',
  110.            ResourceName = '$template->resource_name'", $this->link);
  111.             if (!$results) {
  112.                 $this->mysqlError();
  113.             }
  114.             return $results;
  115.         } else {
  116.             return false;
  117.         }
  118.     }
  119.  
  120.     /**
  121.     * Empty cache folder
  122.     *
  123.     * @param integer $exp_time expiration time
  124.     * @return integer number of cache files deleted
  125.     */
  126.     public function clearAll($exp_time = null)
  127.     {
  128.         if ($exp_time === null) {
  129.             $results = mysql_query("truncate table SMARTY_CACHE", $this->link);
  130.         } else {
  131.             $results = mysql_query("delete ignore from SMARTY_CACHE where UNIX_TIMESTAMP(Timestamp) <= '$exp_time'", $this->link);
  132.         }
  133.         if (!$results) {
  134.             $this->mysqlError();
  135.         }
  136.     }
  137.     /**
  138.     * Empty cache for a specific template
  139.     *
  140.     * @param string $resource_name template name
  141.     * @param string $cache_id cache id
  142.     * @param string $compile_id compile id
  143.     * @param integer $exp_time expiration time
  144.     * @return integer number of cache files deleted
  145.     */
  146.     public function clear($resource_name, $cache_id, $compile_id, $exp_time)
  147.     {
  148.         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  149.         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  150.  
  151.         $where = '';
  152.         $and = '';
  153.         if (isset($resource_name)) {
  154.             $where = "ResourceName = '$resource_name' ";
  155.             $and = 'and ';
  156.         }
  157.         if (isset($_cache_id)) {
  158.             $length = strlen($_cache_id);
  159.             $where .= $and . "SUBSTRING(CacheId,1,$length) = '$_cache_id' ";
  160.             $and = 'and ';
  161.         }
  162.         if (isset($_compile_id)) {
  163.             $where .= $and . "CompileId = '$_compile_id' ";
  164.             $and = 'and ';
  165.         }
  166.         if (isset($exp_time)) {
  167.             $where .= $and . "UNIX_TIMESTAMP(Timestamp) <= '$exp_time' ";
  168.         }
  169.         $results = mysql_query("delete ignore from SMARTY_CACHE where $where", $this->link);
  170.         if (!$results) {
  171.             $this->mysqlError();
  172.         }
  173.         return mysql_affected_rows();
  174.     }
  175.  
  176.     /**
  177.     * Get system filepath to cached file
  178.     *
  179.     * @param string $resource_name template name
  180.     * @param string $cache_id cache id
  181.     * @param string $compile_id compile id
  182.     * @return string filepath of cache file
  183.     */
  184.     private function buildCachedFilepath ($resource_name, $cache_id, $compile_id)
  185.     {
  186.         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  187.         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  188.         return abs(crc32($resource_name . $_cache_id . $_compile_id));
  189.     }
  190.  
  191.     /**
  192.     * MYSQL Error
  193.     */
  194.     private function mysqlError ()
  195.     {
  196.         $error = mysql_error($this->link);
  197.         throw new Exception("Cache resource MYSQL error '{$error}'");
  198.     }
  199. }
  200. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement