3 * @file src/Util/JsonLD.php
5 namespace Friendica\Util;
7 use Friendica\Core\Cache\Duration;
8 use Friendica\Core\Logger;
13 * This class contain methods to work with JsonLD data
18 * Loader for LD-JSON validation
22 * @return mixed the loaded data
23 * @throws \JsonLdException
25 public static function documentLoader($url)
29 $x = debug_backtrace();
32 if ($n['function'] === __FUNCTION__) {
39 Logger::error('jsonld bomb detected at: ' . $url);
43 $result = DI::cache()->get('documentLoader:' . $url);
44 if (!is_null($result)) {
48 $data = jsonld_default_document_loader($url);
49 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
54 * Normalises a given JSON array
58 * @return mixed|bool normalized JSON string
61 public static function normalize($json)
63 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
65 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
68 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
70 catch (Exception $e) {
73 $currentException = $e;
75 $messages[] = $currentException->getMessage();
76 } while($currentException = $currentException->getPrevious());
78 Logger::warning('JsonLD normalize error');
79 Logger::notice('JsonLD normalize error', ['messages' => $messages]);
80 Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
81 Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
88 * Compacts a given JSON array
92 * @return array Compacted JSON array
95 public static function compact($json)
97 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
99 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
100 'w3id' => 'https://w3id.org/security#',
101 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
102 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
103 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
104 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
105 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
106 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
107 'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
108 'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id']];
110 // Preparation for adding possibly missing content to the context
111 if (!empty($json['@context']) && is_string($json['@context'])) {
112 $json['@context'] = [$json['@context']];
115 // Workaround for servers with missing context
116 // See issue https://github.com/nextcloud/social/issues/330
117 if (!empty($json['@context']) && is_array($json['@context'])) {
118 $json['@context'][] = 'https://w3id.org/security/v1';
121 // Trying to avoid memory problems with large content fields
122 if (!empty($json['object']['source']['content'])) {
123 $content = $json['object']['source']['content'];
124 $json['object']['source']['content'] = '';
127 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
130 $compacted = jsonld_compact($jsonobj, $context);
132 catch (Exception $e) {
134 Logger::error('compacting error');
135 // Sooner or later we should log some details as well - but currently this leads to memory issues
136 // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
139 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
141 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
142 $json['as:object']['as:source']['as:content'] = $content;
149 * Fetches an element array from a JSON array
155 * @return array fetched element
157 public static function fetchElementArray($array, $element, $key = '@id')
163 if (!isset($array[$element])) {
167 // If it isn't an array yet, make it to one
168 if (!is_int(key($array[$element]))) {
169 $array[$element] = [$array[$element]];
174 foreach ($array[$element] as $entry) {
175 if (!is_array($entry)) {
176 $elements[] = $entry;
177 } elseif (isset($entry[$key])) {
178 $elements[] = $entry[$key];
179 } elseif (!empty($entry) || !is_array($entry)) {
180 $elements[] = $entry;
188 * Fetches an element from a JSON array
196 * @return string fetched element
198 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
204 if (!isset($array[$element])) {
208 if (!is_array($array[$element])) {
209 return $array[$element];
212 if (is_null($type) || is_null($type_value)) {
213 $element_array = self::fetchElementArray($array, $element, $key);
214 if (is_null($element_array)) {
218 return array_shift($element_array);
221 $element_array = self::fetchElementArray($array, $element);
222 if (is_null($element_array)) {
226 foreach ($element_array as $entry) {
227 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {