| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  | * Smarty Internal Plugin CacheResource File | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * Implements the file system as resource for the HTML cache | 
					
						
							|  |  |  | * Version ussing nocache inserts | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * @package Smarty | 
					
						
							|  |  |  | * @subpackage Cacher | 
					
						
							|  |  |  | * @author Uwe Tews  | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  | * This class does contain all necessary methods for the HTML cache on file system | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2009-08-08 17:28:23 +00:00
										 |  |  | class Smarty_Internal_CacheResource_File { | 
					
						
							|  |  |  |     function __construct($smarty) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->smarty = $smarty; | 
					
						
							|  |  |  |     }  | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |     * Returns the filepath of the cached template output | 
					
						
							|  |  |  |     *  | 
					
						
							|  |  |  |     * @param object $template current template | 
					
						
							|  |  |  |     * @return string the cache filepath | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     public function getCachedFilepath($template) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-12-17 16:58:44 +00:00
										 |  |  |         return $this->buildCachedFilepath ($template->getTemplateFilepath(), $template->cache_id, $template->compile_id); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Returns the timpestamp of the cached template output | 
					
						
							|  |  |  |     *  | 
					
						
							|  |  |  |     * @param object $template current template | 
					
						
							|  |  |  |     * @return integer |booelan the template timestamp or false if the file does not exist | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     public function getCachedTimestamp($template) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-12-27 19:12:51 +00:00
										 |  |  |         //return @filemtime($template->getCachedFilepath()); 
 | 
					
						
							|  |  |  |         return ($template->getCachedFilepath() && file_exists($template->getCachedFilepath())) ? filemtime($template->getCachedFilepath()) : false ; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Returns the cached template output | 
					
						
							|  |  |  |     *  | 
					
						
							|  |  |  |     * @param object $template current template | 
					
						
							|  |  |  |     * @return string |booelan the template content or false if the file does not exist | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     public function getCachedContents($template) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-09-19 13:22:32 +00:00
										 |  |  |         ob_start(); | 
					
						
							|  |  |  |         $_smarty_tpl = $template; | 
					
						
							|  |  |  |         include $template->getCachedFilepath(); | 
					
						
							|  |  |  |         return ob_get_clean(); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Writes the rendered template output to cache file | 
					
						
							|  |  |  |     *  | 
					
						
							|  |  |  |     * @param object $template current template | 
					
						
							|  |  |  |     * @return boolean status | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2009-09-19 13:22:32 +00:00
										 |  |  |     public function writeCachedContent($template, $content) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |         if (!$template->resource_object->isEvaluated) { | 
					
						
							| 
									
										
										
										
											2009-11-17 17:46:03 +00:00
										 |  |  |             return Smarty_Internal_Write_File::writeFile($template->getCachedFilepath(), $content, $this->smarty); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Empty cache folder | 
					
						
							|  |  |  |     *  | 
					
						
							|  |  |  |     * @param integer $exp_time expiration time | 
					
						
							|  |  |  |     * @return integer number of cache files deleted | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     public function clearAll($exp_time = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->clear(null, null, null, $exp_time); | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Empty cache for a specific template | 
					
						
							|  |  |  |     *  | 
					
						
							|  |  |  |     * @param string $resource_name template name | 
					
						
							|  |  |  |     * @param string $cache_id cache id | 
					
						
							|  |  |  |     * @param string $compile_id compile id | 
					
						
							|  |  |  |     * @param integer $exp_time expiration time | 
					
						
							|  |  |  |     * @return integer number of cache files deleted | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     public function clear($resource_name, $cache_id, $compile_id, $exp_time) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null; | 
					
						
							|  |  |  |         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; | 
					
						
							|  |  |  |         $_dir_sep = $this->smarty->use_sub_dirs ? '/' : '^'; | 
					
						
							|  |  |  |         $_compile_id_offset = $this->smarty->use_sub_dirs ? 3 : 0; | 
					
						
							|  |  |  |         $_dir = rtrim($this->smarty->cache_dir, '/\\') . DS; | 
					
						
							|  |  |  |         $_dir_length = strlen($_dir); | 
					
						
							|  |  |  |         if (isset($_cache_id)) { | 
					
						
							|  |  |  |             $_cache_id_parts = explode('|', $_cache_id); | 
					
						
							|  |  |  |             $_cache_id_parts_count = count($_cache_id_parts); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         }  | 
					
						
							|  |  |  |         $_count = 0; | 
					
						
							|  |  |  |         $_cacheDirs = new RecursiveDirectoryIterator($_dir); | 
					
						
							|  |  |  |         $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST); | 
					
						
							|  |  |  |         foreach ($_cache as $_file) { | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |             if (strpos($_file, '.svn') !== false) continue;  | 
					
						
							|  |  |  |             // directory ?
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             if ($_file->isDir()) { | 
					
						
							|  |  |  |                 if (!$_cache->isDot()) { | 
					
						
							|  |  |  |                     // delete folder if empty
 | 
					
						
							|  |  |  |                     @rmdir($_file->getPathname()); | 
					
						
							|  |  |  |                 }  | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |                 $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length))); | 
					
						
							|  |  |  |                 $_parts_count = count($_parts);  | 
					
						
							|  |  |  |                 // check name
 | 
					
						
							|  |  |  |                 if (isset($resource_name)) { | 
					
						
							|  |  |  |                     $_filename_parts = explode('.', $_parts[$_parts_count-1]); | 
					
						
							|  |  |  |                     $_resourcename_parts = explode('.', $resource_name . '.php'); | 
					
						
							|  |  |  |                     if (count($_filename_parts)-1 != count($_resourcename_parts)) { | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     }  | 
					
						
							|  |  |  |                     for ($i = 0; $i < count($_resourcename_parts); $i++) { | 
					
						
							|  |  |  |                         if ($_filename_parts[$i + 1] != $_resourcename_parts[$i]) continue 2; | 
					
						
							|  |  |  |                     }  | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 }  | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |                 // check compile id
 | 
					
						
							|  |  |  |                 if (isset($_compile_id) && $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 }  | 
					
						
							|  |  |  |                 // check cache id
 | 
					
						
							|  |  |  |                 if (isset($_cache_id)) { | 
					
						
							|  |  |  |                     // count of cache id parts
 | 
					
						
							|  |  |  |                     $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset; | 
					
						
							|  |  |  |                     if ($_parts_count < $_cache_id_parts_count) { | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     }  | 
					
						
							|  |  |  |                     for ($i = 0; $i < $_cache_id_parts_count; $i++) { | 
					
						
							|  |  |  |                         if ($_parts[$i] != $_cache_id_parts[$i]) continue 2; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                     }  | 
					
						
							|  |  |  |                 }  | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |                 // expired ?
 | 
					
						
							|  |  |  |                 if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 }  | 
					
						
							|  |  |  |                 $_count += @unlink((string) $_file) ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             }  | 
					
						
							|  |  |  |         }  | 
					
						
							|  |  |  |         return $_count; | 
					
						
							|  |  |  |     }  | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Get system filepath to cached file | 
					
						
							|  |  |  |     *  | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |     * @param string $source_file_path template source file path | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     * @param string $cache_id cache id | 
					
						
							|  |  |  |     * @param string $compile_id compile id | 
					
						
							|  |  |  |     * @return string filepath of cache file | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |     private function buildCachedFilepath ($source_file_path, $cache_id, $compile_id) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null; | 
					
						
							|  |  |  |         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; | 
					
						
							|  |  |  |         $_filepath = (string)abs(crc32($source_file_path));  | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         // if use_sub_dirs, break file into directories
 | 
					
						
							|  |  |  |         if ($this->smarty->use_sub_dirs) { | 
					
						
							| 
									
										
										
										
											2009-08-29 22:57:29 +00:00
										 |  |  |             $_filepath = substr($_filepath, 0, 2) . DS | 
					
						
							|  |  |  |              . substr($_filepath, 2, 2) . DS | 
					
						
							|  |  |  |              . substr($_filepath, 4, 2) . DS | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |              . $_filepath; | 
					
						
							|  |  |  |         }  | 
					
						
							| 
									
										
										
										
											2009-08-29 22:57:29 +00:00
										 |  |  |         $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^'; | 
					
						
							| 
									
										
										
										
											2009-11-17 17:46:03 +00:00
										 |  |  |         if (isset($_cache_id)) { | 
					
						
							|  |  |  |             $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $_cache_id = ''; | 
					
						
							|  |  |  |         }  | 
					
						
							| 
									
										
										
										
											2009-11-17 17:46:03 +00:00
										 |  |  |         if (isset($_compile_id)) { | 
					
						
							|  |  |  |             $_compile_id = $_compile_id . $_compile_dir_sep; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $_compile_id = ''; | 
					
						
							|  |  |  |         }  | 
					
						
							| 
									
										
										
										
											2009-04-29 19:31:33 +00:00
										 |  |  |         $_cache_dir = $this->smarty->cache_dir; | 
					
						
							| 
									
										
										
										
											2009-09-19 13:22:32 +00:00
										 |  |  |         if (strpos('/\\', substr($_cache_dir, -1)) === false) { | 
					
						
							| 
									
										
										
										
											2009-08-29 22:57:29 +00:00
										 |  |  |             $_cache_dir .= DS; | 
					
						
							| 
									
										
										
										
											2009-04-29 19:25:44 +00:00
										 |  |  |         }  | 
					
						
							| 
									
										
										
										
											2009-12-27 15:06:49 +00:00
										 |  |  |         return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($source_file_path) . '.php'; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     }  | 
					
						
							|  |  |  | }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 |