3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
24 use Friendica\Core\Cache\Duration;
25 use Friendica\Core\Logger;
27 use Friendica\Core\System;
29 use Friendica\Protocol\ActivityPub;
32 * This class contain methods to work with JsonLD data
37 * Loader for LD-JSON validation
41 * @return mixed the loaded data
42 * @throws \JsonLdException
44 public static function documentLoader($url)
48 $x = debug_backtrace();
51 if ($n['function'] === __FUNCTION__) {
58 Logger::error('jsonld bomb detected at: ' . $url);
62 $result = DI::cache()->get('documentLoader:' . $url);
63 if (!is_null($result)) {
67 $data = jsonld_default_document_loader($url);
68 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
72 public static function removeSecurityLink(array $json)
74 if (!is_array($json['@context'])) {
78 if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) {
79 unset($json['@context'][$key]);
80 $json['@context'] = array_values(array_filter($json['@context']));
86 public static function fixContext(array $json)
88 // Preparation for adding possibly missing content to the context
89 if (!empty($json['@context']) && is_string($json['@context'])) {
90 $json['@context'] = [$json['@context']];
93 if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) {
94 unset($json['@context'][$key]);
95 $json['@context'] = array_values(array_filter($json['@context']));
98 $last_entry = count($json['@context']) - 1;
101 'w3id' => 'https://w3id.org/security#',
102 'signature' => 'w3id:signature',
103 'RsaSignature2017' => 'w3id:RsaSignature2017',
104 'created' => 'w3id:created',
105 'creator' => 'w3id:creator',
106 'nonce' => 'w3id:nonce',
107 'signatureValue' => 'w3id:signatureValue',
108 'publicKey' => 'w3id:publicKey',
109 'publicKeyPem' => 'w3id:publicKeyPem'];
111 if (is_array($json['@context'][$last_entry])) {
112 $json['@context'][$last_entry] = array_merge($json['@context'][$last_entry], $additional);
114 $json['@context'][] = $additional;
121 * Normalises a given JSON array
125 * @return mixed|bool normalized JSON string
128 public static function normalize($json)
130 $json = self::removeSecurityLink($json);
132 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
134 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
137 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
139 catch (Exception $e) {
142 $currentException = $e;
144 $messages[] = $currentException->getMessage();
145 } while($currentException = $currentException->getPrevious());
147 Logger::warning('JsonLD normalize error');
148 Logger::notice('JsonLD normalize error', ['messages' => $messages]);
149 Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
150 Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
157 * Compacts a given JSON array
161 * @return array Compacted JSON array
164 public static function compact($json)
166 $context = $json['@context'] ?? [];
167 $json['@context'] = ActivityPub::CONTEXT;
169 $compacted = self::internalCompact($json);
170 if (empty($compacted)) {
171 Logger::info('Failed to compact with our context');
172 $json['@context'] = $context;
173 $compacted = self::internalCompact($json);
174 if (empty($compacted)) {
175 Logger::info('Failed to compact with original context');
177 Logger::info('Successful compacted with original context');
184 private static function internalCompact($json)
186 $json = self::fixContext($json);
188 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
190 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
191 'w3id' => (object)['@id' => 'https://w3id.org/security#', '@type' => '@id'],
192 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
193 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
194 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
195 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
196 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
197 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
198 'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
199 'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
200 'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
201 'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']];
203 // Trying to avoid memory problems with large content fields
204 if (!empty($json['object']['source']['content'])) {
205 $content = $json['object']['source']['content'];
206 $json['object']['source']['content'] = '';
209 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
212 $compacted = jsonld_compact($jsonobj, $context);
214 catch (Exception $e) {
216 Logger::error('compacting error', ['line' => $e->getLine(), 'message' => $e->getMessage(),'callstack' => System::callstack(20)]);
219 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
221 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
222 $json['as:object']['as:source']['as:content'] = $content;
229 * Fetches an element array from a JSON array
235 * @return array fetched element
237 public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
239 if (!isset($array[$element])) {
243 // If it isn't an array yet, make it to one
244 if (!is_int(key($array[$element]))) {
245 $array[$element] = [$array[$element]];
250 foreach ($array[$element] as $entry) {
251 if (!is_array($entry) || is_null($key)) {
253 } elseif (isset($entry[$key])) {
254 $item = $entry[$key];
257 if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
266 * Fetches an element from a JSON array
274 * @return string fetched element
276 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
282 if (!isset($array[$element])) {
286 if (!is_array($array[$element])) {
287 return $array[$element];
290 if (is_null($type) || is_null($type_value)) {
291 $element_array = self::fetchElementArray($array, $element, $key);
292 if (is_null($element_array)) {
296 return array_shift($element_array);
299 $element_array = self::fetchElementArray($array, $element);
300 if (is_null($element_array)) {
304 foreach ($element_array as $entry) {
305 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {