| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * Smarty Internal Plugin | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage Cacher | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * Cache Handler API | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage Cacher | 
					
						
							|  |  |  |  * @author     Rodney Rehm | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | abstract class Smarty_CacheResource | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * resource types provided by the core | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-08-03 05:40:34 +02:00
										 |  |  |     protected static $sysplugins = array('file' => 'smarty_internal_cacheresource_file.php',); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * populate Cached Object with meta data from Resource | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached   $cached    cached object | 
					
						
							|  |  |  |      * @param Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     abstract public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * populate Cached Object with timestamp and exists from Resource | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     abstract public function populateTimestamp(Smarty_Template_Cached $cached); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * Read the cached template and process header | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached   $cached    cached object | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |      * @param boolean                  $update    flag if called because cache update | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |      * @return boolean true or false if the cached content does not exist | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |     abstract public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null, | 
					
						
							|  |  |  |                                      $update = false); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * Write the rendered template output to cache | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * @param string                   $content   content to cache | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return boolean success | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Read cached template from cache | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string  content | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     abstract function readCachedContent(Smarty_Internal_Template $_template); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * Return cached content | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return null|string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function getCachedContent(Smarty_Internal_Template $_template) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($_template->cached->handler->process($_template)) { | 
					
						
							|  |  |  |             ob_start(); | 
					
						
							| 
									
										
										
										
											2015-08-17 21:52:32 +02:00
										 |  |  |             $unifunc = $_template->cached->unifunc; | 
					
						
							|  |  |  |             $unifunc($_template); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             return ob_get_clean(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * Empty cache | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty  $smarty   Smarty object | 
					
						
							|  |  |  |      * @param integer $exp_time expiration time (number of seconds, not timestamp) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return integer number of cache files deleted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     abstract public function clearAll(Smarty $smarty, $exp_time = null); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * Empty cache for a specific template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty  $smarty        Smarty object | 
					
						
							|  |  |  |      * @param string  $resource_name template name | 
					
						
							|  |  |  |      * @param string  $cache_id      cache id | 
					
						
							|  |  |  |      * @param string  $compile_id    compile id | 
					
						
							|  |  |  |      * @param integer $exp_time      expiration time (number of seconds, not timestamp) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return integer number of cache files deleted | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Smarty                 $smarty | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool|null | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function locked(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // theoretically locking_timeout should be checked against time_limit (max_execution_time)
 | 
					
						
							|  |  |  |         $start = microtime(true); | 
					
						
							|  |  |  |         $hadLock = null; | 
					
						
							| 
									
										
										
										
											2015-05-08 07:27:45 +02:00
										 |  |  |         while ($this->hasLock($smarty, $cached)) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             $hadLock = true; | 
					
						
							|  |  |  |             if (microtime(true) - $start > $smarty->locking_timeout) { | 
					
						
							|  |  |  |                 // abort waiting for lock release
 | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             sleep(1); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         return $hadLock; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check is cache is locked for this template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty                 $smarty | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // check if lock exists
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Lock cache for this template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty                 $smarty | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // create lock
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Unlock cache for this template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty                 $smarty | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // release lock
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * Load Cache Resource Handler | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty $smarty Smarty object | 
					
						
							|  |  |  |      * @param string $type   name of the cache resource | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws SmartyException | 
					
						
							|  |  |  |      * @return Smarty_CacheResource Cache Resource Handler | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     public static function load(Smarty $smarty, $type = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!isset($type)) { | 
					
						
							|  |  |  |             $type = $smarty->caching_type; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-10-15 10:46:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // try smarty's cache
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         if (isset($smarty->_cache[ 'cacheresource_handlers' ][ $type ])) { | 
					
						
							|  |  |  |             return $smarty->_cache[ 'cacheresource_handlers' ][ $type ]; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         // try registered resource
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         if (isset($smarty->registered_cache_resources[ $type ])) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             // do not cache these instances as they may vary from instance to instance
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = $smarty->registered_cache_resources[ $type ]; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         // try sysplugins dir
 | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         if (isset(self::$sysplugins[ $type ])) { | 
					
						
							| 
									
										
										
										
											2015-01-01 23:53:59 +01:00
										 |  |  |             $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type); | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = new $cache_resource_class(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         // try plugins dir
 | 
					
						
							|  |  |  |         $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type); | 
					
						
							|  |  |  |         if ($smarty->loadPlugin($cache_resource_class)) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = new $cache_resource_class(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         // give up
 | 
					
						
							|  |  |  |         throw new SmartyException("Unable to load cache resource '{$type}'"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |