Blame Identity/Webenv/phpBB/3.0.4/includes/acm/acm_file.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package acm
ef5584
* @version $Id: acm_file.php 9076 2008-11-22 19:06:42Z acydburn $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* ACM File Based Caching
ef5584
* @package acm
ef5584
*/
ef5584
class acm
ef5584
{
ef5584
	var $vars = array();
ef5584
	var $var_expires = array();
ef5584
	var $is_modified = false;
ef5584
ef5584
	var $sql_rowset = array();
ef5584
	var $sql_row_pointer = array();
ef5584
	var $cache_dir = '';
ef5584
ef5584
	/**
ef5584
	* Set cache path
ef5584
	*/
ef5584
	function acm()
ef5584
	{
ef5584
		global $phpbb_root_path;
ef5584
		$this->cache_dir = $phpbb_root_path . 'cache/';
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Load global cache
ef5584
	*/
ef5584
	function load()
ef5584
	{
ef5584
		global $phpEx;
ef5584
		if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
ef5584
		{
ef5584
			@include($this->cache_dir . 'data_global.' . $phpEx);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Unload cache object
ef5584
	*/
ef5584
	function unload()
ef5584
	{
ef5584
		$this->save();
ef5584
		unset($this->vars);
ef5584
		unset($this->var_expires);
ef5584
		unset($this->sql_rowset);
ef5584
		unset($this->sql_row_pointer);
ef5584
ef5584
		$this->vars = array();
ef5584
		$this->var_expires = array();
ef5584
		$this->sql_rowset = array();
ef5584
		$this->sql_row_pointer = array();
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Save modified objects
ef5584
	*/
ef5584
	function save()
ef5584
	{
ef5584
		if (!$this->is_modified)
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		global $phpEx;
ef5584
ef5584
		if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb'))
ef5584
		{
ef5584
			@flock($fp, LOCK_EX);
ef5584
			fwrite($fp, "vars = " . var_export($this->vars, true) . ";\n\n\$this->var_expires = " . var_export($this->var_expires, true) . "\n?>");
ef5584
			@flock($fp, LOCK_UN);
ef5584
			fclose($fp);
ef5584
ef5584
			if (!function_exists('phpbb_chmod'))
ef5584
			{
ef5584
				global $phpbb_root_path;
ef5584
				include($phpbb_root_path . 'includes/functions.' . $phpEx);
ef5584
			}
ef5584
ef5584
			phpbb_chmod($this->cache_dir . 'data_global.' . $phpEx, CHMOD_WRITE);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			// Now, this occurred how often? ... phew, just tell the user then...
ef5584
			if (!@is_writable($this->cache_dir))
ef5584
			{
ef5584
				trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR);
ef5584
			}
ef5584
ef5584
			trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR);
ef5584
		}
ef5584
ef5584
		$this->is_modified = false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Tidy cache
ef5584
	*/
ef5584
	function tidy()
ef5584
	{
ef5584
		global $phpEx;
ef5584
ef5584
		$dir = @opendir($this->cache_dir);
ef5584
ef5584
		if (!$dir)
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		while (($entry = readdir($dir)) !== false)
ef5584
		{
ef5584
			if (!preg_match('/^(sql_|data_(?!global))/', $entry))
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$expired = true;
ef5584
			@include($this->cache_dir . $entry);
ef5584
			if ($expired)
ef5584
			{
ef5584
				$this->remove_file($this->cache_dir . $entry);
ef5584
			}
ef5584
		}
ef5584
		closedir($dir);
ef5584
ef5584
		if (file_exists($this->cache_dir . 'data_global.' . $phpEx))
ef5584
		{
ef5584
			if (!sizeof($this->vars))
ef5584
			{
ef5584
				$this->load();
ef5584
			}
ef5584
ef5584
			foreach ($this->var_expires as $var_name => $expires)
ef5584
			{
ef5584
				if (time() > $expires)
ef5584
				{
ef5584
					$this->destroy($var_name);
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		set_config('cache_last_gc', time(), true);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get saved cache object
ef5584
	*/
ef5584
	function get($var_name)
ef5584
	{
ef5584
		if ($var_name[0] == '_')
ef5584
		{
ef5584
			global $phpEx;
ef5584
ef5584
			if (!$this->_exists($var_name))
ef5584
			{
ef5584
				return false;
ef5584
			}
ef5584
ef5584
			@include($this->cache_dir . "data{$var_name}.$phpEx");
ef5584
			return (isset($data)) ? $data : false;
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Put data into cache
ef5584
	*/
ef5584
	function put($var_name, $var, $ttl = 31536000)
ef5584
	{
ef5584
		if ($var_name[0] == '_')
ef5584
		{
ef5584
			global $phpEx;
ef5584
ef5584
			if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb'))
ef5584
			{
ef5584
				@flock($fp, LOCK_EX);
ef5584
				fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data =  " . (sizeof($var) ? "unserialize(" . var_export(serialize($var), true) . ");" : 'array();') . "\n\n?>");
ef5584
				@flock($fp, LOCK_UN);
ef5584
				fclose($fp);
ef5584
ef5584
				if (!function_exists('phpbb_chmod'))
ef5584
				{
ef5584
					global $phpbb_root_path;
ef5584
					include($phpbb_root_path . 'includes/functions.' . $phpEx);
ef5584
				}
ef5584
ef5584
				phpbb_chmod($this->cache_dir . "data{$var_name}.$phpEx", CHMOD_WRITE);
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$this->vars[$var_name] = $var;
ef5584
			$this->var_expires[$var_name] = time() + $ttl;
ef5584
			$this->is_modified = true;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Purge cache data
ef5584
	*/
ef5584
	function purge()
ef5584
	{
ef5584
		// Purge all phpbb cache files
ef5584
		$dir = @opendir($this->cache_dir);
ef5584
ef5584
		if (!$dir)
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		while (($entry = readdir($dir)) !== false)
ef5584
		{
ef5584
			if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$this->remove_file($this->cache_dir . $entry);
ef5584
		}
ef5584
		closedir($dir);
ef5584
ef5584
		unset($this->vars);
ef5584
		unset($this->var_expires);
ef5584
		unset($this->sql_rowset);
ef5584
		unset($this->sql_row_pointer);
ef5584
ef5584
		$this->vars = array();
ef5584
		$this->var_expires = array();
ef5584
		$this->sql_rowset = array();
ef5584
		$this->sql_row_pointer = array();
ef5584
ef5584
		$this->is_modified = false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Destroy cache data
ef5584
	*/
ef5584
	function destroy($var_name, $table = '')
ef5584
	{
ef5584
		global $phpEx;
ef5584
ef5584
		if ($var_name == 'sql' && !empty($table))
ef5584
		{
ef5584
			if (!is_array($table))
ef5584
			{
ef5584
				$table = array($table);
ef5584
			}
ef5584
ef5584
			$dir = @opendir($this->cache_dir);
ef5584
ef5584
			if (!$dir)
ef5584
			{
ef5584
				return;
ef5584
			}
ef5584
ef5584
			while (($entry = readdir($dir)) !== false)
ef5584
			{
ef5584
				if (strpos($entry, 'sql_') !== 0)
ef5584
				{
ef5584
					continue;
ef5584
				}
ef5584
ef5584
				// The following method is more failproof than simply assuming the query is on line 3 (which it should be)
ef5584
				$check_line = @file_get_contents($this->cache_dir . $entry);
ef5584
ef5584
				if (empty($check_line))
ef5584
				{
ef5584
					continue;
ef5584
				}
ef5584
ef5584
				// Now get the contents between /* and */
ef5584
				$check_line = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3);
ef5584
ef5584
				$found = false;
ef5584
				foreach ($table as $check_table)
ef5584
				{
ef5584
					// Better catch partial table names than no table names. ;)
ef5584
					if (strpos($check_line, $check_table) !== false)
ef5584
					{
ef5584
						$found = true;
ef5584
						break;
ef5584
					}
ef5584
				}
ef5584
ef5584
				if ($found)
ef5584
				{
ef5584
					$this->remove_file($this->cache_dir . $entry);
ef5584
				}
ef5584
			}
ef5584
			closedir($dir);
ef5584
ef5584
			return;
ef5584
		}
ef5584
ef5584
		if (!$this->_exists($var_name))
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		if ($var_name[0] == '_')
ef5584
		{
ef5584
			$this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true);
ef5584
		}
ef5584
		else if (isset($this->vars[$var_name]))
ef5584
		{
ef5584
			$this->is_modified = true;
ef5584
			unset($this->vars[$var_name]);
ef5584
			unset($this->var_expires[$var_name]);
ef5584
ef5584
			// We save here to let the following cache hits succeed
ef5584
			$this->save();
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Check if a given cache entry exist
ef5584
	*/
ef5584
	function _exists($var_name)
ef5584
	{
ef5584
		if ($var_name[0] == '_')
ef5584
		{
ef5584
			global $phpEx;
ef5584
			return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			if (!sizeof($this->vars))
ef5584
			{
ef5584
				$this->load();
ef5584
			}
ef5584
ef5584
			if (!isset($this->var_expires[$var_name]))
ef5584
			{
ef5584
				return false;
ef5584
			}
ef5584
ef5584
			return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Load cached sql query
ef5584
	*/
ef5584
	function sql_load($query)
ef5584
	{
ef5584
		global $phpEx;
ef5584
ef5584
		// Remove extra spaces and tabs
ef5584
		$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
ef5584
		$query_id = sizeof($this->sql_rowset);
ef5584
ef5584
		if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		@include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
ef5584
ef5584
		if (!isset($expired))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
		else if ($expired)
ef5584
		{
ef5584
			$this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx", true);
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$this->sql_row_pointer[$query_id] = 0;
ef5584
ef5584
		return $query_id;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Save sql query
ef5584
	*/
ef5584
	function sql_save($query, &$query_result, $ttl)
ef5584
	{
ef5584
		global $db, $phpEx;
ef5584
ef5584
		// Remove extra spaces and tabs
ef5584
		$query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
ef5584
		$filename = $this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx;
ef5584
ef5584
		if ($fp = @fopen($filename, 'wb'))
ef5584
		{
ef5584
			@flock($fp, LOCK_EX);
ef5584
ef5584
			$query_id = sizeof($this->sql_rowset);
ef5584
			$this->sql_rowset[$query_id] = array();
ef5584
			$this->sql_row_pointer[$query_id] = 0;
ef5584
ef5584
			while ($row = $db->sql_fetchrow($query_result))
ef5584
			{
ef5584
				$this->sql_rowset[$query_id][] = $row;
ef5584
			}
ef5584
			$db->sql_freeresult($query_result);
ef5584
ef5584
			$file = "
ef5584
			$file .= "\n\$expired = (time() > " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n";
ef5584
ef5584
			fwrite($fp, $file . "\n\$this->sql_rowset[\$query_id] = " . (sizeof($this->sql_rowset[$query_id]) ? "unserialize(" . var_export(serialize($this->sql_rowset[$query_id]), true) . ");" : 'array();') . "\n\n?>");
ef5584
			@flock($fp, LOCK_UN);
ef5584
			fclose($fp);
ef5584
ef5584
			if (!function_exists('phpbb_chmod'))
ef5584
			{
ef5584
				global $phpbb_root_path;
ef5584
				include($phpbb_root_path . 'includes/functions.' . $phpEx);
ef5584
			}
ef5584
ef5584
			phpbb_chmod($filename, CHMOD_WRITE);
ef5584
ef5584
			$query_result = $query_id;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Ceck if a given sql query exist in cache
ef5584
	*/
ef5584
	function sql_exists($query_id)
ef5584
	{
ef5584
		return isset($this->sql_rowset[$query_id]);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Fetch row from cache (database)
ef5584
	*/
ef5584
	function sql_fetchrow($query_id)
ef5584
	{
ef5584
		if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Fetch a field from the current row of a cached database result (database)
ef5584
	*/
ef5584
	function sql_fetchfield($query_id, $field)
ef5584
	{
ef5584
		if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
ef5584
		{
ef5584
			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;
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Seek a specific row in an a cached database result (database)
ef5584
	*/
ef5584
	function sql_rowseek($rownum, $query_id)
ef5584
	{
ef5584
		if ($rownum >= sizeof($this->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$this->sql_row_pointer[$query_id] = $rownum;
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Free memory used for a cached database result (database)
ef5584
	*/
ef5584
	function sql_freeresult($query_id)
ef5584
	{
ef5584
		if (!isset($this->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		unset($this->sql_rowset[$query_id]);
ef5584
		unset($this->sql_row_pointer[$query_id]);
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Removes/unlinks file
ef5584
	*/
ef5584
	function remove_file($filename, $check = false)
ef5584
	{
ef5584
		if ($check && !@is_writable($this->cache_dir))
ef5584
		{
ef5584
			// E_USER_ERROR - not using language entry - intended.
ef5584
			trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
ef5584
		}
ef5584
ef5584
		return @unlink($filename);
ef5584
	}
ef5584
}
ef5584
ef5584
?>