use Friendica\Core\System;
use Friendica\DI;
use Friendica\Protocol\ActivityPub;
+use stdClass;
/**
* This class contain methods to work with JsonLD data
$url = DI::basePath() . '/static/apschema.jsonld';
break;
default:
- Logger::info('Got url', ['url' =>$url]);
+ Logger::info('Got url', ['url' => $url]);
break;
}
}
$x = debug_backtrace();
if ($x) {
foreach ($x as $n) {
- if ($n['function'] === __FUNCTION__) {
- $recursion ++;
+ if ($n['function'] === __FUNCTION__) {
+ $recursion++;
}
}
}
try {
$normalized = jsonld_normalize($jsonobj, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
- }
- catch (Exception $e) {
+ } catch (Exception $e) {
$normalized = false;
$messages = [];
$currentException = $e;
do {
$messages[] = $currentException->getMessage();
- } while($currentException = $currentException->getPrevious());
+ } while ($currentException = $currentException->getPrevious());
Logger::warning('JsonLD normalize error');
Logger::notice('JsonLD normalize error', ['messages' => $messages]);
{
jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
- $context = (object)['as' => 'https://www.w3.org/ns/activitystreams#',
+ $context = (object)[
+ 'as' => 'https://www.w3.org/ns/activitystreams#',
'w3id' => 'https://w3id.org/security#',
'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id'],
'vcard' => (object)['@id' => 'http://www.w3.org/2006/vcard/ns#', '@type' => '@id'],
$orig_json = $json;
+ $jsonobj = self::fixInvalidJsonLD($json);
+
+ try {
+ $compacted = jsonld_compact($jsonobj, $context);
+ } catch (Exception $e) {
+ $compacted = false;
+ Logger::notice('compacting error', ['msg' => $e->getMessage(), 'previous' => $e->getPrevious(), 'line' => $e->getLine()]);
+ if ($logfailed && DI::config()->get('debug', 'ap_log_failure')) {
+ $tempfile = tempnam(System::getTempPath(), 'failed-jsonld');
+ file_put_contents($tempfile, json_encode(['json' => $orig_json, 'msg' => $e->getMessage(), 'previous' => $e->getPrevious()], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
+ Logger::notice('Failed message stored', ['file' => $tempfile]);
+ }
+ }
+
+ $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
+
+ if ($json === false) {
+ Logger::notice('JSON encode->decode failed', ['orig_json' => $orig_json, 'compacted' => $compacted]);
+ $json = [];
+ }
+
+ return $json;
+ }
+
+ private static function fixInvalidJsonLD(array $json): stdClass
+ {
if (empty($json['@context'])) {
$json['@context'] = ActivityPub::CONTEXT;
}
// Workaround for servers with missing context
// See issue https://github.com/nextcloud/social/issues/330
if (!in_array('https://w3id.org/security/v1', $json['@context'])) {
+ Logger::debug('Missing security context');
$json['@context'][] = 'https://w3id.org/security/v1';
}
}
+ // Issue 14448: Peertube transmits an unexpected type and schema URL.
+ array_walk_recursive($json['@context'], function (&$value, $key) {
+ if ($key == '@type' && $value == '@json') {
+ Logger::debug('"@json" converted to "@id"');
+ $value = '@id';
+ }
+ if ($key == 'sc' && $value == 'http://schema.org/') {
+ Logger::debug('schema.org path fixed');
+ $value = 'http://schema.org#';
+ }
+ });
+
// Bookwyrm transmits "id" fields with "null", which isn't allowed.
array_walk_recursive($json, function (&$value, $key) {
if ($key == 'id' && is_null($value)) {
+ Logger::debug('Fixed null id');
$value = '';
}
});
- $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
-
- try {
- $compacted = jsonld_compact($jsonobj, $context);
- }
- catch (Exception $e) {
- $compacted = false;
- Logger::notice('compacting error', ['msg' => $e->getMessage(), 'previous' => $e->getPrevious(), 'line' => $e->getLine()]);
- if ($logfailed && DI::config()->get('debug', 'ap_log_failure')) {
- $tempfile = tempnam(System::getTempPath(), 'failed-jsonld');
- file_put_contents($tempfile, json_encode(['json' => $orig_json, 'msg' => $e->getMessage(), 'previous' => $e->getPrevious()], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
- Logger::notice('Failed message stored', ['file' => $tempfile]);
- }
- }
-
- $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
-
- if ($json === false) {
- Logger::notice('JSON encode->decode failed', ['orig_json' => $orig_json, 'compacted' => $compacted]);
- $json = [];
- }
-
- return $json;
+ return json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
-
/**
* Fetches an element array from a JSON array
*