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