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;
30 * This class contain methods to work with JsonLD data
35 * Loader for LD-JSON validation
39 * @return mixed the loaded data
40 * @throws \JsonLdException
42 public static function documentLoader($url)
45 case 'https://w3id.org/security/v1':
46 $url = DI::baseUrl() . '/static/security-v1.jsonld';
48 case 'https://w3id.org/identity/v1':
49 $url = DI::baseUrl() . '/static/identity-v1.jsonld';
51 case 'https://www.w3.org/ns/activitystreams':
52 $url = DI::baseUrl() . '/static/activitystreams.jsonld';
55 Logger::info('Got url', ['url' =>$url]);
61 $x = debug_backtrace();
64 if ($n['function'] === __FUNCTION__) {
71 Logger::error('jsonld bomb detected at: ' . $url);
75 $result = DI::cache()->get('documentLoader:' . $url);
76 if (!is_null($result)) {
80 $data = jsonld_default_document_loader($url);
81 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
86 * Normalises a given JSON array
90 * @return mixed|bool normalized JSON string
93 public static function normalize($json)
95 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
97 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
100 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
102 catch (Exception $e) {
105 $currentException = $e;
107 $messages[] = $currentException->getMessage();
108 } while($currentException = $currentException->getPrevious());
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]);
120 * Compacts a given JSON array
124 * @return array Compacted JSON array
127 public static function compact($json)
129 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
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 'mobilizon' => (object)['@id' => 'https://joinmobilizon.org/ns#', '@type' => '@id'],
146 // Preparation for adding possibly missing content to the context
147 if (!empty($json['@context']) && is_string($json['@context'])) {
148 $json['@context'] = [$json['@context']];
151 // Workaround for servers with missing context
152 // See issue https://github.com/nextcloud/social/issues/330
153 if (!empty($json['@context']) && is_array($json['@context'])) {
154 $json['@context'][] = 'https://w3id.org/security/v1';
157 // Trying to avoid memory problems with large content fields
158 if (!empty($json['object']['source']['content'])) {
159 $content = $json['object']['source']['content'];
160 $json['object']['source']['content'] = '';
163 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
166 $compacted = jsonld_compact($jsonobj, $context);
168 catch (Exception $e) {
170 Logger::notice('compacting error', ['line' => $e->getLine(), 'exception' => $e]);
173 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
175 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
176 $json['as:object']['as:source']['as:content'] = $content;
183 * Fetches an element array from a JSON array
189 * @return array fetched element
191 public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
193 if (!isset($array[$element])) {
197 // If it isn't an array yet, make it to one
198 if (!is_array($array[$element]) || !is_int(key($array[$element]))) {
199 $array[$element] = [$array[$element]];
204 foreach ($array[$element] as $entry) {
205 if (!is_array($entry) || is_null($key)) {
207 } elseif (isset($entry[$key])) {
208 $item = $entry[$key];
211 if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
220 * Fetches an element from a JSON array
228 * @return string fetched element
230 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
236 if (!isset($array[$element])) {
240 if (!is_array($array[$element])) {
241 return $array[$element];
244 if (is_null($type) || is_null($type_value)) {
245 $element_array = self::fetchElementArray($array, $element, $key);
246 if (is_null($element_array)) {
250 return array_shift($element_array);
253 $element_array = self::fetchElementArray($array, $element);
254 if (is_null($element_array)) {
258 foreach ($element_array as $entry) {
259 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {