Automatically set default account when adding or removing one
[tt-rss-micropub.git] / init.php
index 4c0f598aae9b061ba1bf9a48d3b1a19b821645ac..5b26c226820075474a8fb7d149c537acd6cf3e59 100644 (file)
--- a/init.php
+++ b/init.php
@@ -83,10 +83,19 @@ class Micropub extends Plugin implements IHandler
             . '?reply=' . urlencode($article['link']);
         // did I tell you I hate dojo/dijit?
 
-        $accounts = array_keys(PluginHost::getInstance()->get($this, 'accounts', []));
+        $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
         if (!count($accounts)) {
             return $article;
         }
+
+        $accountUrls = array_keys($accounts);
+        $defaultAccount = null;
+        foreach ($accounts as $url => $account) {
+            if ($account['default']) {
+                $defaultAccount = $url;
+            }
+        }
+
         ob_start();
         include __DIR__ . '/commentform.phtml';
         $html = ob_get_clean();
@@ -118,6 +127,13 @@ class Micropub extends Plugin implements IHandler
             $accordionActive = '';
         }
 
+        foreach ($accounts as $url => $account) {
+            $accounts[$url]['checked'] = '';
+            if ($account['default']) {
+                $accounts[$url]['checked'] = 'checked="checked"';
+            }
+        }
+
         //FIXME: default identity
         include __DIR__ . '/settings.phtml';
     }
@@ -162,6 +178,8 @@ class Micropub extends Plugin implements IHandler
             return $this->postAction();
         } else if ($mode == 'deleteIdentity') {
             return $this->deleteIdentityAction();
+        } else if ($mode == 'setDefaultIdentity') {
+            return $this->setDefaultIdentityAction();
         } else {
             return $this->errorOut('Unsupported mode');
         }
@@ -357,6 +375,7 @@ class Micropub extends Plugin implements IHandler
             'access_token' => $data['access_token'],
             'scope'        => $data['scope'],
         ];
+        $accounts = $this->fixDefaultIdentity($accounts);
         $host->set($this, 'accounts', $accounts);
 
         //all fine now.
@@ -383,9 +402,10 @@ class Micropub extends Plugin implements IHandler
         }
 
         unset($accounts[$me]);
+        $accounts = $this->fixDefaultIdentity($accounts);
         $host->set($this, 'accounts', $accounts);
-        header('Content-type: application/json');
 
+        header('Content-type: application/json');
         echo json_encode(
             [
                 'code'     => '200',
@@ -395,6 +415,63 @@ class Micropub extends Plugin implements IHandler
         exit();
     }
 
+    /**
+     * Backend preferences action: Make a given account the default
+     */
+    protected function setDefaultIdentityAction()
+    {
+        if (!isset($_POST['me'])) {
+            return $this->errorOut('"me" parameter missing');
+        }
+        $me = trim($_POST['me']);
+
+        $host = PluginHost::getInstance();
+        $accounts = $host->get($this, 'accounts', []);
+        if (!isset($accounts[$me])) {
+            return $this->errorOut('Unknown identity');
+        }
+        foreach ($accounts as $url => $data) {
+            $accounts[$url]['default'] = ($url == $me);
+        }
+        $host->set($this, 'accounts', $accounts);
+
+        header('Content-type: application/json');
+        echo json_encode(
+            [
+                'code'     => '200',
+                'message'  => 'Default account set',
+            ]
+        );
+        exit();
+    }
+
+    /**
+     * Set the default identity if there is none
+     *
+     * @param array $accounts Array of account data arrays
+     *
+     * @return array Array of account data arrays
+     */
+    protected function fixDefaultIdentity($accounts)
+    {
+        if (!count($accounts)) {
+            return $accounts;
+        }
+
+        $hasDefault = false;
+        foreach ($accounts as $account) {
+            if ($account['default']) {
+                $hasDefault = true;
+            }
+        }
+
+        if (!$hasDefault) {
+            reset($accounts);
+            $accounts[key($accounts)]['default'] = true;
+        }
+        return $accounts;
+    }
+
     /**
      * Send an error message.
      * Automatically in the correct format (plain text or json)
@@ -455,9 +532,27 @@ class Micropub extends Plugin implements IHandler
         return $links;
     }
 
+    /**
+     * If a valid CSRF token is necessary or not
+     *
+     * @param string $method Plugin method name (here: "action")
+     *
+     * @return boolean True if an invalid CSRF token shall be ignored
+     */
     function csrf_ignore($method)
     {
-        return true;
+        $mode = null;
+        if (isset($_POST['mode'])) {
+            $mode = $_POST['mode'];
+        } else if (isset($_GET['mode'])) {
+            $mode = $_GET['mode'];
+        }
+
+        if ($mode == 'authreturn') {
+            return true;
+        }
+
+        return false;
     }
 
     /**