3 * @file src/Util/JsonLD.php
5 namespace Friendica\Util;
7 use Friendica\Core\Cache;
8 use Friendica\Core\Logger;
12 * @brief This class contain methods to work with JsonLD data
17 * @brief Loader for LD-JSON validation
21 * @return mixed the loaded data
22 * @throws \JsonLdException
24 public static function documentLoader($url)
28 $x = debug_backtrace();
31 if ($n['function'] === __FUNCTION__) {
38 Logger::error('jsonld bomb detected at: ' . $url);
42 $result = Cache::get('documentLoader:' . $url);
43 if (!is_null($result)) {
47 $data = jsonld_default_document_loader($url);
48 Cache::set('documentLoader:' . $url, $data, Cache::DAY);
53 * @brief Normalises a given JSON array
57 * @return mixed|bool normalized JSON string
60 public static function normalize($json)
62 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
64 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
67 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
69 catch (Exception $e) {
71 Logger::error('normalise error');
72 // Sooner or later we should log some details as well - but currently this leads to memory issues
73 // Logger::log('normalise error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
80 * @brief Compacts a given JSON array
84 * @return array Compacted JSON array
87 public static function compact($json)
89 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
91 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
92 'w3id' => 'https://w3id.org/security#',
93 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
94 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
95 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
96 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
97 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
98 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
99 'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
100 'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id']];
102 // Preparation for adding possibly missing content to the context
103 if (!empty($json['@context']) && is_string($json['@context'])) {
104 $json['@context'] = [$json['@context']];
107 // Workaround for servers with missing context
108 // See issue https://github.com/nextcloud/social/issues/330
109 if (!empty($json['@context']) && is_array($json['@context'])) {
110 $json['@context'][] = 'https://w3id.org/security/v1';
113 // Trying to avoid memory problems with large content fields
114 if (!empty($json['object']['source']['content'])) {
115 $content = $json['object']['source']['content'];
116 $json['object']['source']['content'] = '';
119 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
122 $compacted = jsonld_compact($jsonobj, $context);
124 catch (Exception $e) {
126 Logger::error('compacting error');
127 // Sooner or later we should log some details as well - but currently this leads to memory issues
128 // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
131 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
133 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
134 $json['as:object']['as:source']['as:content'] = $content;
141 * @brief Fetches an element array from a JSON array
147 * @return array fetched element
149 public static function fetchElementArray($array, $element, $key = '@id')
155 if (!isset($array[$element])) {
159 // If it isn't an array yet, make it to one
160 if (!is_int(key($array[$element]))) {
161 $array[$element] = [$array[$element]];
166 foreach ($array[$element] as $entry) {
167 if (!is_array($entry)) {
168 $elements[] = $entry;
169 } elseif (isset($entry[$key])) {
170 $elements[] = $entry[$key];
171 } elseif (!empty($entry) || !is_array($entry)) {
172 $elements[] = $entry;
180 * @brief Fetches an element from a JSON array
188 * @return string fetched element
190 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
196 if (!isset($array[$element])) {
200 if (!is_array($array[$element])) {
201 return $array[$element];
204 if (is_null($type) || is_null($type_value)) {
205 $element_array = self::fetchElementArray($array, $element, $key);
206 if (is_null($element_array)) {
210 return array_shift($element_array);
213 $element_array = self::fetchElementArray($array, $element);
214 if (is_null($element_array)) {
218 foreach ($element_array as $entry) {
219 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {