]> git.mxchange.org Git - friendica.git/blob - src/Core/Hook.php
Merge pull request #5997 from annando/notice-relocation
[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 Name of the hook
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 integer $priority of the hook
116          * @param string  $name     of the hook to call
117          * @param mixed   $data     to transmit to the callback handler
118          */
119         public static function fork($priority, $name, $data = null)
120         {
121                 if (array_key_exists($name, self::$hooks)) {
122                         foreach (self::$hooks[$name] as $hook) {
123                                 Worker::add($priority, 'ForkHook', $name, $hook, $data);
124                         }
125                 }
126         }
127
128         /**
129          * @brief Calls a hook.
130          *
131          * Use this function when you want to be able to allow a hook to manipulate
132          * the provided data.
133          *
134          * @param string       $name  of the hook to call
135          * @param string|array &$data to transmit to the callback handler
136          */
137         public static function callAll($name, &$data = null)
138         {
139                 if (array_key_exists($name, self::$hooks)) {
140                         foreach (self::$hooks[$name] as $hook) {
141                                 self::callSingle(self::getApp(), $name, $hook, $data);
142                         }
143                 }
144         }
145
146         /**
147          * @brief Calls a single hook.
148          *
149          * @param App $a
150          * @param string         $name of the hook to call
151          * @param array          $hook Hook data
152          * @param string|array   &$data to transmit to the callback handler
153          */
154         public static function callSingle(App $a, $name, $hook, &$data = null)
155         {
156                 // Don't run a theme's hook if the user isn't using the theme
157                 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
158                         return;
159                 }
160
161                 @include_once($hook[0]);
162                 if (function_exists($hook[1])) {
163                         $func = $hook[1];
164                         $func($a, $data);
165                 } else {
166                         // remove orphan hooks
167                         $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
168                         DBA::delete('hook', $condition, ['cascade' => false]);
169                 }
170         }
171
172         /**
173          * Checks if an app_menu hook exist for the provided addon name.
174          * Return true if the addon is an app
175          *
176          * @param string $name Name of the addon
177          * @return boolean
178          */
179         public static function isAddonApp($name)
180         {
181                 if (array_key_exists('app_menu', self::$hooks)) {
182                         foreach (self::$hooks['app_menu'] as $hook) {
183                                 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {
184                                         return true;
185                                 }
186                         }
187                 }
188
189                 return false;
190         }
191 }