4  * Smarty Internal Plugin CacheResource File
 
   6  * Implements the file system as resource for the HTML cache
 
   7  * Version ussing nocache inserts
 
  15  * This class does contain all necessary methods for the HTML cache on file system
 
  17 class Smarty_Internal_CacheResource_File {
 
  18     function __construct($smarty)
 
  20         $this->smarty = $smarty;
 
  23      * Returns the filepath of the cached template output
 
  25      * @param object $_template current template
 
  26      * @return string the cache filepath
 
  28     public function getCachedFilepath($_template)
 
  30         $_source_file_path = str_replace(':', '.', $_template->getTemplateFilepath());
 
  31         $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;
 
  32         $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
 
  33         $_filepath = $_template->templateUid; 
 
  34         // if use_sub_dirs, break file into directories
 
  35         if ($this->smarty->use_sub_dirs) {
 
  36             $_filepath = substr($_filepath, 0, 2) . DS
 
  37              . substr($_filepath, 2, 2) . DS
 
  38              . substr($_filepath, 4, 2) . DS
 
  41         $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
 
  42         if (isset($_cache_id)) {
 
  43             $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
 
  47         if (isset($_compile_id)) {
 
  48             $_compile_id = $_compile_id . $_compile_dir_sep;
 
  52         $_cache_dir = $this->smarty->cache_dir;
 
  53         if (strpos('/\\', substr($_cache_dir, -1)) === false) {
 
  56         return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
 
  60      * Returns the timpestamp of the cached template output
 
  62      * @param object $_template current template
 
  63      * @return integer |booelan the template timestamp or false if the file does not exist
 
  65     public function getCachedTimestamp($_template)
 
  67         // return @filemtime ($_template->getCachedFilepath());
 
  68         return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ;
 
  72      * Returns the cached template output
 
  74      * @param object $_template current template
 
  75      * @return string |booelan the template content or false if the file does not exist
 
  77     public function getCachedContents($_template, $no_render = false)
 
  82         $_smarty_tpl = $_template;
 
  83         include $_template->getCachedFilepath();
 
  87           return ob_get_clean();
 
  92      * Writes the rendered template output to cache file
 
  94      * @param object $_template current template
 
  95      * @return boolean status
 
  97     public function writeCachedContent($_template, $content)
 
  99         if (!$_template->resource_object->isEvaluated) {
 
 100             if (Smarty_Internal_Write_File::writeFile($_template->getCachedFilepath(), $content, $this->smarty) === true) {
 
 101                 $_template->cached_timestamp = filemtime($_template->getCachedFilepath());
 
 111      * @param integer $exp_time expiration time
 
 112      * @return integer number of cache files deleted
 
 114     public function clearAll($exp_time = null)
 
 116         return $this->clear(null, null, null, $exp_time);
 
 119      * Empty cache for a specific template
 
 121      * @param string $resource_name template name
 
 122      * @param string $cache_id cache id
 
 123      * @param string $compile_id compile id
 
 124      * @param integer $exp_time expiration time
 
 125      * @return integer number of cache files deleted
 
 127     public function clear($resource_name, $cache_id, $compile_id, $exp_time)
 
 129         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
 
 130         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
 
 131         $_dir_sep = $this->smarty->use_sub_dirs ? '/' : '^';
 
 132         $_compile_id_offset = $this->smarty->use_sub_dirs ? 3 : 0;
 
 133         $_dir = rtrim($this->smarty->cache_dir, '/\\') . DS;
 
 134         $_dir_length = strlen($_dir);
 
 135         if (isset($_cache_id)) {
 
 136             $_cache_id_parts = explode('|', $_cache_id);
 
 137             $_cache_id_parts_count = count($_cache_id_parts);
 
 138             if ($this->smarty->use_sub_dirs) {
 
 139                 foreach ($_cache_id_parts as $id_part) {
 
 140                     $_dir .= $id_part . DS;
 
 144         if (isset($resource_name)) {
 
 145             $_save_stat = $this->smarty->caching;
 
 146             $this->smarty->caching = true;
 
 147             $tpl = new $this->smarty->template_class($resource_name, $this->smarty); 
 
 148             // remove from template cache
 
 149             unset($this->smarty->template_objects[crc32($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
 
 150             $this->smarty->caching = $_save_stat;
 
 151             if ($tpl->isExisting()) {
 
 152                 $_resourcename_parts = basename(str_replace('^', '/', $tpl->getCachedFilepath()));
 
 158         if (file_exists($_dir)) {
 
 159             $_cacheDirs = new RecursiveDirectoryIterator($_dir);
 
 160             $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
 
 161             foreach ($_cache as $_file) {
 
 162                 if (strpos($_file, '.svn') !== false) continue; 
 
 164                 if ($_file->isDir()) {
 
 165                     if (!$_cache->isDot()) {
 
 166                         // delete folder if empty
 
 167                         @rmdir($_file->getPathname());
 
 170                     $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
 
 171                     $_parts_count = count($_parts); 
 
 173                     if (isset($resource_name)) {
 
 174                         if ($_parts[$_parts_count-1] != $_resourcename_parts) {
 
 179                     if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
 
 183                     if (isset($_cache_id)) {
 
 184                         // count of cache id parts
 
 185                         $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
 
 186                         if ($_parts_count < $_cache_id_parts_count) {
 
 189                         for ($i = 0; $i < $_cache_id_parts_count; $i++) {
 
 190                             if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
 
 194                     if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) {
 
 197                     $_count += @unlink((string) $_file) ? 1 : 0;