Merge pull request #5989 from annando/ap-profile
[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                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id']];
92
93                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
94
95
96                 try {
97                         $compacted = jsonld_compact($jsonobj, $context);
98                 }
99                 catch (Exception $e) {
100                         $compacted = false;
101                         logger('compacting error:' . print_r($e, true), LOGGER_DEBUG);
102                 }
103
104                 return json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
105         }
106
107         /**
108          * @brief Fetches an element array from a JSON array
109          *
110          * @param $array
111          * @param $element
112          * @param $key
113          *
114          * @return fetched element array
115          */
116         public static function fetchElementArray($array, $element, $key = '@id')
117         {
118                 if (empty($array)) {
119                         return null;
120                 }
121
122                 if (!isset($array[$element])) {
123                         return null;
124                 }
125
126                 // If it isn't an array yet, make it to one
127                 if (!is_int(key($array[$element]))) {
128                         $array[$element] = [$array[$element]];
129                 }
130
131                 $elements = [];
132
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;
140                         }
141                 }
142
143                 return $elements;
144         }
145
146         /**
147          * @brief Fetches an element from a JSON array
148          *
149          * @param $array
150          * @param $element
151          * @param $key
152          * @param $type
153          * @param $type_value
154          *
155          * @return fetched element
156          */
157         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
158         {
159                 if (empty($array)) {
160                         return null;
161                 }
162
163                 if (!isset($array[$element])) {
164                         return null;
165                 }
166
167                 if (!is_array($array[$element])) {
168                         return $array[$element];
169                 }
170
171                 if (is_null($type) || is_null($type_value)) {
172                         $element_array = self::fetchElementArray($array, $element, $key);
173                         if (is_null($element_array)) {
174                                 return null;
175                         }
176
177                         return array_shift($element_array);
178                 }
179
180                 $element_array = self::fetchElementArray($array, $element);
181                 if (is_null($element_array)) {
182                         return null;
183                 }
184
185                 foreach ($element_array as $entry) {
186                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
187                                 return $entry[$key];
188                         }
189                 }
190
191                 return null;
192         }
193 }