From 4c72d2c34ab1d2e5ae3aa402bc36fc6edb75e747 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 16 May 2021 14:32:06 +0000 Subject: [PATCH 1/2] Quickfix for current Json-LD problems --- src/Util/JsonLD.php | 49 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php index a0a272fdd1..15733ac7f8 100644 --- a/src/Util/JsonLD.php +++ b/src/Util/JsonLD.php @@ -67,6 +67,40 @@ class JsonLD return $data; } + public static function fixContext(array $json) + { + // Preparation for adding possibly missing content to the context + if (!empty($json['@context']) && is_string($json['@context'])) { + $json['@context'] = [$json['@context']]; + } + + if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) { + unset($json['@context'][$key]); + $json['@context'] = array_values(array_filter($json['@context'])); + } + + $last_entry = count($json['@context']) - 1; + + $additional = [ + 'w3id' => 'https://w3id.org/security#', + 'signature' => 'w3id:signature', + 'RsaSignature2017' => 'w3id:RsaSignature2017', + 'created' => 'w3id:created', + 'creator' => 'w3id:creator', + 'nonce' => 'w3id:nonce', + 'signatureValue' => 'w3id:signatureValue', + 'publicKey' => 'w3id:publicKey', + 'publicKeyPem' => 'w3id:publicKeyPem']; + + if (is_array($json['@context'][$last_entry])) { + $json['@context'][$last_entry] = array_merge($json['@context'][$last_entry], $additional); + } else { + $json['@context'][] = $additional; + } + + return $json; + } + /** * Normalises a given JSON array * @@ -77,6 +111,8 @@ class JsonLD */ public static function normalize($json) { + $json = self::fixContext($json); + jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader'); $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); @@ -111,6 +147,8 @@ class JsonLD */ public static function compact($json) { + $json = self::fixContext($json); + jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader'); $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#', @@ -126,17 +164,6 @@ class JsonLD 'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'], 'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']]; - // Preparation for adding possibly missing content to the context - if (!empty($json['@context']) && is_string($json['@context'])) { - $json['@context'] = [$json['@context']]; - } - - // Workaround for servers with missing context - // See issue https://github.com/nextcloud/social/issues/330 - if (!empty($json['@context']) && is_array($json['@context'])) { - $json['@context'][] = 'https://w3id.org/security/v1'; - } - // Trying to avoid memory problems with large content fields if (!empty($json['object']['source']['content'])) { $content = $json['object']['source']['content']; From 5ea44960e02c6b68a9ba723d108d273ba600cd3c Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 16 May 2021 14:59:02 +0000 Subject: [PATCH 2/2] Additional change to the quickfix --- src/Util/JsonLD.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php index 15733ac7f8..50396474a6 100644 --- a/src/Util/JsonLD.php +++ b/src/Util/JsonLD.php @@ -152,7 +152,7 @@ class JsonLD jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader'); $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#', - 'w3id' => 'https://w3id.org/security#', + 'w3id' => (object)['@id' => 'https://w3id.org/security#', '@type' => '@id'], 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'], 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'], 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],