3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
25 use Friendica\Database\DBA;
27 use Friendica\Util\Strings;
30 * Some functions to handle hooks
35 * Array of registered hooks
39 * ["<hook name>"] => [
41 * 1 => "<hook function name>"
48 private static $hooks = [];
53 public static function loadHooks()
56 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
58 while ($hook = DBA::fetch($stmt)) {
59 self::add($hook['hook'], $hook['file'], $hook['function']);
65 * Adds a new hook to the hooks array.
67 * This function is meant to be called by modules on each page load as it works after loadHooks has been called.
71 * @param string $function
73 public static function add($hook, $file, $function)
75 if (!array_key_exists($hook, self::$hooks)) {
76 self::$hooks[$hook] = [];
78 self::$hooks[$hook][] = [$file, $function];
84 * This function is meant to be called once when an addon is enabled for example as it doesn't add to the current hooks.
86 * @param string $hook the name of the hook
87 * @param string $file the name of the file that hooks into
88 * @param string $function the name of the function that the hook will call
89 * @param int $priority A priority (defaults to 0)
91 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
93 public static function register($hook, $file, $function, $priority = 0)
95 $file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
97 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
98 if (DBA::exists('hook', $condition)) {
102 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
108 * Unregisters a hook.
110 * @param string $hook the name of the hook
111 * @param string $file the name of the file that hooks into
112 * @param string $function the name of the function that the hook called
114 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
116 public static function unregister($hook, $file, $function)
118 $relative_file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
120 // This here is only needed for fixing a problem that existed on the develop branch
121 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
122 DBA::delete('hook', $condition);
124 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
125 $result = DBA::delete('hook', $condition);
130 * Returns the list of callbacks for a single hook
132 * @param string $name Name of the hook
135 public static function getByName($name)
139 if (isset(self::$hooks[$name])) {
140 $return = self::$hooks[$name];
149 * Use this function when you want to fork a hook via the worker.
151 * @param integer $priority of the hook
152 * @param string $name of the hook to call
153 * @param mixed $data to transmit to the callback handler
154 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
156 public static function fork($priority, $name, $data = null)
158 if (array_key_exists($name, self::$hooks)) {
159 foreach (self::$hooks[$name] as $hook) {
160 // Call a hook to check if this hook call needs to be forked
161 if (array_key_exists('hook_fork', self::$hooks)) {
162 $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
164 foreach (self::$hooks['hook_fork'] as $fork_hook) {
165 if ($hook[0] != $fork_hook[0]) {
168 self::callSingle(DI::app(), 'hook_fork', $fork_hook, $hookdata);
171 if (!$hookdata['execute']) {
176 Worker::add($priority, 'ForkHook', $name, $hook, $data);
184 * Use this function when you want to be able to allow a hook to manipulate
187 * @param string $name of the hook to call
188 * @param string|array &$data to transmit to the callback handler
189 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
191 public static function callAll($name, &$data = null)
193 if (array_key_exists($name, self::$hooks)) {
194 foreach (self::$hooks[$name] as $hook) {
195 self::callSingle(DI::app(), $name, $hook, $data);
201 * Calls a single hook.
204 * @param string $name of the hook to call
205 * @param array $hook Hook data
206 * @param string|array &$data to transmit to the callback handler
207 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
209 public static function callSingle(App $a, $name, $hook, &$data = null)
211 // Don't run a theme's hook if the user isn't using the theme
212 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
216 @include_once($hook[0]);
217 if (function_exists($hook[1])) {
221 // remove orphan hooks
222 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
223 DBA::delete('hook', $condition, ['cascade' => false]);
228 * Checks if an app_menu hook exist for the provided addon name.
229 * Return true if the addon is an app
231 * @param string $name Name of the addon
234 public static function isAddonApp($name)
236 $name = Strings::sanitizeFilePathItem($name);
238 if (array_key_exists('app_menu', self::$hooks)) {
239 foreach (self::$hooks['app_menu'] as $hook) {
240 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {