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 return self::insert(['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
106 * Unregisters a hook.
108 * @param string $hook the name of the hook
109 * @param string $file the name of the file that hooks into
110 * @param string $function the name of the function that the hook called
112 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
114 public static function unregister($hook, $file, $function)
116 $relative_file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
118 // This here is only needed for fixing a problem that existed on the develop branch
119 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
120 self::delete($condition);
122 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
123 $result = self::delete($condition);
128 * Returns the list of callbacks for a single hook
130 * @param string $name Name of the hook
133 public static function getByName($name)
137 if (isset(self::$hooks[$name])) {
138 $return = self::$hooks[$name];
147 * Use this function when you want to fork a hook via the worker.
149 * @param integer $priority of the hook
150 * @param string $name of the hook to call
151 * @param mixed $data to transmit to the callback handler
152 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
154 public static function fork($priority, $name, $data = null)
156 if (array_key_exists($name, self::$hooks)) {
157 foreach (self::$hooks[$name] as $hook) {
158 // Call a hook to check if this hook call needs to be forked
159 if (array_key_exists('hook_fork', self::$hooks)) {
160 $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
162 foreach (self::$hooks['hook_fork'] as $fork_hook) {
163 if ($hook[0] != $fork_hook[0]) {
166 self::callSingle(DI::app(), 'hook_fork', $fork_hook, $hookdata);
169 if (!$hookdata['execute']) {
174 Worker::add($priority, 'ForkHook', $name, $hook, $data);
182 * Use this function when you want to be able to allow a hook to manipulate
185 * @param string $name of the hook to call
186 * @param string|array &$data to transmit to the callback handler
187 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
189 public static function callAll($name, &$data = null)
191 if (array_key_exists($name, self::$hooks)) {
192 foreach (self::$hooks[$name] as $hook) {
193 self::callSingle(DI::app(), $name, $hook, $data);
199 * Calls a single hook.
202 * @param string $name of the hook to call
203 * @param array $hook Hook data
204 * @param string|array &$data to transmit to the callback handler
205 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
207 public static function callSingle(App $a, $name, $hook, &$data = null)
209 // Don't run a theme's hook if the user isn't using the theme
210 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
214 @include_once($hook[0]);
215 if (function_exists($hook[1])) {
219 // remove orphan hooks
220 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
221 self::delete($condition, ['cascade' => false]);
226 * Checks if an app_menu hook exist for the provided addon name.
227 * Return true if the addon is an app
229 * @param string $name Name of the addon
232 public static function isAddonApp($name)
234 $name = Strings::sanitizeFilePathItem($name);
236 if (array_key_exists('app_menu', self::$hooks)) {
237 foreach (self::$hooks['app_menu'] as $hook) {
238 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {
248 * Deletes one or more hook records
250 * We have to clear the cached routerDispatchData because addons can provide routes
252 * @param array $condition
253 * @param array $options
257 public static function delete(array $condition, array $options = [])
259 $result = DBA::delete('hook', $condition, $options);
262 DI::cache()->delete('routerDispatchData');
269 * Inserts a hook record
271 * We have to clear the cached routerDispatchData because addons can provide routes
273 * @param array $condition
277 private static function insert(array $condition)
279 $result = DBA::insert('hook', $condition);
282 DI::cache()->delete('routerDispatchData');