]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Addon.php
Renamed System::redirect() to $a->redirect()
[friendica.git] / src / Core / Addon.php
index 3adc7fc0ec284d0714fa96135a42c8e8ff161287..d2f89ce279de91867fe1edf9de53c3a8c8514620 100644 (file)
@@ -4,19 +4,74 @@
  */
 namespace Friendica\Core;
 
-use Friendica\Core\Config;
-use Friendica\Database\DBM;
-use Friendica\Core\Worker;
-
-use dba;
+use Friendica\App;
+use Friendica\BaseObject;
+use Friendica\Database\DBA;
 
 require_once 'include/dba.php';
 
 /**
  * Some functions to handle addons
  */
-class Addon
+class Addon extends BaseObject
 {
+       /**
+        * @brief Synchronise addons:
+        *
+        * system.addon contains a comma-separated list of names
+        * of addons which are used on this system.
+        * Go through the database list of already installed addons, and if we have
+        * an entry, but it isn't in the config list, call the uninstall procedure
+        * and mark it uninstalled in the database (for now we'll remove it).
+        * Then go through the config list and if we have a addon that isn't installed,
+        * call the install procedure and add it to the database.
+        *
+        */
+       public static function check()
+       {
+               $a = self::getApp();
+
+               $r = DBA::select('addon', [], ['installed' => 1]);
+               if (DBA::isResult($r)) {
+                       $installed = DBA::toArray($r);
+               } else {
+                       $installed = [];
+               }
+
+               $addons = Config::get('system', 'addon');
+               $addons_arr = [];
+
+               if ($addons) {
+                       $addons_arr = explode(',', str_replace(' ', '', $addons));
+               }
+
+               $a->addons = $addons_arr;
+
+               $installed_arr = [];
+
+               if (count($installed)) {
+                       foreach ($installed as $i) {
+                               if (!in_array($i['name'], $addons_arr)) {
+                                       self::uninstall($i['name']);
+                               } else {
+                                       $installed_arr[] = $i['name'];
+                               }
+                       }
+               }
+
+               if (count($addons_arr)) {
+                       foreach ($addons_arr as $p) {
+                               if (!in_array($p, $installed_arr)) {
+                                       self::install($p);
+                               }
+                       }
+               }
+
+               self::loadHooks();
+
+               return;
+       }
+
        /**
         * @brief uninstalls an addon.
         *
@@ -26,7 +81,7 @@ class Addon
        public static function uninstall($addon)
        {
                logger("Addons: uninstalling " . $addon);
-               dba::delete('addon', ['name' => $addon]);
+               DBA::delete('addon', ['name' => $addon]);
 
                @include_once('addon/' . $addon . '/' . $addon . '.php');
                if (function_exists($addon . '_uninstall')) {
@@ -57,7 +112,7 @@ class Addon
 
                        $addon_admin = (function_exists($addon."_addon_admin") ? 1 : 0);
 
-                       dba::insert('addon', ['name' => $addon, 'installed' => true,
+                       DBA::insert('addon', ['name' => $addon, 'installed' => true,
                                                'timestamp' => $t, 'plugin_admin' => $addon_admin]);
 
                        // we can add the following with the previous SQL
@@ -65,7 +120,7 @@ class Addon
                        // This way the system won't fall over dead during the update.
 
                        if (file_exists('addon/' . $addon . '/.hidden')) {
-                               dba::update('addon', ['hidden' => true], ['name' => $addon]);
+                               DBA::update('addon', ['hidden' => true], ['name' => $addon]);
                        }
                        return true;
                } else {
@@ -81,9 +136,9 @@ class Addon
        {
                $addons = Config::get('system', 'addon');
                if (strlen($addons)) {
-                       $r = dba::select('addon', [], ['installed' => 1]);
-                       if (DBM::is_result($r)) {
-                               $installed = dba::inArray($r);
+                       $r = DBA::select('addon', [], ['installed' => 1]);
+                       if (DBA::isResult($r)) {
+                               $installed = DBA::toArray($r);
                        } else {
                                $installed = [];
                        }
@@ -110,7 +165,7 @@ class Addon
                                                                        $func = $addon . '_install';
                                                                        $func();
                                                                }
-                                                               dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
+                                                               DBA::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
                                                        }
                                                }
                                        }
@@ -127,7 +182,7 @@ class Addon
         */
        public static function isEnabled($addon)
        {
-               return dba::exists('addon', ['installed' => true, 'name' => $addon]);
+               return DBA::exists('addon', ['installed' => true, 'name' => $addon]);
        }
 
 
@@ -142,13 +197,15 @@ class Addon
         */
        public static function registerHook($hook, $file, $function, $priority = 0)
        {
+               $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
+
                $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
-               $exists = dba::exists('hook', $condition);
+               $exists = DBA::exists('hook', $condition);
                if ($exists) {
                        return true;
                }
 
-               $r = dba::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
+               $r = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
 
                return $r;
        }
@@ -163,8 +220,14 @@ class Addon
         */
        public static function unregisterHook($hook, $file, $function)
        {
+               $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
+
+               // This here is only needed for fixing a problem that existed on the develop branch
                $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
-               $r = dba::delete('hook', $condition);
+               DBA::delete('hook', $condition);
+
+               $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
+               $r = DBA::delete('hook', $condition);
                return $r;
        }
 
@@ -173,17 +236,17 @@ class Addon
         */
        public static function loadHooks()
        {
-               $a = get_app();
+               $a = self::getApp();
                $a->hooks = [];
-               $r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
+               $r = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
 
-               while ($rr = dba::fetch($r)) {
+               while ($rr = DBA::fetch($r)) {
                        if (! array_key_exists($rr['hook'], $a->hooks)) {
                                $a->hooks[$rr['hook']] = [];
                        }
                        $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
                }
-               dba::close($r);
+               DBA::close($r);
        }
 
        /**
@@ -196,7 +259,7 @@ class Addon
         */
        public static function forkHooks($priority, $name, $data = null)
        {
-               $a = get_app();
+               $a = self::getApp();
 
                if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
                        foreach ($a->hooks[$name] as $hook) {
@@ -216,7 +279,7 @@ class Addon
         */
        public static function callHooks($name, &$data = null)
        {
-               $a = get_app();
+               $a = self::getApp();
 
                if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
                        foreach ($a->hooks[$name] as $hook) {
@@ -228,14 +291,15 @@ class Addon
        /**
         * @brief Calls a single hook.
         *
-        * @param string $name of the hook to call
-        * @param array $hook Hook data
-        * @param string|array &$data to transmit to the callback handler
+        * @param App $a
+        * @param string         $name of the hook to call
+        * @param array          $hook Hook data
+        * @param string|array   &$data to transmit to the callback handler
         */
-       public static function callSingleHook($a, $name, $hook, &$data = null)
+       public static function callSingleHook(App $a, $name, $hook, &$data = null)
        {
                // Don't run a theme's hook if the user isn't using the theme
-               if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false) {
+               if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
                        return;
                }
 
@@ -246,7 +310,7 @@ class Addon
                } else {
                        // remove orphan hooks
                        $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
-                       dba::delete('hook', $condition, ['cascade' => false]);
+                       DBA::delete('hook', $condition, ['cascade' => false]);
                }
        }
 
@@ -256,7 +320,7 @@ class Addon
         */
        public static function isApp($name)
        {
-               $a = get_app();
+               $a = self::getApp();
 
                if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
                        foreach ($a->hooks['app_menu'] as $hook) {
@@ -287,7 +351,7 @@ class Addon
         */
        public static function getInfo($addon)
        {
-               $a = get_app();
+               $a = self::getApp();
 
                $info = [
                        'name' => $addon,
@@ -304,7 +368,7 @@ class Addon
 
                $stamp1 = microtime(true);
                $f = file_get_contents("addon/$addon/$addon.php");
-               $a->save_timestamp($stamp1, "file");
+               $a->saveTimestamp($stamp1, "file");
 
                $r = preg_match("|/\*.*\*/|msU", $f, $m);
 
@@ -313,7 +377,12 @@ class Addon
                        foreach ($ll as $l) {
                                $l = trim($l, "\t\n\r */");
                                if ($l != "") {
-                                       list($type, $v) = array_map("trim", explode(":", $l, 2));
+                                       $addon_info = array_map("trim", explode(":", $l, 2));
+                                       if (count($addon_info) < 2) {
+                                               continue;
+                                       }
+
+                                       list($type, $v) = $addon_info;
                                        $type = strtolower($type);
                                        if ($type == "author" || $type == "maintainer") {
                                                $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);