| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty Internal Plugin | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * @package    Smarty | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @subpackage Cacher | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Cache Handler API | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * @package    Smarty | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @subpackage Cacher | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |  * @author     Rodney Rehm | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * fetch cached content and its modification time from data source | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  string  $id         unique cache content identifier | 
					
						
							|  |  |  |      * @param  string  $name       template name | 
					
						
							|  |  |  |      * @param  string  $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string  $compile_id compile id | 
					
						
							|  |  |  |      * @param  string  $content    cached content | 
					
						
							|  |  |  |      * @param  integer $mtime      cache modification timestamp (epoch) | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Fetch cached content's modification timestamp from data source | 
					
						
							|  |  |  |      * {@internal implementing this method is optional. | 
					
						
							|  |  |  |      *  Only implement it if modification times can be accessed faster than loading the complete cached content.}} | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * @param  string $id         unique cache content identifier | 
					
						
							|  |  |  |      * @param  string $name       template name | 
					
						
							|  |  |  |      * @param  string $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string $compile_id compile id | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return integer|boolean timestamp (epoch) the template was modified, or false if not found | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function fetchTimestamp($id, $name, $cache_id, $compile_id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Save content to cache | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  string       $id         unique cache content identifier | 
					
						
							|  |  |  |      * @param  string       $name       template name | 
					
						
							|  |  |  |      * @param  string       $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string       $compile_id compile id | 
					
						
							|  |  |  |      * @param  integer|null $exp_time   seconds till expiration or null | 
					
						
							|  |  |  |      * @param  string       $content    content to cache | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @return boolean      success | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Delete content from cache | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  string       $name       template name | 
					
						
							|  |  |  |      * @param  string       $cache_id   cache id | 
					
						
							|  |  |  |      * @param  string       $compile_id compile id | 
					
						
							|  |  |  |      * @param  integer|null $exp_time   seconds till expiration time in seconds or null | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @return integer      number of deleted caches | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     abstract protected function delete($name, $cache_id, $compile_id, $exp_time); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * populate Cached Object with meta data from Resource | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  Smarty_Template_Cached   $cached    cached object | 
					
						
							|  |  |  |      * @param  Smarty_Internal_Template $_template template object | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null; | 
					
						
							|  |  |  |         $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id); | 
					
						
							|  |  |  |         $this->populateTimestamp($cached); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * populate Cached Object with timestamp and exists from Resource | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * @param Smarty_Template_Cached $cached | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function populateTimestamp(Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id); | 
					
						
							|  |  |  |         if ($mtime !== null) { | 
					
						
							|  |  |  |             $cached->timestamp = $mtime; | 
					
						
							|  |  |  |             $cached->exists = !!$cached->timestamp; | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $timestamp = null; | 
					
						
							|  |  |  |         $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp); | 
					
						
							|  |  |  |         $cached->timestamp = isset($timestamp) ? $timestamp : false; | 
					
						
							|  |  |  |         $cached->exists = !!$cached->timestamp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Read the cached template and process the header | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * @param  Smarty_Template_Cached   $cached    cached object | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return boolean                 true or false if the cached content does not exist | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null) | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (!$cached) { | 
					
						
							|  |  |  |             $cached = $_template->cached; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $content = $cached->content ? $cached->content : null; | 
					
						
							|  |  |  |         $timestamp = $cached->timestamp ? $cached->timestamp : null; | 
					
						
							|  |  |  |         if ($content === null || !$timestamp) { | 
					
						
							|  |  |  |             $this->fetch( | 
					
						
							|  |  |  |                 $_template->cached->filepath, | 
					
						
							|  |  |  |                 $_template->source->name, | 
					
						
							|  |  |  |                 $_template->cache_id, | 
					
						
							|  |  |  |                 $_template->compile_id, | 
					
						
							|  |  |  |                 $content, | 
					
						
							|  |  |  |                 $timestamp | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (isset($content)) { | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |             /** @var Smarty_Internal_Template $_smarty_tpl | 
					
						
							|  |  |  |              * used in evaluated code | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             $_smarty_tpl = $_template; | 
					
						
							|  |  |  |             eval("?>" . $content); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Write the rendered template output to cache | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * @param  string                   $content   content to cache | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @return boolean                  success | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function writeCachedContent(Smarty_Internal_Template $_template, $content) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->save( | 
					
						
							|  |  |  |             $_template->cached->filepath, | 
					
						
							|  |  |  |             $_template->source->name, | 
					
						
							|  |  |  |             $_template->cache_id, | 
					
						
							|  |  |  |             $_template->compile_id, | 
					
						
							|  |  |  |             $_template->properties['cache_lifetime'], | 
					
						
							|  |  |  |             $content | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Empty cache | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  Smarty  $smarty   Smarty object | 
					
						
							|  |  |  |      * @param  integer $exp_time expiration time (number of seconds, not timestamp) | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return integer number of cache files deleted | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     public function clearAll(Smarty $smarty, $exp_time = null) | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->cache = array(); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         return $this->delete(null, null, null, $exp_time); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Empty cache for a specific template | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @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) | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |      * @return integer number of cache files deleted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->cache = array(); | 
					
						
							| 
									
										
										
										
											2014-01-08 21:05:58 +00:00
										 |  |  |         $cache_name = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($resource_name)) { | 
					
						
							|  |  |  |             $_save_stat = $smarty->caching; | 
					
						
							|  |  |  |             $smarty->caching = true; | 
					
						
							|  |  |  |             $tpl = new $smarty->template_class($resource_name, $smarty); | 
					
						
							|  |  |  |             $smarty->caching = $_save_stat; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($tpl->source->exists) { | 
					
						
							|  |  |  |                 $cache_name = $tpl->source->name; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // remove from template cache
 | 
					
						
							|  |  |  |             if ($smarty->allow_ambiguous_resources) { | 
					
						
							|  |  |  |                 $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (isset($_templateId[150])) { | 
					
						
							|  |  |  |                 $_templateId = sha1($_templateId); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             unset($smarty->template_objects[$_templateId]); | 
					
						
							|  |  |  |             // template object no longer needed
 | 
					
						
							|  |  |  |             unset($tpl); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-08 21:05:58 +00:00
										 |  |  |         return $this->delete($cache_name, $cache_id, $compile_id, $exp_time); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-01-20 14:47:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check is cache is locked for this template | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param  Smarty                 $smarty Smarty object | 
					
						
							|  |  |  |      * @param  Smarty_Template_Cached $cached cached object | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return boolean               true or false if cache is locked | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $id = $cached->filepath; | 
					
						
							|  |  |  |         $name = $cached->source->name . '.lock'; | 
					
						
							| 
									
										
										
										
											2012-01-20 14:47:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |         $mtime = $this->fetchTimestamp($id, $name, null, null); | 
					
						
							|  |  |  |         if ($mtime === null) { | 
					
						
							| 
									
										
										
										
											2011-10-14 16:22:00 +00:00
										 |  |  |             $this->fetch($id, $name, null, null, $content, $mtime); | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-01-20 14:47:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |         return $mtime && time() - $mtime < $smarty->locking_timeout; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Lock cache for this template | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param Smarty                 $smarty Smarty object | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |      * @param Smarty_Template_Cached $cached cached object | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool|void | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cached->is_locked = true; | 
					
						
							| 
									
										
										
										
											2012-01-20 14:47:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |         $id = $cached->filepath; | 
					
						
							|  |  |  |         $name = $cached->source->name . '.lock'; | 
					
						
							|  |  |  |         $this->save($id, $name, null, null, $smarty->locking_timeout, ''); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Unlock cache for this template | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |      * @param Smarty                 $smarty Smarty object | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |      * @param Smarty_Template_Cached $cached cached object | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool|void | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cached->is_locked = false; | 
					
						
							| 
									
										
										
										
											2012-01-20 14:47:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 16:17:27 +00:00
										 |  |  |         $name = $cached->source->name . '.lock'; | 
					
						
							|  |  |  |         $this->delete($name, null, null, null); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | } |