Blame Identity/Webenv/phpBB/3.0.4/includes/hooks/index.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package phpBB3
ef5584
* @version $Id: index.php 8479 2008-03-29 00:22:48Z naderman $
ef5584
* @copyright (c) 2007 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
* phpBB Hook Class
ef5584
* @package phpBB3
ef5584
*/
ef5584
class phpbb_hook
ef5584
{
ef5584
	/**
ef5584
	* Registered hooks
ef5584
	*/
ef5584
	var $hooks = array();
ef5584
ef5584
	/**
ef5584
	* Results returned by functions called
ef5584
	*/
ef5584
	var $hook_result = array();
ef5584
ef5584
	/**
ef5584
	* internal pointer
ef5584
	*/
ef5584
	var $current_hook = NULL;
ef5584
ef5584
	/**
ef5584
	* Initialize hook class.
ef5584
	*
ef5584
	* @param array $valid_hooks array containing the hookable functions/methods
ef5584
	*/
ef5584
	function phpbb_hook($valid_hooks)
ef5584
	{
ef5584
		foreach ($valid_hooks as $_null => $method)
ef5584
		{
ef5584
			$this->add_hook($method);
ef5584
		}
ef5584
ef5584
		if (function_exists('phpbb_hook_register'))
ef5584
		{
ef5584
			phpbb_hook_register($this);
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Register function/method to be called within hook
ef5584
	* This function is normally called by the modification/application to attach/register the functions.
ef5584
	*
ef5584
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
ef5584
	* @param mixed $hook The replacement function/method to be called. Passing function name or array with object/class definition
ef5584
	* @param string $mode Specify the priority/chain mode. 'normal' -> hook gets appended to the chain. 'standalone' -> only the specified hook gets called - later hooks are not able to overwrite this (E_NOTICE is triggered then). 'first' -> hook is called as the first one within the chain. 'last' -> hook is called as the last one within the chain.
ef5584
	*/
ef5584
	function register($definition, $hook, $mode = 'normal')
ef5584
	{
ef5584
		$class = (!is_array($definition)) ? '__global' : $definition[0];
ef5584
		$function = (!is_array($definition)) ? $definition : $definition[1];
ef5584
ef5584
		// Method able to be hooked?
ef5584
		if (isset($this->hooks[$class][$function]))
ef5584
		{
ef5584
			switch ($mode)
ef5584
			{
ef5584
				case 'standalone':
ef5584
					if (!isset($this->hooks[$class][$function]['standalone']))
ef5584
					{
ef5584
						$this->hooks[$class][$function] = array('standalone' => $hook);
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						trigger_error('Hook not able to be called standalone, previous hook already standalone.', E_NOTICE);
ef5584
					}
ef5584
				break;
ef5584
ef5584
				case 'first':
ef5584
				case 'last':
ef5584
					$this->hooks[$class][$function][$mode][] = $hook;
ef5584
				break;
ef5584
ef5584
				case 'normal':
ef5584
				default:
ef5584
					$this->hooks[$class][$function]['normal'][] = $hook;
ef5584
				break;
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Calling all functions/methods attached to a specified hook.
ef5584
	* Called by the function allowing hooks...
ef5584
	*
ef5584
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
ef5584
	* @return bool False if no hook got executed, true otherwise
ef5584
	*/
ef5584
	function call_hook($definition)
ef5584
	{
ef5584
		$class = (!is_array($definition)) ? '__global' : $definition[0];
ef5584
		$function = (!is_array($definition)) ? $definition : $definition[1];
ef5584
ef5584
		if (!empty($this->hooks[$class][$function]))
ef5584
		{
ef5584
			// Developer tries to call a hooked function within the hooked function...
ef5584
			if ($this->current_hook !== NULL && $this->current_hook['class'] === $class && $this->current_hook['function'] === $function)
ef5584
			{
ef5584
				return false;
ef5584
			}
ef5584
ef5584
			// Call the hook with the arguments attached and store result
ef5584
			$arguments = func_get_args();
ef5584
			$this->current_hook = array('class' => $class, 'function' => $function);
ef5584
			$arguments[0] = &$this;
ef5584
ef5584
			// Call the hook chain...
ef5584
			if (isset($this->hooks[$class][$function]['standalone']))
ef5584
			{
ef5584
				$this->hook_result[$class][$function] = call_user_func_array($this->hooks[$class][$function]['standalone'], $arguments);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				foreach (array('first', 'normal', 'last') as $mode)
ef5584
				{
ef5584
					if (!isset($this->hooks[$class][$function][$mode]))
ef5584
					{
ef5584
						continue;
ef5584
					}
ef5584
ef5584
					foreach ($this->hooks[$class][$function][$mode] as $hook)
ef5584
					{
ef5584
						$this->hook_result[$class][$function] = call_user_func_array($hook, $arguments);
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
ef5584
			$this->current_hook = NULL;
ef5584
			return true;
ef5584
		}
ef5584
ef5584
		$this->current_hook = NULL;
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get result from previously called functions/methods for the same hook
ef5584
	*
ef5584
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
ef5584
	* @return mixed False if nothing returned if there is no result, else array('result' => ... )
ef5584
	*/
ef5584
	function previous_hook_result($definition)
ef5584
	{
ef5584
		$class = (!is_array($definition)) ? '__global' : $definition[0];
ef5584
		$function = (!is_array($definition)) ? $definition : $definition[1];
ef5584
ef5584
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
ef5584
		{
ef5584
			return array('result' => $this->hook_result[$class][$function]);
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Check if the called functions/methods returned something.
ef5584
	*
ef5584
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
ef5584
	* @return bool True if results are there, false if not
ef5584
	*/
ef5584
	function hook_return($definition)
ef5584
	{
ef5584
		$class = (!is_array($definition)) ? '__global' : $definition[0];
ef5584
		$function = (!is_array($definition)) ? $definition : $definition[1];
ef5584
ef5584
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
ef5584
		{
ef5584
			return true;
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Give actual result from called functions/methods back.
ef5584
	*
ef5584
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
ef5584
	* @return mixed The result
ef5584
	*/
ef5584
	function hook_return_result($definition)
ef5584
	{
ef5584
		$class = (!is_array($definition)) ? '__global' : $definition[0];
ef5584
		$function = (!is_array($definition)) ? $definition : $definition[1];
ef5584
ef5584
		if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
ef5584
		{
ef5584
			$result = $this->hook_result[$class][$function];
ef5584
			unset($this->hook_result[$class][$function]);
ef5584
			return $result;
ef5584
		}
ef5584
ef5584
		return;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Add new function to the allowed hooks.
ef5584
	*
ef5584
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
ef5584
	*/
ef5584
	function add_hook($definition)
ef5584
	{
ef5584
		if (!is_array($definition))
ef5584
		{
ef5584
			$definition = array('__global', $definition);
ef5584
		}
ef5584
ef5584
		$this->hooks[$definition[0]][$definition[1]] = array();
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Remove function from the allowed hooks.
ef5584
	*
ef5584
	* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
ef5584
	*/
ef5584
	function remove_hook($definition)
ef5584
	{
ef5584
		$class = (!is_array($definition)) ? '__global' : $definition[0];
ef5584
		$function = (!is_array($definition)) ? $definition : $definition[1];
ef5584
ef5584
		if (isset($this->hooks[$class][$function]))
ef5584
		{
ef5584
			unset($this->hooks[$class][$function]);
ef5584
ef5584
			if (isset($this->hook_result[$class][$function]))
ef5584
			{
ef5584
				unset($this->hook_result[$class][$function]);
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
}
ef5584
ef5584
?>