2003-03-19 02:04:46 +00:00
|
|
|
<?php
|
2007-10-05 14:36:34 +00:00
|
|
|
/**
|
2005-04-09 12:26:45 +00:00
|
|
|
*
|
2006-05-05 17:56:33 +00:00
|
|
|
* @package acm
|
2005-04-09 12:26:45 +00:00
|
|
|
* @version $Id$
|
2007-10-05 14:36:34 +00:00
|
|
|
* @copyright (c) 2005 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
2005-04-09 12:26:45 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-10-05 14:36:34 +00:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2005-04-09 12:26:45 +00:00
|
|
|
/**
|
|
|
|
* ACM File Based Caching
|
2006-06-13 21:06:29 +00:00
|
|
|
* @package acm
|
2005-04-09 12:26:45 +00:00
|
|
|
*/
|
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();
|
2006-08-01 16:14:14 +00:00
|
|
|
var $sql_row_pointer = array();
|
2007-03-20 03:40:18 +00:00
|
|
|
var $cache_dir = '';
|
2003-03-19 02:04:46 +00:00
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Set cache path
|
|
|
|
*/
|
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/';
|
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Load global cache
|
|
|
|
*/
|
2003-03-19 02:04:46 +00:00
|
|
|
function load()
|
|
|
|
{
|
|
|
|
global $phpEx;
|
2004-05-26 18:55:28 +00:00
|
|
|
if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
|
|
|
|
{
|
2007-07-10 17:36:59 +00:00
|
|
|
@include($this->cache_dir . 'data_global.' . $phpEx);
|
2004-05-26 18:55:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-05-26 15:04:27 +00:00
|
|
|
|
|
|
|
return true;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Unload cache object
|
|
|
|
*/
|
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);
|
2006-08-01 16:14:14 +00:00
|
|
|
unset($this->sql_row_pointer);
|
2007-11-17 12:14:27 +00:00
|
|
|
|
|
|
|
$this->vars = array();
|
|
|
|
$this->var_expires = array();
|
|
|
|
$this->sql_rowset = array();
|
|
|
|
$this->sql_row_pointer = array();
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Save modified objects
|
|
|
|
*/
|
2007-10-05 14:36:34 +00:00
|
|
|
function save()
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2003-08-10 19:14:44 +00:00
|
|
|
if (!$this->is_modified)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $phpEx;
|
|
|
|
|
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);
|
2007-01-04 16:07:38 +00:00
|
|
|
fwrite($fp, "<?php\n\$this->vars = " . var_export($this->vars, true) . ";\n\n\$this->var_expires = " . var_export($this->var_expires, true) . "\n?>");
|
2003-03-19 02:04:46 +00:00
|
|
|
@flock($fp, LOCK_UN);
|
|
|
|
fclose($fp);
|
2007-06-30 15:04:49 +00:00
|
|
|
|
|
|
|
@chmod($this->cache_dir . 'data_global.' . $phpEx, 0666);
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
2006-09-24 14:43:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Now, this occurred how often? ... phew, just tell the user then...
|
2007-05-03 14:29:22 +00:00
|
|
|
if (!@is_writable($this->cache_dir))
|
2006-09-24 14:43:33 +00:00
|
|
|
{
|
2007-05-03 14:29:22 +00:00
|
|
|
trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR);
|
2006-09-24 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Tidy cache
|
|
|
|
*/
|
2003-11-26 23:34:33 +00:00
|
|
|
function tidy()
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
global $phpEx;
|
|
|
|
|
2007-01-20 17:58:27 +00:00
|
|
|
$dir = @opendir($this->cache_dir);
|
|
|
|
|
|
|
|
if (!$dir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-28 16:46:49 +00:00
|
|
|
while (($entry = readdir($dir)) !== false)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
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;
|
2007-03-30 10:49:06 +00:00
|
|
|
@include($this->cache_dir . $entry);
|
2003-11-26 23:34:33 +00:00
|
|
|
if ($expired)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2007-07-03 15:36:18 +00:00
|
|
|
$this->remove_file($this->cache_dir . $entry);
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-20 17:58:27 +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
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Get saved cache object
|
|
|
|
*/
|
2003-11-26 23:34:33 +00:00
|
|
|
function get($var_name)
|
|
|
|
{
|
2006-10-07 17:40:07 +00:00
|
|
|
if ($var_name[0] == '_')
|
2003-11-26 23:34:33 +00:00
|
|
|
{
|
|
|
|
global $phpEx;
|
|
|
|
|
2006-01-05 12:10:31 +00:00
|
|
|
if (!$this->_exists($var_name))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-10 17:36:59 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Put data into cache
|
|
|
|
*/
|
2003-11-26 23:34:33 +00:00
|
|
|
function put($var_name, $var, $ttl = 31536000)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2006-10-07 17:40:07 +00:00
|
|
|
if ($var_name[0] == '_')
|
2003-11-26 23:34:33 +00:00
|
|
|
{
|
|
|
|
global $phpEx;
|
2003-03-19 02:04:46 +00:00
|
|
|
|
2007-01-04 16:07:38 +00:00
|
|
|
if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb'))
|
2003-11-26 23:34:33 +00:00
|
|
|
{
|
|
|
|
@flock($fp, LOCK_EX);
|
2007-01-04 16:07:38 +00:00
|
|
|
fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = " . var_export($var, true) . ";\n?>");
|
2003-11-26 23:34:33 +00:00
|
|
|
@flock($fp, LOCK_UN);
|
|
|
|
fclose($fp);
|
2007-06-30 15:04:49 +00:00
|
|
|
|
|
|
|
@chmod($this->cache_dir . "data{$var_name}.$phpEx", 0666);
|
2003-11-26 23:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-08-22 21:26:06 +00:00
|
|
|
/**
|
|
|
|
* Purge cache data
|
|
|
|
*/
|
|
|
|
function purge()
|
|
|
|
{
|
2006-09-02 13:33:06 +00:00
|
|
|
// Purge all phpbb cache files
|
2007-01-20 17:58:27 +00:00
|
|
|
$dir = @opendir($this->cache_dir);
|
|
|
|
|
|
|
|
if (!$dir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-08-22 21:26:06 +00:00
|
|
|
while (($entry = readdir($dir)) !== false)
|
|
|
|
{
|
|
|
|
if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-03 15:36:18 +00:00
|
|
|
$this->remove_file($this->cache_dir . $entry);
|
2006-08-22 21:26:06 +00:00
|
|
|
}
|
2007-01-20 17:58:27 +00:00
|
|
|
closedir($dir);
|
2006-08-22 21:26:06 +00:00
|
|
|
|
|
|
|
unset($this->vars);
|
|
|
|
unset($this->var_expires);
|
|
|
|
unset($this->sql_rowset);
|
|
|
|
unset($this->sql_row_pointer);
|
|
|
|
|
2007-11-17 12:14:27 +00:00
|
|
|
$this->vars = array();
|
|
|
|
$this->var_expires = array();
|
|
|
|
$this->sql_rowset = array();
|
|
|
|
$this->sql_row_pointer = array();
|
|
|
|
|
2006-08-22 21:26:06 +00:00
|
|
|
$this->is_modified = false;
|
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Destroy cache data
|
|
|
|
*/
|
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;
|
|
|
|
|
2003-08-12 15:46:03 +00:00
|
|
|
if ($var_name == 'sql' && !empty($table))
|
|
|
|
{
|
2007-03-03 14:56:33 +00:00
|
|
|
if (!is_array($table))
|
|
|
|
{
|
|
|
|
$table = array($table);
|
|
|
|
}
|
2003-08-12 15:46:03 +00:00
|
|
|
|
2007-01-20 17:58:27 +00:00
|
|
|
$dir = @opendir($this->cache_dir);
|
|
|
|
|
|
|
|
if (!$dir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-28 16:46:49 +00:00
|
|
|
while (($entry = readdir($dir)) !== false)
|
2003-08-12 15:46:03 +00:00
|
|
|
{
|
2006-03-15 13:03:57 +00:00
|
|
|
if (strpos($entry, 'sql_') !== 0)
|
2003-08-12 15:46:03 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-03-03 14:56:33 +00:00
|
|
|
// The following method is more failproof than simply assuming the query is on line 3 (which it should be)
|
2007-07-10 17:36:59 +00:00
|
|
|
$check_line = @file_get_contents($this->cache_dir . $entry);
|
|
|
|
|
|
|
|
if (empty($check_line))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2007-03-03 14:56:33 +00:00
|
|
|
|
|
|
|
// Now get the contents between /* and */
|
|
|
|
$check_line = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3);
|
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach ($table as $check_table)
|
|
|
|
{
|
2007-07-11 15:03:06 +00:00
|
|
|
// Better catch partial table names than no table names. ;)
|
|
|
|
if (strpos($check_line, $check_table) !== false)
|
2007-03-03 14:56:33 +00:00
|
|
|
{
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-12 15:46:03 +00:00
|
|
|
|
2007-03-03 14:56:33 +00:00
|
|
|
if ($found)
|
2003-08-12 15:46:03 +00:00
|
|
|
{
|
2007-07-03 15:36:18 +00:00
|
|
|
$this->remove_file($this->cache_dir . $entry);
|
2003-08-12 15:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-20 17:58:27 +00:00
|
|
|
closedir($dir);
|
2006-03-15 13:03:57 +00:00
|
|
|
|
|
|
|
return;
|
2003-08-12 15:46:03 +00:00
|
|
|
}
|
2006-03-15 13:03:57 +00:00
|
|
|
|
|
|
|
if (!$this->_exists($var_name))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-07 17:40:07 +00:00
|
|
|
if ($var_name[0] == '_')
|
2003-11-26 23:34:33 +00:00
|
|
|
{
|
2007-07-03 15:36:18 +00:00
|
|
|
$this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx");
|
2003-11-26 23:34:33 +00:00
|
|
|
}
|
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-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Check if a given cache entry exist
|
|
|
|
*/
|
2006-01-05 12:10:31 +00:00
|
|
|
function _exists($var_name)
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
2006-10-07 17:40:07 +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
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Load cached sql query
|
|
|
|
*/
|
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);
|
2006-03-15 13:03:57 +00:00
|
|
|
$query_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
|
|
|
{
|
2007-07-03 15:36:18 +00:00
|
|
|
$this->remove_file($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
|
|
|
|
2006-08-01 16:14:14 +00:00
|
|
|
$this->sql_row_pointer[$query_id] = 0;
|
|
|
|
|
2003-08-10 19:14:44 +00:00
|
|
|
return $query_id;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Save sql query
|
|
|
|
*/
|
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);
|
2007-06-30 15:04:49 +00:00
|
|
|
$filename = $this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx;
|
2003-04-02 23:13:47 +00:00
|
|
|
|
2007-06-30 15:04:49 +00:00
|
|
|
if ($fp = @fopen($filename, 'wb'))
|
2003-03-19 02:04:46 +00:00
|
|
|
{
|
|
|
|
@flock($fp, LOCK_EX);
|
|
|
|
|
2006-03-15 13:03:57 +00:00
|
|
|
$query_id = sizeof($this->sql_rowset);
|
2003-03-19 02:04:46 +00:00
|
|
|
$this->sql_rowset[$query_id] = array();
|
2006-08-01 16:14:14 +00:00
|
|
|
$this->sql_row_pointer[$query_id] = 0;
|
2003-03-19 02:04:46 +00:00
|
|
|
|
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
|
|
|
$db->sql_freeresult($query_result);
|
2003-03-19 02:04:46 +00:00
|
|
|
|
2007-03-03 14:56:33 +00:00
|
|
|
$file = "<?php\n\n/* " . str_replace('*/', '*\/', $query) . " */\n";
|
2007-01-04 16:07:38 +00:00
|
|
|
$file .= "\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n";
|
|
|
|
|
|
|
|
fwrite($fp, $file . "\n\$this->sql_rowset[\$query_id] = " . var_export($this->sql_rowset[$query_id], true) . ";\n?>");
|
2003-03-19 02:04:46 +00:00
|
|
|
@flock($fp, LOCK_UN);
|
|
|
|
fclose($fp);
|
2003-08-10 19:14:44 +00:00
|
|
|
|
2007-06-30 15:04:49 +00:00
|
|
|
@chmod($filename, 0666);
|
|
|
|
|
2003-08-10 19:14:44 +00:00
|
|
|
$query_result = $query_id;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Ceck if a given sql query exist in cache
|
|
|
|
*/
|
2003-03-19 02:04:46 +00:00
|
|
|
function sql_exists($query_id)
|
|
|
|
{
|
|
|
|
return isset($this->sql_rowset[$query_id]);
|
|
|
|
}
|
|
|
|
|
2006-06-13 21:06:29 +00:00
|
|
|
/**
|
|
|
|
* Fetch row from cache (database)
|
|
|
|
*/
|
2003-03-19 02:04:46 +00:00
|
|
|
function sql_fetchrow($query_id)
|
|
|
|
{
|
2006-08-01 16:14:14 +00:00
|
|
|
if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
|
|
|
|
{
|
|
|
|
return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a field from the current row of a cached database result (database)
|
|
|
|
*/
|
|
|
|
function sql_fetchfield($query_id, $field)
|
|
|
|
{
|
|
|
|
if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
|
|
|
|
{
|
2006-08-03 15:23:34 +00:00
|
|
|
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;
|
2006-08-01 16:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seek a specific row in an a cached database result (database)
|
|
|
|
*/
|
2006-09-23 11:10:37 +00:00
|
|
|
function sql_rowseek($rownum, $query_id)
|
2006-08-01 16:14:14 +00:00
|
|
|
{
|
|
|
|
if ($rownum >= sizeof($this->sql_rowset[$query_id]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sql_row_pointer[$query_id] = $rownum;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free memory used for a cached database result (database)
|
|
|
|
*/
|
|
|
|
function sql_freeresult($query_id)
|
|
|
|
{
|
|
|
|
if (!isset($this->sql_rowset[$query_id]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($this->sql_rowset[$query_id]);
|
|
|
|
unset($this->sql_row_pointer[$query_id]);
|
|
|
|
|
|
|
|
return true;
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
2007-07-03 15:36:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes/unlinks file
|
|
|
|
*/
|
|
|
|
function remove_file($filename)
|
|
|
|
{
|
|
|
|
if (!@unlink($filename))
|
|
|
|
{
|
|
|
|
// E_USER_ERROR - not using language entry - intended.
|
|
|
|
trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|
2005-04-30 14:14:08 +00:00
|
|
|
|
2003-03-19 02:04:46 +00:00
|
|
|
?>
|