From 70e9f60c42ecc31a562037182482d34ae817299a Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Fri, 1 Dec 2017 09:27:06 -0800 Subject: [PATCH] update jsonfeed detection --- lib/XRay/Feeds.php | 14 ++++++++------ lib/XRay/Formats/JSONFeed.php | 4 +--- lib/XRay/Parser.php | 7 +++++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/XRay/Feeds.php b/lib/XRay/Feeds.php index ca49b23..c388629 100644 --- a/lib/XRay/Feeds.php +++ b/lib/XRay/Feeds.php @@ -55,12 +55,14 @@ class Feeds { 'url' => $result['url'], 'type' => 'rss' ]; - } elseif(strpos($contentType, 'application/json') !== false - && substr($body, 0, 1) == '{' && strpos(substr($body, 0, 100), 'https://jsonfeed.org/version/1')) { - $feeds[] = [ - 'url' => $result['url'], - 'type' => 'jsonfeed' - ]; + } elseif(strpos($contentType, 'application/json') !== false && substr($body, 0, 1) == '{') { + $feeddata = json_decode($body, true); + if($feeddata && isset($feeddata['version']) && $feeddata['version'] == 'https://jsonfeed.org/version/1') { + $feeds[] = [ + 'url' => $result['url'], + 'type' => 'jsonfeed' + ]; + } } else { // Some other document was returned, parse the HTML and look for rel alternates and Microformats diff --git a/lib/XRay/Formats/JSONFeed.php b/lib/XRay/Formats/JSONFeed.php index 9117996..ae04071 100644 --- a/lib/XRay/Formats/JSONFeed.php +++ b/lib/XRay/Formats/JSONFeed.php @@ -10,7 +10,7 @@ class JSONFeed extends Format { public static function matches_host($url) { return true; } public static function matches($url) { return true; } - public static function parse($json, $url) { + public static function parse($feed, $url) { $result = [ 'data' => [ 'type' => 'unknown', @@ -18,8 +18,6 @@ class JSONFeed extends Format { 'url' => $url, ]; - $feed = json_decode($json, true); - if($feed) { $result['data']['type'] = 'feed'; diff --git a/lib/XRay/Parser.php b/lib/XRay/Parser.php index 539bd77..252d537 100644 --- a/lib/XRay/Parser.php +++ b/lib/XRay/Parser.php @@ -46,8 +46,11 @@ class Parser { return Formats\XML::parse($body, $url); } - if(substr($body, 0, 1) == '{' && strpos(substr($body, 0, 100), 'https://jsonfeed.org/version/1')) { - return Formats\JSONFeed::parse($body, $url); + if(substr($body, 0, 1) == '{') { + $feeddata = json_decode($body, true); + if($feeddata && isset($feeddata['version']) && $feeddata['version'] == 'https://jsonfeed.org/version/1') { + return Formats\JSONFeed::parse($feeddata, $url); + } } // No special parsers matched, parse for Microformats now