Duplicated line removed
[friendica.git/.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                         'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']];
92
93                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
94
95                 $compacted = jsonld_compact($jsonobj, $context);
96
97                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
98         }
99
100         /**
101          * @brief Fetches an element array from a JSON array
102          *
103          * @param $array
104          * @param $element
105          * @param $key
106          *
107          * @return fetched element array
108          */
109         public static function fetchElementArray($array, $element, $key = '@id')
110         {
111                 if (empty($array)) {
112                         return null;
113                 }
114
115                 if (!isset($array[$element])) {
116                         return null;
117                 }
118
119                 // If it isn't an array yet, make it to one
120                 if (!is_int(key($array[$element]))) {
121                         $array[$element] = [$array[$element]];
122                 }
123
124                 $elements = [];
125
126                 foreach ($array[$element] as $entry) {
127                         if (!is_array($entry)) {
128                                 $elements[] = $entry;
129                         } elseif (!empty($entry[$key])) {
130                                 $elements[] = $entry[$key];
131                         } else {
132                                 $elements[] = $entry;
133                         }
134                 }
135
136                 return $elements;
137         }
138
139         /**
140          * @brief Fetches an element from a JSON array
141          *
142          * @param $array
143          * @param $element
144          * @param $key
145          * @param $type
146          * @param $type_value
147          *
148          * @return fetched element
149          */
150         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
151         {
152                 if (empty($array)) {
153                         return null;
154                 }
155
156                 if (!isset($array[$element])) {
157                         return null;
158                 }
159
160                 if (!is_array($array[$element])) {
161                         return $array[$element];
162                 }
163
164                 if (is_null($type) || is_null($type_value)) {
165                         $element_array = self::fetchElementArray($array, $element, $key);
166                         if (is_null($element_array)) {
167                                 return null;
168                         }
169
170                         return array_shift($element_array);
171                 }
172
173                 $element_array = self::fetchElementArray($array, $element);
174                 if (is_null($element_array)) {
175                         return null;
176                 }
177
178                 foreach ($element_array as $entry) {
179                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
180                                 return $entry[$key];
181                         }
182                 }
183
184                 return null;
185         }
186 }