| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Smarty Internal Plugin CacheResource File | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage Cacher | 
					
						
							|  |  |  |  * @author     Uwe Tews | 
					
						
							|  |  |  |  * @author     Rodney Rehm | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * This class does contain all necessary methods for the HTML cache on file system | 
					
						
							|  |  |  |  * Implements the file system as resource for the HTML cache Version ussing nocache inserts. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage Cacher | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Smarty_Internal_CacheResource_File extends Smarty_CacheResource | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +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 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |         $source = &$_template->source; | 
					
						
							|  |  |  |         $smarty = &$_template->smarty; | 
					
						
							| 
									
										
										
										
											2017-10-26 04:37:17 +02:00
										 |  |  |         $_compile_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^'; | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |         $_filepath = sha1($source->uid . $smarty->_joined_template_dir); | 
					
						
							|  |  |  |         $cached->filepath = $smarty->getCacheDir(); | 
					
						
							|  |  |  |         if (isset($_template->cache_id)) { | 
					
						
							| 
									
										
										
										
											2017-05-19 22:35:55 +02:00
										 |  |  |             $cached->filepath .= preg_replace(array('![^\w|]+!', | 
					
						
							| 
									
										
										
										
											2017-10-26 04:37:17 +02:00
										 |  |  |                                                     '![|]+!'), | 
					
						
							|  |  |  |                                               array('_', | 
					
						
							|  |  |  |                                                     $_compile_dir_sep), | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |                                               $_template->cache_id) . $_compile_dir_sep; | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |         if (isset($_template->compile_id)) { | 
					
						
							|  |  |  |             $cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep; | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |         // if use_sub_dirs, break file into directories
 | 
					
						
							|  |  |  |         if ($smarty->use_sub_dirs) { | 
					
						
							| 
									
										
										
										
											2017-10-26 04:37:17 +02:00
										 |  |  |             $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DIRECTORY_SEPARATOR . $_filepath[ 2 ] . | 
					
						
							|  |  |  |                                  $_filepath[ 3 ] . | 
					
						
							|  |  |  |                                  DIRECTORY_SEPARATOR . | 
					
						
							|  |  |  |                                  $_filepath[ 4 ] . $_filepath[ 5 ] . DIRECTORY_SEPARATOR; | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |         $cached->filepath .= $_filepath; | 
					
						
							| 
									
										
										
										
											2016-05-16 18:17:58 +02:00
										 |  |  |         $basename = $source->handler->getBasename($source); | 
					
						
							|  |  |  |         if (!empty($basename)) { | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |             $cached->filepath .= '.' . $basename; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |         if ($smarty->cache_locking) { | 
					
						
							|  |  |  |             $cached->lock_id = $cached->filepath . '.lock'; | 
					
						
							| 
									
										
										
										
											2016-03-11 01:07:26 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |         $cached->filepath .= '.php'; | 
					
						
							| 
									
										
										
										
											2015-05-06 23:10:39 +02:00
										 |  |  |         $cached->timestamp = $cached->exists = is_file($cached->filepath); | 
					
						
							| 
									
										
										
										
											2015-05-05 00:41:30 +02:00
										 |  |  |         if ($cached->exists) { | 
					
						
							|  |  |  |             $cached->timestamp = filemtime($cached->filepath); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * populate Cached Object with timestamp and exists from Resource | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached cached object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function populateTimestamp(Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-05 00:41:30 +02:00
										 |  |  |         $cached->timestamp = $cached->exists = is_file($cached->filepath); | 
					
						
							|  |  |  |         if ($cached->exists) { | 
					
						
							|  |  |  |             $cached->timestamp = filemtime($cached->filepath); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Read the cached template and process its header | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |      * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached    $cached      cached object | 
					
						
							|  |  |  |      * @param bool                      $update      flag if called because cache update | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-07-20 04:01:43 +02:00
										 |  |  |      * @return boolean true or false if the cached content does not exist | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-10-26 04:37:17 +02:00
										 |  |  |     public function process(Smarty_Internal_Template $_smarty_tpl, | 
					
						
							|  |  |  |                             Smarty_Template_Cached $cached = null, | 
					
						
							| 
									
										
										
										
											2016-05-15 11:13:31 +02:00
										 |  |  |                             $update = false) | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-11 01:07:26 +01:00
										 |  |  |         $_smarty_tpl->cached->valid = false; | 
					
						
							| 
									
										
										
										
											2015-10-29 21:41:21 +01:00
										 |  |  |         if ($update && defined('HHVM_VERSION')) { | 
					
						
							| 
									
										
										
										
											2016-03-11 01:07:26 +01:00
										 |  |  |             eval("?>" . file_get_contents($_smarty_tpl->cached->filepath)); | 
					
						
							| 
									
										
										
										
											2015-12-25 10:02:10 +01:00
										 |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2015-07-26 18:04:40 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2016-03-11 01:07:26 +01:00
										 |  |  |             return @include $_smarty_tpl->cached->filepath; | 
					
						
							| 
									
										
										
										
											2015-07-26 18:04:40 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +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 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function writeCachedContent(Smarty_Internal_Template $_template, $content) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-10-26 04:37:17 +02:00
										 |  |  |         if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, | 
					
						
							|  |  |  |                                                            $content, | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                                                            $_template->smarty) === true | 
					
						
							|  |  |  |         ) { | 
					
						
							| 
									
										
										
										
											2017-05-19 22:35:55 +02:00
										 |  |  |             if (function_exists('opcache_invalidate') && | 
					
						
							|  |  |  |                 (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1 | 
					
						
							|  |  |  |             ) { | 
					
						
							| 
									
										
										
										
											2015-12-25 10:29:29 +01:00
										 |  |  |                 opcache_invalidate($_template->cached->filepath, true); | 
					
						
							| 
									
										
										
										
											2017-10-26 04:37:17 +02:00
										 |  |  |             } else if (function_exists('apc_compile_file')) { | 
					
						
							| 
									
										
										
										
											2015-12-25 10:29:29 +01:00
										 |  |  |                 apc_compile_file($_template->cached->filepath); | 
					
						
							| 
									
										
										
										
											2015-07-20 04:01:43 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-05-05 00:41:30 +02:00
										 |  |  |             $cached = $_template->cached; | 
					
						
							| 
									
										
										
										
											2015-05-06 23:10:39 +02:00
										 |  |  |             $cached->timestamp = $cached->exists = is_file($cached->filepath); | 
					
						
							| 
									
										
										
										
											2015-05-05 00:41:30 +02:00
										 |  |  |             if ($cached->exists) { | 
					
						
							|  |  |  |                 $cached->timestamp = filemtime($cached->filepath); | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |                 return true; | 
					
						
							| 
									
										
										
										
											2012-04-12 16:55:35 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 10:20:06 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Read cached template from cache | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  Smarty_Internal_Template $_template template object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string  content | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function readCachedContent(Smarty_Internal_Template $_template) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_file($_template->cached->filepath)) { | 
					
						
							|  |  |  |             return file_get_contents($_template->cached->filepath); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Empty cache | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty  $smarty | 
					
						
							|  |  |  |      * @param integer $exp_time expiration time (number of seconds, not timestamp) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return integer number of cache files deleted | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function clearAll(Smarty $smarty, $exp_time = null) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-19 16:39:19 +02:00
										 |  |  |         return $smarty->ext->_cacheResourceFile->clear($smarty, null, null, null, $exp_time); | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Empty cache for a specific template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty  $smarty | 
					
						
							|  |  |  |      * @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 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-19 16:39:19 +02:00
										 |  |  |         return $smarty->ext->_cacheResourceFile->clear($smarty, $resource_name, $cache_id, $compile_id, $exp_time); | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check is cache is locked for this template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty                 $smarty Smarty object | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached cached object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return boolean true or false if cache is locked | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (version_compare(PHP_VERSION, '5.3.0', '>=')) { | 
					
						
							|  |  |  |             clearstatcache(true, $cached->lock_id); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             clearstatcache(); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-05 00:41:30 +02:00
										 |  |  |         if (is_file($cached->lock_id)) { | 
					
						
							| 
									
										
										
										
											2016-03-11 01:07:26 +01:00
										 |  |  |             $t = filemtime($cached->lock_id); | 
					
						
							| 
									
										
										
										
											2015-05-05 00:41:30 +02:00
										 |  |  |             return $t && (time() - $t < $smarty->locking_timeout); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Lock cache for this template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty                 $smarty Smarty object | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached cached object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool|void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cached->is_locked = true; | 
					
						
							|  |  |  |         touch($cached->lock_id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Unlock cache for this template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty                 $smarty Smarty object | 
					
						
							|  |  |  |      * @param Smarty_Template_Cached $cached cached object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool|void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cached->is_locked = false; | 
					
						
							|  |  |  |         @unlink($cached->lock_id); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-06-06 02:40:04 +00:00
										 |  |  | } |