From: Christian Weiske Date: Tue, 30 May 2017 20:20:59 +0000 (+0200) Subject: Automatically set default account when adding or removing one X-Git-Tag: v0.1.0~2 X-Git-Url: https://git.cweiske.de/tt-rss-micropub.git/commitdiff_plain/b6abb2b21098e38267960f938d544ab7f530246f?ds=sidebyside Automatically set default account when adding or removing one --- diff --git a/init.php b/init.php index 7fced8e..5b26c22 100644 --- a/init.php +++ b/init.php @@ -375,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. @@ -401,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', @@ -428,13 +430,12 @@ class Micropub extends Plugin implements IHandler 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'); + header('Content-type: application/json'); echo json_encode( [ 'code' => '200', @@ -444,6 +445,33 @@ class Micropub extends Plugin implements IHandler 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) @@ -504,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; } /** diff --git a/settings.phtml b/settings.phtml index 97e3b8f..083f3d7 100644 --- a/settings.phtml +++ b/settings.phtml @@ -40,7 +40,8 @@ - +