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