2003-03-19 02:04:46 +00:00
|
|
|
<?php
|
2005-04-09 12:26:45 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package acm_file
|
|
|
|
* @version $Id$
|
|
|
|
* @copyright (c) 2005 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package acm_file
|
|
|
|
* ACM File Based Caching
|
|
|
|
*/
|
2003-03-19 02:04:46 +00:00
|
|
|
class acm
|
|
|
|
{
|
2004-12-19 17:59:15 +00:00
|
|
|
var $vars = array();
|
2003-11-26 23:34:33 +00:00
|
|
|
var $var_expires = array();
|
2005-10-04 21:39:47 +00:00
|
|
|
var $is_modified = false;
|
2003-03-19 02:04:46 +00:00
|
|
|
|
|
|
|
var $sql_rowset = array();
|
|
|
|
|
2003-08-11 21:45:50 +00:00
|
|
|
function acm()
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
global $phpbb_root_path;
|
|
|
|
$this->cache_dir = $phpbb_root_path . 'cache/';
|
|
|
|
}
|
|
|
|
|
|
|
|
function load()
|
|
|
|
{
|
|
|
|
global $phpEx;
|
2004-05-26 18:55:28 +00:00
|
|
|
if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
|
|
|
|
{
|
2005-04-10 11:21:01 +00:00
|
|
|
include($this->cache_dir . 'data_global.' . $phpEx);
|
2004-05-26 18:55:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function unload()
|
|
|
|
{
|
|
|
|
$this->save();
|
|
|
|
unset($this->vars);
|
2003-11-26 23:34:33 +00:00
|
|
|
unset($this->var_expires);
|
2003-03-19 02:04:46 +00:00
|
|
|
unset($this->sql_rowset);
|
|
|
|
}
|
|
|
|
|
|
|
|
function save()
|
|
|
|
{
|
2003-08-10 19:14:44 +00:00
|
|
|
if (!$this->is_modified)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $phpEx;
|
2003-11-26 23:34:33 +00:00
|
|
|
$file = '<?php $this->vars=' . $this->format_array($this->vars) . ";\n\$this->var_expires=" . $this->format_array($this->var_expires) . ' ?>';
|
2003-03-19 02:04:46 +00:00
|
|
|
|
2003-07-17 15:16:25 +00:00
|
|
|
if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb'))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
@flock($fp, LOCK_EX);
|
|
|
|
fwrite($fp, $file);
|
|
|
|
@flock($fp, LOCK_UN);
|
|
|
|
fclose($fp);
|
|
|
|
}
|
2003-08-17 20:15:28 +00:00
|
|
|
|
2005-10-04 21:39:47 +00:00
|
|
|
$this->is_modified = false;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
function tidy()
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
global $phpEx;
|
|
|
|
|
|
|
|
$dir = opendir($this->cache_dir);
|
|
|
|
while ($entry = readdir($dir))
|
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
if (!preg_match('/^(sql_|data_(?!global))/', $entry))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-17 15:16:25 +00:00
|
|
|
|
2005-10-04 21:39:47 +00:00
|
|
|
$expired = true;
|
2003-11-26 23:34:33 +00:00
|
|
|
include($this->cache_dir . $entry);
|
|
|
|
if ($expired)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
unlink($this->cache_dir . $entry);
|
|
|
|
}
|
|
|
|
}
|
2003-08-17 20:15:28 +00:00
|
|
|
@closedir($dir);
|
2003-07-17 15:16:25 +00:00
|
|
|
|
|
|
|
if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2004-12-19 17:59:15 +00:00
|
|
|
if (!sizeof($this->vars))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
$this->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->var_expires as $var_name => $expires)
|
|
|
|
{
|
|
|
|
if (time() > $expires)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-08-10 19:14:44 +00:00
|
|
|
$this->destroy($var_name);
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-30 14:14:08 +00:00
|
|
|
|
|
|
|
set_config('cache_last_gc', time(), true);
|
2003-11-26 23:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get($var_name)
|
|
|
|
{
|
|
|
|
if ($var_name{0} == '_')
|
|
|
|
{
|
|
|
|
global $phpEx;
|
|
|
|
|
2006-01-05 12:10:31 +00:00
|
|
|
if (!$this->_exists($var_name))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
include($this->cache_dir . 'data' . $var_name . ".$phpEx");
|
2006-01-05 12:10:31 +00:00
|
|
|
return (isset($data)) ? $data : false;
|
2003-11-26 23:34:33 +00:00
|
|
|
}
|
2003-03-19 02:04:46 +00:00
|
|
|
else
|
|
|
|
{
|
2006-01-05 12:10:31 +00:00
|
|
|
return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
function put($var_name, $var, $ttl = 31536000)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
if ($var_name{0} == '_')
|
|
|
|
{
|
|
|
|
global $phpEx;
|
2003-03-19 02:04:46 +00:00
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
if ($fp = @fopen($this->cache_dir . 'data' . $var_name . ".$phpEx", 'wb'))
|
|
|
|
{
|
|
|
|
@flock($fp, LOCK_EX);
|
2005-12-04 20:25:51 +00:00
|
|
|
fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = unserialize('" . str_replace("'", "\\'", str_replace('\\', '\\\\', serialize($var))) . "');\n?>");
|
2003-11-26 23:34:33 +00:00
|
|
|
@flock($fp, LOCK_UN);
|
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->vars[$var_name] = $var;
|
|
|
|
$this->var_expires[$var_name] = time() + $ttl;
|
2005-12-04 20:25:51 +00:00
|
|
|
$this->is_modified = true;
|
2003-11-26 23:34:33 +00:00
|
|
|
}
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2003-08-12 15:46:03 +00:00
|
|
|
function destroy($var_name, $table = '')
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
global $phpEx;
|
|
|
|
|
2006-01-11 18:56:07 +00:00
|
|
|
if (!$this->_exists($var_name))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-08-12 15:46:03 +00:00
|
|
|
if ($var_name == 'sql' && !empty($table))
|
|
|
|
{
|
|
|
|
$regex = '(' . ((is_array($table)) ? implode('|', $table) : $table) . ')';
|
|
|
|
|
|
|
|
$dir = opendir($this->cache_dir);
|
|
|
|
while ($entry = readdir($dir))
|
|
|
|
{
|
|
|
|
if (substr($entry, 0, 4) != 'sql_')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fp = fopen($this->cache_dir . $entry, 'rb');
|
|
|
|
$file = fread($fp, filesize($this->cache_dir . $entry));
|
|
|
|
@fclose($fp);
|
|
|
|
|
|
|
|
if (preg_match('#/\*.*?\W' . $regex . '\W.*?\*/#s', $file, $m))
|
|
|
|
{
|
|
|
|
unlink($this->cache_dir . $entry);
|
|
|
|
}
|
|
|
|
}
|
2003-08-12 22:20:44 +00:00
|
|
|
@closedir($dir);
|
2003-08-12 15:46:03 +00:00
|
|
|
}
|
2005-12-04 20:25:51 +00:00
|
|
|
else if ($var_name{0} == '_')
|
2003-11-26 23:34:33 +00:00
|
|
|
{
|
|
|
|
@unlink($this->cache_dir . 'data' . $var_name . ".$phpEx");
|
|
|
|
}
|
2005-12-04 20:25:51 +00:00
|
|
|
else if (isset($this->vars[$var_name]))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2005-12-04 20:25:51 +00:00
|
|
|
$this->is_modified = true;
|
2003-08-10 19:14:44 +00:00
|
|
|
unset($this->vars[$var_name]);
|
2003-11-26 23:34:33 +00:00
|
|
|
unset($this->var_expires[$var_name]);
|
2006-03-09 18:32:50 +00:00
|
|
|
|
|
|
|
// We save here to let the following cache hits succeed
|
|
|
|
$this->save();
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-05 12:10:31 +00:00
|
|
|
function _exists($var_name)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
if ($var_name{0} == '_')
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
global $phpEx;
|
|
|
|
return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
2003-11-26 23:34:33 +00:00
|
|
|
else
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2004-12-19 17:59:15 +00:00
|
|
|
if (!sizeof($this->vars))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
$this->load();
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2004-05-02 13:06:57 +00:00
|
|
|
if (!isset($this->var_expires[$var_name]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
|
2003-11-26 23:34:33 +00:00
|
|
|
}
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function format_array($array)
|
|
|
|
{
|
|
|
|
$lines = array();
|
|
|
|
foreach ($array as $k => $v)
|
|
|
|
{
|
|
|
|
if (is_array($v))
|
|
|
|
{
|
|
|
|
$lines[] = "'$k'=>" . $this->format_array($v);
|
|
|
|
}
|
2005-12-04 20:25:51 +00:00
|
|
|
else if (is_int($v))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
$lines[] = "'$k'=>$v";
|
|
|
|
}
|
2005-12-04 20:25:51 +00:00
|
|
|
else if (is_bool($v))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2005-12-04 20:25:51 +00:00
|
|
|
$lines[] = "'$k'=>" . (($v) ? 'true' : 'false');
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-11-26 23:34:33 +00:00
|
|
|
$lines[] = "'$k'=>'" . str_replace("'", "\\'", str_replace('\\', '\\\\', $v)) . "'";
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'array(' . implode(',', $lines) . ')';
|
|
|
|
}
|
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
function sql_load($query)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-08-10 19:14:44 +00:00
|
|
|
global $phpEx;
|
2003-03-19 02:04:46 +00:00
|
|
|
|
2003-04-02 23:13:47 +00:00
|
|
|
// Remove extra spaces and tabs
|
|
|
|
$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
|
2005-04-10 18:07:12 +00:00
|
|
|
$query_id = 'Cache id #' . sizeof($this->sql_rowset);
|
2003-04-02 23:13:47 +00:00
|
|
|
|
2004-05-26 18:55:28 +00:00
|
|
|
if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-05-02 13:06:57 +00:00
|
|
|
@include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
|
2004-05-26 18:55:28 +00:00
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
if (!isset($expired))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2005-12-04 20:25:51 +00:00
|
|
|
return false;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
2005-12-04 20:25:51 +00:00
|
|
|
else if ($expired)
|
2003-11-26 23:34:33 +00:00
|
|
|
{
|
|
|
|
unlink($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
|
2005-12-04 20:25:51 +00:00
|
|
|
return false;
|
2003-11-26 23:34:33 +00:00
|
|
|
}
|
2003-08-10 19:14:44 +00:00
|
|
|
|
|
|
|
return $query_id;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
function sql_save($query, &$query_result, $ttl)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
global $db, $phpEx;
|
2003-04-02 23:13:47 +00:00
|
|
|
|
|
|
|
// Remove extra spaces and tabs
|
|
|
|
$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
|
|
|
|
|
2003-07-17 15:16:25 +00:00
|
|
|
if ($fp = @fopen($this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx, 'wb'))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
@flock($fp, LOCK_EX);
|
|
|
|
|
|
|
|
$lines = array();
|
2005-01-02 19:50:07 +00:00
|
|
|
$query_id = 'Cache id #' . sizeof($this->sql_rowset);
|
2003-03-19 02:04:46 +00:00
|
|
|
$this->sql_rowset[$query_id] = array();
|
|
|
|
|
2003-08-10 19:14:44 +00:00
|
|
|
while ($row = $db->sql_fetchrow($query_result))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
$this->sql_rowset[$query_id][] = $row;
|
|
|
|
|
2003-11-26 23:34:33 +00:00
|
|
|
$lines[] = "unserialize('" . str_replace("'", "\\'", str_replace('\\', '\\\\', serialize($row))) . "')";
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
2003-11-26 23:34:33 +00:00
|
|
|
$db->sql_freeresult($query_result);
|
2003-03-19 02:04:46 +00:00
|
|
|
|
2005-12-04 20:25:51 +00:00
|
|
|
fwrite($fp, "<?php\n\n/*\n$query\n*/\n\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$this->sql_rowset[\$query_id] = array(" . implode(',', $lines) . ') ?>');
|
2003-03-19 02:04:46 +00:00
|
|
|
@flock($fp, LOCK_UN);
|
|
|
|
fclose($fp);
|
2003-08-10 19:14:44 +00:00
|
|
|
|
|
|
|
$query_result = $query_id;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sql_exists($query_id)
|
|
|
|
{
|
|
|
|
return isset($this->sql_rowset[$query_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sql_fetchrow($query_id)
|
|
|
|
{
|
|
|
|
return array_shift($this->sql_rowset[$query_id]);
|
|
|
|
}
|
|
|
|
}
|
2005-04-30 14:14:08 +00:00
|
|
|
|
2003-03-19 02:04:46 +00:00
|
|
|
?>
|