1: <?php
  2:   3:   4:   5:   6:   7:   8:   9: 
 10: 
 11:  12:  13:  14:  15:  16:  17: 
 18: class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
 19: {
 20:      21:  22:  23:  24:  25:  26:  27: 
 28:     public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
 29:     {
 30:         $_source_file_path = str_replace(':', '.', $_template->source->filepath);
 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->source->uid;
 34:         
 35:         if ($_template->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 = $_template->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 = $_template->smarty->getCacheDir();
 53:         if ($_template->smarty->cache_locking) {
 54:             
 55:             
 56:             if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) {
 57:                 $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir;
 58:             } else {
 59:                 $_lock_dir = $_cache_dir;
 60:             }
 61:             $cached->lock_id = $_lock_dir . sha1($_cache_id . $_compile_id . $_template->source->uid) . '.lock';
 62:         }
 63:         $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
 64:         $cached->timestamp = @filemtime($cached->filepath);
 65:         $cached->exists = !!$cached->timestamp;
 66:     }
 67: 
 68:      69:  70:  71:  72:  73:  74: 
 75:     public function populateTimestamp(Smarty_Template_Cached $cached)
 76:     {
 77:         $cached->timestamp = @filemtime($cached->filepath);
 78:         $cached->exists = !!$cached->timestamp;
 79:     }
 80: 
 81:      82:  83:  84:  85:  86:  87:  88: 
 89:     public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
 90:     {
 91:          92:  93: 
 94:         $_smarty_tpl = $_template;
 95: 
 96:         return @include $_template->cached->filepath;
 97:     }
 98: 
 99:     100: 101: 102: 103: 104: 105: 106: 
107:     public function writeCachedContent(Smarty_Internal_Template $_template, $content)
108:     {
109:         if (Smarty_Internal_Write_File::writeFile($_template->cached->filepath, $content, $_template->smarty) === true) {
110:             $_template->cached->timestamp = @filemtime($_template->cached->filepath);
111:             $_template->cached->exists = !!$_template->cached->timestamp;
112:             if ($_template->cached->exists) {
113:                 return true;
114:             }
115:         }
116: 
117:         return false;
118:     }
119: 
120:     121: 122: 123: 124: 125: 126: 127: 
128:     public function clearAll(Smarty $smarty, $exp_time = null)
129:     {
130:         return $this->clear($smarty, null, null, null, $exp_time);
131:     }
132: 
133:     134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 
144:     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
145:     {
146:         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
147:         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
148:         $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
149:         $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
150:         if (($_dir = realpath($smarty->getCacheDir())) === false) {
151:             return 0;
152:         }
153:         $_dir .= '/';
154:         $_dir_length = strlen($_dir);
155:         if (isset($_cache_id)) {
156:             $_cache_id_parts = explode('|', $_cache_id);
157:             $_cache_id_parts_count = count($_cache_id_parts);
158:             if ($smarty->use_sub_dirs) {
159:                 foreach ($_cache_id_parts as $id_part) {
160:                     $_dir .= $id_part . DS;
161:                 }
162:             }
163:         }
164:         if (isset($resource_name)) {
165:             $_save_stat = $smarty->caching;
166:             $smarty->caching = true;
167:             $tpl = new $smarty->template_class($resource_name, $smarty);
168:             $smarty->caching = $_save_stat;
169: 
170:             
171:             $tpl->source; 
172:             if ($smarty->allow_ambiguous_resources) {
173:                 $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
174:             } else {
175:                 $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
176:             }
177:             if (isset($_templateId[150])) {
178:                 $_templateId = sha1($_templateId);
179:             }
180:             unset($smarty->template_objects[$_templateId]);
181: 
182:             if ($tpl->source->exists) {
183:                 $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
184:             } else {
185:                 return 0;
186:             }
187:         }
188:         $_count = 0;
189:         $_time = time();
190:         if (file_exists($_dir)) {
191:             $_cacheDirs = new RecursiveDirectoryIterator($_dir);
192:             $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
193:             foreach ($_cache as $_file) {
194:                 if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
195:                     continue;
196:                 }
197:                 
198:                 if ($_file->isDir()) {
199:                     if (!$_cache->isDot()) {
200:                         
201:                         @rmdir($_file->getPathname());
202:                     }
203:                 } else {
204:                     $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length)));
205:                     $_parts_count = count($_parts);
206:                     
207:                     if (isset($resource_name)) {
208:                         if ($_parts[$_parts_count - 1] != $_resourcename_parts) {
209:                             continue;
210:                         }
211:                     }
212:                     
213:                     if (isset($_compile_id) && (!isset($_parts[$_parts_count - 2 - $_compile_id_offset]) || $_parts[$_parts_count - 2 - $_compile_id_offset] != $_compile_id)) {
214:                         continue;
215:                     }
216:                     
217:                     if (isset($_cache_id)) {
218:                         
219:                         $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
220:                         if ($_parts_count < $_cache_id_parts_count) {
221:                             continue;
222:                         }
223:                         for ($i = 0; $i < $_cache_id_parts_count; $i ++) {
224:                             if ($_parts[$i] != $_cache_id_parts[$i]) {
225:                                 continue 2;
226:                             }
227:                         }
228:                     }
229:                     
230:                     if (isset($exp_time)) {
231:                         if ($exp_time < 0) {
232:                             preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match);
233:                             if ($_time < (@filemtime($_file) + $match[1])) {
234:                                 continue;
235:                             }
236:                         } else {
237:                             if ($_time - @filemtime($_file) < $exp_time) {
238:                                 continue;
239:                             }
240:                         }
241:                     }
242:                     $_count += @unlink((string) $_file) ? 1 : 0;
243:                 }
244:             }
245:         }
246: 
247:         return $_count;
248:     }
249: 
250:     251: 252: 253: 254: 255: 256: 257: 
258:     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
259:     {
260:         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
261:             clearstatcache(true, $cached->lock_id);
262:         } else {
263:             clearstatcache();
264:         }
265:         $t = @filemtime($cached->lock_id);
266: 
267:         return $t && (time() - $t < $smarty->locking_timeout);
268:     }
269: 
270:     271: 272: 273: 274: 275: 276: 277: 
278:     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
279:     {
280:         $cached->is_locked = true;
281:         touch($cached->lock_id);
282:     }
283: 
284:     285: 286: 287: 288: 289: 290: 291: 
292:     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
293:     {
294:         $cached->is_locked = false;
295:         @unlink($cached->lock_id);
296:     }
297: }
298: