]> git.mxchange.org Git - friendica.git/blob - src/Util/JsonLD.php
Merge pull request #10257 from annando/apcontact-no-normalize
[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                 switch ($url) {
45                         case 'https://w3id.org/security/v1':
46                                 $url = DI::baseUrl() . '/static/security-v1.jsonld';
47                                 break;
48                         case 'https://w3id.org/identity/v1':
49                                 $url = DI::baseUrl() . '/static/identity-v1.jsonld';
50                                 break;
51                         case 'https://www.w3.org/ns/activitystreams':
52                                 $url = DI::baseUrl() . '/static/activitystreams.jsonld';
53                                 break;
54                         default:
55                                 Logger::info('Got url', ['url' =>$url]);
56                                 break;
57                 }
58
59                 $recursion = 0;
60
61                 $x = debug_backtrace();
62                 if ($x) {
63                         foreach ($x as $n) {
64                                 if ($n['function'] === __FUNCTION__)  {
65                                         $recursion ++;
66                                 }
67                         }
68                 }
69
70                 if ($recursion > 5) {
71                         Logger::error('jsonld bomb detected at: ' . $url);
72                         exit();
73                 }
74
75                 $result = DI::cache()->get('documentLoader:' . $url);
76                 if (!is_null($result)) {
77                         return $result;
78                 }
79
80                 $data = jsonld_default_document_loader($url);
81                 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
82                 return $data;
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                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
96
97                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
98
99                 try {
100                         $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
101                 }
102                 catch (Exception $e) {
103                         $normalized = false;
104                         $messages = [];
105                         $currentException = $e;
106                         do {
107                                 $messages[] = $currentException->getMessage();
108                         } while($currentException = $currentException->getPrevious());
109
110                         Logger::warning('JsonLD normalize error');
111                         Logger::notice('JsonLD normalize error', ['messages' => $messages]);
112                         Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
113                         Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
114                 }
115
116                 return $normalized;
117         }
118
119         /**
120          * Compacts a given JSON array
121          *
122          * @param array $json
123          *
124          * @return array Compacted JSON array
125          * @throws Exception
126          */
127         public static function compact($json)
128         {
129                 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
130
131                 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
132                         'w3id' => 'https://w3id.org/security#',
133                         'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
134                         'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
135                         'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
136                         'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
137                         'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
138                         'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
139                         'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
140                         'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
141                         'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
142                         'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']];
143
144                 // Preparation for adding possibly missing content to the context
145                 if (!empty($json['@context']) && is_string($json['@context'])) {
146                         $json['@context'] = [$json['@context']];
147                 }
148
149                 // Workaround for servers with missing context
150                 // See issue https://github.com/nextcloud/social/issues/330
151                 if (!empty($json['@context']) && is_array($json['@context'])) {
152                         $json['@context'][] = 'https://w3id.org/security/v1';
153                 }
154
155                 // Trying to avoid memory problems with large content fields
156                 if (!empty($json['object']['source']['content'])) {
157                         $content = $json['object']['source']['content'];
158                         $json['object']['source']['content'] = '';
159                 }
160
161                 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
162
163                 try {
164                         $compacted = jsonld_compact($jsonobj, $context);
165                 }
166                 catch (Exception $e) {
167                         $compacted = false;
168                         Logger::error('compacting error', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
169                 }
170
171                 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
172
173                 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
174                         $json['as:object']['as:source']['as:content'] = $content;
175                 }
176
177                 return $json;
178         }
179
180         /**
181          * Fetches an element array from a JSON array
182          *
183          * @param $array
184          * @param $element
185          * @param $key
186          *
187          * @return array fetched element
188          */
189         public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
190         {
191                 if (!isset($array[$element])) {
192                         return null;
193                 }
194
195                 // If it isn't an array yet, make it to one
196                 if (!is_int(key($array[$element]))) {
197                         $array[$element] = [$array[$element]];
198                 }
199
200                 $elements = [];
201
202                 foreach ($array[$element] as $entry) {
203                         if (!is_array($entry) || is_null($key)) {
204                                 $item = $entry;
205                         } elseif (isset($entry[$key])) {
206                                 $item = $entry[$key];
207                         }
208
209                         if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
210                                 $elements[] = $item;
211                         }
212                 }
213
214                 return $elements;
215         }
216
217         /**
218          * Fetches an element from a JSON array
219          *
220          * @param $array
221          * @param $element
222          * @param $key
223          * @param $type
224          * @param $type_value
225          *
226          * @return string fetched element
227          */
228         public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
229         {
230                 if (empty($array)) {
231                         return null;
232                 }
233
234                 if (!isset($array[$element])) {
235                         return null;
236                 }
237
238                 if (!is_array($array[$element])) {
239                         return $array[$element];
240                 }
241
242                 if (is_null($type) || is_null($type_value)) {
243                         $element_array = self::fetchElementArray($array, $element, $key);
244                         if (is_null($element_array)) {
245                                 return null;
246                         }
247
248                         return array_shift($element_array);
249                 }
250
251                 $element_array = self::fetchElementArray($array, $element);
252                 if (is_null($element_array)) {
253                         return null;
254                 }
255
256                 foreach ($element_array as $entry) {
257                         if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {
258                                 return $entry[$key];
259                         }
260                 }
261
262                 return null;
263         }
264 }