Blame Identity/Models/Html/phpBB/3.0.4/includes/hooks/index.php

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