3 * @file src/Core/Hook.php
5 namespace Friendica\Core;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBA;
11 use Friendica\Util\Strings;
14 * Some functions to handle hooks
16 class Hook extends BaseObject
19 * Array of registered hooks
23 * ["<hook name>"] => [
25 * 1 => "<hook function name>"
32 private static $hooks = [];
37 public static function loadHooks()
40 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
42 while ($hook = DBA::fetch($stmt)) {
43 self::add($hook['hook'], $hook['file'], $hook['function']);
49 * @brief Adds a new hook to the hooks array.
51 * This function is meant to be called by modules on each page load as it works after loadHooks has been called.
55 * @param string $function
57 public static function add($hook, $file, $function)
59 if (!array_key_exists($hook, self::$hooks)) {
60 self::$hooks[$hook] = [];
62 self::$hooks[$hook][] = [$file, $function];
66 * @brief Registers a hook.
68 * This function is meant to be called once when an addon is enabled for example as it doesn't add to the current hooks.
70 * @param string $hook the name of the hook
71 * @param string $file the name of the file that hooks into
72 * @param string $function the name of the function that the hook will call
73 * @param int $priority A priority (defaults to 0)
75 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
77 public static function register($hook, $file, $function, $priority = 0)
79 $file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
81 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
82 if (DBA::exists('hook', $condition)) {
86 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
94 * @param string $hook the name of the hook
95 * @param string $file the name of the file that hooks into
96 * @param string $function the name of the function that the hook called
98 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
100 public static function unregister($hook, $file, $function)
102 $relative_file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
104 // This here is only needed for fixing a problem that existed on the develop branch
105 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
106 DBA::delete('hook', $condition);
108 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
109 $result = DBA::delete('hook', $condition);
114 * Returns the list of callbacks for a single hook
116 * @param string $name Name of the hook
119 public static function getByName($name)
123 if (isset(self::$hooks[$name])) {
124 $return = self::$hooks[$name];
131 * @brief Forks a hook.
133 * Use this function when you want to fork a hook via the worker.
135 * @param integer $priority of the hook
136 * @param string $name of the hook to call
137 * @param mixed $data to transmit to the callback handler
138 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
140 public static function fork($priority, $name, $data = null)
142 if (array_key_exists($name, self::$hooks)) {
143 foreach (self::$hooks[$name] as $hook) {
144 // Call a hook to check if this hook call needs to be forked
145 if (array_key_exists('hook_fork', self::$hooks)) {
146 $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
148 foreach (self::$hooks['hook_fork'] as $fork_hook) {
149 if ($hook[0] != $fork_hook[0]) {
152 self::callSingle(DI::app(), 'hook_fork', $fork_hook, $hookdata);
155 if (!$hookdata['execute']) {
160 Worker::add($priority, 'ForkHook', $name, $hook, $data);
166 * @brief Calls a hook.
168 * Use this function when you want to be able to allow a hook to manipulate
171 * @param string $name of the hook to call
172 * @param string|array &$data to transmit to the callback handler
173 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
175 public static function callAll($name, &$data = null)
177 if (array_key_exists($name, self::$hooks)) {
178 foreach (self::$hooks[$name] as $hook) {
179 self::callSingle(DI::app(), $name, $hook, $data);
185 * @brief Calls a single hook.
188 * @param string $name of the hook to call
189 * @param array $hook Hook data
190 * @param string|array &$data to transmit to the callback handler
191 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
193 public static function callSingle(App $a, $name, $hook, &$data = null)
195 // Don't run a theme's hook if the user isn't using the theme
196 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
200 @include_once($hook[0]);
201 if (function_exists($hook[1])) {
205 // remove orphan hooks
206 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
207 DBA::delete('hook', $condition, ['cascade' => false]);
212 * Checks if an app_menu hook exist for the provided addon name.
213 * Return true if the addon is an app
215 * @param string $name Name of the addon
218 public static function isAddonApp($name)
220 $name = Strings::sanitizeFilePathItem($name);
222 if (array_key_exists('app_menu', self::$hooks)) {
223 foreach (self::$hooks['app_menu'] as $hook) {
224 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {