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::log('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::log('normalise error:' . print_r($e, true), Logger::DEBUG);
78 * @brief Compacts a given JSON array
82 * @return array Compacted JSON array
85 public static function compact($json)
87 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
89 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
90 'w3id' => 'https://w3id.org/security#',
91 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
92 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
93 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
94 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
95 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
96 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
97 'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id']];
99 // Workaround for Nextcloud Social
100 // See issue https://github.com/nextcloud/social/issues/330
101 if (!empty($json['@context']) && is_array($json['@context'])) {
102 $json['@context'][] = 'https://w3id.org/security/v1';
105 // Trying to avoid memory problems with large content fields
106 if (!empty($json['object']['source']['content'])) {
107 $content = $json['object']['source']['content'];
108 $json['object']['source']['content'] = '';
111 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
114 $compacted = jsonld_compact($jsonobj, $context);
116 catch (Exception $e) {
118 Logger::log('compacting error:' . print_r($e, true), Logger::DEBUG);
121 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
123 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
124 $json['as:object']['as:source']['as:content'] = $content;
131 * @brief Fetches an element array from a JSON array
137 * @return array fetched element
139 public static function fetchElementArray($array, $element, $key = '@id')
145 if (!isset($array[$element])) {
149 // If it isn't an array yet, make it to one
150 if (!is_int(key($array[$element]))) {
151 $array[$element] = [$array[$element]];
156 foreach ($array[$element] as $entry) {
157 if (!is_array($entry)) {
158 $elements[] = $entry;
159 } elseif (!empty($entry[$key])) {
160 $elements[] = $entry[$key];
161 } elseif (!empty($entry) || !is_array($entry)) {
162 $elements[] = $entry;
170 * @brief Fetches an element from a JSON array
178 * @return string fetched element
180 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
186 if (!isset($array[$element])) {
190 if (!is_array($array[$element])) {
191 return $array[$element];
194 if (is_null($type) || is_null($type_value)) {
195 $element_array = self::fetchElementArray($array, $element, $key);
196 if (is_null($element_array)) {
200 return array_shift($element_array);
203 $element_array = self::fetchElementArray($array, $element);
204 if (is_null($element_array)) {
208 foreach ($element_array as $entry) {
209 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {