3 * @copyright Copyright (C) 2010-2021, 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\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']];
144 // Preparation for adding possibly missing content to the context
145 if (!empty($json['@context']) && is_string($json['@context'])) {
146 $json['@context'] = [$json['@context']];
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';
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'] = '';
161 $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
164 $compacted = jsonld_compact($jsonobj, $context);
166 catch (Exception $e) {
168 Logger::error('compacting error', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
171 $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
173 if (isset($json['as:object']['as:source']['as:content']) && !empty($content)) {
174 $json['as:object']['as:source']['as:content'] = $content;
181 * Fetches an element array from a JSON array
187 * @return array fetched element
189 public static function fetchElementArray($array, $element, $key = null, $type = null, $type_value = null)
191 if (!isset($array[$element])) {
195 // If it isn't an array yet, make it to one
196 if (!is_array($array[$element]) || !is_int(key($array[$element]))) {
197 $array[$element] = [$array[$element]];
202 foreach ($array[$element] as $entry) {
203 if (!is_array($entry) || is_null($key)) {
205 } elseif (isset($entry[$key])) {
206 $item = $entry[$key];
209 if (isset($item) && (is_null($type) || is_null($type_value) || isset($item[$type]) && $item[$type] == $type_value)) {
218 * Fetches an element from a JSON array
226 * @return string fetched element
228 public static function fetchElement($array, $element, $key = '@id', $type = null, $type_value = null)
234 if (!isset($array[$element])) {
238 if (!is_array($array[$element])) {
239 return $array[$element];
242 if (is_null($type) || is_null($type_value)) {
243 $element_array = self::fetchElementArray($array, $element, $key);
244 if (is_null($element_array)) {
248 return array_shift($element_array);
251 $element_array = self::fetchElementArray($array, $element);
252 if (is_null($element_array)) {
256 foreach ($element_array as $entry) {
257 if (isset($entry[$key]) && isset($entry[$type]) && ($entry[$type] == $type_value)) {