]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Issue 12815: JsonLD parsing problems with data-integrity
[friendica.git] / src / Util / JsonLD.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Logger;
26 use Exception;
27 use Friendica\Core\System;
28 use Friendica\DI;
29
30 /**
31  * This class contain methods to work with JsonLD data
32  */
33 class JsonLD
34 {
35         /**
36          * Loader for LD-JSON validation
37          *
38          * @param $url
39          *
40          * @return mixed the loaded data
41          * @throws \JsonLdException
42          */
43         public static function documentLoader($url)
44         {
45                 switch ($url) {
46                         case 'https://w3id.org/security/v1':
47                                 $url = DI::basePath() . '/static/security-v1.jsonld';
48                                 break;
49                         case 'https://w3id.org/identity/v1':
50                                 $url = DI::basePath() . '/static/identity-v1.jsonld';
51                                 break;
52                         case 'https://w3id.org/security/data-integrity/v1':
53                                 $url = DI::basePath() . '/static/security-data-integrity-v1.jsonld';
54                                 break;
55                         case 'https://www.w3.org/ns/activitystreams':
56                                 $url = DI::basePath() . '/static/activitystreams.jsonld';
57                                 break;
58                         case 'https://funkwhale.audio/ns':
59                                 $url = DI::basePath() . '/static/funkwhale.audio.jsonld';
60                                 break;
61                         case 'http://schema.org':
62                                 $url = DI::basePath() . '/static/schema.jsonld';
63                                 break;
64                         case 'http://joinmastodon.org/ns':
65                                 $url = DI::basePath() . '/static/joinmastodon.jsonld';
66                                 break;
67                         default:
68                                 switch (parse_url($url, PHP_URL_PATH)) {
69                                         case '/schemas/litepub-0.1.jsonld';
70                                                 $url = DI::basePath() . '/static/litepub-0.1.jsonld';
71                                                 break;
72                                         case '/apschema/v1.2':
73                                         case '/apschema/v1.9':
74                                         case '/apschema/v1.10':
75                                                 $url = DI::basePath() . '/static/apschema.jsonld';
76                                                 break;
77                                         default:
78                                                 Logger::info('Got url', ['url' =>$url]);
79                                                 break;
80                                 }
81                 }
82
83                 $recursion = 0;
84
85                 $x = debug_backtrace();
86                 if ($x) {
87                         foreach ($x as $n) {
88                                 if ($n['function'] === __FUNCTION__)  {
89                                         $recursion ++;
90                                 }
91                         }
92                 }
93
94                 if ($recursion > 5) {
95                         Logger::error('jsonld bomb detected at: ' . $url);
96                         System::exit();
97                 }
98
99                 $result = DI::cache()->get('documentLoader:' . $url);
100                 if (!is_null($result)) {
101                         return $result;
102                 }
103
104                 $data = jsonld_default_document_loader($url);
105                 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
106                 return $data;
107         }
108
109         /**
110          * Normalises a given JSON array
111          *
112          * @param array $json
113          *
114          * @return mixed|bool normalized JSON string
115          * @throws Exception
116          */
117         public static function normalize($json)
118         {
119                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
120
121                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
122
123                 try {
124                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
125                 }
126                 catch (Exception $e) {
127                         $normalized = false;
128                         $messages = [];
129                         $currentException = $e;
130                         do {
131                                 $messages[] = $currentException->getMessage();
132                         } while($currentException = $currentException->getPrevious());
133
134                         Logger::warning('JsonLD normalize error');
135                         Logger::notice('JsonLD normalize error', ['messages' => $messages]);
136                         Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
137                         Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
138                 }
139
140                 return $normalized;
141         }
142
143         /**
144          * Compacts a given JSON array
145          *
146          * @param array $json
147          * @param bool  $logfailed
148          *
149          * @return array Compacted JSON array
150          * @throws Exception
151          */
152         public static function compact($json, bool $logfailed = true): array
153         {
154                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
155
156                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
157                         'w3id' => 'https://w3id.org/security#',
158                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
159                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
160                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
161                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
162                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
163                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
164                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
165                         'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
166                         'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
167                         'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id'],
168                         'mobilizon' => (object)['@id' => 'https://joinmobilizon.org/ns#', '@type' => '@id'],
169                         'fedibird' => (object)['@id' => 'http://fedibird.com/ns#', '@type' => '@id'],
170                         'misskey' => (object)['@id' => 'https://misskey-hub.net/ns#', '@type' => '@id'],
171                 ];
172
173                 $orig_json = $json;
174
175                 // Preparation for adding possibly missing content to the context
176                 if (!empty($json['@context']) && is_string($json['@context'])) {
177                         $json['@context'] = [$json['@context']];
178                 }
179
180                 if (!empty($json['@context']) && is_array($json['@context'])) {
181                         // Remove empty entries from the context (a problem with WriteFreely)
182                         $json['@context'] = array_filter($json['@context']);
183
184                         // Workaround for servers with missing context
185                         // See issue https://github.com/nextcloud/social/issues/330
186                         if (!in_array('https://w3id.org/security/v1', $json['@context'])) {
187                                 $json['@context'][] = 'https://w3id.org/security/v1';
188                         }
189                 }
190
191                 // Bookwyrm transmits "id" fields with "null", which isn't allowed.
192                 array_walk_recursive($json, function (&$value, $key) {
193                         if ($key == 'id' && is_null($value)) {
194                                 $value = '';
195                         }
196                 });
197
198                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
199
200                 try {
201                         $compacted = jsonld_compact($jsonobj, $context);
202                 }
203                 catch (Exception $e) {
204                         $compacted = false;
205                         Logger::notice('compacting error', ['msg' => $e->getMessage(), 'previous' => $e->getPrevious(), 'line' => $e->getLine()]);
206                         if ($logfailed && DI::config()->get('debug', 'ap_log_failure')) {
207                                 $tempfile = tempnam(System::getTempPath(), 'failed-jsonld');
208                                 file_put_contents($tempfile, json_encode(['json' => $orig_json, 'callstack' => System::callstack(20), 'msg' => $e->getMessage(), 'previous' => $e->getPrevious()], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
209                                 Logger::notice('Failed message stored', ['file' => $tempfile]);
210                         }
211                 }
212
213                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
214
215                 if ($json === false) {
216                         Logger::notice('JSON encode->decode failed', ['orig_json' => $orig_json, 'compacted' => $compacted]);
217                         $json = [];
218                 }
219
220                 return $json;
221         }
222
223         /**
224          * Fetches an element array from a JSON array
225          *
226          * @param $array
227          * @param $element
228          * @param $key
229          *
230          * @return array fetched element
231          */
232         public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
233         {
234                 if (!isset($array[$element])) {
235                         return null;
236                 }
237
238                 // If it isn't an array yet, make it to one
239                 if (!is_array($array[$element]) || !is_int(key($array[$element]))) {
240                         $array[$element] = [$array[$element]];
241                 }
242
243                 $elements = [];
244
245                 foreach ($array[$element] as $entry) {
246                         if (!is_array($entry) || is_null($key)) {
247                                 $item = $entry;
248                         } elseif (isset($entry[$key])) {
249                                 $item = $entry[$key];
250                         }
251
252                         if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
253                                 $elements[] = $item;
254                         }
255                 }
256
257                 return $elements;
258         }
259
260         /**
261          * Fetches an element from a JSON array
262          *
263          * @param $array
264          * @param $element
265          * @param $key
266          * @param $type
267          * @param $type_value
268          *
269          * @return string fetched element
270          */
271         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
272         {
273                 if (empty($array)) {
274                         return null;
275                 }
276
277                 if (!isset($array[$element])) {
278                         return null;
279                 }
280
281                 if (!is_array($array[$element])) {
282                         return $array[$element];
283                 }
284
285                 if (is_null($type) || is_null($type_value)) {
286                         $element_array = self::fetchElementArray($array, $element, $key);
287                         if (is_null($element_array)) {
288                                 return null;
289                         }
290
291                         return array_shift($element_array);
292                 }
293
294                 $element_array = self::fetchElementArray($array, $element);
295                 if (is_null($element_array)) {
296                         return null;
297                 }
298
299                 foreach ($element_array as $entry) {
300                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
301                                 return $entry[$key];
302                         }
303                 }
304
305                 return null;
306         }
307 }