6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Define file-based cache.
23 class phpbb_acm_file
extends phpbb_acm_abstract
26 * @var string The cache directory to use
28 public $cache_dir = '';
31 * @var array|bool The cache types this class supports. True indicates support for all types.
33 public $supported = true;
38 * @param string $cache_prefix The cache prefix the instance is responsible for
41 public function __construct($cache_prefix)
43 $this->cache_dir
= PHPBB_ROOT_PATH
. 'cache/';
44 $this->cache_prefix
= $cache_prefix;
48 * {@link phpbb_acm_abstract::get() get()}
50 public function get($var_name)
52 if ($var_name[0] === '#')
54 $var_name = substr($var_name, 1);
55 return $this->get_global($var_name);
58 if (!$this->exists($var_name))
63 @include
($this->cache_dir
. $this->cache_prefix
. '_' . $var_name . '.' . PHP_EXT
);
65 // If no data there, then the file expired...
69 $this->destroy($var_name);
77 * {@link phpbb_acm_abstract::put() put()}
79 public function put($var_name, $data, $ttl = 31536000)
81 if ($var_name[0] === '#')
83 $var_name = substr($var_name, 1);
84 return $this->put_global($var_name, $data, $ttl);
87 $filename = $this->cache_dir
. $this->cache_prefix
. '_' . $var_name . '.' . PHP_EXT
;
89 if ($fp = @fopen
($filename, 'wb'))
92 fwrite($fp, "<?php\n\$expired = (time() > " . (time() +
$ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($data) ?
"unserialize(" . var_export(serialize($data), true) . ");" : 'array();'));
96 phpbb
::$system->chmod($filename, phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
);
104 * {@link phpbb_acm_abstract::exists() exists()}
106 public function exists($var_name)
108 if ($var_name[0] === '#')
110 $var_name = substr($var_name, 1);
111 return $this->exists_global($var_name);
114 return file_exists($this->cache_dir
. $this->cache_prefix
. '_' . $var_name . '.' . PHP_EXT
);
118 * {@link phpbb_acm_abstract::destroy() destroy()}
120 public function destroy($var_name)
122 if ($var_name[0] === '#')
124 $var_name = substr($var_name, 1);
125 $this->destroy_global($var_name);
128 if (!$this->exists($var_name))
133 $this->remove_file($this->cache_dir
. $this->cache_prefix
. '_' . $var_name . '.' . PHP_EXT
, true);
137 * {@link phpbb_acm_abstract::load() load()}
139 public function load()
141 // grab the global cache
142 if (file_exists($this->cache_dir
. $this->cache_prefix
. '_global.' . PHP_EXT
))
144 @include
($this->cache_dir
. $this->cache_prefix
. '_global.' . PHP_EXT
);
152 * {@link phpbb_acm_abstract::unload() unload()}
154 public function unload()
156 if (!$this->is_modified
)
161 $filename = $this->cache_dir
. $this->cache_prefix
. '_global.' . PHP_EXT
;
163 if ($fp = @fopen
($filename, 'wb'))
165 @flock
($fp, LOCK_EX
);
166 fwrite($fp, "<?php\n\$this->vars = unserialize(" . var_export(serialize($this->vars
), true) . ");\n\$this->var_expires = unserialize(" . var_export(serialize($this->var_expires
), true) . ");");
167 @flock
($fp, LOCK_UN
);
170 phpbb
::$system->chmod($filename, phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
);
174 // Now, this occurred how often? ... phew, just tell the user then...
175 if (!@is_writable
($this->cache_dir
))
177 trigger_error($this->cache_dir
. ' is NOT writable.', E_USER_ERROR
);
180 trigger_error('Not able to open ' . $filename, E_USER_ERROR
);
183 $this->is_modified
= false;
185 // To reset the global vars
186 $this->vars
= $this->var_expires
= array();
190 * Tidy local cache data. Also see {@link phpbb_acm_abstract::tidy() tidy()}
193 protected function tidy_local()
195 $dir = @opendir
($this->cache_dir
);
202 while (($entry = readdir($dir)) !== false)
204 if (strpos($entry, $this->cache_prefix
. '_') !== 0 ||
strpos($entry, $this->cache_prefix
. '_global') === 0)
210 @include
($this->cache_dir
. $entry);
214 $this->remove_file($this->cache_dir
. $entry);
221 * Purge local cache data. Also see {@link phpbb_acm_abstract::purge() purge()}
224 protected function purge_local()
226 $dir = @opendir
($this->cache_dir
);
233 while (($entry = readdir($dir)) !== false)
235 if (strpos($entry, $this->cache_prefix
. '_') !== 0 ||
strpos($entry, $this->cache_prefix
. '_global') === 0)
240 $this->remove_file($this->cache_dir
. $entry);
246 * Get modified date for cache entry
248 * @param string $var_name The cache variable name
251 public function get_modified_date($var_name)
253 return @filemtime
($this->cache_dir
. $this->cache_prefix
. '_' . $var_name . '.' . PHP_EXT
);
257 * Removes/unlinks file
259 * @param string $filename The filename to remove
260 * @param bool $check If true the cache directory is checked for correct directory permissions.
263 protected function remove_file($filename, $check = false)
265 if ($check && !@is_writable
($this->cache_dir
))
267 // E_USER_ERROR - not using language entry - intended.
268 trigger_error('Unable to remove files within ' . $this->cache_dir
. '. Please check directory permissions.', E_USER_ERROR
);
271 return @unlink
($filename);
276 * Special implementation for cache type 'sql'
279 class phpbb_acm_file_sql
extends phpbb_acm_file
282 * {@link phpbb_acm_abstract::destroy() destroy()}
284 public function destroy($var_name)
286 if ($var_name[0] === '#')
288 $var_name = substr($var_name, 1);
289 $this->destroy_global($var_name);
292 $table = (!is_array($var_name)) ?
array($var_name) : $var_name;
293 $dir = @opendir
($this->cache_dir
);
300 while (($entry = readdir($dir)) !== false)
302 if (strpos($entry, $this->cache_prefix
. '_') !== 0)
307 // The following method is more failproof than simply assuming the query is on line 3 (which it should be)
308 @include
($this->cache_dir
. $entry);
312 $this->remove_file($this->cache_dir
. $entry);
317 $data = $data['query'];
320 foreach ($table as $check_table)
322 // Better catch partial table names than no table names. ;)
323 if (strpos($data, $check_table) !== false)
332 $this->remove_file($this->cache_dir
. $entry);