From 34cb0aa406969967fa521211116564805ef6b631 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 20 Sep 2018 05:30:07 +0000 Subject: [PATCH] JSON-LD stuff is now in a separate file --- src/Protocol/ActivityPub.php | 47 ------------------------- src/Util/JsonLD.php | 68 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 47 deletions(-) create mode 100644 src/Util/JsonLD.php diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 4a2394a24..36cc32d64 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -19,8 +19,6 @@ use Friendica\Util\DateTimeFormat; use Friendica\Util\Crypto; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; -use Friendica\Core\Cache; -use digitalbazaar\jsonld; /** * @brief ActivityPub Protocol class @@ -58,51 +56,6 @@ class ActivityPub { const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public'; -public static function jsonld_document_loader($url) -{ - $recursion = 0; - - $x = debug_backtrace(); - if ($x) { - foreach ($x as $n) { - if ($n['function'] === __FUNCTION__) { - $recursion ++; - } - } - } - - if ($recursion > 5) { - logger('jsonld bomb detected at: ' . $url); - exit(); - } - - $result = Cache::get('jsonld_document_loader:' . $url); - if (!is_null($result)) { - return $result; - } - - $data = jsonld_default_document_loader($url); - Cache::set('jsonld_document_loader:' . $url, $data, CACHE_DAY); - return $data; -} - - public static function compactJsonLD($json) - { - jsonld_set_document_loader('Friendica\Protocol\ActivityPub::jsonld_document_loader'); - - $context = (object)['as' => 'https://www.w3.org/ns/activitystreams', - 'w3sec' => 'https://w3id.org/security', - 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'], - 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'], - 'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']]; - - $jsonobj = json_decode(json_encode($json)); - - $compacted = jsonld_compact($jsonobj, $context); - - return json_decode(json_encode($compacted), true); - } - public static function isRequest() { return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') || diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php new file mode 100644 index 000000000..e8ff5888d --- /dev/null +++ b/src/Util/JsonLD.php @@ -0,0 +1,68 @@ + 5) { + logger('jsonld bomb detected at: ' . $url); + exit(); + } + + $result = Cache::get('documentLoader:' . $url); + if (!is_null($result)) { + return $result; + } + + $data = jsonld_default_document_loader($url); + Cache::set('documentLoader:' . $url, $data, CACHE_DAY); + return $data; + } + + public static function normalize($json) + { + jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader'); + + $jsonobj = json_decode(json_encode($json)); + + return jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads')); + } + + public static function compact($json) + { + jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader'); + + $context = (object)['as' => 'https://www.w3.org/ns/activitystreams', + 'w3sec' => 'https://w3id.org/security', + 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'], + 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'], + 'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']]; + + $jsonobj = json_decode(json_encode($json)); + + $compacted = jsonld_compact($jsonobj, $context); + + return json_decode(json_encode($compacted), true); + } +}