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