1 <?php
3 /**
4 * Smarty Internal Plugin CacheResource File
5 *
6 * Implements the file system as resource for the HTML cache
7 * Version ussing nocache inserts
8 *
9 * @package Smarty
10 * @subpackage Cacher
11 * @author Uwe Tews
12 */
14 /**
15 * This class does contain all necessary methods for the HTML cache on file system
16 */
17 class Smarty_Internal_CacheResource_File {
18 function __construct($smarty)
19 {
20 $this->smarty = $smarty;
21 }
22 /**
23 * Returns the filepath of the cached template output
24 *
25 * @param object $_template current template
26 * @return string the cache filepath
27 */
28 public function getCachedFilepath($_template)
29 {
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
39 . $_filepath;
40 }
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;
44 } else {
45 $_cache_id = '';
46 }
47 if (isset($_compile_id)) {
48 $_compile_id = $_compile_id . $_compile_dir_sep;
49 } else {
50 $_compile_id = '';
51 }
52 $_cache_dir = $this->smarty->cache_dir;
53 if (strpos('/\\', substr($_cache_dir, -1)) === false) {
54 $_cache_dir .= DS;
55 }
56 return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
57 }
59 /**
60 * Returns the timpestamp of the cached template output
61 *
62 * @param object $_template current template
63 * @return integer |booelan the template timestamp or false if the file does not exist
64 */
65 public function getCachedTimestamp($_template)
66 {
67 // return @filemtime ($_template->getCachedFilepath());
68 return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ;
69 }
71 /**
72 * Returns the cached template output
73 *
74 * @param object $_template current template
75 * @return string |booelan the template content or false if the file does not exist
76 */
77 public function getCachedContents($_template)
78 {
79 ob_start();
80 $_smarty_tpl = $_template;
81 include $_template->getCachedFilepath();
82 return ob_get_clean();
83 }
85 /**
86 * Writes the rendered template output to cache file
87 *
88 * @param object $_template current template
89 * @return boolean status
90 */
91 public function writeCachedContent($_template, $content)
92 {
93 if (!$_template->resource_object->isEvaluated) {
94 if (Smarty_Internal_Write_File::writeFile($_template->getCachedFilepath(), $content, $this->smarty) === true) {
95 $_template->cached_timestamp = filemtime($_template->getCachedFilepath());
96 return true;
97 }
98 }
99 return false;
100 }
102 /**
103 * Empty cache folder
104 *
105 * @param integer $exp_time expiration time
106 * @return integer number of cache files deleted
107 */
108 public function clearAll($exp_time = null)
109 {
110 return $this->clear(null, null, null, $exp_time);
111 }
112 /**
113 * Empty cache for a specific template
114 *
115 * @param string $resource_name template name
116 * @param string $cache_id cache id
117 * @param string $compile_id compile id
118 * @param integer $exp_time expiration time
119 * @return integer number of cache files deleted
120 */
121 public function clear($resource_name, $cache_id, $compile_id, $exp_time)
122 {
123 $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
124 $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
125 $_dir_sep = $this->smarty->use_sub_dirs ? '/' : '^';
126 $_compile_id_offset = $this->smarty->use_sub_dirs ? 3 : 0;
127 $_dir = rtrim($this->smarty->cache_dir, '/\\') . DS;
128 $_dir_length = strlen($_dir);
129 if (isset($_cache_id)) {
130 $_cache_id_parts = explode('|', $_cache_id);
131 $_cache_id_parts_count = count($_cache_id_parts);
132 }
133 if (isset($resource_name)) {
134 $tpl = $this->smarty->createTemplate($resource_name);
135 if ($tpl->isExisting()) {
136 $_resourcename_parts = basename(str_replace('^','/',$tpl->getCachedFilepath()));
137 } else {
138 return 0;
139 }
140 }
141 $_count = 0;
142 $_cacheDirs = new RecursiveDirectoryIterator($_dir);
143 $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
144 foreach ($_cache as $_file) {
145 if (strpos($_file, '.svn') !== false) continue;
146 // directory ?
147 if ($_file->isDir()) {
148 if (!$_cache->isDot()) {
149 // delete folder if empty
150 @rmdir($_file->getPathname());
151 }
152 } else {
153 $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
154 $_parts_count = count($_parts);
155 // check name
156 if (isset($resource_name)) {
157 if ($_parts[$_parts_count-1] != $_resourcename_parts) {
158 continue;
159 }
160 }
161 // check compile id
162 if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
163 continue;
164 }
165 // check cache id
166 if (isset($_cache_id)) {
167 // count of cache id parts
168 $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
169 if ($_parts_count < $_cache_id_parts_count) {
170 continue;
171 }
172 for ($i = 0; $i < $_cache_id_parts_count; $i++) {
173 if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
174 }
175 }
176 // expired ?
177 if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) {
178 continue;
179 }
180 $_count += @unlink((string) $_file) ? 1 : 0;
181 }
182 }
183 return $_count;
184 }
185 }
187 ?>