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

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