1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/acm/acm_file.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,732 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package acm
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005, 2009 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* ACM File Based Caching
1.24 +* @package acm
1.25 +*/
1.26 +class acm
1.27 +{
1.28 + var $vars = array();
1.29 + var $var_expires = array();
1.30 + var $is_modified = false;
1.31 +
1.32 + var $sql_rowset = array();
1.33 + var $sql_row_pointer = array();
1.34 + var $cache_dir = '';
1.35 +
1.36 + /**
1.37 + * Set cache path
1.38 + */
1.39 + function acm()
1.40 + {
1.41 + global $phpbb_root_path;
1.42 + $this->cache_dir = $phpbb_root_path . 'cache/';
1.43 + }
1.44 +
1.45 + /**
1.46 + * Load global cache
1.47 + */
1.48 + function load()
1.49 + {
1.50 + return $this->_read('data_global');
1.51 + }
1.52 +
1.53 + /**
1.54 + * Unload cache object
1.55 + */
1.56 + function unload()
1.57 + {
1.58 + $this->save();
1.59 + unset($this->vars);
1.60 + unset($this->var_expires);
1.61 + unset($this->sql_rowset);
1.62 + unset($this->sql_row_pointer);
1.63 +
1.64 + $this->vars = array();
1.65 + $this->var_expires = array();
1.66 + $this->sql_rowset = array();
1.67 + $this->sql_row_pointer = array();
1.68 + }
1.69 +
1.70 + /**
1.71 + * Save modified objects
1.72 + */
1.73 + function save()
1.74 + {
1.75 + if (!$this->is_modified)
1.76 + {
1.77 + return;
1.78 + }
1.79 +
1.80 + global $phpEx;
1.81 +
1.82 + if (!$this->_write('data_global'))
1.83 + {
1.84 + if (!function_exists('phpbb_is_writable'))
1.85 + {
1.86 + global $phpbb_root_path;
1.87 + include($phpbb_root_path . 'includes/functions.' . $phpEx);
1.88 + }
1.89 +
1.90 + // Now, this occurred how often? ... phew, just tell the user then...
1.91 + if (!phpbb_is_writable($this->cache_dir))
1.92 + {
1.93 + // We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload())
1.94 + die('Fatal: ' . $this->cache_dir . ' is NOT writable.');
1.95 + exit;
1.96 + }
1.97 +
1.98 + die('Fatal: Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx);
1.99 + exit;
1.100 + }
1.101 +
1.102 + $this->is_modified = false;
1.103 + }
1.104 +
1.105 + /**
1.106 + * Tidy cache
1.107 + */
1.108 + function tidy()
1.109 + {
1.110 + global $phpEx;
1.111 +
1.112 + $dir = @opendir($this->cache_dir);
1.113 +
1.114 + if (!$dir)
1.115 + {
1.116 + return;
1.117 + }
1.118 +
1.119 + $time = time();
1.120 +
1.121 + while (($entry = readdir($dir)) !== false)
1.122 + {
1.123 + if (!preg_match('/^(sql_|data_(?!global))/', $entry))
1.124 + {
1.125 + continue;
1.126 + }
1.127 +
1.128 + if (!($handle = @fopen($this->cache_dir . $entry, 'rb')))
1.129 + {
1.130 + continue;
1.131 + }
1.132 +
1.133 + // Skip the PHP header
1.134 + fgets($handle);
1.135 +
1.136 + // Skip expiration
1.137 + $expires = (int) fgets($handle);
1.138 +
1.139 + fclose($handle);
1.140 +
1.141 + if ($time >= $expires)
1.142 + {
1.143 + $this->remove_file($this->cache_dir . $entry);
1.144 + }
1.145 + }
1.146 + closedir($dir);
1.147 +
1.148 + if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
1.149 + {
1.150 + if (!sizeof($this->vars))
1.151 + {
1.152 + $this->load();
1.153 + }
1.154 +
1.155 + foreach ($this->var_expires as $var_name => $expires)
1.156 + {
1.157 + if ($time >= $expires)
1.158 + {
1.159 + $this->destroy($var_name);
1.160 + }
1.161 + }
1.162 + }
1.163 +
1.164 + set_config('cache_last_gc', time(), true);
1.165 + }
1.166 +
1.167 + /**
1.168 + * Get saved cache object
1.169 + */
1.170 + function get($var_name)
1.171 + {
1.172 + if ($var_name[0] == '_')
1.173 + {
1.174 + global $phpEx;
1.175 +
1.176 + if (!$this->_exists($var_name))
1.177 + {
1.178 + return false;
1.179 + }
1.180 +
1.181 + return $this->_read('data' . $var_name);
1.182 + }
1.183 + else
1.184 + {
1.185 + return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
1.186 + }
1.187 + }
1.188 +
1.189 + /**
1.190 + * Put data into cache
1.191 + */
1.192 + function put($var_name, $var, $ttl = 31536000)
1.193 + {
1.194 + if ($var_name[0] == '_')
1.195 + {
1.196 + $this->_write('data' . $var_name, $var, time() + $ttl);
1.197 + }
1.198 + else
1.199 + {
1.200 + $this->vars[$var_name] = $var;
1.201 + $this->var_expires[$var_name] = time() + $ttl;
1.202 + $this->is_modified = true;
1.203 + }
1.204 + }
1.205 +
1.206 + /**
1.207 + * Purge cache data
1.208 + */
1.209 + function purge()
1.210 + {
1.211 + // Purge all phpbb cache files
1.212 + $dir = @opendir($this->cache_dir);
1.213 +
1.214 + if (!$dir)
1.215 + {
1.216 + return;
1.217 + }
1.218 +
1.219 + while (($entry = readdir($dir)) !== false)
1.220 + {
1.221 + if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
1.222 + {
1.223 + continue;
1.224 + }
1.225 +
1.226 + $this->remove_file($this->cache_dir . $entry);
1.227 + }
1.228 + closedir($dir);
1.229 +
1.230 + unset($this->vars);
1.231 + unset($this->var_expires);
1.232 + unset($this->sql_rowset);
1.233 + unset($this->sql_row_pointer);
1.234 +
1.235 + $this->vars = array();
1.236 + $this->var_expires = array();
1.237 + $this->sql_rowset = array();
1.238 + $this->sql_row_pointer = array();
1.239 +
1.240 + $this->is_modified = false;
1.241 + }
1.242 +
1.243 + /**
1.244 + * Destroy cache data
1.245 + */
1.246 + function destroy($var_name, $table = '')
1.247 + {
1.248 + global $phpEx;
1.249 +
1.250 + if ($var_name == 'sql' && !empty($table))
1.251 + {
1.252 + if (!is_array($table))
1.253 + {
1.254 + $table = array($table);
1.255 + }
1.256 +
1.257 + $dir = @opendir($this->cache_dir);
1.258 +
1.259 + if (!$dir)
1.260 + {
1.261 + return;
1.262 + }
1.263 +
1.264 + while (($entry = readdir($dir)) !== false)
1.265 + {
1.266 + if (strpos($entry, 'sql_') !== 0)
1.267 + {
1.268 + continue;
1.269 + }
1.270 +
1.271 + if (!($handle = @fopen($this->cache_dir . $entry, 'rb')))
1.272 + {
1.273 + continue;
1.274 + }
1.275 +
1.276 + // Skip the PHP header
1.277 + fgets($handle);
1.278 +
1.279 + // Skip expiration
1.280 + fgets($handle);
1.281 +
1.282 + // Grab the query, remove the LF
1.283 + $query = substr(fgets($handle), 0, -1);
1.284 +
1.285 + fclose($handle);
1.286 +
1.287 + foreach ($table as $check_table)
1.288 + {
1.289 + // Better catch partial table names than no table names. ;)
1.290 + if (strpos($query, $check_table) !== false)
1.291 + {
1.292 + $this->remove_file($this->cache_dir . $entry);
1.293 + break;
1.294 + }
1.295 + }
1.296 + }
1.297 + closedir($dir);
1.298 +
1.299 + return;
1.300 + }
1.301 +
1.302 + if (!$this->_exists($var_name))
1.303 + {
1.304 + return;
1.305 + }
1.306 +
1.307 + if ($var_name[0] == '_')
1.308 + {
1.309 + $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true);
1.310 + }
1.311 + else if (isset($this->vars[$var_name]))
1.312 + {
1.313 + $this->is_modified = true;
1.314 + unset($this->vars[$var_name]);
1.315 + unset($this->var_expires[$var_name]);
1.316 +
1.317 + // We save here to let the following cache hits succeed
1.318 + $this->save();
1.319 + }
1.320 + }
1.321 +
1.322 + /**
1.323 + * Check if a given cache entry exist
1.324 + */
1.325 + function _exists($var_name)
1.326 + {
1.327 + if ($var_name[0] == '_')
1.328 + {
1.329 + global $phpEx;
1.330 + return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
1.331 + }
1.332 + else
1.333 + {
1.334 + if (!sizeof($this->vars))
1.335 + {
1.336 + $this->load();
1.337 + }
1.338 +
1.339 + if (!isset($this->var_expires[$var_name]))
1.340 + {
1.341 + return false;
1.342 + }
1.343 +
1.344 + return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
1.345 + }
1.346 + }
1.347 +
1.348 + /**
1.349 + * Load cached sql query
1.350 + */
1.351 + function sql_load($query)
1.352 + {
1.353 + // Remove extra spaces and tabs
1.354 + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
1.355 +
1.356 + if (($rowset = $this->_read('sql_' . md5($query))) === false)
1.357 + {
1.358 + return false;
1.359 + }
1.360 +
1.361 + $query_id = sizeof($this->sql_rowset);
1.362 + $this->sql_rowset[$query_id] = $rowset;
1.363 + $this->sql_row_pointer[$query_id] = 0;
1.364 +
1.365 + return $query_id;
1.366 + }
1.367 +
1.368 + /**
1.369 + * Save sql query
1.370 + */
1.371 + function sql_save($query, &$query_result, $ttl)
1.372 + {
1.373 + global $db;
1.374 +
1.375 + // Remove extra spaces and tabs
1.376 + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
1.377 +
1.378 + $query_id = sizeof($this->sql_rowset);
1.379 + $this->sql_rowset[$query_id] = array();
1.380 + $this->sql_row_pointer[$query_id] = 0;
1.381 +
1.382 + while ($row = $db->sql_fetchrow($query_result))
1.383 + {
1.384 + $this->sql_rowset[$query_id][] = $row;
1.385 + }
1.386 + $db->sql_freeresult($query_result);
1.387 +
1.388 + if ($this->_write('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl + time(), $query))
1.389 + {
1.390 + $query_result = $query_id;
1.391 + }
1.392 + }
1.393 +
1.394 + /**
1.395 + * Ceck if a given sql query exist in cache
1.396 + */
1.397 + function sql_exists($query_id)
1.398 + {
1.399 + return isset($this->sql_rowset[$query_id]);
1.400 + }
1.401 +
1.402 + /**
1.403 + * Fetch row from cache (database)
1.404 + */
1.405 + function sql_fetchrow($query_id)
1.406 + {
1.407 + if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
1.408 + {
1.409 + return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
1.410 + }
1.411 +
1.412 + return false;
1.413 + }
1.414 +
1.415 + /**
1.416 + * Fetch a field from the current row of a cached database result (database)
1.417 + */
1.418 + function sql_fetchfield($query_id, $field)
1.419 + {
1.420 + if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
1.421 + {
1.422 + 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;
1.423 + }
1.424 +
1.425 + return false;
1.426 + }
1.427 +
1.428 + /**
1.429 + * Seek a specific row in an a cached database result (database)
1.430 + */
1.431 + function sql_rowseek($rownum, $query_id)
1.432 + {
1.433 + if ($rownum >= sizeof($this->sql_rowset[$query_id]))
1.434 + {
1.435 + return false;
1.436 + }
1.437 +
1.438 + $this->sql_row_pointer[$query_id] = $rownum;
1.439 + return true;
1.440 + }
1.441 +
1.442 + /**
1.443 + * Free memory used for a cached database result (database)
1.444 + */
1.445 + function sql_freeresult($query_id)
1.446 + {
1.447 + if (!isset($this->sql_rowset[$query_id]))
1.448 + {
1.449 + return false;
1.450 + }
1.451 +
1.452 + unset($this->sql_rowset[$query_id]);
1.453 + unset($this->sql_row_pointer[$query_id]);
1.454 +
1.455 + return true;
1.456 + }
1.457 +
1.458 + /**
1.459 + * Read cached data from a specified file
1.460 + *
1.461 + * @access private
1.462 + * @param string $filename Filename to write
1.463 + * @return mixed False if an error was encountered, otherwise the data type of the cached data
1.464 + */
1.465 + function _read($filename)
1.466 + {
1.467 + global $phpEx;
1.468 +
1.469 + $file = "{$this->cache_dir}$filename.$phpEx";
1.470 +
1.471 + $type = substr($filename, 0, strpos($filename, '_'));
1.472 +
1.473 + if (!file_exists($file))
1.474 + {
1.475 + return false;
1.476 + }
1.477 +
1.478 + if (!($handle = @fopen($file, 'rb')))
1.479 + {
1.480 + return false;
1.481 + }
1.482 +
1.483 + // Skip the PHP header
1.484 + fgets($handle);
1.485 +
1.486 + if ($filename == 'data_global')
1.487 + {
1.488 + $this->vars = $this->var_expires = array();
1.489 +
1.490 + $time = time();
1.491 +
1.492 + while (($expires = (int) fgets($handle)) && !feof($handle))
1.493 + {
1.494 + // Number of bytes of data
1.495 + $bytes = substr(fgets($handle), 0, -1);
1.496 +
1.497 + if (!is_numeric($bytes) || ($bytes = (int) $bytes) === 0)
1.498 + {
1.499 + // We cannot process the file without a valid number of bytes
1.500 + // so we discard it
1.501 + fclose($handle);
1.502 +
1.503 + $this->vars = $this->var_expires = array();
1.504 + $this->is_modified = false;
1.505 +
1.506 + $this->remove_file($file);
1.507 +
1.508 + return false;
1.509 + }
1.510 +
1.511 + if ($time >= $expires)
1.512 + {
1.513 + fseek($handle, $bytes, SEEK_CUR);
1.514 +
1.515 + continue;
1.516 + }
1.517 +
1.518 + $var_name = substr(fgets($handle), 0, -1);
1.519 +
1.520 + // Read the length of bytes that consists of data.
1.521 + $data = fread($handle, $bytes - strlen($var_name));
1.522 + $data = @unserialize($data);
1.523 +
1.524 + // Don't use the data if it was invalid
1.525 + if ($data !== false)
1.526 + {
1.527 + $this->vars[$var_name] = $data;
1.528 + $this->var_expires[$var_name] = $expires;
1.529 + }
1.530 +
1.531 + // Absorb the LF
1.532 + fgets($handle);
1.533 + }
1.534 +
1.535 + fclose($handle);
1.536 +
1.537 + $this->is_modified = false;
1.538 +
1.539 + return true;
1.540 + }
1.541 + else
1.542 + {
1.543 + $data = false;
1.544 + $line = 0;
1.545 +
1.546 + while (($buffer = fgets($handle)) && !feof($handle))
1.547 + {
1.548 + $buffer = substr($buffer, 0, -1); // Remove the LF
1.549 +
1.550 + // $buffer is only used to read integers
1.551 + // if it is non numeric we have an invalid
1.552 + // cache file, which we will now remove.
1.553 + if (!is_numeric($buffer))
1.554 + {
1.555 + break;
1.556 + }
1.557 +
1.558 + if ($line == 0)
1.559 + {
1.560 + $expires = (int) $buffer;
1.561 +
1.562 + if (time() >= $expires)
1.563 + {
1.564 + break;
1.565 + }
1.566 +
1.567 + if ($type == 'sql')
1.568 + {
1.569 + // Skip the query
1.570 + fgets($handle);
1.571 + }
1.572 + }
1.573 + else if ($line == 1)
1.574 + {
1.575 + $bytes = (int) $buffer;
1.576 +
1.577 + // Never should have 0 bytes
1.578 + if (!$bytes)
1.579 + {
1.580 + break;
1.581 + }
1.582 +
1.583 + // Grab the serialized data
1.584 + $data = fread($handle, $bytes);
1.585 +
1.586 + // Read 1 byte, to trigger EOF
1.587 + fread($handle, 1);
1.588 +
1.589 + if (!feof($handle))
1.590 + {
1.591 + // Somebody tampered with our data
1.592 + $data = false;
1.593 + }
1.594 + break;
1.595 + }
1.596 + else
1.597 + {
1.598 + // Something went wrong
1.599 + break;
1.600 + }
1.601 + $line++;
1.602 + }
1.603 + fclose($handle);
1.604 +
1.605 + // unserialize if we got some data
1.606 + $data = ($data !== false) ? @unserialize($data) : $data;
1.607 +
1.608 + if ($data === false)
1.609 + {
1.610 + $this->remove_file($file);
1.611 + return false;
1.612 + }
1.613 +
1.614 + return $data;
1.615 + }
1.616 + }
1.617 +
1.618 + /**
1.619 + * Write cache data to a specified file
1.620 + *
1.621 + * 'data_global' is a special case and the generated format is different for this file:
1.622 + * <code>
1.623 + * <?php exit; ?>
1.624 + * (expiration)
1.625 + * (length of var and serialised data)
1.626 + * (var)
1.627 + * (serialised data)
1.628 + * ... (repeat)
1.629 + * </code>
1.630 + *
1.631 + * The other files have a similar format:
1.632 + * <code>
1.633 + * <?php exit; ?>
1.634 + * (expiration)
1.635 + * (query) [SQL files only]
1.636 + * (length of serialised data)
1.637 + * (serialised data)
1.638 + * </code>
1.639 + *
1.640 + * @access private
1.641 + * @param string $filename Filename to write
1.642 + * @param mixed $data Data to store
1.643 + * @param int $expires Timestamp when the data expires
1.644 + * @param string $query Query when caching SQL queries
1.645 + * @return bool True if the file was successfully created, otherwise false
1.646 + */
1.647 + function _write($filename, $data = null, $expires = 0, $query = '')
1.648 + {
1.649 + global $phpEx;
1.650 +
1.651 + $file = "{$this->cache_dir}$filename.$phpEx";
1.652 +
1.653 + if ($handle = @fopen($file, 'wb'))
1.654 + {
1.655 + @flock($handle, LOCK_EX);
1.656 +
1.657 + // File header
1.658 + fwrite($handle, '<' . '?php exit; ?' . '>');
1.659 +
1.660 + if ($filename == 'data_global')
1.661 + {
1.662 + // Global data is a different format
1.663 + foreach ($this->vars as $var => $data)
1.664 + {
1.665 + if (strpos($var, "\r") !== false || strpos($var, "\n") !== false)
1.666 + {
1.667 + // CR/LF would cause fgets() to read the cache file incorrectly
1.668 + // do not cache test entries, they probably won't be read back
1.669 + // the cache keys should really be alphanumeric with a few symbols.
1.670 + continue;
1.671 + }
1.672 + $data = serialize($data);
1.673 +
1.674 + // Write out the expiration time
1.675 + fwrite($handle, "\n" . $this->var_expires[$var] . "\n");
1.676 +
1.677 + // Length of the remaining data for this var (ignoring two LF's)
1.678 + fwrite($handle, strlen($data . $var) . "\n");
1.679 + fwrite($handle, $var . "\n");
1.680 + fwrite($handle, $data);
1.681 + }
1.682 + }
1.683 + else
1.684 + {
1.685 + fwrite($handle, "\n" . $expires . "\n");
1.686 +
1.687 + if (strpos($filename, 'sql_') === 0)
1.688 + {
1.689 + fwrite($handle, $query . "\n");
1.690 + }
1.691 + $data = serialize($data);
1.692 +
1.693 + fwrite($handle, strlen($data) . "\n");
1.694 + fwrite($handle, $data);
1.695 + }
1.696 +
1.697 + @flock($handle, LOCK_UN);
1.698 + fclose($handle);
1.699 +
1.700 + if (!function_exists('phpbb_chmod'))
1.701 + {
1.702 + global $phpbb_root_path;
1.703 + include($phpbb_root_path . 'includes/functions.' . $phpEx);
1.704 + }
1.705 +
1.706 + phpbb_chmod($file, CHMOD_READ | CHMOD_WRITE);
1.707 +
1.708 + return true;
1.709 + }
1.710 +
1.711 + return false;
1.712 + }
1.713 +
1.714 + /**
1.715 + * Removes/unlinks file
1.716 + */
1.717 + function remove_file($filename, $check = false)
1.718 + {
1.719 + if (!function_exists('phpbb_is_writable'))
1.720 + {
1.721 + global $phpbb_root_path, $phpEx;
1.722 + include($phpbb_root_path . 'includes/functions.' . $phpEx);
1.723 + }
1.724 +
1.725 + if ($check && !phpbb_is_writable($this->cache_dir))
1.726 + {
1.727 + // E_USER_ERROR - not using language entry - intended.
1.728 + trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
1.729 + }
1.730 +
1.731 + return @unlink($filename);
1.732 + }
1.733 +}
1.734 +
1.735 +?>
1.736 \ No newline at end of file