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) {
67 logger('normalise error:' . print_r($e, true), LOGGER_DEBUG);
74 * @brief Compacts a given JSON array
78 * @return comacted JSON array
80 public static function compact($json)
82 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
84 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams',
85 'w3sec' => 'https://w3id.org/security',
86 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
87 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
88 'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
90 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
92 $compacted = jsonld_compact($jsonobj, $context);
94 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
98 * @brief Fetches an element from a JSON array
106 * @return fetched element
108 public static function fetchElement($array, $element, $key, $type = null, $type_value = null)
114 if (empty($array[$element])) {
118 if (is_string($array[$element])) {
119 return $array[$element];
122 if (is_null($type_value)) {
123 if (!empty($array[$element][$key])) {
124 return $array[$element][$key];
127 if (!empty($array[$element][0][$key])) {
128 return $array[$element][0][$key];
134 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
135 return $array[$element][$key];
138 /// @todo Add array search