| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-09-19 16:39:19 +02:00
										 |  |  |  * Smarty cache resource file clear method | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsInternal | 
					
						
							|  |  |  |  * @author     Uwe Tews | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 16:39:19 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Smarty Internal Runtime Cache Resource File Class | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsInternal | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Smarty_Internal_Runtime_CacheResourceFile | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Empty cache for a specific template | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Smarty  $smarty | 
					
						
							|  |  |  |      * @param string  $resource_name template name | 
					
						
							|  |  |  |      * @param string  $cache_id      cache id | 
					
						
							|  |  |  |      * @param string  $compile_id    compile id | 
					
						
							|  |  |  |      * @param integer $exp_time      expiration time (number of seconds, not timestamp) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return integer number of cache files deleted | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-09-19 16:39:19 +02:00
										 |  |  |     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null; | 
					
						
							| 
									
										
										
										
											2015-08-06 21:42:20 +02:00
										 |  |  |         $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null; | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |         $_dir_sep = $smarty->use_sub_dirs ? '/' : '^'; | 
					
						
							|  |  |  |         $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0; | 
					
						
							|  |  |  |         $_dir = $smarty->getCacheDir(); | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |         if ($_dir === '/') { //We should never want to delete this!
 | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $_dir_length = strlen($_dir); | 
					
						
							|  |  |  |         if (isset($_cache_id)) { | 
					
						
							|  |  |  |             $_cache_id_parts = explode('|', $_cache_id); | 
					
						
							|  |  |  |             $_cache_id_parts_count = count($_cache_id_parts); | 
					
						
							|  |  |  |             if ($smarty->use_sub_dirs) { | 
					
						
							|  |  |  |                 foreach ($_cache_id_parts as $id_part) { | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  |                     $_dir .= $id_part . '/'; | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (isset($resource_name)) { | 
					
						
							|  |  |  |             $_save_stat = $smarty->caching; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |             $smarty->caching = Smarty::CACHING_LIFETIME_CURRENT; | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |             $tpl = new $smarty->template_class($resource_name, $smarty); | 
					
						
							|  |  |  |             $smarty->caching = $_save_stat; | 
					
						
							|  |  |  |             // remove from template cache
 | 
					
						
							|  |  |  |             $tpl->source; // have the template registered before unset()
 | 
					
						
							|  |  |  |             if ($tpl->source->exists) { | 
					
						
							|  |  |  |                 $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $_count = 0; | 
					
						
							|  |  |  |         $_time = time(); | 
					
						
							|  |  |  |         if (file_exists($_dir)) { | 
					
						
							|  |  |  |             $_cacheDirs = new RecursiveDirectoryIterator($_dir); | 
					
						
							|  |  |  |             $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST); | 
					
						
							|  |  |  |             foreach ($_cache as $_file) { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                 if (substr(basename($_file->getPathname()), 0, 1) === '.') { | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-10-26 06:14:22 +02:00
										 |  |  |                 $_filepath = (string)$_file; | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                 // directory ?
 | 
					
						
							|  |  |  |                 if ($_file->isDir()) { | 
					
						
							|  |  |  |                     if (!$_cache->isDot()) { | 
					
						
							|  |  |  |                         // delete folder if empty
 | 
					
						
							|  |  |  |                         @rmdir($_file->getPathname()); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2016-09-19 14:59:49 +02:00
										 |  |  |                     // delete only php files
 | 
					
						
							| 
									
										
										
										
											2017-10-26 06:14:22 +02:00
										 |  |  |                     if (substr($_filepath, -4) !== '.php') { | 
					
						
							| 
									
										
										
										
											2016-09-19 14:59:49 +02:00
										 |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length))); | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                     $_parts_count = count($_parts); | 
					
						
							|  |  |  |                     // check name
 | 
					
						
							|  |  |  |                     if (isset($resource_name)) { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                         if ($_parts[ $_parts_count - 1 ] !== $_resourcename_parts) { | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                             continue; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     // check compile id
 | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  |                     if (isset($_compile_id) && (!isset($_parts[ $_parts_count - 2 - $_compile_id_offset ]) | 
					
						
							|  |  |  |                                                 || $_parts[ $_parts_count - 2 - $_compile_id_offset ] !== $_compile_id) | 
					
						
							| 
									
										
										
										
											2015-09-15 00:15:12 +02:00
										 |  |  |                     ) { | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     // check cache id
 | 
					
						
							|  |  |  |                     if (isset($_cache_id)) { | 
					
						
							|  |  |  |                         // count of cache id parts
 | 
					
						
							| 
									
										
										
										
											2015-09-15 00:15:12 +02:00
										 |  |  |                         $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : | 
					
						
							|  |  |  |                             $_parts_count - 1 - $_compile_id_offset; | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                         if ($_parts_count < $_cache_id_parts_count) { | 
					
						
							|  |  |  |                             continue; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2017-10-26 06:14:22 +02:00
										 |  |  |                         for ($i = 0; $i < $_cache_id_parts_count; $i++) { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                             if ($_parts[ $i ] !== $_cache_id_parts[ $i ]) { | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                                 continue 2; | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2017-05-21 16:58:58 +02:00
										 |  |  |                     if (is_file($_filepath)) { | 
					
						
							|  |  |  |                         // expired ?
 | 
					
						
							|  |  |  |                         if (isset($exp_time)) { | 
					
						
							|  |  |  |                             if ($exp_time < 0) { | 
					
						
							|  |  |  |                                 preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_filepath), $match); | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                                 if ($_time < (filemtime($_filepath) + $match[ 1 ])) { | 
					
						
							| 
									
										
										
										
											2017-05-21 16:58:58 +02:00
										 |  |  |                                     continue; | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                             } else { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                                 if ($_time - filemtime($_filepath) < $exp_time) { | 
					
						
							| 
									
										
										
										
											2017-05-21 16:58:58 +02:00
										 |  |  |                                     continue; | 
					
						
							|  |  |  |                                 } | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2017-05-21 16:58:58 +02:00
										 |  |  |                         $_count += @unlink($_filepath) ? 1 : 0; | 
					
						
							|  |  |  |                         if (function_exists('opcache_invalidate') | 
					
						
							|  |  |  |                             && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1) | 
					
						
							|  |  |  |                         ) { | 
					
						
							|  |  |  |                             opcache_invalidate($_filepath, true); | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                         } elseif (function_exists('apc_delete_file')) { | 
					
						
							| 
									
										
										
										
											2017-05-21 16:58:58 +02:00
										 |  |  |                             apc_delete_file($_filepath); | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2015-09-15 00:15:12 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2015-07-20 04:38:13 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $_count; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-12 09:58:15 +02:00
										 |  |  | } |