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;
30 * This class contain methods to work with JsonLD data
35 * Loader for LD-JSON validation
39 * @return mixed the loaded data
40 * @throws \JsonLdException
42 public static function documentLoader($url)
46 $x = debug_backtrace();
49 if ($n['function'] === __FUNCTION__) {
56 Logger::error('jsonld bomb detected at: ' . $url);
60 $result = DI::cache()->get('documentLoader:' . $url);
61 if (!is_null($result)) {
65 $data = jsonld_default_document_loader($url);
66 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
70 public static function fixContext(array $json)
72 // Preparation for adding possibly missing content to the context
73 if (!empty($json['@context']) && is_string($json['@context'])) {
74 $json['@context'] = [$json['@context']];
77 if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) {
78 unset($json['@context'][$key]);
79 $json['@context'] = array_values(array_filter($json['@context']));
82 $last_entry = count($json['@context']) - 1;
85 'w3id' => 'https://w3id.org/security#',
86 'signature' => 'w3id:signature',
87 'RsaSignature2017' => 'w3id:RsaSignature2017',
88 'created' => 'w3id:created',
89 'creator' => 'w3id:creator',
90 'nonce' => 'w3id:nonce',
91 'signatureValue' => 'w3id:signatureValue',
92 'publicKey' => 'w3id:publicKey',
93 'publicKeyPem' => 'w3id:publicKeyPem'];
95 if (is_array($json['@context'][$last_entry])) {
96 $json['@context'][$last_entry] = array_merge($json['@context'][$last_entry], $additional);
98 $json['@context'][] = $additional;
105 * Normalises a given JSON array
109 * @return mixed|bool normalized JSON string
112 public static function normalize($json)
114 $json = self::fixContext($json);
116 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
118 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
121 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
123 catch (Exception $e) {
126 $currentException = $e;
128 $messages[] = $currentException->getMessage();
129 } while($currentException = $currentException->getPrevious());
131 Logger::warning('JsonLD normalize error');
132 Logger::notice('JsonLD normalize error', ['messages' => $messages]);
133 Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
134 Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
141 * Compacts a given JSON array
145 * @return array Compacted JSON array
148 public static function compact($json)
150 $json = self::fixContext($json);
152 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
154 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
155 'w3id' => (object)['@id' => 'https://w3id.org/security#', '@type' => '@id'],
156 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
157 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
158 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
159 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
160 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
161 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
162 'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
163 'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
164 'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
165 'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']];
167 // Trying to avoid memory problems with large content fields
168 if (!empty($json['object']['source']['content'])) {
169 $content = $json['object']['source']['content'];
170 $json['object']['source']['content'] = '';
173 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
176 $compacted = jsonld_compact($jsonobj, $context);
178 catch (Exception $e) {
180 Logger::error('compacting error');
181 // Sooner or later we should log some details as well - but currently this leads to memory issues
182 // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
185 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
187 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
188 $json['as:object']['as:source']['as:content'] = $content;
195 * Fetches an element array from a JSON array
201 * @return array fetched element
203 public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
205 if (!isset($array[$element])) {
209 // If it isn't an array yet, make it to one
210 if (!is_int(key($array[$element]))) {
211 $array[$element] = [$array[$element]];
216 foreach ($array[$element] as $entry) {
217 if (!is_array($entry) || is_null($key)) {
219 } elseif (isset($entry[$key])) {
220 $item = $entry[$key];
223 if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
232 * Fetches an element from a JSON array
240 * @return string fetched element
242 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
248 if (!isset($array[$element])) {
252 if (!is_array($array[$element])) {
253 return $array[$element];
256 if (is_null($type) || is_null($type_value)) {
257 $element_array = self::fetchElementArray($array, $element, $key);
258 if (is_null($element_array)) {
262 return array_shift($element_array);
265 $element_array = self::fetchElementArray($array, $element);
266 if (is_null($element_array)) {
270 foreach ($element_array as $entry) {
271 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {