3 * @file src/Core/Addon.php
\r
5 namespace Friendica\Core;
\r
8 use Friendica\Core\Config;
\r
9 use Friendica\Core\System;
\r
10 use Friendica\Database\DBM;
\r
13 require_once 'include/dba.php';
\r
16 * Some functions to handle addons
\r
21 * @brief uninstalls an addon.
\r
23 * @param string $addon name of the addon
\r
26 public static function uninstall($addon)
\r
28 logger("Addons: uninstalling " . $addon);
\r
29 dba::delete('addon', ['name' => $addon]);
\r
31 @include_once('addon/' . $addon . '/' . $addon . '.php');
\r
32 if (function_exists($addon . '_uninstall')) {
\r
33 $func = $addon . '_uninstall';
\r
39 * @brief installs an addon.
\r
41 * @param string $addon name of the addon
\r
44 public static function install($addon)
\r
46 // silently fail if addon was removed
\r
48 if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) {
\r
51 logger("Addons: installing " . $addon);
\r
52 $t = @filemtime('addon/' . $addon . '/' . $addon . '.php');
\r
53 @include_once('addon/' . $addon . '/' . $addon . '.php');
\r
54 if (function_exists($addon . '_install')) {
\r
55 $func = $addon . '_install';
\r
58 $addon_admin = (function_exists($addon."_plugin_admin") ? 1 : 0);
\r
60 dba::insert('addon', ['name' => $addon, 'installed' => true,
\r
61 'timestamp' => $t, 'plugin_admin' => $addon_admin]);
\r
63 // we can add the following with the previous SQL
\r
64 // once most site tables have been updated.
\r
65 // This way the system won't fall over dead during the update.
\r
67 if (file_exists('addon/' . $addon . '/.hidden')) {
\r
68 dba::update('addon', ['hidden' => true], ['name' => $addon]);
\r
72 logger("Addons: FAILED installing " . $addon);
\r
78 * reload all updated addons
\r
80 public static function reload()
\r
82 $addons = Config::get('system', 'addon');
\r
83 if (strlen($addons)) {
\r
85 $r = dba::select('addon', [], ['installed' => 1]);
\r
86 if (DBM::is_result($r)) {
\r
92 $addon_list = explode(',', $addons);
\r
94 if (count($addon_list)) {
\r
95 foreach ($addon_list as $addon) {
\r
97 $addon = trim($addon);
\r
99 $fname = 'addon/' . $addon . '/' . $addon . '.php';
\r
101 if (file_exists($fname)) {
\r
102 $t = @filemtime($fname);
\r
103 foreach ($installed as $i) {
\r
104 if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
\r
105 logger('Reloading addon: ' . $i['name']);
\r
106 @include_once($fname);
\r
108 if (function_exists($addon . '_uninstall')) {
\r
109 $func = $addon . '_uninstall';
\r
112 if (function_exists($addon . '_install')) {
\r
113 $func = $addon . '_install';
\r
116 dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
\r
126 * @brief check if addon is enabled
\r
128 * @param string $addon
\r
131 public static function isEnabled($addon)
\r
133 return dba::exists('addon', ['installed' => true, 'name' => $addon]);
\r
138 * @brief registers a hook.
\r
140 * @param string $hook the name of the hook
\r
141 * @param string $file the name of the file that hooks into
\r
142 * @param string $function the name of the function that the hook will call
\r
143 * @param int $priority A priority (defaults to 0)
\r
144 * @return mixed|bool
\r
146 public static function registerHook($hook, $file, $function, $priority = 0)
\r
148 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
\r
149 $exists = dba::exists('hook', $condition);
\r
154 $r = dba::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
\r
160 * @brief unregisters a hook.
\r
162 * @param string $hook the name of the hook
\r
163 * @param string $file the name of the file that hooks into
\r
164 * @param string $function the name of the function that the hook called
\r
167 public static function unregisterHook($hook, $file, $function)
\r
169 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
\r
170 $r = dba::delete('hook', $condition);
\r
175 public static function loadHooks() {
\r
178 $r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
\r
180 while ($rr = dba::fetch($r)) {
\r
181 if (! array_key_exists($rr['hook'],$a->hooks)) {
\r
182 $a->hooks[$rr['hook']] = [];
\r
184 $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
\r
190 * @brief Calls a hook.
\r
192 * Use this function when you want to be able to allow a hook to manipulate
\r
193 * the provided data.
\r
195 * @param string $name of the hook to call
\r
196 * @param string|array &$data to transmit to the callback handler
\r
198 public static function callHooks($name, &$data = null)
\r
202 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
\r
203 foreach ($a->hooks[$name] as $hook) {
\r
204 self::callSingleHook($a, $name, $hook, $data);
\r
210 * @brief Calls a single hook.
\r
212 * @param string $name of the hook to call
\r
213 * @param array $hook Hook data
\r
214 * @param string|array &$data to transmit to the callback handler
\r
216 public static function callSingleHook($a, $name, $hook, &$data = null)
\r
218 // Don't run a theme's hook if the user isn't using the theme
\r
219 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
\r
222 @include_once($hook[0]);
\r
223 if (function_exists($hook[1])) {
\r
227 // remove orphan hooks
\r
228 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
\r
229 dba::delete('hook', $condition);
\r
234 * check if an app_menu hook exist for addon $name.
\r
235 * Return true if the addon is an app
\r
237 function isApp($name)
\r
241 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
\r
242 foreach ($a->hooks['app_menu'] as $hook) {
\r
243 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
\r
252 * @brief Parse addon comment in search of addon infos.
\r
257 * * Description: An addon which plugs in
\r
258 * . * Version: 1.2.3
\r
259 * * Author: John <profile url>
\r
260 * * Author: Jane <email>
\r
263 * @param string $addon the name of the addon
\r
264 * @return array with the addon information
\r
267 public static function getInfo($addon) {
\r
273 'description' => "",
\r
279 if (!is_file("addon/$addon/$addon.php")) {
\r
283 $stamp1 = microtime(true);
\r
284 $f = file_get_contents("addon/$addon/$addon.php");
\r
285 $a->save_timestamp($stamp1, "file");
\r
287 $r = preg_match("|/\*.*\*/|msU", $f, $m);
\r
290 $ll = explode("\n", $m[0]);
\r
291 foreach ( $ll as $l ) {
\r
292 $l = trim($l,"\t\n\r */");
\r
294 list($k,$v) = array_map("trim", explode(":",$l,2));
\r
295 $k= strtolower($k);
\r
296 if ($k == "author") {
\r
297 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
\r
299 $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];
\r
301 $info['author'][] = ['name'=>$v];
\r
304 if (array_key_exists($k,$info)) {
\r