1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/acm/acm_memory.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,439 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package acm
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 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 Abstract Memory Class
1.24 +* @package acm
1.25 +*/
1.26 +class acm_memory
1.27 +{
1.28 + var $key_prefix;
1.29 +
1.30 + var $vars = array();
1.31 + var $is_modified = false;
1.32 +
1.33 + var $sql_rowset = array();
1.34 + var $sql_row_pointer = array();
1.35 + var $cache_dir = '';
1.36 +
1.37 + /**
1.38 + * Set cache path
1.39 + */
1.40 + function acm_memory()
1.41 + {
1.42 + global $phpbb_root_path, $dbname, $table_prefix;
1.43 +
1.44 + $this->cache_dir = $phpbb_root_path . 'cache/';
1.45 + $this->key_prefix = substr(md5($dbname . $table_prefix), 0, 8) . '_';
1.46 +
1.47 + if (!isset($this->extension) || !extension_loaded($this->extension))
1.48 + {
1.49 + global $acm_type;
1.50 +
1.51 + trigger_error("Could not find required extension [{$this->extension}] for the ACM module $acm_type.", E_USER_ERROR);
1.52 + }
1.53 +
1.54 + if (isset($this->function) && !function_exists($this->function))
1.55 + {
1.56 + global $acm_type;
1.57 +
1.58 + trigger_error("The required function [{$this->function}] is not available for the ACM module $acm_type.", E_USER_ERROR);
1.59 + }
1.60 + }
1.61 +
1.62 + /**
1.63 + * Load global cache
1.64 + */
1.65 + function load()
1.66 + {
1.67 + // grab the global cache
1.68 + $this->vars = $this->_read('global');
1.69 +
1.70 + if ($this->vars !== false)
1.71 + {
1.72 + return true;
1.73 + }
1.74 +
1.75 + return false;
1.76 + }
1.77 +
1.78 + /**
1.79 + * Unload cache object
1.80 + */
1.81 + function unload()
1.82 + {
1.83 + $this->save();
1.84 + unset($this->vars);
1.85 + unset($this->sql_rowset);
1.86 + unset($this->sql_row_pointer);
1.87 +
1.88 + $this->vars = array();
1.89 + $this->sql_rowset = array();
1.90 + $this->sql_row_pointer = array();
1.91 + }
1.92 +
1.93 + /**
1.94 + * Save modified objects
1.95 + */
1.96 + function save()
1.97 + {
1.98 + if (!$this->is_modified)
1.99 + {
1.100 + return;
1.101 + }
1.102 +
1.103 + $this->_write('global', $this->vars, 2592000);
1.104 +
1.105 + $this->is_modified = false;
1.106 + }
1.107 +
1.108 + /**
1.109 + * Tidy cache
1.110 + */
1.111 + function tidy()
1.112 + {
1.113 + // cache has auto GC, no need to have any code here :)
1.114 +
1.115 + set_config('cache_last_gc', time(), true);
1.116 + }
1.117 +
1.118 + /**
1.119 + * Get saved cache object
1.120 + */
1.121 + function get($var_name)
1.122 + {
1.123 + if ($var_name[0] == '_')
1.124 + {
1.125 + if (!$this->_exists($var_name))
1.126 + {
1.127 + return false;
1.128 + }
1.129 +
1.130 + return $this->_read($var_name);
1.131 + }
1.132 + else
1.133 + {
1.134 + return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
1.135 + }
1.136 + }
1.137 +
1.138 + /**
1.139 + * Put data into cache
1.140 + */
1.141 + function put($var_name, $var, $ttl = 2592000)
1.142 + {
1.143 + if ($var_name[0] == '_')
1.144 + {
1.145 + $this->_write($var_name, $var, $ttl);
1.146 + }
1.147 + else
1.148 + {
1.149 + $this->vars[$var_name] = $var;
1.150 + $this->is_modified = true;
1.151 + }
1.152 + }
1.153 +
1.154 + /**
1.155 + * Purge cache data
1.156 + */
1.157 + function purge()
1.158 + {
1.159 + // Purge all phpbb cache files
1.160 + $dir = @opendir($this->cache_dir);
1.161 +
1.162 + if (!$dir)
1.163 + {
1.164 + return;
1.165 + }
1.166 +
1.167 + while (($entry = readdir($dir)) !== false)
1.168 + {
1.169 + if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
1.170 + {
1.171 + continue;
1.172 + }
1.173 +
1.174 + $this->remove_file($this->cache_dir . $entry);
1.175 + }
1.176 + closedir($dir);
1.177 +
1.178 + unset($this->vars);
1.179 + unset($this->sql_rowset);
1.180 + unset($this->sql_row_pointer);
1.181 +
1.182 + $this->vars = array();
1.183 + $this->sql_rowset = array();
1.184 + $this->sql_row_pointer = array();
1.185 +
1.186 + $this->is_modified = false;
1.187 + }
1.188 +
1.189 +
1.190 + /**
1.191 + * Destroy cache data
1.192 + */
1.193 + function destroy($var_name, $table = '')
1.194 + {
1.195 + if ($var_name == 'sql' && !empty($table))
1.196 + {
1.197 + if (!is_array($table))
1.198 + {
1.199 + $table = array($table);
1.200 + }
1.201 +
1.202 + foreach ($table as $table_name)
1.203 + {
1.204 + // gives us the md5s that we want
1.205 + $temp = $this->_read('sql_' . $table_name);
1.206 +
1.207 + if ($temp === false)
1.208 + {
1.209 + continue;
1.210 + }
1.211 +
1.212 + // delete each query ref
1.213 + foreach ($temp as $md5_id => $void)
1.214 + {
1.215 + $this->_delete('sql_' . $md5_id);
1.216 + }
1.217 +
1.218 + // delete the table ref
1.219 + $this->_delete('sql_' . $table_name);
1.220 + }
1.221 +
1.222 + return;
1.223 + }
1.224 +
1.225 + if (!$this->_exists($var_name))
1.226 + {
1.227 + return;
1.228 + }
1.229 +
1.230 + if ($var_name[0] == '_')
1.231 + {
1.232 + $this->_delete($var_name);
1.233 + }
1.234 + else if (isset($this->vars[$var_name]))
1.235 + {
1.236 + $this->is_modified = true;
1.237 + unset($this->vars[$var_name]);
1.238 +
1.239 + // We save here to let the following cache hits succeed
1.240 + $this->save();
1.241 + }
1.242 + }
1.243 +
1.244 + /**
1.245 + * Check if a given cache entry exist
1.246 + */
1.247 + function _exists($var_name)
1.248 + {
1.249 + if ($var_name[0] == '_')
1.250 + {
1.251 + return $this->_isset($var_name);
1.252 + }
1.253 + else
1.254 + {
1.255 + if (!sizeof($this->vars))
1.256 + {
1.257 + $this->load();
1.258 + }
1.259 +
1.260 + return isset($this->vars[$var_name]);
1.261 + }
1.262 + }
1.263 +
1.264 + /**
1.265 + * Load cached sql query
1.266 + */
1.267 + function sql_load($query)
1.268 + {
1.269 + // Remove extra spaces and tabs
1.270 + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
1.271 + $query_id = sizeof($this->sql_rowset);
1.272 +
1.273 + if (($result = $this->_read('sql_' . md5($query))) === false)
1.274 + {
1.275 + return false;
1.276 + }
1.277 +
1.278 + $this->sql_rowset[$query_id] = $result;
1.279 + $this->sql_row_pointer[$query_id] = 0;
1.280 +
1.281 + return $query_id;
1.282 + }
1.283 +
1.284 + /**
1.285 + * Save sql query
1.286 + */
1.287 + function sql_save($query, &$query_result, $ttl)
1.288 + {
1.289 + global $db;
1.290 +
1.291 + // Remove extra spaces and tabs
1.292 + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
1.293 + $hash = md5($query);
1.294 +
1.295 + // determine which tables this query belongs to
1.296 + // Some queries use backticks, namely the get_database_size() query
1.297 + // don't check for conformity, the SQL would error and not reach here.
1.298 + if (!preg_match('/FROM \\(?(`?\\w+`?(?: \\w+)?(?:, ?`?\\w+`?(?: \\w+)?)*)\\)?/', $query, $regs))
1.299 + {
1.300 + // Bail out if the match fails.
1.301 + return;
1.302 + }
1.303 + $tables = array_map('trim', explode(',', $regs[1]));
1.304 +
1.305 + foreach ($tables as $table_name)
1.306 + {
1.307 + // Remove backticks
1.308 + $table_name = ($table_name[0] == '`') ? substr($table_name, 1, -1) : $table_name;
1.309 +
1.310 + if (($pos = strpos($table_name, ' ')) !== false)
1.311 + {
1.312 + $table_name = substr($table_name, 0, $pos);
1.313 + }
1.314 +
1.315 + $temp = $this->_read('sql_' . $table_name);
1.316 +
1.317 + if ($temp === false)
1.318 + {
1.319 + $temp = array();
1.320 + }
1.321 +
1.322 + $temp[$hash] = true;
1.323 +
1.324 + // This must never expire
1.325 + $this->_write('sql_' . $table_name, $temp, 0);
1.326 + }
1.327 +
1.328 + // store them in the right place
1.329 + $query_id = sizeof($this->sql_rowset);
1.330 + $this->sql_rowset[$query_id] = array();
1.331 + $this->sql_row_pointer[$query_id] = 0;
1.332 +
1.333 + while ($row = $db->sql_fetchrow($query_result))
1.334 + {
1.335 + $this->sql_rowset[$query_id][] = $row;
1.336 + }
1.337 + $db->sql_freeresult($query_result);
1.338 +
1.339 + $this->_write('sql_' . $hash, $this->sql_rowset[$query_id], $ttl);
1.340 +
1.341 + $query_result = $query_id;
1.342 + }
1.343 +
1.344 + /**
1.345 + * Ceck if a given sql query exist in cache
1.346 + */
1.347 + function sql_exists($query_id)
1.348 + {
1.349 + return isset($this->sql_rowset[$query_id]);
1.350 + }
1.351 +
1.352 + /**
1.353 + * Fetch row from cache (database)
1.354 + */
1.355 + function sql_fetchrow($query_id)
1.356 + {
1.357 + if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
1.358 + {
1.359 + return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
1.360 + }
1.361 +
1.362 + return false;
1.363 + }
1.364 +
1.365 + /**
1.366 + * Fetch a field from the current row of a cached database result (database)
1.367 + */
1.368 + function sql_fetchfield($query_id, $field)
1.369 + {
1.370 + if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
1.371 + {
1.372 + 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.373 + }
1.374 +
1.375 + return false;
1.376 + }
1.377 +
1.378 + /**
1.379 + * Seek a specific row in an a cached database result (database)
1.380 + */
1.381 + function sql_rowseek($rownum, $query_id)
1.382 + {
1.383 + if ($rownum >= sizeof($this->sql_rowset[$query_id]))
1.384 + {
1.385 + return false;
1.386 + }
1.387 +
1.388 + $this->sql_row_pointer[$query_id] = $rownum;
1.389 + return true;
1.390 + }
1.391 +
1.392 + /**
1.393 + * Free memory used for a cached database result (database)
1.394 + */
1.395 + function sql_freeresult($query_id)
1.396 + {
1.397 + if (!isset($this->sql_rowset[$query_id]))
1.398 + {
1.399 + return false;
1.400 + }
1.401 +
1.402 + unset($this->sql_rowset[$query_id]);
1.403 + unset($this->sql_row_pointer[$query_id]);
1.404 +
1.405 + return true;
1.406 + }
1.407 +
1.408 + /**
1.409 + * Removes/unlinks file
1.410 + */
1.411 + function remove_file($filename, $check = false)
1.412 + {
1.413 + if (!function_exists('phpbb_is_writable'))
1.414 + {
1.415 + global $phpbb_root_path, $phpEx;
1.416 + include($phpbb_root_path . 'includes/functions.' . $phpEx);
1.417 + }
1.418 +
1.419 + if ($check && !phpbb_is_writable($this->cache_dir))
1.420 + {
1.421 + // E_USER_ERROR - not using language entry - intended.
1.422 + trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
1.423 + }
1.424 +
1.425 + return @unlink($filename);
1.426 + }
1.427 +
1.428 + /**
1.429 + * Check if a cache var exists
1.430 + *
1.431 + * @access protected
1.432 + * @param string $var Cache key
1.433 + * @return bool True if it exists, otherwise false
1.434 + */
1.435 + function _isset($var)
1.436 + {
1.437 + // Most caches don't need to check
1.438 + return true;
1.439 + }
1.440 +}
1.441 +
1.442 +?>
1.443 \ No newline at end of file