]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Use local copy of the w3id security context
[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         private static function replaceSecurityLink(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                         $json['@context'][$key] = DI::baseUrl() . '/static/w3id-security-v1.json';
80                 }
81
82                 return $json;
83         }
84
85         /**
86          * Normalises a given JSON array
87          *
88          * @param array $json
89          *
90          * @return mixed|bool normalized JSON string
91          * @throws Exception
92          */
93         public static function normalize($json)
94         {
95                 $json = self::replaceSecurityLink($json);
96
97                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
98
99                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
100
101                 try {
102                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
103                 }
104                 catch (Exception $e) {
105                         $normalized = false;
106                         $messages = [];
107                         $currentException = $e;
108                         do {
109                                 $messages[] = $currentException->getMessage();
110                         } while($currentException = $currentException->getPrevious());
111
112                         Logger::warning('JsonLD normalize error');
113                         Logger::notice('JsonLD normalize error', ['messages' => $messages]);
114                         Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
115                         Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
116                 }
117
118                 return $normalized;
119         }
120
121         /**
122          * Compacts a given JSON array
123          *
124          * @param array $json
125          *
126          * @return array Compacted JSON array
127          * @throws Exception
128          */
129         public static function compact($json)
130         {
131                 $compacted = self::internalCompact($json, false);
132                 if (empty($compacted)) {
133                         $json['@context'] = ActivityPub::CONTEXT;
134                         $compacted = self::internalCompact($json, true);
135                 }
136
137                 return $compacted;
138         }
139
140         private static function internalCompact($json, bool $error_log)
141         {
142                 $json = self::replaceSecurityLink($json);
143
144                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
145
146                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
147                         'w3id' => 'https://w3id.org/security#',
148                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
149                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
150                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
151                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
152                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
153                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
154                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
155                         'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
156                         'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
157                         'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']];
158
159                 // Preparation for adding possibly missing content to the context
160                 if (!empty($json['@context']) && is_string($json['@context'])) {
161                         $json['@context'] = [$json['@context']];
162                 }
163
164                 // Workaround for servers with missing context
165                 // See issue https://github.com/nextcloud/social/issues/330
166                 if (!empty($json['@context']) && is_array($json['@context'])) {
167                         $json['@context'][] = DI::baseUrl() . '/static/w3id-security-v1.json';
168                 }
169
170                 // Trying to avoid memory problems with large content fields
171                 if (!empty($json['object']['source']['content'])) {
172                         $content = $json['object']['source']['content'];
173                         $json['object']['source']['content'] = '';
174                 }
175
176                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
177
178                 try {
179                         $compacted = jsonld_compact($jsonobj, $context);
180                 }
181                 catch (Exception $e) {
182                         $compacted = false;
183                         if ($error_log) {
184                                 Logger::error('compacting error', ['line' => $e->getLine(), 'message' => $e->getMessage(),'callstack' => System::callstack(20)]);
185                         }
186                 }
187
188                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
189
190                 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
191                         $json['as:object']['as:source']['as:content'] = $content;
192                 }
193
194                 return $json;
195         }
196
197         /**
198          * Fetches an element array from a JSON array
199          *
200          * @param $array
201          * @param $element
202          * @param $key
203          *
204          * @return array fetched element
205          */
206         public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
207         {
208                 if (!isset($array[$element])) {
209                         return null;
210                 }
211
212                 // If it isn't an array yet, make it to one
213                 if (!is_int(key($array[$element]))) {
214                         $array[$element] = [$array[$element]];
215                 }
216
217                 $elements = [];
218
219                 foreach ($array[$element] as $entry) {
220                         if (!is_array($entry) || is_null($key)) {
221                                 $item = $entry;
222                         } elseif (isset($entry[$key])) {
223                                 $item = $entry[$key];
224                         }
225
226                         if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
227                                 $elements[] = $item;
228                         }
229                 }
230
231                 return $elements;
232         }
233
234         /**
235          * Fetches an element from a JSON array
236          *
237          * @param $array
238          * @param $element
239          * @param $key
240          * @param $type
241          * @param $type_value
242          *
243          * @return string fetched element
244          */
245         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
246         {
247                 if (empty($array)) {
248                         return null;
249                 }
250
251                 if (!isset($array[$element])) {
252                         return null;
253                 }
254
255                 if (!is_array($array[$element])) {
256                         return $array[$element];
257                 }
258
259                 if (is_null($type) || is_null($type_value)) {
260                         $element_array = self::fetchElementArray($array, $element, $key);
261                         if (is_null($element_array)) {
262                                 return null;
263                         }
264
265                         return array_shift($element_array);
266                 }
267
268                 $element_array = self::fetchElementArray($array, $element);
269                 if (is_null($element_array)) {
270                         return null;
271                 }
272
273                 foreach ($element_array as $entry) {
274                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
275                                 return $entry[$key];
276                         }
277                 }
278
279                 return null;
280         }
281 }