From 93938bbaa4ba0813107b8f14a23ae22d979d4827 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 31 Dec 2018 09:34:53 -0100 Subject: [PATCH] compare from a list Signed-off-by: Maxence Lange --- lib/Controller/ActivityPubController.php | 37 ++++++++++++++---------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index 9e0a9a84..b6c9b958 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -316,27 +316,34 @@ class ActivityPubController extends Controller { * @return bool */ private function checkSourceActivityStreams(): bool { + $accepted = [ + 'application/ld+json', + 'application/activity+json' + ]; - // uncomment this line to display the result that would be return to an ActivityPub service (TEST) - // return true; + $accepts = explode(',', $this->request->getHeader('Accept')); + $accepts = array_map([$this, 'trimHeader'], $accepts); - // TODO - cleaner to being able to parse: - // - 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' - // - 'application/activity+json, application/ld+json' - $accept = explode(',', $this->request->getHeader('Accept')); - $accept = array_map('trim', $accept); - - if (in_array('application/ld+json', $accept)) { - return true; - } - - if ($this->request->getHeader('Accept') - === 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"') { - return true; + foreach ($accepts as $accept) { + if (in_array($accept, $accepted)) { + return true; + } } return false; } + + + private function trimHeader(string $header) { + $header = trim($header); + + $pos = strpos($header, ';'); + if ($pos === false) { + return $header; + } + + return substr($header, 0, $pos); + } }