3 * @file src/Core/Hook.php
5 namespace Friendica\Core;
8 use Friendica\Database\DBA;
10 use Friendica\Util\Strings;
13 * Some functions to handle hooks
18 * Array of registered hooks
22 * ["<hook name>"] => [
24 * 1 => "<hook function name>"
31 private static $hooks = [];
36 public static function loadHooks()
39 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
41 while ($hook = DBA::fetch($stmt)) {
42 self::add($hook['hook'], $hook['file'], $hook['function']);
48 * @brief Adds a new hook to the hooks array.
50 * This function is meant to be called by modules on each page load as it works after loadHooks has been called.
54 * @param string $function
56 public static function add($hook, $file, $function)
58 if (!array_key_exists($hook, self::$hooks)) {
59 self::$hooks[$hook] = [];
61 self::$hooks[$hook][] = [$file, $function];
65 * @brief Registers a hook.
67 * This function is meant to be called once when an addon is enabled for example as it doesn't add to the current hooks.
69 * @param string $hook the name of the hook
70 * @param string $file the name of the file that hooks into
71 * @param string $function the name of the function that the hook will call
72 * @param int $priority A priority (defaults to 0)
74 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
76 public static function register($hook, $file, $function, $priority = 0)
78 $file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
80 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
81 if (DBA::exists('hook', $condition)) {
85 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
93 * @param string $hook the name of the hook
94 * @param string $file the name of the file that hooks into
95 * @param string $function the name of the function that the hook called
97 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
99 public static function unregister($hook, $file, $function)
101 $relative_file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
103 // This here is only needed for fixing a problem that existed on the develop branch
104 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
105 DBA::delete('hook', $condition);
107 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
108 $result = DBA::delete('hook', $condition);
113 * Returns the list of callbacks for a single hook
115 * @param string $name Name of the hook
118 public static function getByName($name)
122 if (isset(self::$hooks[$name])) {
123 $return = self::$hooks[$name];
130 * @brief Forks a hook.
132 * Use this function when you want to fork a hook via the worker.
134 * @param integer $priority of the hook
135 * @param string $name of the hook to call
136 * @param mixed $data to transmit to the callback handler
137 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
139 public static function fork($priority, $name, $data = null)
141 if (array_key_exists($name, self::$hooks)) {
142 foreach (self::$hooks[$name] as $hook) {
143 // Call a hook to check if this hook call needs to be forked
144 if (array_key_exists('hook_fork', self::$hooks)) {
145 $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
147 foreach (self::$hooks['hook_fork'] as $fork_hook) {
148 if ($hook[0] != $fork_hook[0]) {
151 self::callSingle(DI::app(), 'hook_fork', $fork_hook, $hookdata);
154 if (!$hookdata['execute']) {
159 Worker::add($priority, 'ForkHook', $name, $hook, $data);
165 * @brief Calls a hook.
167 * Use this function when you want to be able to allow a hook to manipulate
170 * @param string $name of the hook to call
171 * @param string|array &$data to transmit to the callback handler
172 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
174 public static function callAll($name, &$data = null)
176 if (array_key_exists($name, self::$hooks)) {
177 foreach (self::$hooks[$name] as $hook) {
178 self::callSingle(DI::app(), $name, $hook, $data);
184 * @brief Calls a single hook.
187 * @param string $name of the hook to call
188 * @param array $hook Hook data
189 * @param string|array &$data to transmit to the callback handler
190 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
192 public static function callSingle(App $a, $name, $hook, &$data = null)
194 // Don't run a theme's hook if the user isn't using the theme
195 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
199 @include_once($hook[0]);
200 if (function_exists($hook[1])) {
204 // remove orphan hooks
205 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
206 DBA::delete('hook', $condition, ['cascade' => false]);
211 * Checks if an app_menu hook exist for the provided addon name.
212 * Return true if the addon is an app
214 * @param string $name Name of the addon
217 public static function isAddonApp($name)
219 $name = Strings::sanitizeFilePathItem($name);
221 if (array_key_exists('app_menu', self::$hooks)) {
222 foreach (self::$hooks['app_menu'] as $hook) {
223 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {