add some properties
[phpbb.git] / phpBB / includes / acm / acm_file.php
bloba6733245bf5da6f165d1704b2affc4d4a46d9737
1 <?php
2 /**
4 * @package acm
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Define file-based cache.
21 * @package acm
23 class phpbb_acm_file extends phpbb_acm_abstract
25 /**
26 * @var string The cache directory to use
28 public $cache_dir = '';
30 /**
31 * @var array|bool The cache types this class supports. True indicates support for all types.
33 public $supported = true;
35 /**
36 * Set cache directory
38 * @param string $cache_prefix The cache prefix the instance is responsible for
39 * @access public
41 public function __construct($cache_prefix)
43 $this->cache_dir = PHPBB_ROOT_PATH . 'cache/';
44 $this->cache_prefix = $cache_prefix;
47 /**
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))
60 return false;
63 @include($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT);
65 // If no data there, then the file expired...
66 if ($expired)
68 // Destroy
69 $this->destroy($var_name);
70 return false;
73 return $data;
76 /**
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'))
91 @flock($fp, LOCK_EX);
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();'));
93 @flock($fp, LOCK_UN);
94 fclose($fp);
96 phpbb::$system->chmod($filename, phpbb::CHMOD_READ | phpbb::CHMOD_WRITE);
99 return $data;
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))
130 return false;
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);
145 return true;
148 return false;
152 * {@link phpbb_acm_abstract::unload() unload()}
154 public function unload()
156 if (!$this->is_modified)
158 return;
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);
168 fclose($fp);
170 phpbb::$system->chmod($filename, phpbb::CHMOD_READ | phpbb::CHMOD_WRITE);
172 else
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()}
191 * @access protected
193 protected function tidy_local()
195 $dir = @opendir($this->cache_dir);
197 if (!$dir)
199 return;
202 while (($entry = readdir($dir)) !== false)
204 if (strpos($entry, $this->cache_prefix . '_') !== 0 || strpos($entry, $this->cache_prefix . '_global') === 0)
206 continue;
209 $expired = true;
210 @include($this->cache_dir . $entry);
212 if ($expired)
214 $this->remove_file($this->cache_dir . $entry);
217 closedir($dir);
221 * Purge local cache data. Also see {@link phpbb_acm_abstract::purge() purge()}
222 * @access protected
224 protected function purge_local()
226 $dir = @opendir($this->cache_dir);
228 if (!$dir)
230 return;
233 while (($entry = readdir($dir)) !== false)
235 if (strpos($entry, $this->cache_prefix . '_') !== 0 || strpos($entry, $this->cache_prefix . '_global') === 0)
237 continue;
240 $this->remove_file($this->cache_dir . $entry);
242 closedir($dir);
246 * Get modified date for cache entry
248 * @param string $var_name The cache variable name
249 * @access public
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.
261 * @access protected
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'
277 * @package acm
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);
295 if (!$dir)
297 return;
300 while (($entry = readdir($dir)) !== false)
302 if (strpos($entry, $this->cache_prefix . '_') !== 0)
304 continue;
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);
310 if (empty($data))
312 $this->remove_file($this->cache_dir . $entry);
313 continue;
316 // Get the query
317 $data = $data['query'];
319 $found = false;
320 foreach ($table as $check_table)
322 // Better catch partial table names than no table names. ;)
323 if (strpos($data, $check_table) !== false)
325 $found = true;
326 break;
330 if ($found)
332 $this->remove_file($this->cache_dir . $entry);
335 closedir($dir);
337 return;