3 * Simple Micropub client to post reponses
7 * @author Christian Weiske <cweiske@cweiske.de>
8 * @license AGPLv3 or later
9 * @link https://www.w3.org/TR/micropub/
11 class Micropub extends Plugin implements IHandler
14 * Dumb workaround for "private $host" in Plugin class
15 * + the re-creation of the plugin instance without calling init().
18 * @see https://discourse.tt-rss.org/t//208
19 * @see https://discourse.tt-rss.org/t//209
21 protected static $myhost;
23 public function __construct()
25 //do nothing. only here to not have micropub() called as constructor
28 public function about()
38 public function api_version()
47 public function init(/*PluginHost*/ $host)
49 static::$myhost = $host;
50 $host->add_hook($host::HOOK_PREFS_TAB, $this);
51 $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
52 //command line option --micropub
54 'micropub', 'Add Micropub identity', $this, ':', 'MODE'
59 * @param array $article Article data. Keys:
72 * - always_display_enclosures
80 public function hook_render_article($article)
82 $quillUrl = 'https://quill.p3k.io/new'
83 . '?reply=' . urlencode($article['link']);
84 // did I tell you I hate dojo/dijit?
86 $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
87 if (!count($accounts)) {
91 $accountUrls = array_keys($accounts);
92 $defaultAccount = null;
93 foreach ($accounts as $url => $account) {
94 if ($account['default']) {
95 $defaultAccount = $url;
100 include __DIR__ . '/commentform.phtml';
101 $html = ob_get_clean();
102 $article['content'] .= $html;
108 * Render our configuration page.
109 * Directly echo it out.
111 * @param string $args Preferences tab that is currently open
115 public function hook_prefs_tab($args)
117 if ($args != "prefPrefs") {
121 $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
122 if (isset($_REQUEST['accordion'])
123 && $_REQUEST['accordion'] == 'micropub'
125 $accordionActive = 'selected="true"';
127 $accordionActive = '';
130 foreach ($accounts as $url => $account) {
131 $accounts[$url]['checked'] = '';
132 if ($account['default']) {
133 $accounts[$url]['checked'] = 'checked="checked"';
137 //FIXME: default identity
138 include __DIR__ . '/settings.phtml';
141 public function get_prefs_js()
143 return file_get_contents(__DIR__ . '/settings.js');
149 public function micropub($args)
151 //we do not get all arguments passed here, to we work around
152 $args = $GLOBALS['argv'];
153 array_shift($args);//update.php
154 array_shift($args);//--micropub
155 $mode = array_shift($args);
156 return $this->action($mode, $args);
161 * Also used by micropub() cli command method.
163 * /backend.php?op=pluginhandler&plugin=micropub&method=action
165 public function action($mode = null, $args = [])
167 if (isset($_POST['mode'])) {
168 $mode = $_POST['mode'];
169 } else if (isset($_GET['mode'])) {
170 $mode = $_GET['mode'];
173 if ($mode == 'authorize') {
174 return $this->authorizeAction($args);
175 } else if ($mode == 'authreturn') {
176 return $this->authreturnAction();
177 } else if ($mode == 'post') {
178 return $this->postAction();
179 } else if ($mode == 'deleteIdentity') {
180 return $this->deleteIdentityAction();
181 } else if ($mode == 'setDefaultIdentity') {
182 return $this->setDefaultIdentityAction();
184 return $this->errorOut('Unsupported mode');
189 * Post a comment, like or bookmark via micropub
191 protected function postAction()
194 if (isset($_POST['action'])) {
195 $action = trim($_POST['action']);
197 if (array_search($action, ['bookmark', 'comment', 'like']) === false) {
198 return $this->errorOut('"action" parameter invalid');
201 if (!isset($_POST['me'])) {
202 return $this->errorOut('"me" parameter missing');
204 $me = trim($_POST['me']);
205 $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
206 if (!isset($accounts[$me])) {
207 return $this->errorOut('"me" parameter invalid');
209 $account = $accounts[$me];
211 if (!isset($_POST['postUrl'])) {
212 return $this->errorOut('"postUrl" parameter missing');
214 $postUrl = trim($_POST['postUrl']);
216 if ($action == 'comment') {
217 if (!isset($_POST['content'])) {
218 return $this->errorOut('"content" parameter missing');
220 $content = trim($_POST['content']);
221 if (!strlen($_POST['content'])) {
222 return $this->errorOut('"content" is empty');
227 $links = $this->getLinks($me);
228 if (!count($links)) {
229 return $this->errorOut('No links found');
231 if (!isset($links['micropub'])) {
232 return $this->errorOut('No micropub endpoint found');
236 'access_token' => $account['access_token'],
240 if ($action == 'bookmark') {
241 $parameters['bookmark-of'] = $postUrl;
243 } else if ($action == 'comment') {
244 $parameters['in-reply-to'] = $postUrl;
245 $parameters['content'] = $content;
247 } else if ($action == 'like') {
248 $parameters['like-of'] = $postUrl;
252 /* unfortunately fetch_file_contents() does not return headers
253 so we have to bring our own way to POST data */
257 'header' => 'Content-type: application/x-www-form-urlencoded',
258 'content' => http_build_query($parameters),
259 'ignore_errors' => true,
263 $links['micropub'], 'r', false,
264 stream_context_create($opts)
266 $meta = stream_get_meta_data($stream);
267 $headers = $meta['wrapper_data'];
268 $content = stream_get_contents($stream);
270 //we hope there were no redirects and this is actually the only
271 // HTTP line in the headers
272 $status = array_shift($headers);
273 list($httpver, $code, $text) = explode(' ', $status, 3);
274 if ($code != 201 && $code != 202) {
275 $errData = json_decode($content);
276 if (isset($errData->error_description)
277 && $errData->error_description != ''
279 return $this->errorOut(
280 'Error creating post: '
281 . $errData->error_description
284 return $this->errorOut(
285 'Error creating post: '
286 . $code . ' ' . $text.$content
291 foreach ($headers as $header) {
292 $parts = explode(':', $header, 2);
293 if (count($parts) == 2 && strtolower($parts[0]) == 'location') {
294 $location = trim($parts[1]);
297 if ($location === null) {
298 return $this->errorOut(
299 'Location header missing in successful creation response.'
303 header('Content-type: application/json');
306 'code' => intval($code),
307 'location' => $location,
308 'message' => 'Post created',
314 protected function authorizeAction($args = [])
317 $url = array_shift($args);
318 } else if (isset($_POST['url'])) {
319 $url = $_POST['url'];
321 if (!filter_var($url, FILTER_VALIDATE_URL)) {
322 return $this->errorOut('Invalid URL');
325 //step 1: micropub discovery
326 $links = $this->getLinks($url);
328 if (!count($links)) {
329 return $this->errorOut('No links found');
331 if (!isset($links['micropub'])) {
332 return $this->errorOut('No micropub endpoint found');
334 if (!isset($links['token_endpoint'])) {
335 return $this->errorOut('No token endpoint found');
337 if (!isset($links['authorization_endpoint'])) {
338 return $this->errorOut('No authorization endpoint found');
341 $redirUrl = get_self_url_prefix() . '/backend.php'
342 . '?op=micropub&method=action&mode=authreturn';
343 $authUrl = $links['authorization_endpoint']
345 . '&redirect_uri=' . urlencode($redirUrl)
346 . '&client_id=' . urlencode(get_self_url_prefix())//FIXME: app info
347 //. '&state=' . 'FIXME'
349 . '&response_type=code';
350 header('Location: ' . $authUrl);
351 echo $authUrl . "\n";
356 * Return from authorization
358 public function authreturnAction()
360 if (!isset($_GET['me'])) {
361 return $this->errorOut('"me" parameter missing');
363 if (!isset($_GET['code'])) {
364 return $this->errorOut('"code" parameter missing');
367 $links = $this->getLinks($_GET['me']);
368 if (!isset($links['token_endpoint'])) {
369 return $this->errorOut('No token endpoint found');
372 //obtain access token from the code
373 $redirUrl = get_self_url_prefix() . '/backend.php'
374 . '?op=micropub&method=action&mode=authreturn';
375 $res = fetch_file_contents(
377 //FIXME: add accept header once this is fixed:
378 // https://discourse.tt-rss.org/t//207
379 'url' => $links['token_endpoint'],
381 'grant_type' => 'authorization_code',
383 'code' => $_GET['code'],
384 'redirect_uri' => $redirUrl,
385 'client_id' => get_self_url_prefix()
390 //we have no way to get the content type :/
391 if ($res{0} == '{') {
393 $data = json_decode($res);
395 parse_str($res, $data);
397 if (!isset($data['access_token'])) {
398 return $this->errorOut('access token missing');
400 if (!isset($data['me'])) {
401 return $this->errorOut('access token missing');
403 if (!isset($data['scope'])) {
404 return $this->errorOut('scope token missing');
407 $host = PluginHost::getInstance();
408 $accounts = $host->get($this, 'accounts', []);
409 $accounts[$data['me']] = [
410 'access_token' => $data['access_token'],
411 'scope' => $data['scope'],
413 $accounts = $this->fixDefaultIdentity($accounts);
414 $host->set($this, 'accounts', $accounts);
417 //the accordion parameter will never work
418 // because fox has serious mental problems
419 // https://discourse.tt-rss.org/t/open-a-certain-accordion-in-preferences-by-url-parameter/234
420 header('Location: prefs.php?accordion=micropub');
424 * Backend preferences action: Remove a given account
426 protected function deleteIdentityAction()
428 if (!isset($_POST['me'])) {
429 return $this->errorOut('"me" parameter missing');
431 $me = trim($_POST['me']);
433 $host = PluginHost::getInstance();
434 $accounts = $host->get($this, 'accounts', []);
435 if (!isset($accounts[$me])) {
436 return $this->errorOut('Unknown identity');
439 unset($accounts[$me]);
440 $accounts = $this->fixDefaultIdentity($accounts);
441 $host->set($this, 'accounts', $accounts);
443 header('Content-type: application/json');
447 'message' => 'Identity removed',
454 * Backend preferences action: Make a given account the default
456 protected function setDefaultIdentityAction()
458 if (!isset($_POST['me'])) {
459 return $this->errorOut('"me" parameter missing');
461 $me = trim($_POST['me']);
463 $host = PluginHost::getInstance();
464 $accounts = $host->get($this, 'accounts', []);
465 if (!isset($accounts[$me])) {
466 return $this->errorOut('Unknown identity');
468 foreach ($accounts as $url => $data) {
469 $accounts[$url]['default'] = ($url == $me);
471 $host->set($this, 'accounts', $accounts);
473 header('Content-type: application/json');
477 'message' => 'Default account set',
484 * Set the default identity if there is none
486 * @param array $accounts Array of account data arrays
488 * @return array Array of account data arrays
490 protected function fixDefaultIdentity($accounts)
492 if (!count($accounts)) {
497 foreach ($accounts as $account) {
498 if ($account['default']) {
505 $accounts[key($accounts)]['default'] = true;
511 * Send an error message.
512 * Automatically in the correct format (plain text or json)
514 * @param string $msg Error message
518 protected function errorOut($msg)
520 header('HTTP/1.0 400 Bad Request');
522 //this does not take "q"uality values into account, I know.
523 if (isset($_SERVER['HTTP_ACCEPT'])
524 && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false
527 header('Content-type: application/json');
534 header('Content-type: text/plain');
541 * Extract link relations from a given URL
543 protected function getLinks($url)
545 //FIXME: HTTP Link header support with HTTP2
546 $html = fetch_file_contents(
551 //Loading invalid HTML is tedious.
552 // quick hack with regex. yay!
553 preg_match_all('#<link[^>]+?>#', $html, $matches);
555 foreach ($matches[0] as $match) {
556 if (substr($match, -2) != '/>') {
557 //make it valid xml...
558 $match = substr($match, 0, -1) . '/>';
560 $sx = simplexml_load_string($match);
561 if (isset($sx['rel']) && isset($sx['href'])
562 && !isset($links[(string) $sx['rel']])
564 $links[(string) $sx['rel']] = (string) $sx['href'];
571 * If a valid CSRF token is necessary or not
573 * @param string $method Plugin method name (here: "action")
575 * @return boolean True if an invalid CSRF token shall be ignored
577 function csrf_ignore($method)
580 if (isset($_POST['mode'])) {
581 $mode = $_POST['mode'];
582 } else if (isset($_GET['mode'])) {
583 $mode = $_GET['mode'];
586 if ($mode == 'authreturn') {
594 * Check which method is allowed via HTTP
596 function before($method)
598 if ($method == 'action') {