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 $_save_stat = $this->smarty->caching;
135 $this->smarty->caching = true;
136 $tpl = new $this->smarty->template_class($resource_name, $this->smarty);
137 // remove from template cache
138 unset($this->smarty->template_objects[crc32($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
139 $this->smarty->caching = $_save_stat;
140 if ($tpl->isExisting()) {
141 $_resourcename_parts = basename(str_replace('^', '/', $tpl->getCachedFilepath()));
142 } else {
143 return 0;
144 }
145 }
146 $_count = 0;
147 $_cacheDirs = new RecursiveDirectoryIterator($_dir);
148 $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
149 foreach ($_cache as $_file) {
150 if (strpos($_file, '.svn') !== false) continue;
151 // directory ?
152 if ($_file->isDir()) {
153 if (!$_cache->isDot()) {
154 // delete folder if empty
155 @rmdir($_file->getPathname());
156 }
157 } else {
158 $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
159 $_parts_count = count($_parts);
160 // check name
161 if (isset($resource_name)) {
162 if ($_parts[$_parts_count-1] != $_resourcename_parts) {
163 continue;
164 }
165 }
166 // check compile id
167 if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
168 continue;
169 }
170 // check cache id
171 if (isset($_cache_id)) {
172 // count of cache id parts
173 $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
174 if ($_parts_count < $_cache_id_parts_count) {
175 continue;
176 }
177 for ($i = 0; $i < $_cache_id_parts_count; $i++) {
178 if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
179 }
180 }
181 // expired ?
182 if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) {
183 continue;
184 }
185 $_count += @unlink((string) $_file) ? 1 : 0;
186 }
187 }
188 return $_count;
189 }
190 }
192 ?>