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$
|
2009-06-05 14:51:17 +00:00
|
|
|
* @copyright (c) 2005, 2009 phpBB Group
|
2007-10-05 14:36:34 +00:00
|
|
|
* @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()
|
|
|
|
{
|
2009-06-05 14:51:17 +00:00
|
|
|
return $this->_read('data_global');
|
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;
|
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
if (!$this->_write('data_global'))
|
2006-09-24 14:43:33 +00:00
|
|
|
{
|
2010-08-19 12:30:24 -04:00
|
|
|
if (!function_exists('phpbb_is_writable'))
|
|
|
|
{
|
|
|
|
global $phpbb_root_path;
|
|
|
|
include($phpbb_root_path . 'includes/functions.' . $phpEx);
|
|
|
|
}
|
|
|
|
|
2006-09-24 14:43:33 +00:00
|
|
|
// Now, this occurred how often? ... phew, just tell the user then...
|
2010-08-10 16:11:39 +02:00
|
|
|
if (!phpbb_is_writable($this->cache_dir))
|
2006-09-24 14:43:33 +00:00
|
|
|
{
|
2009-03-01 13:37:53 +00:00
|
|
|
// We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload())
|
|
|
|
die($this->cache_dir . ' is NOT writable.');
|
|
|
|
exit;
|
2006-09-24 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 13:37:53 +00:00
|
|
|
die('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx);
|
|
|
|
exit;
|
2006-09-24 14:43:33 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
$time = time();
|
|
|
|
|
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
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
if (!($handle = @fopen($this->cache_dir . $entry, 'rb')))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the PHP header
|
|
|
|
fgets($handle);
|
|
|
|
|
|
|
|
// Skip expiration
|
|
|
|
$expires = (int) fgets($handle);
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
if ($time >= $expires)
|
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)
|
|
|
|
{
|
2009-06-05 14:51:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-28 14:36:27 +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;
|
|
|
|
}
|
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
return $this->_read('data' . $var_name);
|
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
|
|
|
{
|
2009-06-05 14:51:17 +00:00
|
|
|
$this->_write('data' . $var_name, $var, time() + $ttl);
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
if (!($handle = @fopen($this->cache_dir . $entry, 'rb')))
|
2007-07-10 17:36:59 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2007-03-03 14:56:33 +00:00
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
// Skip the PHP header
|
|
|
|
fgets($handle);
|
|
|
|
|
|
|
|
// Skip expiration
|
|
|
|
fgets($handle);
|
|
|
|
|
|
|
|
// Grab the query, remove the LF
|
|
|
|
$query = substr(fgets($handle), 0, -1);
|
|
|
|
|
|
|
|
fclose($handle);
|
2007-03-03 14:56:33 +00:00
|
|
|
|
|
|
|
foreach ($table as $check_table)
|
|
|
|
{
|
2007-07-11 15:03:06 +00:00
|
|
|
// Better catch partial table names than no table names. ;)
|
2009-06-05 14:51:17 +00:00
|
|
|
if (strpos($query, $check_table) !== false)
|
2007-03-03 14:56:33 +00:00
|
|
|
{
|
2009-06-05 14:51:17 +00:00
|
|
|
$this->remove_file($this->cache_dir . $entry);
|
2007-03-03 14:56:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2008-02-23 11:45:38 +00:00
|
|
|
$this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true);
|
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-04-02 23:13:47 +00:00
|
|
|
// Remove extra spaces and tabs
|
|
|
|
$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
|
2004-05-26 18:55:28 +00:00
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
if (($rowset = $this->_read('sql_' . md5($query))) === false)
|
2003-11-26 23:34:33 +00:00
|
|
|
{
|
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
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
$query_id = sizeof($this->sql_rowset);
|
|
|
|
$this->sql_rowset[$query_id] = $rowset;
|
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
|
|
|
{
|
2009-06-05 14:51:17 +00:00
|
|
|
global $db;
|
2003-04-02 23:13:47 +00:00
|
|
|
|
|
|
|
// Remove extra spaces and tabs
|
|
|
|
$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
|
2003-08-10 19:14:44 +00:00
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
$query_id = sizeof($this->sql_rowset);
|
|
|
|
$this->sql_rowset[$query_id] = array();
|
|
|
|
$this->sql_row_pointer[$query_id] = 0;
|
2008-11-22 18:00:45 +00:00
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
while ($row = $db->sql_fetchrow($query_result))
|
|
|
|
{
|
|
|
|
$this->sql_rowset[$query_id][] = $row;
|
|
|
|
}
|
|
|
|
$db->sql_freeresult($query_result);
|
2007-06-30 15:04:49 +00:00
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
if ($this->_write('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl + time(), $query))
|
|
|
|
{
|
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]))
|
|
|
|
{
|
2009-12-25 15:55:35 +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
|
|
|
|
2009-06-05 14:51:17 +00:00
|
|
|
/**
|
|
|
|
* Read cached data from a specified file
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $filename Filename to write
|
|
|
|
* @return mixed False if an error was encountered, otherwise the data type of the cached data
|
|
|
|
*/
|
|
|
|
function _read($filename)
|
|
|
|
{
|
|
|
|
global $phpEx;
|
|
|
|
|
|
|
|
$file = "{$this->cache_dir}$filename.$phpEx";
|
|
|
|
|
|
|
|
$type = substr($filename, 0, strpos($filename, '_'));
|
|
|
|
|
|
|
|
if (!file_exists($file))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($handle = @fopen($file, 'rb')))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the PHP header
|
|
|
|
fgets($handle);
|
|
|
|
|
|
|
|
if ($filename == 'data_global')
|
|
|
|
{
|
|
|
|
$this->vars = $this->var_expires = array();
|
|
|
|
|
|
|
|
$time = time();
|
|
|
|
|
|
|
|
while (($expires = (int) fgets($handle)) && !feof($handle))
|
|
|
|
{
|
|
|
|
// Number of bytes of data
|
|
|
|
$bytes = substr(fgets($handle), 0, -1);
|
|
|
|
|
|
|
|
if (!is_numeric($bytes) || ($bytes = (int) $bytes) === 0)
|
|
|
|
{
|
|
|
|
// We cannot process the file without a valid number of bytes
|
|
|
|
// so we discard it
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
$this->vars = $this->var_expires = array();
|
|
|
|
$this->is_modified = false;
|
|
|
|
|
|
|
|
$this->remove_file($file);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($time >= $expires)
|
|
|
|
{
|
|
|
|
fseek($handle, $bytes, SEEK_CUR);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_name = substr(fgets($handle), 0, -1);
|
|
|
|
|
|
|
|
// Read the length of bytes that consists of data.
|
|
|
|
$data = fread($handle, $bytes - strlen($var_name));
|
|
|
|
$data = @unserialize($data);
|
|
|
|
|
|
|
|
// Don't use the data if it was invalid
|
|
|
|
if ($data !== false)
|
|
|
|
{
|
|
|
|
$this->vars[$var_name] = $data;
|
|
|
|
$this->var_expires[$var_name] = $expires;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absorb the LF
|
|
|
|
fgets($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
$this->is_modified = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$data = false;
|
|
|
|
$line = 0;
|
|
|
|
|
|
|
|
while (($buffer = fgets($handle)) && !feof($handle))
|
|
|
|
{
|
|
|
|
$buffer = substr($buffer, 0, -1); // Remove the LF
|
|
|
|
|
|
|
|
// $buffer is only used to read integers
|
|
|
|
// if it is non numeric we have an invalid
|
|
|
|
// cache file, which we will now remove.
|
|
|
|
if (!is_numeric($buffer))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($line == 0)
|
|
|
|
{
|
|
|
|
$expires = (int) $buffer;
|
|
|
|
|
|
|
|
if (time() >= $expires)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type == 'sql')
|
|
|
|
{
|
|
|
|
// Skip the query
|
|
|
|
fgets($handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($line == 1)
|
|
|
|
{
|
|
|
|
$bytes = (int) $buffer;
|
|
|
|
|
|
|
|
// Never should have 0 bytes
|
|
|
|
if (!$bytes)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the serialized data
|
|
|
|
$data = fread($handle, $bytes);
|
|
|
|
|
|
|
|
// Read 1 byte, to trigger EOF
|
|
|
|
fread($handle, 1);
|
|
|
|
|
|
|
|
if (!feof($handle))
|
|
|
|
{
|
|
|
|
// Somebody tampered with our data
|
|
|
|
$data = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Something went wrong
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$line++;
|
|
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
// unserialize if we got some data
|
|
|
|
$data = ($data !== false) ? @unserialize($data) : $data;
|
|
|
|
|
|
|
|
if ($data === false)
|
|
|
|
{
|
|
|
|
$this->remove_file($file);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write cache data to a specified file
|
|
|
|
*
|
|
|
|
* 'data_global' is a special case and the generated format is different for this file:
|
|
|
|
* <code>
|
|
|
|
* <?php exit; ?>
|
|
|
|
* (expiration)
|
|
|
|
* (length of var and serialised data)
|
|
|
|
* (var)
|
|
|
|
* (serialised data)
|
|
|
|
* ... (repeat)
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* The other files have a similar format:
|
|
|
|
* <code>
|
|
|
|
* <?php exit; ?>
|
|
|
|
* (expiration)
|
|
|
|
* (query) [SQL files only]
|
|
|
|
* (length of serialised data)
|
|
|
|
* (serialised data)
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param string $filename Filename to write
|
|
|
|
* @param mixed $data Data to store
|
|
|
|
* @param int $expires Timestamp when the data expires
|
|
|
|
* @param string $query Query when caching SQL queries
|
|
|
|
* @return bool True if the file was successfully created, otherwise false
|
|
|
|
*/
|
|
|
|
function _write($filename, $data = null, $expires = 0, $query = '')
|
|
|
|
{
|
|
|
|
global $phpEx;
|
|
|
|
|
|
|
|
$file = "{$this->cache_dir}$filename.$phpEx";
|
|
|
|
|
|
|
|
if ($handle = @fopen($file, 'wb'))
|
|
|
|
{
|
|
|
|
@flock($handle, LOCK_EX);
|
|
|
|
|
|
|
|
// File header
|
|
|
|
fwrite($handle, '<' . '?php exit; ?' . '>');
|
|
|
|
|
|
|
|
if ($filename == 'data_global')
|
|
|
|
{
|
|
|
|
// Global data is a different format
|
|
|
|
foreach ($this->vars as $var => $data)
|
|
|
|
{
|
|
|
|
if (strpos($var, "\r") !== false || strpos($var, "\n") !== false)
|
|
|
|
{
|
|
|
|
// CR/LF would cause fgets() to read the cache file incorrectly
|
|
|
|
// do not cache test entries, they probably won't be read back
|
|
|
|
// the cache keys should really be alphanumeric with a few symbols.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$data = serialize($data);
|
|
|
|
|
|
|
|
// Write out the expiration time
|
|
|
|
fwrite($handle, "\n" . $this->var_expires[$var] . "\n");
|
|
|
|
|
|
|
|
// Length of the remaining data for this var (ignoring two LF's)
|
|
|
|
fwrite($handle, strlen($data . $var) . "\n");
|
|
|
|
fwrite($handle, $var . "\n");
|
|
|
|
fwrite($handle, $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fwrite($handle, "\n" . $expires . "\n");
|
|
|
|
|
|
|
|
if (strpos($filename, 'sql_') === 0)
|
|
|
|
{
|
|
|
|
fwrite($handle, $query . "\n");
|
|
|
|
}
|
|
|
|
$data = serialize($data);
|
|
|
|
|
|
|
|
fwrite($handle, strlen($data) . "\n");
|
|
|
|
fwrite($handle, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
@flock($handle, LOCK_UN);
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
if (!function_exists('phpbb_chmod'))
|
|
|
|
{
|
|
|
|
global $phpbb_root_path;
|
|
|
|
include($phpbb_root_path . 'includes/functions.' . $phpEx);
|
|
|
|
}
|
|
|
|
|
|
|
|
phpbb_chmod($file, CHMOD_READ | CHMOD_WRITE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-03 15:36:18 +00:00
|
|
|
/**
|
|
|
|
* Removes/unlinks file
|
|
|
|
*/
|
2008-02-23 11:45:38 +00:00
|
|
|
function remove_file($filename, $check = false)
|
2007-07-03 15:36:18 +00:00
|
|
|
{
|
2010-08-19 12:30:24 -04:00
|
|
|
if (!function_exists('phpbb_is_writable'))
|
|
|
|
{
|
|
|
|
global $phpbb_root_path, $phpEx;
|
|
|
|
include($phpbb_root_path . 'includes/functions.' . $phpEx);
|
|
|
|
}
|
|
|
|
|
2010-08-10 16:11:39 +02:00
|
|
|
if ($check && !phpbb_is_writable($this->cache_dir))
|
2007-07-03 15:36:18 +00:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
2008-02-23 11:45:38 +00:00
|
|
|
|
|
|
|
return @unlink($filename);
|
2007-07-03 15:36:18 +00:00
|
|
|
}
|
2003-03-19 02:04:46 +00:00
|
|
|
}
|