From d66960f4d566db163c23bae92f7e276cd1ac2803 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 7 Nov 2019 20:21:50 +0100 Subject: [PATCH 1/3] [ticket/16204] Remove hooks system PHPBB3-16204 --- phpBB/common.php | 4 - phpBB/includes/functions.php | 59 +++---- phpBB/includes/hooks/index.php | 250 ----------------------------- phpBB/phpbb/template/base.php | 22 --- phpBB/phpbb/template/twig/twig.php | 6 - phpBB/phpbb/user.php | 4 - 6 files changed, 21 insertions(+), 324 deletions(-) delete mode 100644 phpBB/includes/hooks/index.php diff --git a/phpBB/common.php b/phpBB/common.php index 0870786824..bfdc2c4fd4 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -143,10 +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'); diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 0a3cb26041..21aabf6006 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1514,7 +1514,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))) @@ -1561,18 +1561,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 @@ -4543,17 +4531,31 @@ function garbage_collection() * 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. @@ -4562,25 +4564,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) * diff --git a/phpBB/includes/hooks/index.php b/phpBB/includes/hooks/index.php deleted file mode 100644 index 821242cbf4..0000000000 --- a/phpBB/includes/hooks/index.php +++ /dev/null @@ -1,250 +0,0 @@ - -* @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]); - } - } - } -} diff --git a/phpBB/phpbb/template/base.php b/phpBB/phpbb/template/base.php index d502aceab8..314cdc4796 100644 --- a/phpBB/phpbb/template/base.php +++ b/phpBB/phpbb/template/base.php @@ -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; - } } diff --git a/phpBB/phpbb/template/twig/twig.php b/phpBB/phpbb/template/twig/twig.php index f322778eda..f0736045de 100644 --- a/phpBB/phpbb/template/twig/twig.php +++ b/phpBB/phpbb/template/twig/twig.php @@ -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; diff --git a/phpBB/phpbb/user.php b/phpBB/phpbb/user.php index 21d156e060..60cc85e46c 100644 --- a/phpBB/phpbb/user.php +++ b/phpBB/phpbb/user.php @@ -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 * From 6c5fa362dd102db0da51df70cacfd3368fc29b34 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 23 Nov 2019 14:40:27 +0100 Subject: [PATCH 2/3] [ticket/16204] Remove hook finder and services file PHPBB3-16204 --- phpBB/common.php | 8 -- phpBB/config/default/container/services.yml | 1 - .../default/container/services_hook.yml | 7 -- phpBB/phpbb/hook/finder.php | 91 ------------------- 4 files changed, 107 deletions(-) delete mode 100644 phpBB/config/default/container/services_hook.yml delete mode 100644 phpBB/phpbb/hook/finder.php diff --git a/phpBB/common.php b/phpBB/common.php index bfdc2c4fd4..1c07628351 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -143,14 +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'); } -/* @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 * diff --git a/phpBB/config/default/container/services.yml b/phpBB/config/default/container/services.yml index 0e9a037290..657af5465a 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -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 } diff --git a/phpBB/config/default/container/services_hook.yml b/phpBB/config/default/container/services_hook.yml deleted file mode 100644 index 10a84184a0..0000000000 --- a/phpBB/config/default/container/services_hook.yml +++ /dev/null @@ -1,7 +0,0 @@ -services: - hook_finder: - class: phpbb\hook\finder - arguments: - - '%core.root_path%' - - '%core.php_ext%' - - '@cache.driver' diff --git a/phpBB/phpbb/hook/finder.php b/phpBB/phpbb/hook/finder.php deleted file mode 100644 index f5a68a1370..0000000000 --- a/phpBB/phpbb/hook/finder.php +++ /dev/null @@ -1,91 +0,0 @@ - -* @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; - } -} From 8d9633d16926823f173a96b07c0292fda2ab2996 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 6 Dec 2019 07:25:11 +0100 Subject: [PATCH 3/3] [ticket/16204] Remove mentions of hooks PHPBB3-16204 --- phpBB/includes/functions.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 21aabf6006..e50a38b512 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1493,7 +1493,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 @@ -4528,7 +4527,6 @@ function garbage_collection() /** * Handler for exit calls in phpBB. -* This function supports hooks. * * Note: This function is called after the template has been outputted. *