includes/acm/acm_memory.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:03:48 +0400
changeset 3 2e6be5f7e4cf
parent 0 ceef6d64c21d
permissions -rw-r--r--
HG ignore
     1 <?php
     2 /**
     3 *
     4 * @package acm
     5 * @version $Id$
     6 * @copyright (c) 2005 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 * @ignore
    13 */
    14 if (!defined('IN_PHPBB'))
    15 {
    16 	exit;
    17 }
    18 
    19 /**
    20 * ACM Abstract Memory Class
    21 * @package acm
    22 */
    23 class acm_memory
    24 {
    25 	var $key_prefix;
    26 
    27 	var $vars = array();
    28 	var $is_modified = false;
    29 
    30 	var $sql_rowset = array();
    31 	var $sql_row_pointer = array();
    32 	var $cache_dir = '';
    33 
    34 	/**
    35 	* Set cache path
    36 	*/
    37 	function acm_memory()
    38 	{
    39 		global $phpbb_root_path, $dbname, $table_prefix;
    40 
    41 		$this->cache_dir	= $phpbb_root_path . 'cache/';
    42 		$this->key_prefix	= substr(md5($dbname . $table_prefix), 0, 8) . '_';
    43 
    44 		if (!isset($this->extension) || !extension_loaded($this->extension))
    45 		{
    46 			global $acm_type;
    47 
    48 			trigger_error("Could not find required extension [{$this->extension}] for the ACM module $acm_type.", E_USER_ERROR);
    49 		}
    50 
    51 		if (isset($this->function) && !function_exists($this->function))
    52 		{
    53 			global $acm_type;
    54 
    55 			trigger_error("The required function [{$this->function}] is not available for the ACM module $acm_type.", E_USER_ERROR);
    56 		}
    57 	}
    58 
    59 	/**
    60 	* Load global cache
    61 	*/
    62 	function load()
    63 	{
    64 		// grab the global cache
    65 		$this->vars = $this->_read('global');
    66 
    67 		if ($this->vars !== false)
    68 		{
    69 			return true;
    70 		}
    71 
    72 		return false;
    73 	}
    74 
    75 	/**
    76 	* Unload cache object
    77 	*/
    78 	function unload()
    79 	{
    80 		$this->save();
    81 		unset($this->vars);
    82 		unset($this->sql_rowset);
    83 		unset($this->sql_row_pointer);
    84 
    85 		$this->vars = array();
    86 		$this->sql_rowset = array();
    87 		$this->sql_row_pointer = array();
    88 	}
    89 
    90 	/**
    91 	* Save modified objects
    92 	*/
    93 	function save()
    94 	{
    95 		if (!$this->is_modified)
    96 		{
    97 			return;
    98 		}
    99 
   100 		$this->_write('global', $this->vars, 2592000);
   101 
   102 		$this->is_modified = false;
   103 	}
   104 
   105 	/**
   106 	* Tidy cache
   107 	*/
   108 	function tidy()
   109 	{
   110 		// cache has auto GC, no need to have any code here :)
   111 
   112 		set_config('cache_last_gc', time(), true);
   113 	}
   114 
   115 	/**
   116 	* Get saved cache object
   117 	*/
   118 	function get($var_name)
   119 	{
   120 		if ($var_name[0] == '_')
   121 		{
   122 			if (!$this->_exists($var_name))
   123 			{
   124 				return false;
   125 			}
   126 
   127 			return $this->_read($var_name);
   128 		}
   129 		else
   130 		{
   131 			return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
   132 		}
   133 	}
   134 
   135 	/**
   136 	* Put data into cache
   137 	*/
   138 	function put($var_name, $var, $ttl = 2592000)
   139 	{
   140 		if ($var_name[0] == '_')
   141 		{
   142 			$this->_write($var_name, $var, $ttl);
   143 		}
   144 		else
   145 		{
   146 			$this->vars[$var_name] = $var;
   147 			$this->is_modified = true;
   148 		}
   149 	}
   150 
   151 	/**
   152 	* Purge cache data
   153 	*/
   154 	function purge()
   155 	{
   156 		// Purge all phpbb cache files
   157 		$dir = @opendir($this->cache_dir);
   158 
   159 		if (!$dir)
   160 		{
   161 			return;
   162 		}
   163 
   164 		while (($entry = readdir($dir)) !== false)
   165 		{
   166 			if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
   167 			{
   168 				continue;
   169 			}
   170 
   171 			$this->remove_file($this->cache_dir . $entry);
   172 		}
   173 		closedir($dir);
   174 
   175 		unset($this->vars);
   176 		unset($this->sql_rowset);
   177 		unset($this->sql_row_pointer);
   178 
   179 		$this->vars = array();
   180 		$this->sql_rowset = array();
   181 		$this->sql_row_pointer = array();
   182 
   183 		$this->is_modified = false;
   184 	}
   185 
   186 
   187 	/**
   188 	* Destroy cache data
   189 	*/
   190 	function destroy($var_name, $table = '')
   191 	{
   192 		if ($var_name == 'sql' && !empty($table))
   193 		{
   194 			if (!is_array($table))
   195 			{
   196 				$table = array($table);
   197 			}
   198 
   199 			foreach ($table as $table_name)
   200 			{
   201 				// gives us the md5s that we want
   202 				$temp = $this->_read('sql_' . $table_name);
   203 
   204 				if ($temp === false)
   205 				{
   206 					continue;
   207 				}
   208 
   209 				// delete each query ref
   210 				foreach ($temp as $md5_id => $void)
   211 				{
   212 					$this->_delete('sql_' . $md5_id);
   213 				}
   214 
   215 				// delete the table ref
   216 				$this->_delete('sql_' . $table_name);
   217 			}
   218 
   219 			return;
   220 		}
   221 
   222 		if (!$this->_exists($var_name))
   223 		{
   224 			return;
   225 		}
   226 
   227 		if ($var_name[0] == '_')
   228 		{
   229 			$this->_delete($var_name);
   230 		}
   231 		else if (isset($this->vars[$var_name]))
   232 		{
   233 			$this->is_modified = true;
   234 			unset($this->vars[$var_name]);
   235 
   236 			// We save here to let the following cache hits succeed
   237 			$this->save();
   238 		}
   239 	}
   240 
   241 	/**
   242 	* Check if a given cache entry exist
   243 	*/
   244 	function _exists($var_name)
   245 	{
   246 		if ($var_name[0] == '_')
   247 		{
   248 			return $this->_isset($var_name);
   249 		}
   250 		else
   251 		{
   252 			if (!sizeof($this->vars))
   253 			{
   254 				$this->load();
   255 			}
   256 
   257 			return isset($this->vars[$var_name]);
   258 		}
   259 	}
   260 
   261 	/**
   262 	* Load cached sql query
   263 	*/
   264 	function sql_load($query)
   265 	{
   266 		// Remove extra spaces and tabs
   267 		$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
   268 		$query_id = sizeof($this->sql_rowset);
   269 
   270 		if (($result = $this->_read('sql_' . md5($query))) === false)
   271 		{
   272 			return false;
   273 		}
   274 
   275 		$this->sql_rowset[$query_id] = $result;
   276 		$this->sql_row_pointer[$query_id] = 0;
   277 
   278 		return $query_id;
   279 	}
   280 
   281 	/**
   282 	* Save sql query
   283 	*/
   284 	function sql_save($query, &$query_result, $ttl)
   285 	{
   286 		global $db;
   287 
   288 		// Remove extra spaces and tabs
   289 		$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
   290 		$hash = md5($query);
   291 
   292 		// determine which tables this query belongs to
   293 		// Some queries use backticks, namely the get_database_size() query
   294 		// don't check for conformity, the SQL would error and not reach here.
   295 		if (!preg_match('/FROM \\(?(`?\\w+`?(?: \\w+)?(?:, ?`?\\w+`?(?: \\w+)?)*)\\)?/', $query, $regs))
   296 		{
   297 			// Bail out if the match fails.
   298 			return;
   299 		}
   300 		$tables = array_map('trim', explode(',', $regs[1]));
   301 
   302 		foreach ($tables as $table_name)
   303 		{
   304 			// Remove backticks
   305 			$table_name = ($table_name[0] == '`') ? substr($table_name, 1, -1) : $table_name;
   306 
   307 			if (($pos = strpos($table_name, ' ')) !== false)
   308 			{
   309 				$table_name = substr($table_name, 0, $pos);
   310 			}
   311 
   312 			$temp = $this->_read('sql_' . $table_name);
   313 
   314 			if ($temp === false)
   315 			{
   316 				$temp = array();
   317 			}
   318 
   319 			$temp[$hash] = true;
   320 
   321 			// This must never expire
   322 			$this->_write('sql_' . $table_name, $temp, 0);
   323 		}
   324 
   325 		// store them in the right place
   326 		$query_id = sizeof($this->sql_rowset);
   327 		$this->sql_rowset[$query_id] = array();
   328 		$this->sql_row_pointer[$query_id] = 0;
   329 
   330 		while ($row = $db->sql_fetchrow($query_result))
   331 		{
   332 			$this->sql_rowset[$query_id][] = $row;
   333 		}
   334 		$db->sql_freeresult($query_result);
   335 
   336 		$this->_write('sql_' . $hash, $this->sql_rowset[$query_id], $ttl);
   337 
   338 		$query_result = $query_id;
   339 	}
   340 
   341 	/**
   342 	* Ceck if a given sql query exist in cache
   343 	*/
   344 	function sql_exists($query_id)
   345 	{
   346 		return isset($this->sql_rowset[$query_id]);
   347 	}
   348 
   349 	/**
   350 	* Fetch row from cache (database)
   351 	*/
   352 	function sql_fetchrow($query_id)
   353 	{
   354 		if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
   355 		{
   356 			return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
   357 		}
   358 
   359 		return false;
   360 	}
   361 
   362 	/**
   363 	* Fetch a field from the current row of a cached database result (database)
   364 	*/
   365 	function sql_fetchfield($query_id, $field)
   366 	{
   367 		if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
   368 		{
   369 			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;
   370 		}
   371 
   372 		return false;
   373 	}
   374 
   375 	/**
   376 	* Seek a specific row in an a cached database result (database)
   377 	*/
   378 	function sql_rowseek($rownum, $query_id)
   379 	{
   380 		if ($rownum >= sizeof($this->sql_rowset[$query_id]))
   381 		{
   382 			return false;
   383 		}
   384 
   385 		$this->sql_row_pointer[$query_id] = $rownum;
   386 		return true;
   387 	}
   388 
   389 	/**
   390 	* Free memory used for a cached database result (database)
   391 	*/
   392 	function sql_freeresult($query_id)
   393 	{
   394 		if (!isset($this->sql_rowset[$query_id]))
   395 		{
   396 			return false;
   397 		}
   398 
   399 		unset($this->sql_rowset[$query_id]);
   400 		unset($this->sql_row_pointer[$query_id]);
   401 
   402 		return true;
   403 	}
   404 
   405 	/**
   406 	* Removes/unlinks file
   407 	*/
   408 	function remove_file($filename, $check = false)
   409 	{
   410 		if (!function_exists('phpbb_is_writable'))
   411 		{
   412 			global $phpbb_root_path, $phpEx;
   413 			include($phpbb_root_path . 'includes/functions.' . $phpEx);
   414 		}
   415 
   416 		if ($check && !phpbb_is_writable($this->cache_dir))
   417 		{
   418 			// E_USER_ERROR - not using language entry - intended.
   419 			trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
   420 		}
   421 
   422 		return @unlink($filename);
   423 	}
   424 
   425 	/**
   426 	* Check if a cache var exists
   427 	*
   428 	* @access protected
   429 	* @param string $var Cache key
   430 	* @return bool True if it exists, otherwise false
   431 	*/
   432 	function _isset($var)
   433 	{
   434 		// Most caches don't need to check
   435 		return true;
   436 	}
   437 }
   438 
   439 ?>