]> git.mxchange.org Git - friendica.git/blob - src/Core/Hook.php
Merge pull request #8912 from annando/subscribed-tags
[friendica.git] / src / Core / Hook.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\App;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Util\Strings;
28
29 /**
30  * Some functions to handle hooks
31  */
32 class Hook
33 {
34         /**
35          * Array of registered hooks
36          *
37          * Format:
38          * [
39          *              ["<hook name>"] => [
40          *                      0 => "<hook file>",
41          *                      1 => "<hook function name>"
42          *              ],
43          *              ...
44          * ]
45          *
46          * @var array
47          */
48         private static $hooks = [];
49
50         /**
51          * Load hooks
52          */
53         public static function loadHooks()
54         {
55                 self::$hooks = [];
56                 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
57
58                 while ($hook = DBA::fetch($stmt)) {
59                         self::add($hook['hook'], $hook['file'], $hook['function']);
60                 }
61                 DBA::close($stmt);
62         }
63
64         /**
65          * Adds a new hook to the hooks array.
66          *
67          * This function is meant to be called by modules on each page load as it works after loadHooks has been called.
68          *
69          * @param string $hook
70          * @param string $file
71          * @param string $function
72          */
73         public static function add($hook, $file, $function)
74         {
75                 if (!array_key_exists($hook, self::$hooks)) {
76                         self::$hooks[$hook] = [];
77                 }
78                 self::$hooks[$hook][] = [$file, $function];
79         }
80
81         /**
82          * Registers a hook.
83          *
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.
85          *
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)
90          * @return mixed|bool
91          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
92          */
93         public static function register($hook, $file, $function, $priority = 0)
94         {
95                 $file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
96
97                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
98                 if (DBA::exists('hook', $condition)) {
99                         return true;
100                 }
101
102                 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
103
104                 return $result;
105         }
106
107         /**
108          * Unregisters a hook.
109          *
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
113          * @return boolean
114          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
115          */
116         public static function unregister($hook, $file, $function)
117         {
118                 $relative_file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
119
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);
123
124                 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
125                 $result = DBA::delete('hook', $condition);
126                 return $result;
127         }
128
129         /**
130          * Returns the list of callbacks for a single hook
131          *
132          * @param  string $name Name of the hook
133          * @return array
134          */
135         public static function getByName($name)
136         {
137                 $return = [];
138
139                 if (isset(self::$hooks[$name])) {
140                         $return = self::$hooks[$name];
141                 }
142
143                 return $return;
144         }
145
146         /**
147          * Forks a hook.
148          *
149          * Use this function when you want to fork a hook via the worker.
150          *
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
155          */
156         public static function fork($priority, $name, $data = null)
157         {
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];
163
164                                         foreach (self::$hooks['hook_fork'] as $fork_hook) {
165                                                 if ($hook[0] != $fork_hook[0]) {
166                                                         continue;
167                                                 }
168                                                 self::callSingle(DI::app(), 'hook_fork', $fork_hook, $hookdata);
169                                         }
170
171                                         if (!$hookdata['execute']) {
172                                                 continue;
173                                         }
174                                 }
175
176                                 Worker::add($priority, 'ForkHook', $name, $hook, $data);
177                         }
178                 }
179         }
180
181         /**
182          * Calls a hook.
183          *
184          * Use this function when you want to be able to allow a hook to manipulate
185          * the provided data.
186          *
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
190          */
191         public static function callAll($name, &$data = null)
192         {
193                 if (array_key_exists($name, self::$hooks)) {
194                         foreach (self::$hooks[$name] as $hook) {
195                                 self::callSingle(DI::app(), $name, $hook, $data);
196                         }
197                 }
198         }
199
200         /**
201          * Calls a single hook.
202          *
203          * @param App             $a
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
208          */
209         public static function callSingle(App $a, $name, $hook, &$data = null)
210         {
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) {
213                         return;
214                 }
215
216                 @include_once($hook[0]);
217                 if (function_exists($hook[1])) {
218                         $func = $hook[1];
219                         $func($a, $data);
220                 } else {
221                         // remove orphan hooks
222                         $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
223                         DBA::delete('hook', $condition, ['cascade' => false]);
224                 }
225         }
226
227         /**
228          * Checks if an app_menu hook exist for the provided addon name.
229          * Return true if the addon is an app
230          *
231          * @param string $name Name of the addon
232          * @return boolean
233          */
234         public static function isAddonApp($name)
235         {
236                 $name = Strings::sanitizeFilePathItem($name);
237
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') {
241                                         return true;
242                                 }
243                         }
244                 }
245
246                 return false;
247         }
248 }