]> git.mxchange.org Git - friendica.git/blob - src/Core/Hook.php
839b55815a5d6f4dc3f4829da596ea06988fe5e7
[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                         if (!array_key_exists($hook['hook'], self::$hooks)) {
42                                 self::$hooks[$hook['hook']] = [];
43                         }
44                         self::$hooks[$hook['hook']][] = [$hook['file'], $hook['function']];
45                 }
46                 DBA::close($stmt);
47         }
48
49         /**
50          * Registers a hook.
51          *
52          * @param string $hook     the name of the hook
53          * @param string $file     the name of the file that hooks into
54          * @param string $function the name of the function that the hook will call
55          * @param int    $priority A priority (defaults to 0)
56          * @return mixed|bool
57          */
58         public static function register($hook, $file, $function, $priority = 0)
59         {
60                 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
61
62                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
63                 if (DBA::exists('hook', $condition)) {
64                         return true;
65                 }
66
67                 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
68
69                 return $result;
70         }
71
72         /**
73          * Unregisters a hook.
74          *
75          * @param string $hook     the name of the hook
76          * @param string $file     the name of the file that hooks into
77          * @param string $function the name of the function that the hook called
78          * @return boolean
79          */
80         public static function unregister($hook, $file, $function)
81         {
82                 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
83
84                 // This here is only needed for fixing a problem that existed on the develop branch
85                 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
86                 DBA::delete('hook', $condition);
87
88                 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
89                 $result = DBA::delete('hook', $condition);
90                 return $result;
91         }
92
93         /**
94          * Returns the list of callbacks for a single hook
95          *
96          * @param  string $name
97          * @return array
98          */
99         public static function getByName($name)
100         {
101                 $return = [];
102
103                 if (isset(self::$hooks[$name])) {
104                         $return = self::$hooks[$name];
105                 }
106
107                 return $return;
108         }
109
110         /**
111          * @brief Forks a hook.
112          *
113          * Use this function when you want to fork a hook via the worker.
114          *
115          * @param string       $name of the hook to call
116          * @param string|array $data to transmit to the callback handler
117          */
118         public static function fork($priority, $name, $data = null)
119         {
120                 if (array_key_exists($name, self::$hooks)) {
121                         foreach (self::$hooks[$name] as $hook) {
122                                 Worker::add($priority, 'ForkHook', $name, $hook, $data);
123                         }
124                 }
125         }
126
127         /**
128          * @brief Calls a hook.
129          *
130          * Use this function when you want to be able to allow a hook to manipulate
131          * the provided data.
132          *
133          * @param string       $name  of the hook to call
134          * @param string|array &$data to transmit to the callback handler
135          */
136         public static function callAll($name, &$data = null)
137         {
138                 if (array_key_exists($name, self::$hooks)) {
139                         foreach (self::$hooks[$name] as $hook) {
140                                 self::callSingle(self::getApp(), $name, $hook, $data);
141                         }
142                 }
143         }
144
145         /**
146          * @brief Calls a single hook.
147          *
148          * @param App $a
149          * @param string         $name of the hook to call
150          * @param array          $hook Hook data
151          * @param string|array   &$data to transmit to the callback handler
152          */
153         public static function callSingle(App $a, $name, $hook, &$data = null)
154         {
155                 // Don't run a theme's hook if the user isn't using the theme
156                 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
157                         return;
158                 }
159
160                 @include_once($hook[0]);
161                 if (function_exists($hook[1])) {
162                         $func = $hook[1];
163                         $func($a, $data);
164                 } else {
165                         // remove orphan hooks
166                         $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
167                         DBA::delete('hook', $condition, ['cascade' => false]);
168                 }
169         }
170
171         /**
172          * check if an app_menu hook exist for addon $name.
173          * Return true if the addon is an app
174          */
175         public static function isAddonApp($name)
176         {
177                 if (array_key_exists('app_menu', self::$hooks)) {
178                         foreach (self::$hooks['app_menu'] as $hook) {
179                                 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {
180                                         return true;
181                                 }
182                         }
183                 }
184
185                 return false;
186         }
187 }