]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Merge pull request #10256 from annando/fix-ap
[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\DI;
28
29 /**
30  * This class contain methods to work with JsonLD data
31  */
32 class JsonLD
33 {
34         /**
35          * Loader for LD-JSON validation
36          *
37          * @param $url
38          *
39          * @return mixed the loaded data
40          * @throws \JsonLdException
41          */
42         public static function documentLoader($url)
43         {
44                 $recursion = 0;
45
46                 $x = debug_backtrace();
47                 if ($x) {
48                         foreach ($x as $n) {
49                                 if ($n['function'] === __FUNCTION__)  {
50                                         $recursion ++;
51                                 }
52                         }
53                 }
54
55                 if ($recursion > 5) {
56                         Logger::error('jsonld bomb detected at: ' . $url);
57                         exit();
58                 }
59
60                 $result = DI::cache()->get('documentLoader:' . $url);
61                 if (!is_null($result)) {
62                         return $result;
63                 }
64
65                 $data = jsonld_default_document_loader($url);
66                 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
67                 return $data;
68         }
69
70         public static function fixContext(array $json)
71         {
72                 // Preparation for adding possibly missing content to the context
73                 if (!empty($json['@context']) && is_string($json['@context'])) {
74                         $json['@context'] = [$json['@context']];
75                 }
76
77                 if (($key = array_search('https://w3id.org/security/v1', $json['@context'])) !== false) {
78                         unset($json['@context'][$key]);
79                         $json['@context'] = array_values(array_filter($json['@context']));
80                 }
81
82                 $last_entry = count($json['@context']) - 1;
83
84                 $additional = [
85                         'w3id' => 'https://w3id.org/security#',
86                         'signature' => 'w3id:signature',
87                         'RsaSignature2017' => 'w3id:RsaSignature2017',
88                         'created' => 'w3id:created',
89                         'creator' => 'w3id:creator',
90                         'nonce' => 'w3id:nonce',
91                         'signatureValue' => 'w3id:signatureValue',
92                         'publicKey' => 'w3id:publicKey',
93                         'publicKeyPem' => 'w3id:publicKeyPem'];
94
95                 if (is_array($json['@context'][$last_entry])) {
96                         $json['@context'][$last_entry] = array_merge($json['@context'][$last_entry], $additional);
97                 } else {
98                         $json['@context'][] = $additional;
99                 }
100
101                 return $json;
102         }
103
104         /**
105          * Normalises a given JSON array
106          *
107          * @param array $json
108          *
109          * @return mixed|bool normalized JSON string
110          * @throws Exception
111          */
112         public static function normalize($json)
113         {
114                 $json = self::fixContext($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          *
145          * @return array Compacted JSON array
146          * @throws Exception
147          */
148         public static function compact($json)
149         {
150                 $json = self::fixContext($json);
151
152                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
153
154                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
155                         'w3id' => (object)['@id' => 'https://w3id.org/security#', '@type' => '@id'],
156                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
157                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
158                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
159                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
160                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
161                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
162                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
163                         'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
164                         'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
165                         'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']];
166
167                 // Trying to avoid memory problems with large content fields
168                 if (!empty($json['object']['source']['content'])) {
169                         $content = $json['object']['source']['content'];
170                         $json['object']['source']['content'] = '';
171                 }
172
173                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
174
175                 try {
176                         $compacted = jsonld_compact($jsonobj, $context);
177                 }
178                 catch (Exception $e) {
179                         $compacted = false;
180                         Logger::error('compacting error');
181                         // Sooner or later we should log some details as well - but currently this leads to memory issues
182                         // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
183                 }
184
185                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
186
187                 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
188                         $json['as:object']['as:source']['as:content'] = $content;
189                 }
190
191                 return $json;
192         }
193
194         /**
195          * Fetches an element array from a JSON array
196          *
197          * @param $array
198          * @param $element
199          * @param $key
200          *
201          * @return array fetched element
202          */
203         public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
204         {
205                 if (!isset($array[$element])) {
206                         return null;
207                 }
208
209                 // If it isn't an array yet, make it to one
210                 if (!is_int(key($array[$element]))) {
211                         $array[$element] = [$array[$element]];
212                 }
213
214                 $elements = [];
215
216                 foreach ($array[$element] as $entry) {
217                         if (!is_array($entry) || is_null($key)) {
218                                 $item = $entry;
219                         } elseif (isset($entry[$key])) {
220                                 $item = $entry[$key];
221                         }
222
223                         if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
224                                 $elements[] = $item;
225                         }
226                 }
227
228                 return $elements;
229         }
230
231         /**
232          * Fetches an element from a JSON array
233          *
234          * @param $array
235          * @param $element
236          * @param $key
237          * @param $type
238          * @param $type_value
239          *
240          * @return string fetched element
241          */
242         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
243         {
244                 if (empty($array)) {
245                         return null;
246                 }
247
248                 if (!isset($array[$element])) {
249                         return null;
250                 }
251
252                 if (!is_array($array[$element])) {
253                         return $array[$element];
254                 }
255
256                 if (is_null($type) || is_null($type_value)) {
257                         $element_array = self::fetchElementArray($array, $element, $key);
258                         if (is_null($element_array)) {
259                                 return null;
260                         }
261
262                         return array_shift($element_array);
263                 }
264
265                 $element_array = self::fetchElementArray($array, $element);
266                 if (is_null($element_array)) {
267                         return null;
268                 }
269
270                 foreach ($element_array as $entry) {
271                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
272                                 return $entry[$key];
273                         }
274                 }
275
276                 return null;
277         }
278 }