]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Changes:
[friendica.git] / src / Util / JsonLD.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, 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/security/data-integrity/v1':
50                                 $url = DI::basePath() . '/static/security-data-integrity-v1.jsonld';
51                                 break;
52                         case 'https://w3id.org/security/multikey/v1':
53                                 $url = DI::basePath() . '/static/security-multikey-v1.jsonld';
54                                 break;
55                         case 'https://w3id.org/identity/v1':
56                                 $url = DI::basePath() . '/static/identity-v1.jsonld';
57                                 break;
58                         case 'https://www.w3.org/ns/activitystreams':
59                                 $url = DI::basePath() . '/static/activitystreams.jsonld';
60                                 break;
61                         case 'https://www.w3.org/ns/did/v1':
62                                 $url = DI::basePath() . '/static/did-v1.jsonld';
63                                 break;
64                         case 'https://funkwhale.audio/ns':
65                                 $url = DI::basePath() . '/static/funkwhale.audio.jsonld';
66                                 break;
67                         case 'http://schema.org':
68                                 $url = DI::basePath() . '/static/schema.jsonld';
69                                 break;
70                         case 'http://joinmastodon.org/ns':
71                                 $url = DI::basePath() . '/static/joinmastodon.jsonld';
72                                 break;
73                         default:
74                                 switch (parse_url($url, PHP_URL_PATH)) {
75                                         case '/schemas/litepub-0.1.jsonld';
76                                                 $url = DI::basePath() . '/static/litepub-0.1.jsonld';
77                                                 break;
78                                         case '/apschema/v1.2':
79                                         case '/apschema/v1.9':
80                                         case '/apschema/v1.10':
81                                                 $url = DI::basePath() . '/static/apschema.jsonld';
82                                                 break;
83                                         default:
84                                                 Logger::info('Got url', ['url' =>$url]);
85                                                 break;
86                                 }
87                 }
88
89                 $recursion = 0;
90
91                 $x = debug_backtrace();
92                 if ($x) {
93                         foreach ($x as $n) {
94                                 if ($n['function'] === __FUNCTION__)  {
95                                         $recursion ++;
96                                 }
97                         }
98                 }
99
100                 if ($recursion > 5) {
101                         Logger::error('jsonld bomb detected at: ' . $url);
102                         System::exit();
103                 }
104
105                 $result = DI::cache()->get('documentLoader:' . $url);
106                 if (!is_null($result)) {
107                         return $result;
108                 }
109
110                 $data = jsonld_default_document_loader($url);
111                 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
112                 return $data;
113         }
114
115         /**
116          * Normalises a given JSON array
117          *
118          * @param array $json
119          *
120          * @return mixed|bool normalized JSON string
121          * @throws Exception
122          */
123         public static function normalize($json)
124         {
125                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
126
127                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
128
129                 try {
130                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
131                 }
132                 catch (Exception $e) {
133                         $normalized = false;
134                         $messages = [];
135                         $currentException = $e;
136                         do {
137                                 $messages[] = $currentException->getMessage();
138                         } while($currentException = $currentException->getPrevious());
139
140                         Logger::warning('JsonLD normalize error');
141                         Logger::notice('JsonLD normalize error', ['messages' => $messages]);
142                         Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
143                         Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
144                 }
145
146                 return $normalized;
147         }
148
149         /**
150          * Compacts a given JSON array
151          *
152          * @param array $json
153          * @param bool  $logfailed
154          *
155          * @return array Compacted JSON array
156          * @throws Exception
157          */
158         public static function compact($json, bool $logfailed = true): array
159         {
160                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
161
162                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
163                         'w3id' => 'https://w3id.org/security#',
164                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
165                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
166                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
167                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
168                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
169                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
170                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
171                         'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
172                         'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
173                         'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id'],
174                         'mobilizon' => (object)['@id' => 'https://joinmobilizon.org/ns#', '@type' => '@id'],
175                         'fedibird' => (object)['@id' => 'http://fedibird.com/ns#', '@type' => '@id'],
176                         'misskey' => (object)['@id' => 'https://misskey-hub.net/ns#', '@type' => '@id'],
177                         'pixelfed' => (object)['@id' => 'http://pixelfed.org/ns#', '@type' => '@id'],
178                 ];
179
180                 $orig_json = $json;
181
182                 // Preparation for adding possibly missing content to the context
183                 if (!empty($json['@context']) && is_string($json['@context'])) {
184                         $json['@context'] = [$json['@context']];
185                 }
186
187                 if (!empty($json['@context']) && is_array($json['@context'])) {
188                         // Remove empty entries from the context (a problem with WriteFreely)
189                         $json['@context'] = array_filter($json['@context']);
190
191                         // Workaround for servers with missing context
192                         // See issue https://github.com/nextcloud/social/issues/330
193                         if (!in_array('https://w3id.org/security/v1', $json['@context'])) {
194                                 $json['@context'][] = 'https://w3id.org/security/v1';
195                         }
196                 }
197
198                 // Bookwyrm transmits "id" fields with "null", which isn't allowed.
199                 array_walk_recursive($json, function (&$value, $key) {
200                         if ($key == 'id' && is_null($value)) {
201                                 $value = '';
202                         }
203                 });
204
205                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
206
207                 try {
208                         $compacted = jsonld_compact($jsonobj, $context);
209                 }
210                 catch (Exception $e) {
211                         $compacted = false;
212                         Logger::notice('compacting error', ['msg' => $e->getMessage(), 'previous' => $e->getPrevious(), 'line' => $e->getLine()]);
213                         if ($logfailed && DI::config()->get('debug', 'ap_log_failure')) {
214                                 $tempfile = tempnam(System::getTempPath(), 'failed-jsonld');
215                                 file_put_contents($tempfile, json_encode(['json' => $orig_json, 'msg' => $e->getMessage(), 'previous' => $e->getPrevious()], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
216                                 Logger::notice('Failed message stored', ['file' => $tempfile]);
217                         }
218                 }
219
220                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
221
222                 if ($json === false) {
223                         Logger::notice('JSON encode->decode failed', ['orig_json' => $orig_json, 'compacted' => $compacted]);
224                         $json = [];
225                 }
226
227                 return $json;
228         }
229
230         /**
231          * Fetches an element array from a JSON array
232          *
233          * @param $array
234          * @param $element
235          * @param $key
236          *
237          * @return array fetched element
238          */
239         public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
240         {
241                 if (!isset($array[$element])) {
242                         return null;
243                 }
244
245                 // If it isn't an array yet, make it to one
246                 if (!is_array($array[$element]) || !is_int(key($array[$element]))) {
247                         $array[$element] = [$array[$element]];
248                 }
249
250                 $elements = [];
251
252                 foreach ($array[$element] as $entry) {
253                         if (!is_array($entry) || is_null($key)) {
254                                 $item = $entry;
255                         } elseif (isset($entry[$key])) {
256                                 $item = $entry[$key];
257                         }
258
259                         if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
260                                 $elements[] = $item;
261                         }
262                 }
263
264                 return $elements;
265         }
266
267         /**
268          * Fetches an element from a JSON array
269          *
270          * @param $array
271          * @param $element
272          * @param $key
273          * @param $type
274          * @param $type_value
275          *
276          * @return string fetched element
277          */
278         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
279         {
280                 if (empty($array)) {
281                         return null;
282                 }
283
284                 if (!isset($array[$element])) {
285                         return null;
286                 }
287
288                 if (!is_array($array[$element])) {
289                         return $array[$element];
290                 }
291
292                 if (is_null($type) || is_null($type_value)) {
293                         $element_array = self::fetchElementArray($array, $element, $key);
294                         if (is_null($element_array)) {
295                                 return null;
296                         }
297
298                         return array_shift($element_array);
299                 }
300
301                 $element_array = self::fetchElementArray($array, $element);
302                 if (is_null($element_array)) {
303                         return null;
304                 }
305
306                 foreach ($element_array as $entry) {
307                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
308                                 return $entry[$key];
309                         }
310                 }
311
312                 return null;
313         }
314 }