5
* @version $Id: acm_file.php,v 1.55 2007/11/17 12:14:27 acydburn Exp $
6
* @copyright (c) 2005 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/';
48
if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
50
@include($this->cache_dir . 'data_global.' . $phpEx);
67
unset($this->var_expires);
68
unset($this->sql_rowset);
69
unset($this->sql_row_pointer);
71
$this->vars = array();
72
$this->var_expires = array();
73
$this->sql_rowset = array();
74
$this->sql_row_pointer = array();
78
* Save modified objects
82
if (!$this->is_modified)
89
if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb'))
92
fwrite($fp, "<?php\n\$this->vars = " . var_export($this->vars, true) . ";\n\n\$this->var_expires = " . var_export($this->var_expires, true) . "\n?>");
96
@chmod($this->cache_dir . 'data_global.' . $phpEx, 0666);
100
// Now, this occurred how often? ... phew, just tell the user then...
101
if (!@is_writable($this->cache_dir))
103
trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR);
106
trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR);
109
$this->is_modified = false;
119
$dir = @opendir($this->cache_dir);
126
while (($entry = readdir($dir)) !== false)
128
if (!preg_match('/^(sql_|data_(?!global))/', $entry))
134
@include($this->cache_dir . $entry);
137
$this->remove_file($this->cache_dir . $entry);
142
if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
144
if (!sizeof($this->vars))
149
foreach ($this->var_expires as $var_name => $expires)
151
if (time() > $expires)
153
$this->destroy($var_name);
158
set_config('cache_last_gc', time(), true);
162
* Get saved cache object
164
function get($var_name)
166
if ($var_name[0] == '_')
170
if (!$this->_exists($var_name))
175
@include($this->cache_dir . "data{$var_name}.$phpEx");
176
return (isset($data)) ? $data : false;
180
return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
185
* Put data into cache
187
function put($var_name, $var, $ttl = 31536000)
189
if ($var_name[0] == '_')
193
if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb'))
195
@flock($fp, LOCK_EX);
196
fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = " . var_export($var, true) . ";\n?>");
197
@flock($fp, LOCK_UN);
200
@chmod($this->cache_dir . "data{$var_name}.$phpEx", 0666);
205
$this->vars[$var_name] = $var;
206
$this->var_expires[$var_name] = time() + $ttl;
207
$this->is_modified = true;
216
// Purge all phpbb cache files
217
$dir = @opendir($this->cache_dir);
224
while (($entry = readdir($dir)) !== false)
226
if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
231
$this->remove_file($this->cache_dir . $entry);
236
unset($this->var_expires);
237
unset($this->sql_rowset);
238
unset($this->sql_row_pointer);
240
$this->vars = array();
241
$this->var_expires = array();
242
$this->sql_rowset = array();
243
$this->sql_row_pointer = array();
245
$this->is_modified = false;
251
function destroy($var_name, $table = '')
255
if ($var_name == 'sql' && !empty($table))
257
if (!is_array($table))
259
$table = array($table);
262
$dir = @opendir($this->cache_dir);
269
while (($entry = readdir($dir)) !== false)
271
if (strpos($entry, 'sql_') !== 0)
276
// The following method is more failproof than simply assuming the query is on line 3 (which it should be)
277
$check_line = @file_get_contents($this->cache_dir . $entry);
279
if (empty($check_line))
284
// Now get the contents between /* and */
285
$check_line = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3);
288
foreach ($table as $check_table)
290
// Better catch partial table names than no table names. ;)
291
if (strpos($check_line, $check_table) !== false)
300
$this->remove_file($this->cache_dir . $entry);
308
if (!$this->_exists($var_name))
313
if ($var_name[0] == '_')
315
$this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx");
317
else if (isset($this->vars[$var_name]))
319
$this->is_modified = true;
320
unset($this->vars[$var_name]);
321
unset($this->var_expires[$var_name]);
323
// We save here to let the following cache hits succeed
329
* Check if a given cache entry exist
331
function _exists($var_name)
333
if ($var_name[0] == '_')
336
return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
340
if (!sizeof($this->vars))
345
if (!isset($this->var_expires[$var_name]))
350
return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
355
* Load cached sql query
357
function sql_load($query)
361
// Remove extra spaces and tabs
362
$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
363
$query_id = sizeof($this->sql_rowset);
365
if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"))
370
@include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
372
if (!isset($expired))
378
$this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
382
$this->sql_row_pointer[$query_id] = 0;
390
function sql_save($query, &$query_result, $ttl)
394
// Remove extra spaces and tabs
395
$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
396
$filename = $this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx;
398
if ($fp = @fopen($filename, 'wb'))
400
@flock($fp, LOCK_EX);
402
$query_id = sizeof($this->sql_rowset);
403
$this->sql_rowset[$query_id] = array();
404
$this->sql_row_pointer[$query_id] = 0;
406
while ($row = $db->sql_fetchrow($query_result))
408
$this->sql_rowset[$query_id][] = $row;
410
$db->sql_freeresult($query_result);
412
$file = "<?php\n\n/* " . str_replace('*/', '*\/', $query) . " */\n";
413
$file .= "\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n";
415
fwrite($fp, $file . "\n\$this->sql_rowset[\$query_id] = " . var_export($this->sql_rowset[$query_id], true) . ";\n?>");
416
@flock($fp, LOCK_UN);
419
@chmod($filename, 0666);
421
$query_result = $query_id;
426
* Ceck if a given sql query exist in cache
428
function sql_exists($query_id)
430
return isset($this->sql_rowset[$query_id]);
434
* Fetch row from cache (database)
436
function sql_fetchrow($query_id)
438
if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
440
return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
447
* Fetch a field from the current row of a cached database result (database)
449
function sql_fetchfield($query_id, $field)
451
if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
453
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;
460
* Seek a specific row in an a cached database result (database)
462
function sql_rowseek($rownum, $query_id)
464
if ($rownum >= sizeof($this->sql_rowset[$query_id]))
469
$this->sql_row_pointer[$query_id] = $rownum;
474
* Free memory used for a cached database result (database)
476
function sql_freeresult($query_id)
478
if (!isset($this->sql_rowset[$query_id]))
483
unset($this->sql_rowset[$query_id]);
484
unset($this->sql_row_pointer[$query_id]);
490
* Removes/unlinks file
492
function remove_file($filename)
494
if (!@unlink($filename))
496
// E_USER_ERROR - not using language entry - intended.
497
trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
b'\\ No newline at end of file'