]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Remove duplicated conditions, improve variables names in Model\APContact
[friendica.git] / src / Util / JsonLD.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Duration;
25 use Friendica\Core\Logger;
26 use Exception;
27 use Friendica\Core\System;
28 use Friendica\DI;
29 use Friendica\Protocol\ActivityPub;
30
31 /**
32  * This class contain methods to work with JsonLD data
33  */
34 class JsonLD
35 {
36         /**
37          * Loader for LD-JSON validation
38          *
39          * @param $url
40          *
41          * @return mixed the loaded data
42          * @throws \JsonLdException
43          */
44         public static function documentLoader($url)
45         {
46                 $recursion = 0;
47
48                 $x = debug_backtrace();
49                 if ($x) {
50                         foreach ($x as $n) {
51                                 if ($n['function'] === __FUNCTION__)  {
52                                         $recursion ++;
53                                 }
54                         }
55                 }
56
57                 if ($recursion > 5) {
58                         Logger::error('jsonld bomb detected at: ' . $url);
59                         exit();
60                 }
61
62                 $result = DI::cache()->get('documentLoader:' . $url);
63                 if (!is_null($result)) {
64                         return $result;
65                 }
66
67                 $data = jsonld_default_document_loader($url);
68                 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
69                 return $data;
70         }
71
72         public static function removeSecurityLink(array $json)
73         {
74                 if (!is_array($json['@context'])) {
75                         return $json;
76                 }
77
78                 if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) {
79                         unset($json['@context'][$key]);
80                         $json['@context'] = array_values(array_filter($json['@context']));
81                 }
82
83                 return $json;
84         }
85
86         public static function fixContext(array $json)
87         {
88                 // Preparation for adding possibly missing content to the context
89                 if (!empty($json['@context']) && is_string($json['@context'])) {
90                         $json['@context'] = [$json['@context']];
91                 }
92
93                 if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) {
94                         unset($json['@context'][$key]);
95                         $json['@context'] = array_values(array_filter($json['@context']));
96                 }
97
98                 $last_entry = count($json['@context']) - 1;
99
100                 $additional = [
101                         'w3id' => 'https://w3id.org/security#',
102                         'signature' => 'w3id:signature',
103                         'RsaSignature2017' => 'w3id:RsaSignature2017',
104                         'created' => 'w3id:created',
105                         'creator' => 'w3id:creator',
106                         'nonce' => 'w3id:nonce',
107                         'signatureValue' => 'w3id:signatureValue',
108                         'publicKey' => 'w3id:publicKey',
109                         'publicKeyPem' => 'w3id:publicKeyPem'];
110
111                 if (is_array($json['@context'][$last_entry])) {
112                         $json['@context'][$last_entry] = array_merge($json['@context'][$last_entry], $additional);
113                 } else {
114                         $json['@context'][] = $additional;
115                 }
116
117                 return $json;
118         }
119
120         /**
121          * Normalises a given JSON array
122          *
123          * @param array $json
124          *
125          * @return mixed|bool normalized JSON string
126          * @throws Exception
127          */
128         public static function normalize($json)
129         {
130                 $json = self::removeSecurityLink($json);
131
132                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
133
134                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
135
136                 try {
137                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
138                 }
139                 catch (Exception $e) {
140                         $normalized = false;
141                         $messages = [];
142                         $currentException = $e;
143                         do {
144                                 $messages[] = $currentException->getMessage();
145                         } while($currentException = $currentException->getPrevious());
146
147                         Logger::warning('JsonLD normalize error');
148                         Logger::notice('JsonLD normalize error', ['messages' => $messages]);
149                         Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
150                         Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
151                 }
152
153                 return $normalized;
154         }
155
156         /**
157          * Compacts a given JSON array
158          *
159          * @param array $json
160          *
161          * @return array Compacted JSON array
162          * @throws Exception
163          */
164         public static function compact($json)
165         {
166                 $context = $json['@context'] ?? [];
167                 $json['@context'] = ActivityPub::CONTEXT;
168
169                 $compacted = self::internalCompact($json);
170                 if (empty($compacted)) {
171                         Logger::info('Failed to compact with our context');
172                         $json['@context'] = $context;
173                         $compacted = self::internalCompact($json);
174                         if (empty($compacted)) {
175                                 Logger::info('Failed to compact with original context');
176                         } else {
177                                 Logger::info('Successful compacted with original context');
178                         }
179                 }
180
181                 return $compacted;
182         }
183
184         private static function internalCompact($json)
185         {
186                 $json = self::fixContext($json);
187
188                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
189
190                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
191                         'w3id' => (object)['@id' => 'https://w3id.org/security#', '@type' => '@id'],
192                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
193                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
194                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
195                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
196                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
197                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
198                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
199                         'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
200                         'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
201                         'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']];
202
203                 // Trying to avoid memory problems with large content fields
204                 if (!empty($json['object']['source']['content'])) {
205                         $content = $json['object']['source']['content'];
206                         $json['object']['source']['content'] = '';
207                 }
208
209                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
210
211                 try {
212                         $compacted = jsonld_compact($jsonobj, $context);
213                 }
214                 catch (Exception $e) {
215                         $compacted = false;
216                         Logger::error('compacting error', ['line' => $e->getLine(), 'message' => $e->getMessage(),'callstack' => System::callstack(20)]);
217                 }
218
219                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
220
221                 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
222                         $json['as:object']['as:source']['as:content'] = $content;
223                 }
224
225                 return $json;
226         }
227
228         /**
229          * Fetches an element array from a JSON array
230          *
231          * @param $array
232          * @param $element
233          * @param $key
234          *
235          * @return array fetched element
236          */
237         public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
238         {
239                 if (!isset($array[$element])) {
240                         return null;
241                 }
242
243                 // If it isn't an array yet, make it to one
244                 if (!is_int(key($array[$element]))) {
245                         $array[$element] = [$array[$element]];
246                 }
247
248                 $elements = [];
249
250                 foreach ($array[$element] as $entry) {
251                         if (!is_array($entry) || is_null($key)) {
252                                 $item = $entry;
253                         } elseif (isset($entry[$key])) {
254                                 $item = $entry[$key];
255                         }
256
257                         if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
258                                 $elements[] = $item;
259                         }
260                 }
261
262                 return $elements;
263         }
264
265         /**
266          * Fetches an element from a JSON array
267          *
268          * @param $array
269          * @param $element
270          * @param $key
271          * @param $type
272          * @param $type_value
273          *
274          * @return string fetched element
275          */
276         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
277         {
278                 if (empty($array)) {
279                         return null;
280                 }
281
282                 if (!isset($array[$element])) {
283                         return null;
284                 }
285
286                 if (!is_array($array[$element])) {
287                         return $array[$element];
288                 }
289
290                 if (is_null($type) || is_null($type_value)) {
291                         $element_array = self::fetchElementArray($array, $element, $key);
292                         if (is_null($element_array)) {
293                                 return null;
294                         }
295
296                         return array_shift($element_array);
297                 }
298
299                 $element_array = self::fetchElementArray($array, $element);
300                 if (is_null($element_array)) {
301                         return null;
302                 }
303
304                 foreach ($element_array as $entry) {
305                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
306                                 return $entry[$key];
307                         }
308                 }
309
310                 return null;
311         }
312 }