3 * @copyright Copyright (C) 2020, Friendica
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\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)
46 $x = debug_backtrace();
49 if ($n['function'] === __FUNCTION__) {
56 Logger::error('jsonld bomb detected at: ' . $url);
60 $result = DI::cache()->get('documentLoader:' . $url);
61 if (!is_null($result)) {
65 $data = jsonld_default_document_loader($url);
66 DI::cache()->set('documentLoader:' . $url, $data, Duration::DAY);
71 * Normalises a given JSON array
75 * @return mixed|bool normalized JSON string
78 public static function normalize($json)
80 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
82 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
85 $normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
87 catch (Exception $e) {
90 $currentException = $e;
92 $messages[] = $currentException->getMessage();
93 } while($currentException = $currentException->getPrevious());
95 Logger::warning('JsonLD normalize error');
96 Logger::notice('JsonLD normalize error', ['messages' => $messages]);
97 Logger::info('JsonLD normalize error', ['trace' => $e->getTraceAsString()]);
98 Logger::debug('JsonLD normalize error', ['jsonobj' => $jsonobj]);
105 * Compacts a given JSON array
109 * @return array Compacted JSON array
112 public static function compact($json)
114 jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
116 $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
117 'w3id' => 'https://w3id.org/security#',
118 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
119 'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
120 'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
121 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
122 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
123 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
124 'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id'],
125 'litepub' => (object)['@id' => 'http://litepub.social/ns#', '@type' => '@id'],
126 'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'],
127 'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id']];
129 // Preparation for adding possibly missing content to the context
130 if (!empty($json['@context']) && is_string($json['@context'])) {
131 $json['@context'] = [$json['@context']];
134 // Workaround for servers with missing context
135 // See issue https://github.com/nextcloud/social/issues/330
136 if (!empty($json['@context']) && is_array($json['@context'])) {
137 $json['@context'][] = 'https://w3id.org/security/v1';
140 // Trying to avoid memory problems with large content fields
141 if (!empty($json['object']['source']['content'])) {
142 $content = $json['object']['source']['content'];
143 $json['object']['source']['content'] = '';
146 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
149 $compacted = jsonld_compact($jsonobj, $context);
151 catch (Exception $e) {
153 Logger::error('compacting error');
154 // Sooner or later we should log some details as well - but currently this leads to memory issues
155 // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
158 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
160 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
161 $json['as:object']['as:source']['as:content'] = $content;
168 * Fetches an element array from a JSON array
174 * @return array fetched element
176 public static function fetchElementArray($array, $element, $key = null)
178 if (!isset($array[$element])) {
182 // If it isn't an array yet, make it to one
183 if (!is_int(key($array[$element]))) {
184 $array[$element] = [$array[$element]];
189 foreach ($array[$element] as $entry) {
190 if (!is_array($entry) || (is_null($key) && is_array($entry))) {
191 $elements[] = $entry;
192 } elseif (!is_null($key) && isset($entry[$key])) {
193 $elements[] = $entry[$key];
201 * Fetches an element from a JSON array
209 * @return string fetched element
211 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
217 if (!isset($array[$element])) {
221 if (!is_array($array[$element])) {
222 return $array[$element];
225 if (is_null($type) || is_null($type_value)) {
226 $element_array = self::fetchElementArray($array, $element, $key);
227 if (is_null($element_array)) {
231 return array_shift($element_array);
234 $element_array = self::fetchElementArray($array, $element);
235 if (is_null($element_array)) {
239 foreach ($element_array as $entry) {
240 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {