6 * @copyright (c) 2005, 2009 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * ACM File Based Caching
26 var $var_expires = array();
27 var $is_modified = false;
29 var $sql_rowset = array();
30 var $sql_row_pointer = array();
38 global $phpbb_root_path;
39 $this->cache_dir = $phpbb_root_path . 'cache/';
47 return $this->_read('data_global');
57 unset($this->var_expires);
58 unset($this->sql_rowset);
59 unset($this->sql_row_pointer);
61 $this->vars = array();
62 $this->var_expires = array();
63 $this->sql_rowset = array();
64 $this->sql_row_pointer = array();
68 * Save modified objects
72 if (!$this->is_modified)
79 if (!$this->_write('data_global'))
81 if (!function_exists('phpbb_is_writable'))
83 global $phpbb_root_path;
84 include($phpbb_root_path . 'includes/functions.' . $phpEx);
87 // Now, this occurred how often? ... phew, just tell the user then...
88 if (!phpbb_is_writable($this->cache_dir))
90 // We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload())
91 die('Fatal: ' . $this->cache_dir . ' is NOT writable.');
95 die('Fatal: Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx);
99 $this->is_modified = false;
109 $dir = @opendir($this->cache_dir);
118 while (($entry = readdir($dir)) !== false)
120 if (!preg_match('/^(sql_|data_(?!global))/', $entry))
125 if (!($handle = @fopen($this->cache_dir . $entry, 'rb')))
130 // Skip the PHP header
134 $expires = (int) fgets($handle);
138 if ($time >= $expires)
140 $this->remove_file($this->cache_dir . $entry);
145 if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
147 if (!sizeof($this->vars))
152 foreach ($this->var_expires as $var_name => $expires)
154 if ($time >= $expires)
156 $this->destroy($var_name);
161 set_config('cache_last_gc', time(), true);
165 * Get saved cache object
167 function get($var_name)
169 if ($var_name[0] == '_')
173 if (!$this->_exists($var_name))
178 return $this->_read('data' . $var_name);
182 return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
187 * Put data into cache
189 function put($var_name, $var, $ttl = 31536000)
191 if ($var_name[0] == '_')
193 $this->_write('data' . $var_name, $var, time() + $ttl);
197 $this->vars[$var_name] = $var;
198 $this->var_expires[$var_name] = time() + $ttl;
199 $this->is_modified = true;
208 // Purge all phpbb cache files
209 $dir = @opendir($this->cache_dir);
216 while (($entry = readdir($dir)) !== false)
218 if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
223 $this->remove_file($this->cache_dir . $entry);
228 unset($this->var_expires);
229 unset($this->sql_rowset);
230 unset($this->sql_row_pointer);
232 $this->vars = array();
233 $this->var_expires = array();
234 $this->sql_rowset = array();
235 $this->sql_row_pointer = array();
237 $this->is_modified = false;
243 function destroy($var_name, $table = '')
247 if ($var_name == 'sql' && !empty($table))
249 if (!is_array($table))
251 $table = array($table);
254 $dir = @opendir($this->cache_dir);
261 while (($entry = readdir($dir)) !== false)
263 if (strpos($entry, 'sql_') !== 0)
268 if (!($handle = @fopen($this->cache_dir . $entry, 'rb')))
273 // Skip the PHP header
279 // Grab the query, remove the LF
280 $query = substr(fgets($handle), 0, -1);
284 foreach ($table as $check_table)
286 // Better catch partial table names than no table names. ;)
287 if (strpos($query, $check_table) !== false)
289 $this->remove_file($this->cache_dir . $entry);
299 if (!$this->_exists($var_name))
304 if ($var_name[0] == '_')
306 $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true);
308 else if (isset($this->vars[$var_name]))
310 $this->is_modified = true;
311 unset($this->vars[$var_name]);
312 unset($this->var_expires[$var_name]);
314 // We save here to let the following cache hits succeed
320 * Check if a given cache entry exist
322 function _exists($var_name)
324 if ($var_name[0] == '_')
327 return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
331 if (!sizeof($this->vars))
336 if (!isset($this->var_expires[$var_name]))
341 return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
346 * Load cached sql query
348 function sql_load($query)
350 // Remove extra spaces and tabs
351 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
353 if (($rowset = $this->_read('sql_' . md5($query))) === false)
358 $query_id = sizeof($this->sql_rowset);
359 $this->sql_rowset[$query_id] = $rowset;
360 $this->sql_row_pointer[$query_id] = 0;
368 function sql_save($query, &$query_result, $ttl)
372 // Remove extra spaces and tabs
373 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
375 $query_id = sizeof($this->sql_rowset);
376 $this->sql_rowset[$query_id] = array();
377 $this->sql_row_pointer[$query_id] = 0;
379 while ($row = $db->sql_fetchrow($query_result))
381 $this->sql_rowset[$query_id][] = $row;
383 $db->sql_freeresult($query_result);
385 if ($this->_write('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl + time(), $query))
387 $query_result = $query_id;
392 * Ceck if a given sql query exist in cache
394 function sql_exists($query_id)
396 return isset($this->sql_rowset[$query_id]);
400 * Fetch row from cache (database)
402 function sql_fetchrow($query_id)
404 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
406 return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
413 * Fetch a field from the current row of a cached database result (database)
415 function sql_fetchfield($query_id, $field)
417 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
419 return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++][$field] : false;
426 * Seek a specific row in an a cached database result (database)
428 function sql_rowseek($rownum, $query_id)
430 if ($rownum >= sizeof($this->sql_rowset[$query_id]))
435 $this->sql_row_pointer[$query_id] = $rownum;
440 * Free memory used for a cached database result (database)
442 function sql_freeresult($query_id)
444 if (!isset($this->sql_rowset[$query_id]))
449 unset($this->sql_rowset[$query_id]);
450 unset($this->sql_row_pointer[$query_id]);
456 * Read cached data from a specified file
459 * @param string $filename Filename to write
460 * @return mixed False if an error was encountered, otherwise the data type of the cached data
462 function _read($filename)
466 $file = "{$this->cache_dir}$filename.$phpEx";
468 $type = substr($filename, 0, strpos($filename, '_'));
470 if (!file_exists($file))
475 if (!($handle = @fopen($file, 'rb')))
480 // Skip the PHP header
483 if ($filename == 'data_global')
485 $this->vars = $this->var_expires = array();
489 while (($expires = (int) fgets($handle)) && !feof($handle))
491 // Number of bytes of data
492 $bytes = substr(fgets($handle), 0, -1);
494 if (!is_numeric($bytes) || ($bytes = (int) $bytes) === 0)
496 // We cannot process the file without a valid number of bytes
500 $this->vars = $this->var_expires = array();
501 $this->is_modified = false;
503 $this->remove_file($file);
508 if ($time >= $expires)
510 fseek($handle, $bytes, SEEK_CUR);
515 $var_name = substr(fgets($handle), 0, -1);
517 // Read the length of bytes that consists of data.
518 $data = fread($handle, $bytes - strlen($var_name));
519 $data = @unserialize($data);
521 // Don't use the data if it was invalid
524 $this->vars[$var_name] = $data;
525 $this->var_expires[$var_name] = $expires;
534 $this->is_modified = false;
543 while (($buffer = fgets($handle)) && !feof($handle))
545 $buffer = substr($buffer, 0, -1); // Remove the LF
547 // $buffer is only used to read integers
548 // if it is non numeric we have an invalid
549 // cache file, which we will now remove.
550 if (!is_numeric($buffer))
557 $expires = (int) $buffer;
559 if (time() >= $expires)
572 $bytes = (int) $buffer;
574 // Never should have 0 bytes
580 // Grab the serialized data
581 $data = fread($handle, $bytes);
583 // Read 1 byte, to trigger EOF
588 // Somebody tampered with our data
595 // Something went wrong
602 // unserialize if we got some data
603 $data = ($data !== false) ? @unserialize($data) : $data;
607 $this->remove_file($file);
616 * Write cache data to a specified file
618 * 'data_global' is a special case and the generated format is different for this file:
622 * (length of var and serialised data)
628 * The other files have a similar format:
632 * (query) [SQL files only]
633 * (length of serialised data)
638 * @param string $filename Filename to write
639 * @param mixed $data Data to store
640 * @param int $expires Timestamp when the data expires
641 * @param string $query Query when caching SQL queries
642 * @return bool True if the file was successfully created, otherwise false
644 function _write($filename, $data = null, $expires = 0, $query = '')
648 $file = "{$this->cache_dir}$filename.$phpEx";
650 if ($handle = @fopen($file, 'wb'))
652 @flock($handle, LOCK_EX);
655 fwrite($handle, '<' . '?php exit; ?' . '>');
657 if ($filename == 'data_global')
659 // Global data is a different format
660 foreach ($this->vars as $var => $data)
662 if (strpos($var, "\r") !== false || strpos($var, "\n") !== false)
664 // CR/LF would cause fgets() to read the cache file incorrectly
665 // do not cache test entries, they probably won't be read back
666 // the cache keys should really be alphanumeric with a few symbols.
669 $data = serialize($data);
671 // Write out the expiration time
672 fwrite($handle, "\n" . $this->var_expires[$var] . "\n");
674 // Length of the remaining data for this var (ignoring two LF's)
675 fwrite($handle, strlen($data . $var) . "\n");
676 fwrite($handle, $var . "\n");
677 fwrite($handle, $data);
682 fwrite($handle, "\n" . $expires . "\n");
684 if (strpos($filename, 'sql_') === 0)
686 fwrite($handle, $query . "\n");
688 $data = serialize($data);
690 fwrite($handle, strlen($data) . "\n");
691 fwrite($handle, $data);
694 @flock($handle, LOCK_UN);
697 if (!function_exists('phpbb_chmod'))
699 global $phpbb_root_path;
700 include($phpbb_root_path . 'includes/functions.' . $phpEx);
703 phpbb_chmod($file, CHMOD_READ | CHMOD_WRITE);
712 * Removes/unlinks file
714 function remove_file($filename, $check = false)
716 if (!function_exists('phpbb_is_writable'))
718 global $phpbb_root_path, $phpEx;
719 include($phpbb_root_path . 'includes/functions.' . $phpEx);
722 if ($check && !phpbb_is_writable($this->cache_dir))
724 // E_USER_ERROR - not using language entry - intended.
725 trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
728 return @unlink($filename);