1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-19 07:00:14 +01:00

Merge pull request #5731 from marc1706/ticket/16204

[ticket/16204] Remove hooks system
This commit is contained in:
Marc Alexander 2020-01-06 22:03:23 +01:00 committed by GitHub
commit 2a5c7b1d43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 21 additions and 433 deletions

View File

@ -143,18 +143,6 @@ if (@is_file($phpbb_root_path . $config['exts_composer_vendor_dir'] . '/autoload
require_once($phpbb_root_path . $config['exts_composer_vendor_dir'] . '/autoload.php');
}
// Add own hook handler
require($phpbb_root_path . 'includes/hooks/index.' . $phpEx);
$phpbb_hook = new phpbb_hook(array('exit_handler', 'phpbb_user_session_handler', 'append_sid', array('template', 'display')));
/* @var $phpbb_hook_finder \phpbb\hook\finder */
$phpbb_hook_finder = $phpbb_container->get('hook_finder');
foreach ($phpbb_hook_finder->find() as $hook)
{
@include($phpbb_root_path . 'includes/hooks/' . $hook . '.' . $phpEx);
}
/**
* Main event which is triggered on every page
*

View File

@ -13,7 +13,6 @@ imports:
- { resource: services_files.yml }
- { resource: services_filesystem.yml }
- { resource: services_help.yml }
- { resource: services_hook.yml }
- { resource: services_http.yml }
- { resource: services_language.yml }
- { resource: services_migrator.yml }

View File

@ -1,7 +0,0 @@
services:
hook_finder:
class: phpbb\hook\finder
arguments:
- '%core.root_path%'
- '%core.php_ext%'
- '@cache.driver'

View File

@ -1481,7 +1481,6 @@ function tracking_unserialize($string, $max_depth = 3)
/**
* Append session id to url.
* This function supports hooks.
*
* @param string $url The url the session id needs to be appended to (can have params)
* @param mixed $params String or array of additional url parameters
@ -1502,7 +1501,7 @@ function tracking_unserialize($string, $max_depth = 3)
*/
function append_sid($url, $params = false, $is_amp = true, $session_id = false, $is_route = false)
{
global $_SID, $_EXTRA_URL, $phpbb_hook, $phpbb_path_helper;
global $_SID, $_EXTRA_URL, $phpbb_path_helper;
global $phpbb_dispatcher;
if ($params === '' || (is_array($params) && empty($params)))
@ -1549,18 +1548,6 @@ function append_sid($url, $params = false, $is_amp = true, $session_id = false,
return $append_sid_overwrite;
}
// The following hook remains for backwards compatibility, though use of
// the event above is preferred.
// Developers using the hook function need to globalise the $_SID and $_EXTRA_URL on their own and also handle it appropriately.
// They could mimic most of what is within this function
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__, $url, $params, $is_amp, $session_id))
{
if ($phpbb_hook->hook_return(__FUNCTION__))
{
return $phpbb_hook->hook_return_result(__FUNCTION__);
}
}
$params_is_array = is_array($params);
// Get anchor
@ -4517,20 +4504,33 @@ function garbage_collection()
/**
* Handler for exit calls in phpBB.
* This function supports hooks.
*
* Note: This function is called after the template has been outputted.
*
* @return void
*/
function exit_handler()
{
global $phpbb_hook;
global $phpbb_dispatcher;
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__))
$exit_handler_override = false;
/**
* This event can either supplement or override the exit_handler() function
*
* To override this function, the event must set $exit_handler_override to
* true to force it to exit directly after calling this event.
*
* @event core.exit_handler
* @var bool exit_handler_override Flag to signify exit is handled elsewhere
* @since 4.0.0-a1
*/
$vars = ['exit_handler_override'];
extract($phpbb_dispatcher->trigger_event('core.exit_handler', compact($vars)));
if ($exit_handler_override)
{
if ($phpbb_hook->hook_return(__FUNCTION__))
{
return $phpbb_hook->hook_return_result(__FUNCTION__);
}
return;
}
// As a pre-caution... some setups display a blank page if the flush() is not there.
@ -4539,25 +4539,6 @@ function exit_handler()
exit;
}
/**
* Handler for init calls in phpBB. This function is called in \phpbb\user::setup();
* This function supports hooks.
*/
function phpbb_user_session_handler()
{
global $phpbb_hook;
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__))
{
if ($phpbb_hook->hook_return(__FUNCTION__))
{
return $phpbb_hook->hook_return_result(__FUNCTION__);
}
}
return;
}
/**
* Casts a numeric string $input to an appropriate numeric type (i.e. integer or float)
*

View File

@ -1,250 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* phpBB Hook Class
*/
class phpbb_hook
{
/**
* Registered hooks
*/
var $hooks = array();
/**
* Results returned by functions called
*/
var $hook_result = array();
/**
* internal pointer
*/
var $current_hook = NULL;
/**
* Initialize hook class.
*
* @param array $valid_hooks array containing the hookable functions/methods
*/
function __construct($valid_hooks)
{
foreach ($valid_hooks as $_null => $method)
{
$this->add_hook($method);
}
if (function_exists('phpbb_hook_register'))
{
phpbb_hook_register($this);
}
}
/**
* Register function/method to be called within hook
* This function is normally called by the modification/application to attach/register the functions.
*
* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
* @param mixed $hook The replacement function/method to be called. Passing function name or array with object/class definition
* @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.
*/
function register($definition, $hook, $mode = 'normal')
{
$class = (!is_array($definition)) ? '__global' : $definition[0];
$function = (!is_array($definition)) ? $definition : $definition[1];
// Method able to be hooked?
if (isset($this->hooks[$class][$function]))
{
switch ($mode)
{
case 'standalone':
if (!isset($this->hooks[$class][$function]['standalone']))
{
$this->hooks[$class][$function] = array('standalone' => $hook);
}
else
{
trigger_error('Hook not able to be called standalone, previous hook already standalone.', E_NOTICE);
}
break;
case 'first':
case 'last':
$this->hooks[$class][$function][$mode][] = $hook;
break;
case 'normal':
default:
$this->hooks[$class][$function]['normal'][] = $hook;
break;
}
}
}
/**
* Calling all functions/methods attached to a specified hook.
* Called by the function allowing hooks...
*
* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
* @return bool False if no hook got executed, true otherwise
*/
function call_hook($definition)
{
$class = (!is_array($definition)) ? '__global' : $definition[0];
$function = (!is_array($definition)) ? $definition : $definition[1];
if (!empty($this->hooks[$class][$function]))
{
// Developer tries to call a hooked function within the hooked function...
if ($this->current_hook !== NULL && $this->current_hook['class'] === $class && $this->current_hook['function'] === $function)
{
return false;
}
// Call the hook with the arguments attached and store result
$arguments = func_get_args();
$this->current_hook = array('class' => $class, 'function' => $function);
$arguments[0] = &$this;
// Call the hook chain...
if (isset($this->hooks[$class][$function]['standalone']))
{
$this->hook_result[$class][$function] = call_user_func_array($this->hooks[$class][$function]['standalone'], $arguments);
}
else
{
foreach (array('first', 'normal', 'last') as $mode)
{
if (!isset($this->hooks[$class][$function][$mode]))
{
continue;
}
foreach ($this->hooks[$class][$function][$mode] as $hook)
{
$this->hook_result[$class][$function] = call_user_func_array($hook, $arguments);
}
}
}
$this->current_hook = NULL;
return true;
}
$this->current_hook = NULL;
return false;
}
/**
* Get result from previously called functions/methods for the same hook
*
* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
* @return mixed False if nothing returned if there is no result, else array('result' => ... )
*/
function previous_hook_result($definition)
{
$class = (!is_array($definition)) ? '__global' : $definition[0];
$function = (!is_array($definition)) ? $definition : $definition[1];
if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
{
return array('result' => $this->hook_result[$class][$function]);
}
return false;
}
/**
* Check if the called functions/methods returned something.
*
* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
* @return bool True if results are there, false if not
*/
function hook_return($definition)
{
$class = (!is_array($definition)) ? '__global' : $definition[0];
$function = (!is_array($definition)) ? $definition : $definition[1];
if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
{
return true;
}
return false;
}
/**
* Give actual result from called functions/methods back.
*
* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
* @return mixed The result
*/
function hook_return_result($definition)
{
$class = (!is_array($definition)) ? '__global' : $definition[0];
$function = (!is_array($definition)) ? $definition : $definition[1];
if (!empty($this->hooks[$class][$function]) && isset($this->hook_result[$class][$function]))
{
$result = $this->hook_result[$class][$function];
unset($this->hook_result[$class][$function]);
return $result;
}
return;
}
/**
* Add new function to the allowed hooks.
*
* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
*/
function add_hook($definition)
{
if (!is_array($definition))
{
$definition = array('__global', $definition);
}
$this->hooks[$definition[0]][$definition[1]] = array();
}
/**
* Remove function from the allowed hooks.
*
* @param mixed $definition Declaring function (with __FUNCTION__) or class with array(__CLASS__, __FUNCTION__)
*/
function remove_hook($definition)
{
$class = (!is_array($definition)) ? '__global' : $definition[0];
$function = (!is_array($definition)) ? $definition : $definition[1];
if (isset($this->hooks[$class][$function]))
{
unset($this->hooks[$class][$function]);
if (isset($this->hook_result[$class][$function]))
{
unset($this->hook_result[$class][$function]);
}
}
}
}

View File

@ -1,91 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\hook;
/**
* The hook finder locates installed hooks.
*/
class finder
{
/**
* @var \phpbb\cache\driver\driver_interface
*/
protected $cache;
/**
* @var string
*/
protected $phpbb_root_path;
/**
* @var string
*/
protected $php_ext;
/**
* Creates a new finder instance.
*
* @param string $phpbb_root_path Path to the phpbb root directory
* @param string $php_ext php file extension
* @param \phpbb\cache\driver\driver_interface $cache A cache instance or null
*/
public function __construct($phpbb_root_path, $php_ext, \phpbb\cache\driver\driver_interface $cache = null)
{
$this->phpbb_root_path = $phpbb_root_path;
$this->cache = $cache;
$this->php_ext = $php_ext;
}
/**
* Finds all hook files.
*
* @param bool $cache Whether the result should be cached
* @return array An array of paths to found hook files
*/
public function find($cache = true)
{
if (!defined('DEBUG') && $cache && $this->cache)
{
$hook_files = $this->cache->get('_hooks');
if ($hook_files !== false)
{
return $hook_files;
}
}
$hook_files = array();
// Now search for hooks...
$dh = @opendir($this->phpbb_root_path . 'includes/hooks/');
if ($dh)
{
while (($file = readdir($dh)) !== false)
{
if (strpos($file, 'hook_') === 0 && substr($file, -strlen('.' . $this->php_ext)) === '.' . $this->php_ext)
{
$hook_files[] = substr($file, 0, -(strlen($this->php_ext) + 1));
}
}
closedir($dh);
}
if ($cache && $this->cache)
{
$this->cache->put('_hooks', $hook_files);
}
return $hook_files;
}
}

View File

@ -168,26 +168,4 @@ abstract class base implements template
{
return $this->context->find_key_index($blockname, $key);
}
/**
* Calls hook if any is defined.
*
* @param string $handle Template handle being displayed.
* @param string $method Method name of the caller.
*/
protected function call_hook($handle, $method)
{
global $phpbb_hook;
if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array('template', $method), $handle, $this))
{
if ($phpbb_hook->hook_return(array('template', $method)))
{
$result = $phpbb_hook->hook_return_result(array('template', $method));
return array($result);
}
}
return false;
}
}

View File

@ -308,12 +308,6 @@ class twig extends \phpbb\template\base
*/
public function display($handle)
{
$result = $this->call_hook($handle, __FUNCTION__);
if ($result !== false)
{
return $result[0];
}
$this->twig->display($this->get_filename_from_handle($handle), $this->get_template_vars());
return $this;

View File

@ -342,10 +342,6 @@ class user extends \phpbb\session
$this->img_lang = $this->lang_name;
// Call phpbb_user_session_handler() in case external application want to "bend" some variables or replace classes...
// After calling it we continue script execution...
phpbb_user_session_handler();
/**
* Execute code at the end of user setup
*