1: <?php
2:
3: /**
4: * Smarty Method ClearCompiledTemplate
5: *
6: * Smarty::clearCompiledTemplate() method
7: *
8: * @package Smarty
9: * @subpackage PluginsInternal
10: * @author Uwe Tews
11: */
12: class Smarty_Internal_Method_ClearCompiledTemplate
13: {
14: /**
15: * Valid for Smarty object
16: *
17: * @var int
18: */
19: public $objMap = 1;
20:
21: /**
22: * Delete compiled template file
23: *
24: * @api Smarty::clearCompiledTemplate()
25: * @link http://www.smarty.net/docs/en/api.clear.compiled.template.tpl
26: *
27: * @param \Smarty $smarty
28: * @param string $resource_name template name
29: * @param string $compile_id compile id
30: * @param integer $exp_time expiration time
31: *
32: * @return int number of template files deleted
33: * @throws \SmartyException
34: */
35: public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $compile_id = null, $exp_time = null)
36: {
37: // clear template objects cache
38: $smarty->_clearTemplateCache();
39: $_compile_dir = $smarty->getCompileDir();
40: if ($_compile_dir === '/') { //We should never want to delete this!
41: return 0;
42: }
43: $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
44: $_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
45: if (isset($resource_name)) {
46: $_save_stat = $smarty->caching;
47: $smarty->caching = Smarty::CACHING_OFF;
48: /* @var Smarty_Internal_Template $tpl */
49: $tpl = $smarty->createTemplate($resource_name);
50: $smarty->caching = $_save_stat;
51: if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
52: $_resource_part_1 = basename(str_replace('^', DIRECTORY_SEPARATOR, $tpl->compiled->filepath));
53: $_resource_part_1_length = strlen($_resource_part_1);
54: } else {
55: return 0;
56: }
57: $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
58: $_resource_part_2_length = strlen($_resource_part_2);
59: }
60: $_dir = $_compile_dir;
61: if ($smarty->use_sub_dirs && isset($_compile_id)) {
62: $_dir .= $_compile_id . $_dir_sep;
63: }
64: if (isset($_compile_id)) {
65: $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
66: $_compile_id_part_length = strlen($_compile_id_part);
67: }
68: $_count = 0;
69: try {
70: $_compileDirs = new RecursiveDirectoryIterator($_dir);
71: // NOTE: UnexpectedValueException thrown for PHP >= 5.3
72: } catch (Exception $e) {
73: return 0;
74: }
75: $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
76: foreach ($_compile as $_file) {
77: if (substr(basename($_file->getPathname()), 0, 1) === '.') {
78: continue;
79: }
80: $_filepath = (string)$_file;
81: if ($_file->isDir()) {
82: if (!$_compile->isDot()) {
83: // delete folder if empty
84: @rmdir($_file->getPathname());
85: }
86: } else {
87: // delete only php files
88: if (substr($_filepath, -4) !== '.php') {
89: continue;
90: }
91: $unlink = false;
92: if ((!isset($_compile_id) ||
93: (isset($_filepath[ $_compile_id_part_length ]) &&
94: $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length)))
95: && (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ])
96: && substr_compare(
97: $_filepath,
98: $_resource_part_1,
99: -$_resource_part_1_length,
100: $_resource_part_1_length
101: ) === 0) || (isset($_filepath[ $_resource_part_2_length ])
102: && substr_compare(
103: $_filepath,
104: $_resource_part_2,
105: -$_resource_part_2_length,
106: $_resource_part_2_length
107: ) === 0))
108: ) {
109: if (isset($exp_time)) {
110: if (is_file($_filepath) && time() - filemtime($_filepath) >= $exp_time) {
111: $unlink = true;
112: }
113: } else {
114: $unlink = true;
115: }
116: }
117: if ($unlink && is_file($_filepath) && @unlink($_filepath)) {
118: $_count++;
119: if (function_exists('opcache_invalidate')
120: && (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api')) < 1)
121: ) {
122: opcache_invalidate($_filepath, true);
123: } elseif (function_exists('apc_delete_file')) {
124: apc_delete_file($_filepath);
125: }
126: }
127: }
128: }
129: return $_count;
130: }
131: }
132: