]> git.mxchange.org Git - friendica.git/blob - src/Core/Hook.php
73aa95ee908197c3cbc77ebc051ebe9fb7d794e6
[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 type $hook
52          * @param type $file
53          * @param type $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          */
74         public static function register($hook, $file, $function, $priority = 0)
75         {
76                 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
77
78                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
79                 if (DBA::exists('hook', $condition)) {
80                         return true;
81                 }
82
83                 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
84
85                 return $result;
86         }
87
88         /**
89          * Unregisters a hook.
90          *
91          * @param string $hook     the name of the hook
92          * @param string $file     the name of the file that hooks into
93          * @param string $function the name of the function that the hook called
94          * @return boolean
95          */
96         public static function unregister($hook, $file, $function)
97         {
98                 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
99
100                 // This here is only needed for fixing a problem that existed on the develop branch
101                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
102                 DBA::delete('hook', $condition);
103
104                 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
105                 $result = DBA::delete('hook', $condition);
106                 return $result;
107         }
108
109         /**
110          * Returns the list of callbacks for a single hook
111          *
112          * @param  string $name Name of the hook
113          * @return array
114          */
115         public static function getByName($name)
116         {
117                 $return = [];
118
119                 if (isset(self::$hooks[$name])) {
120                         $return = self::$hooks[$name];
121                 }
122
123                 return $return;
124         }
125
126         /**
127          * @brief Forks a hook.
128          *
129          * Use this function when you want to fork a hook via the worker.
130          *
131          * @param integer $priority of the hook
132          * @param string  $name     of the hook to call
133          * @param mixed   $data     to transmit to the callback handler
134          */
135         public static function fork($priority, $name, $data = null)
136         {
137                 if (array_key_exists($name, self::$hooks)) {
138                         foreach (self::$hooks[$name] as $hook) {
139                                 // Call a hook to check if this hook call needs to be forked
140                                 $hookdata = ['name' => $name, 'data' => $data, 'execute' => true];
141
142                                 if (array_key_exists('hook_fork', self::$hooks)) {
143                                         foreach (self::$hooks['hook_fork'] as $fork_hook) {
144                                                 if ($hook[0] != $fork_hook[0]) {
145                                                         continue;
146                                                 }
147                                                 self::callSingle(self::getApp(), 'hook_fork', $fork_hook, $hookdata);
148                                         }
149                                 }
150
151                                 if (!$hookdata['execute']) {
152                                         continue;
153                                 }
154
155                                 Worker::add($priority, 'ForkHook', $name, $hook, $data);
156                         }
157                 }
158         }
159
160         /**
161          * @brief Calls a hook.
162          *
163          * Use this function when you want to be able to allow a hook to manipulate
164          * the provided data.
165          *
166          * @param string       $name  of the hook to call
167          * @param string|array &$data to transmit to the callback handler
168          */
169         public static function callAll($name, &$data = null)
170         {
171                 if (array_key_exists($name, self::$hooks)) {
172                         foreach (self::$hooks[$name] as $hook) {
173                                 self::callSingle(self::getApp(), $name, $hook, $data);
174                         }
175                 }
176         }
177
178         /**
179          * @brief Calls a single hook.
180          *
181          * @param App $a
182          * @param string         $name of the hook to call
183          * @param array          $hook Hook data
184          * @param string|array   &$data to transmit to the callback handler
185          */
186         public static function callSingle(App $a, $name, $hook, &$data = null)
187         {
188                 // Don't run a theme's hook if the user isn't using the theme
189                 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
190                         return;
191                 }
192
193                 @include_once($hook[0]);
194                 if (function_exists($hook[1])) {
195                         $func = $hook[1];
196                         $func($a, $data);
197                 } else {
198                         // remove orphan hooks
199                         $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
200                         DBA::delete('hook', $condition, ['cascade' => false]);
201                 }
202         }
203
204         /**
205          * Checks if an app_menu hook exist for the provided addon name.
206          * Return true if the addon is an app
207          *
208          * @param string $name Name of the addon
209          * @return boolean
210          */
211         public static function isAddonApp($name)
212         {
213                 if (array_key_exists('app_menu', self::$hooks)) {
214                         foreach (self::$hooks['app_menu'] as $hook) {
215                                 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {
216                                         return true;
217                                 }
218                         }
219                 }
220
221                 return false;
222         }
223 }