]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
b4c362884c5b1dcee3063eeea79fe4c94598ffda
[friendica.git] / src / Util / JsonLD.php
1 <?php
2 /**
3  * @file src/Util/JsonLD.php
4  */
5 namespace Friendica\Util;
6
7 use Friendica\Core\Cache;
8 use Exception;
9
10 /**
11  * @brief This class contain methods to work with JsonLD data
12  */
13 class JsonLD
14 {
15         /**
16          * @brief Loader for LD-JSON validation
17          *
18          * @param $url
19          *
20          * @return the loaded data
21          */
22         public static function documentLoader($url)
23         {
24                 $recursion = 0;
25
26                 $x = debug_backtrace();
27                 if ($x) {
28                         foreach ($x as $n) {
29                                 if ($n['function'] === __FUNCTION__)  {
30                                         $recursion ++;
31                                 }
32                         }
33                 }
34
35                 if ($recursion > 5) {
36                         logger('jsonld bomb detected at: ' . $url);
37                         exit();
38                 }
39
40                 $result = Cache::get('documentLoader:' . $url);
41                 if (!is_null($result)) {
42                         return $result;
43                 }
44
45                 $data = jsonld_default_document_loader($url);
46                 Cache::set('documentLoader:' . $url, $data, CACHE_DAY);
47                 return $data;
48         }
49
50         /**
51          * @brief Normalises a given JSON array
52          *
53          * @param array $json
54          *
55          * @return normalized JSON string
56          */
57         public static function normalize($json)
58         {
59                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
60
61                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
62
63                 try {
64                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
65                 }
66                 catch (Exception $e) {
67                         $normalized = false;
68                         logger('normalise error:' . print_r($e, true), LOGGER_DEBUG);
69                 }
70
71                 return $normalized;
72         }
73
74         /**
75          * @brief Compacts a given JSON array
76          *
77          * @param array $json
78          *
79          * @return comacted JSON array
80          */
81         public static function compact($json)
82         {
83                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
84
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                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
92                         'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
93
94                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
95
96                 $compacted = jsonld_compact($jsonobj, $context);
97
98                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
99         }
100
101         /**
102          * @brief Fetches an element array from a JSON array
103          *
104          * @param $array
105          * @param $element
106          * @param $key
107          *
108          * @return fetched element array
109          */
110         public static function fetchElementArray($array, $element, $key = '@id')
111         {
112                 if (empty($array)) {
113                         return null;
114                 }
115
116                 if (!isset($array[$element])) {
117                         return null;
118                 }
119
120                 // If it isn't an array yet, make it to one
121                 if (!is_int(key($array[$element]))) {
122                         $array[$element] = [$array[$element]];
123                 }
124
125                 $elements = [];
126
127                 foreach ($array[$element] as $entry) {
128                         if (!is_array($entry)) {
129                                 $elements[] = $entry;
130                         } elseif (!empty($entry[$key])) {
131                                 $elements[] = $entry[$key];
132                         } else {
133                                 $elements[] = $entry;
134                         }
135                 }
136
137                 return $elements;
138         }
139
140         /**
141          * @brief Fetches an element from a JSON array
142          *
143          * @param $array
144          * @param $element
145          * @param $key
146          * @param $type
147          * @param $type_value
148          *
149          * @return fetched element
150          */
151         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
152         {
153                 if (empty($array)) {
154                         return null;
155                 }
156
157                 if (!isset($array[$element])) {
158                         return null;
159                 }
160
161                 if (!is_array($array[$element])) {
162                         return $array[$element];
163                 }
164
165                 if (is_null($type) || is_null($type_value)) {
166                         $element_array = self::fetchElementArray($array, $element, $key);
167                         if (is_null($element_array)) {
168                                 return null;
169                         }
170
171                         return array_shift($element_array);
172                 }
173
174                 $element_array = self::fetchElementArray($array, $element);
175                 if (is_null($element_array)) {
176                         return null;
177                 }
178
179                 foreach ($element_array as $entry) {
180                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
181                                 return $entry[$key];
182                         }
183                 }
184
185                 return null;
186         }
187 }