3 * @file src/Util/JsonLD.php
5 namespace Friendica\Util;
7 use Friendica\Core\Cache;
11 * @brief This class contain methods to work with JsonLD data
16 * @brief Loader for LD-JSON validation
20 * @return the loaded data
22 public static function documentLoader($url)
26 $x = debug_backtrace();
29 if ($n['function'] === __FUNCTION__) {
36 logger('jsonld bomb detected at: ' . $url);
40 $result = Cache::get('documentLoader:' . $url);
41 if (!is_null($result)) {
45 $data = jsonld_default_document_loader($url);
46 Cache::set('documentLoader:' . $url, $data, CACHE_DAY);
51 * @brief Normalises a given JSON array
55 * @return normalized JSON string
57 public static function normalize($json)
59 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
61 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
64 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
66 catch (Exception $e) {
68 logger('normalise error:' . print_r($e, true), LOGGER_DEBUG);
75 * @brief Compacts a given JSON array
79 * @return comacted JSON array
81 public static function compact($json)
83 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
85 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
86 'w3id' => 'https://w3id.org/security#',
87 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
88 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
89 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
90 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
91 'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
93 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
97 $compacted = jsonld_compact($jsonobj, $context);
99 catch (Exception $e) {
101 logger('compacting error:' . print_r($e, true), LOGGER_DEBUG);
104 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
108 * @brief Fetches an element array from a JSON array
114 * @return fetched element array
116 public static function fetchElementArray($array, $element, $key = '@id')
122 if (!isset($array[$element])) {
126 // If it isn't an array yet, make it to one
127 if (!is_int(key($array[$element]))) {
128 $array[$element] = [$array[$element]];
133 foreach ($array[$element] as $entry) {
134 if (!is_array($entry)) {
135 $elements[] = $entry;
136 } elseif (!empty($entry[$key])) {
137 $elements[] = $entry[$key];
138 } elseif (!empty($entry) || !is_array($entry)) {
139 $elements[] = $entry;
147 * @brief Fetches an element from a JSON array
155 * @return fetched element
157 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
163 if (!isset($array[$element])) {
167 if (!is_array($array[$element])) {
168 return $array[$element];
171 if (is_null($type) || is_null($type_value)) {
172 $element_array = self::fetchElementArray($array, $element, $key);
173 if (is_null($element_array)) {
177 return array_shift($element_array);
180 $element_array = self::fetchElementArray($array, $element);
181 if (is_null($element_array)) {
185 foreach ($element_array as $entry) {
186 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {