]> git.mxchange.org Git - friendica.git/blob - src/Core/Hook.php
Merge pull request #6598 from annando/worker-index
[friendica.git] / src / Core / Hook.php
1 <?php
2 /**
3  * @file src/Core/Hook.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\App;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBA;
10
11 /**
12  * Some functions to handle hooks
13  */
14 class Hook extends BaseObject
15 {
16         /**
17          * Array of registered hooks
18          *
19          * Format:
20          * [
21          *              ["<hook name>"] => [
22          *                      0 => "<hook file>",
23          *                      1 => "<hook function name>"
24          *              ],
25          *              ...
26          * ]
27          *
28          * @var array
29          */
30         private static $hooks = [];
31
32         /**
33          * Load hooks
34          */
35         public static function loadHooks()
36         {
37                 self::$hooks = [];
38                 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
39
40                 while ($hook = DBA::fetch($stmt)) {
41                         self::add($hook['hook'], $hook['file'], $hook['function']);
42                 }
43                 DBA::close($stmt);
44         }
45
46         /**
47          * @brief Adds a new hook to the hooks array.
48          *
49          * This function is meant to be called by modules on each page load as it works after loadHooks has been called.
50          *
51          * @param string $hook
52          * @param string $file
53          * @param string $function
54          */
55         public static function add($hook, $file, $function)
56         {
57                 if (!array_key_exists($hook, self::$hooks)) {
58                         self::$hooks[$hook] = [];
59                 }
60                 self::$hooks[$hook][] = [$file, $function];
61         }
62
63         /**
64          * @brief Registers a hook.
65          *
66          * This function is meant to be called once when an addon is enabled for example as it doesn't add to the current hooks.
67          *
68          * @param string $hook     the name of the hook
69          * @param string $file     the name of the file that hooks into
70          * @param string $function the name of the function that the hook will call
71          * @param int    $priority A priority (defaults to 0)
72          * @return mixed|bool
73          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
74          */
75         public static function register($hook, $file, $function, $priority = 0)
76         {
77                 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
78
79                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
80                 if (DBA::exists('hook', $condition)) {
81                         return true;
82                 }
83
84                 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
85
86                 return $result;
87         }
88
89         /**
90          * Unregisters a hook.
91          *
92          * @param string $hook     the name of the hook
93          * @param string $file     the name of the file that hooks into
94          * @param string $function the name of the function that the hook called
95          * @return boolean
96          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
97          */
98         public static function unregister($hook, $file, $function)
99         {
100                 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
101
102                 // This here is only needed for fixing a problem that existed on the develop branch
103                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
104                 DBA::delete('hook', $condition);
105
106                 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
107                 $result = DBA::delete('hook', $condition);
108                 return $result;
109         }
110
111         /**
112          * Returns the list of callbacks for a single hook
113          *
114          * @param  string $name Name of the hook
115          * @return array
116          */
117         public static function getByName($name)
118         {
119                 $return = [];
120
121                 if (isset(self::$hooks[$name])) {
122                         $return = self::$hooks[$name];
123                 }
124
125                 return $return;
126         }
127
128         /**
129          * @brief Forks a hook.
130          *
131          * Use this function when you want to fork a hook via the worker.
132          *
133          * @param integer $priority of the hook
134          * @param string  $name     of the hook to call
135          * @param mixed   $data     to transmit to the callback handler
136          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
137          */
138         public static function fork($priority, $name, $data = null)
139         {
140                 if (array_key_exists($name, self::$hooks)) {
141                         foreach (self::$hooks[$name] as $hook) {
142                                 // Call a hook to check if this hook call needs to be forked
143                                 if (array_key_exists('hook_fork', self::$hooks)) {
144                                         $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
145
146                                         foreach (self::$hooks['hook_fork'] as $fork_hook) {
147                                                 if ($hook[0] != $fork_hook[0]) {
148                                                         continue;
149                                                 }
150                                                 self::callSingle(self::getApp(), 'hook_fork', $fork_hook, $hookdata);
151                                         }
152
153                                         if (!$hookdata['execute']) {
154                                                 continue;
155                                         }
156                                 }
157
158                                 Worker::add($priority, 'ForkHook', $name, $hook, $data);
159                         }
160                 }
161         }
162
163         /**
164          * @brief Calls a hook.
165          *
166          * Use this function when you want to be able to allow a hook to manipulate
167          * the provided data.
168          *
169          * @param string        $name of the hook to call
170          * @param string|array &$data to transmit to the callback handler
171          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
172          */
173         public static function callAll($name, &$data = null)
174         {
175                 if (array_key_exists($name, self::$hooks)) {
176                         foreach (self::$hooks[$name] as $hook) {
177                                 self::callSingle(self::getApp(), $name, $hook, $data);
178                         }
179                 }
180         }
181
182         /**
183          * @brief Calls a single hook.
184          *
185          * @param App             $a
186          * @param string          $name of the hook to call
187          * @param array           $hook Hook data
188          * @param string|array   &$data to transmit to the callback handler
189          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
190          */
191         public static function callSingle(App $a, $name, $hook, &$data = null)
192         {
193                 // Don't run a theme's hook if the user isn't using the theme
194                 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
195                         return;
196                 }
197
198                 @include_once($hook[0]);
199                 if (function_exists($hook[1])) {
200                         $func = $hook[1];
201                         $func($a, $data);
202                 } else {
203                         // remove orphan hooks
204                         $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
205                         DBA::delete('hook', $condition, ['cascade' => false]);
206                 }
207         }
208
209         /**
210          * Checks if an app_menu hook exist for the provided addon name.
211          * Return true if the addon is an app
212          *
213          * @param string $name Name of the addon
214          * @return boolean
215          */
216         public static function isAddonApp($name)
217         {
218                 if (array_key_exists('app_menu', self::$hooks)) {
219                         foreach (self::$hooks['app_menu'] as $hook) {
220                                 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {
221                                         return true;
222                                 }
223                         }
224                 }
225
226                 return false;
227         }
228 }