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