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 the loaded data
23 public static function documentLoader($url)
27 $x = debug_backtrace();
30 if ($n['function'] === __FUNCTION__) {
37 Logger::log('jsonld bomb detected at: ' . $url);
41 $result = Cache::get('documentLoader:' . $url);
42 if (!is_null($result)) {
46 $data = jsonld_default_document_loader($url);
47 Cache::set('documentLoader:' . $url, $data, Cache::DAY);
52 * @brief Normalises a given JSON array
56 * @return normalized JSON string
58 public static function normalize($json)
60 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
62 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
65 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
67 catch (Exception $e) {
69 Logger::log('normalise error:' . print_r($e, true), Logger::DEBUG);
76 * @brief Compacts a given JSON array
80 * @return comacted JSON array
82 public static function compact($json)
84 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
86 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
87 'w3id' => 'https://w3id.org/security#',
88 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
89 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
90 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
91 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
92 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
93 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
94 'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id']];
96 // Workaround for Nextcloud Social
97 // See issue https://github.com/nextcloud/social/issues/330
98 if (is_array($json['@context'])) {
99 $json['@context'][] = 'https://w3id.org/security/v1';
102 // Trying to avoid memory problems with large content fields
103 if (!empty($json['object']['source']['content'])) {
104 $content = $json['object']['source']['content'];
105 $json['object']['source']['content'] = '';
108 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
111 $compacted = jsonld_compact($jsonobj, $context);
113 catch (Exception $e) {
115 Logger::log('compacting error:' . print_r($e, true), Logger::DEBUG);
118 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
120 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
121 $json['as:object']['as:source']['as:content'] = $content;
128 * @brief Fetches an element array from a JSON array
134 * @return fetched element array
136 public static function fetchElementArray($array, $element, $key = '@id')
142 if (!isset($array[$element])) {
146 // If it isn't an array yet, make it to one
147 if (!is_int(key($array[$element]))) {
148 $array[$element] = [$array[$element]];
153 foreach ($array[$element] as $entry) {
154 if (!is_array($entry)) {
155 $elements[] = $entry;
156 } elseif (!empty($entry[$key])) {
157 $elements[] = $entry[$key];
158 } elseif (!empty($entry) || !is_array($entry)) {
159 $elements[] = $entry;
167 * @brief Fetches an element from a JSON array
175 * @return fetched element
177 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
183 if (!isset($array[$element])) {
187 if (!is_array($array[$element])) {
188 return $array[$element];
191 if (is_null($type) || is_null($type_value)) {
192 $element_array = self::fetchElementArray($array, $element, $key);
193 if (is_null($element_array)) {
197 return array_shift($element_array);
200 $element_array = self::fetchElementArray($array, $element);
201 if (is_null($element_array)) {
205 foreach ($element_array as $entry) {
206 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {