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