3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Util;
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Logger;
27 use Friendica\Core\System;
31 * This class contain methods to work with JsonLD data
36 * Loader for LD-JSON validation
40 * @return mixed the loaded data
41 * @throws \JsonLdException
43 public static function documentLoader($url)
46 case 'https://w3id.org/security/v1':
47 $url = DI::baseUrl() . '/static/security-v1.jsonld';
49 case 'https://w3id.org/identity/v1':
50 $url = DI::baseUrl() . '/static/identity-v1.jsonld';
52 case 'https://www.w3.org/ns/activitystreams':
53 $url = DI::baseUrl() . '/static/activitystreams.jsonld';
56 Logger::info('Got url', ['url' =>$url]);
62 $x = debug_backtrace();
65 if ($n['function'] === __FUNCTION__) {
72 Logger::error('jsonld bomb detected at: ' . $url);
76 $result = DI::cache()->get('documentLoader:' . $url);
77 if (!is_null($result)) {
81 $data = jsonld_default_document_loader($url);
82 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
87 * Normalises a given JSON array
91 * @return mixed|bool normalized JSON string
94 public static function normalize($json)
96 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
98 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
101 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
103 catch (Exception $e) {
106 $currentException = $e;
108 $messages[] = $currentException->getMessage();
109 } while($currentException = $currentException->getPrevious());
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]);
121 * Compacts a given JSON array
125 * @return array Compacted JSON array
128 public static function compact($json)
130 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
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'],
147 // Preparation for adding possibly missing content to the context
148 if (!empty($json['@context']) && is_string($json['@context'])) {
149 $json['@context'] = [$json['@context']];
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';
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'] = '';
164 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
167 $compacted = jsonld_compact($jsonobj, $context);
169 catch (Exception $e) {
171 Logger::notice('compacting error', ['line' => $e->getLine(), 'exception' => $e]);
174 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
176 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
177 $json['as:object']['as:source']['as:content'] = $content;
184 * Fetches an element array from a JSON array
190 * @return array fetched element
192 public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
194 if (!isset($array[$element])) {
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]];
205 foreach ($array[$element] as $entry) {
206 if (!is_array($entry) || is_null($key)) {
208 } elseif (isset($entry[$key])) {
209 $item = $entry[$key];
212 if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
221 * Fetches an element from a JSON array
229 * @return string fetched element
231 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
237 if (!isset($array[$element])) {
241 if (!is_array($array[$element])) {
242 return $array[$element];
245 if (is_null($type) || is_null($type_value)) {
246 $element_array = self::fetchElementArray($array, $element, $key);
247 if (is_null($element_array)) {
251 return array_shift($element_array);
254 $element_array = self::fetchElementArray($array, $element);
255 if (is_null($element_array)) {
259 foreach ($element_array as $entry) {
260 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {