From 735a0023ccf008d510e0e06f68f87a719251d679 Mon Sep 17 00:00:00 2001 From: brunoccast Date: Mon, 15 Jul 2019 16:22:34 +0100 Subject: [PATCH] [ActivityPub] Routes ActivityPubPlugin: - Update routes to properly use URLMapper - Minor updates --- plugins/ActivityPub/ActivityPubPlugin.php | 108 +++++--------------- plugins/ActivityPub/lib/AcceptHeader.php | 116 ---------------------- 2 files changed, 24 insertions(+), 200 deletions(-) delete mode 100644 plugins/ActivityPub/lib/AcceptHeader.php diff --git a/plugins/ActivityPub/ActivityPubPlugin.php b/plugins/ActivityPub/ActivityPubPlugin.php index 3e27cff425..57e40c224c 100644 --- a/plugins/ActivityPub/ActivityPubPlugin.php +++ b/plugins/ActivityPub/ActivityPubPlugin.php @@ -26,13 +26,9 @@ defined('GNUSOCIAL') || die(); -// Ensure proper timezone -date_default_timezone_set('GMT'); - // Import required files by the plugin require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'httpsignature.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'discoveryhints.php'; -require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'AcceptHeader.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'explorer.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'postman.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'inbox_handler.php'; @@ -135,29 +131,32 @@ class ActivityPubPlugin extends Plugin */ public function onRouterInitialized(URLMapper $m) { - if (ActivityPubURLMapperOverwrite::should()) { - ActivityPubURLMapperOverwrite::variable( - $m, - 'user/:id', - ['id' => '[0-9]+'], - 'apActorProfile' - ); + $acceptHeaders = [ + 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' => 0, + 'application/activity+json' => 1, + 'application/json' => 2, + 'application/ld+json' => 3 + ]; - // Special route for webfinger purposes - ActivityPubURLMapperOverwrite::variable( - $m, - ':nickname', - ['nickname' => Nickname::DISPLAY_FMT], - 'apActorProfile' - ); - } + $m->connect('user/:id', + ['action' => 'apActorProfile'], + ['id' => '[0-9]+'], + $acceptHeaders); - // No .json here for convenience purposes on Notice grabber - $m->connect( - 'note/:id', - ['action' => 'apNotice'], - ['id' => '[0-9]+'] - ); + $m->connect(':nickname', + ['action' => 'apActorProfile'], + ['nickname' => Nickname::DISPLAY_FMT], + $acceptHeaders); + + $m->connect(':nickname/', + ['action' => 'apActorProfile'], + ['nickname' => Nickname::DISPLAY_FMT], + $acceptHeaders); + + $m->connect('notice/:id', + ['action' => 'apNotice'], + ['id' => '[0-9]+'], + $acceptHeaders); $m->connect( 'user/:id/liked.json', @@ -956,62 +955,3 @@ class ActivityPubReturn exit; } } - -/** - * Overwrites variables in URL-mapping - */ -class ActivityPubURLMapperOverwrite extends URLMapper -{ - /** - * Overwrites a route. - * - * @author Hannes Mannerheim - * @param URLMapper $m - * @param string $path - * @param string $paramPatterns - * @param string $newaction - * @return void - * @throws Exception - */ - public static function variable($m, $path, $paramPatterns, $newaction) - { - $m->connect($path, array('action' => $newaction), $paramPatterns); - $regex = self::makeRegex($path, $paramPatterns); - foreach ($m->variables as $n => $v) { - if ($v[1] == $regex) { - $m->variables[$n][0]['action'] = $newaction; - } - } - } - - /** - * Determines whether the route should or not be overwrited. - * If ACCEPT header isn't set false will be returned. - * - * @author Diogo Cordeiro - * @return boolean true if it should, false otherwise - */ - public static function should() - { - // Do not operate without Accept Header - if (!isset($_SERVER['HTTP_ACCEPT'])) { - return false; - } - - $mimes = [ - 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' => 0, - 'application/activity+json' => 1, - 'application/json' => 2, - 'application/ld+json' => 3 - ]; - - $acceptheader = new AcceptHeader($_SERVER['HTTP_ACCEPT']); - foreach ($acceptheader as $ah) { - if (isset($mimes[$ah['raw']])) { - return true; - } - } - - return false; - } -} diff --git a/plugins/ActivityPub/lib/AcceptHeader.php b/plugins/ActivityPub/lib/AcceptHeader.php deleted file mode 100644 index e146ec8231..0000000000 --- a/plugins/ActivityPub/lib/AcceptHeader.php +++ /dev/null @@ -1,116 +0,0 @@ - - */ -class AcceptHeader extends \ArrayObject -{ - /** - * Constructor - * - * @param string $header Value of the Accept header - * @return void - */ - public function __construct($header) - { - $acceptedTypes = $this->_parse($header); - usort($acceptedTypes, [$this, '_compare']); - parent::__construct($acceptedTypes); - } - - /** - * Parse the accept header and return an array containing - * all the informations about the Accepted types - * - * @param string $data Value of the Accept header - * @return array - */ - private function _parse($data) - { - $array = []; - $items = explode(',', $data); - foreach ($items as $item) { - $elems = explode(';', $item); - - $acceptElement = []; - $mime = current($elems); - list($type, $subtype) = explode('/', $mime); - $acceptElement['type'] = trim($type); - $acceptElement['subtype'] = trim($subtype); - $acceptElement['raw'] = $mime; - - $acceptElement['params'] = []; - while (next($elems)) { - list($name, $value) = explode('=', current($elems)); - $acceptElement['params'][trim($name)] = trim($value); - } - - $array[] = $acceptElement; - } - return $array; - } - - /** - * Compare two Accepted types with their parameters to know - * if one media type should be used instead of an other - * - * @param array $a The first media type and its parameters - * @param array $b The second media type and its parameters - * @return int - */ - private function _compare($a, $b) - { - $a_q = isset($a['params']['q']) ? floatval($a['params']['q']) : 1.0; - $b_q = isset($b['params']['q']) ? floatval($b['params']['q']) : 1.0; - if ($a_q === $b_q) { - $a_count = count($a['params']); - $b_count = count($b['params']); - if ($a_count === $b_count) { - if ($r = $this->_compareSubType($a['subtype'], $b['subtype'])) { - return $r; - } else { - return $this->_compareSubType($a['type'], $b['type']); - } - } else { - return $a_count < $b_count; - } - } else { - return $a_q < $b_q; - } - } - - /** - * Compare two subtypes - * - * @param string $a First subtype to compare - * @param string $b Second subtype to compare - * @return int - */ - private function _compareSubType($a, $b) - { - if ($a === '*' && $b !== '*') { - return 1; - } elseif ($b === '*' && $a !== '*') { - return -1; - } else { - return 0; - } - } -}