Merge branch 'dev' of ../p3 into dev

This commit is contained in:
nobody 2021-08-03 15:44:38 -07:00
commit af8f724264
2 changed files with 57 additions and 47 deletions

View file

@ -80,22 +80,23 @@ class Activity {
$channel = get_sys_channel(); $channel = get_sys_channel();
} }
$idn = parse_url($url); $parsed = parse_url($url);
if ($idn['host'] !== punify($idn['host'])) {
$url = str_replace($idn['host'],punify($idn['host']),$url); // perform IDN substitution
if ($parsed['host'] !== punify($parsed['host'])) {
$url = str_replace($parsed['host'],punify($parsed['host']),$url);
} }
logger('fetch: ' . $url, LOGGER_DEBUG); logger('fetch: ' . $url, LOGGER_DEBUG);
if (strpos($url,'x-zot:') === 0) { if (isset($parsed['scheme']) && $parsed['scheme'] === 'x-zot') {
$x = ZotURL::fetch($url,$channel,$hub); $x = ZotURL::fetch($url,$channel,$hub);
} }
else { else {
$m = parse_url($url);
// handle bearcaps // handle bearcaps
if ($m['scheme'] === 'bear' && $m['query']) { if (isset($parsed['scheme']) && isset($parsed['query']) && $parsed['scheme'] === 'bear' && $parsed['query'] !== EMPTY_STR) {
$params = explode('&',$m['query']); $params = explode('&', $parsed['query']);
if ($params) { if ($params) {
foreach ($params as $p) { foreach ($params as $p) {
if (substr($p,0,2) === 'u=') { if (substr($p,0,2) === 'u=') {
@ -105,18 +106,26 @@ class Activity {
$token = substr($p,2); $token = substr($p,2);
} }
} }
// re-parse the URL because it changed and we need the host in the next section // the entire URL just changed so parse it again
$m = parse_url($url); $parsed = parse_url($url);
} }
} }
// Ignore fragments; as we are not in a browser and some platforms (e.g. Django or at least funkwhale) don't handle them well
unset($parsed['fragment']);
// rebuild the url
$url = unparse_url($parsed);
logger('fetch_actual: ' . $url, LOGGER_DEBUG);
$headers = [ $headers = [
'Accept' => 'application/activity+json, application/x-zot-activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"', 'Accept' => 'application/activity+json, application/x-zot-activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'Host' => $m['host'], 'Host' => $parsed['host'],
'Date' => datetime_convert('UTC','UTC', 'now', 'D, d M Y H:i:s \\G\\M\\T'), 'Date' => datetime_convert('UTC','UTC', 'now', 'D, d M Y H:i:s \\G\\M\\T'),
'(request-target)' => 'get ' . get_request_string($url) '(request-target)' => 'get ' . get_request_string($url)
]; ];
if (isset($token)) { if (isset($token)) {
$headers['Authorization'] = 'Bearer ' . $token; $headers['Authorization'] = 'Bearer ' . $token;
} }
@ -129,15 +138,12 @@ class Activity {
$y = json_decode($x['body'],true); $y = json_decode($x['body'],true);
logger('returned: ' . json_encode($y,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); logger('returned: ' . json_encode($y,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
$m = parse_url($url); $site_url = unparse_url( ['scheme' => $parsed['scheme'], 'host' => $parsed['host'], 'port' => ((array_key_exists('port',$parsed) && intval($parsed['port'])) ? $parsed['port'] : 0) ] );
if ($m) { q("update site set site_update = '%s' where site_url = '%s' and site_update < %s - INTERVAL %s",
$site_url = unparse_url( ['scheme' => $m['scheme'], 'host' => $m['host'], 'port' => ((array_key_exists('port',$m) && intval($m['port'])) ? $m['port'] : 0) ] ); dbesc(datetime_convert()),
q("update site set site_update = '%s' where site_url = '%s' and site_update < %s - INTERVAL %s", dbesc($site_url),
dbesc(datetime_convert()), db_utcnow(), db_quoteinterval('1 DAY')
dbesc($site_url), );
db_utcnow(), db_quoteinterval('1 DAY')
);
}
// check for a valid signature, but only if this is not an actor object. If it is signed, it must be valid. // check for a valid signature, but only if this is not an actor object. If it is signed, it must be valid.
// Ignore actors because of the potential for infinite recursion if we perform this step while // Ignore actors because of the potential for infinite recursion if we perform this step while
@ -153,8 +159,6 @@ class Activity {
} }
return json_decode($x['body'], true); return json_decode($x['body'], true);
} }
else { else {
logger('fetch failed: ' . $url); logger('fetch failed: ' . $url);

View file

@ -285,24 +285,28 @@ class HTTPSig {
static function get_activitystreams_key($id,$force = false) { static function get_activitystreams_key($id,$force = false) {
// remove fragment // Check the local cache first, but remove any fragments like #main-key since these won't be present in our cached data
$url = ((strpos($id,'#')) ? substr($id,0,strpos($id,'#')) : $id); $cache_url = ((strpos($id,'#')) ? substr($id,0,strpos($id,'#')) : $id);
// $force is used to ignore the local cache and only use the remote data; for instance the cached key might be stale
if (! $force) { if (! $force) {
$x = q("select * from xchan left join hubloc on xchan_hash = hubloc_hash where hubloc_addr = '%s' or hubloc_id_url = '%s' order by hubloc_id desc", $x = q("select * from xchan left join hubloc on xchan_hash = hubloc_hash where hubloc_addr = '%s' or hubloc_id_url = '%s' order by hubloc_id desc",
dbesc(str_replace('acct:','',$url)), dbesc(str_replace('acct:','',$cache_url)),
dbesc($url) dbesc($cache_url)
); );
if ($x) {
$best = Libzot::zot_record_preferred($x);
}
if ($best && $best['xchan_pubkey']) {
return [ 'portable_id' => $best['xchan_hash'], 'public_key' => $best['xchan_pubkey'] , 'hubloc' => $best ];
}
} }
if ($x) { // The record wasn't in cache. Fetch it now.
$best = Libzot::zot_record_preferred($x);
}
if ($best && $best['xchan_pubkey']) {
return [ 'portable_id' => $best['xchan_hash'], 'public_key' => $best['xchan_pubkey'] , 'hubloc' => $best ];
}
$r = Activity::fetch($id); $r = Activity::fetch($id);
@ -314,6 +318,9 @@ class HTTPSig {
} }
} }
} }
// No key was found
return false; return false;
} }
@ -325,14 +332,14 @@ class HTTPSig {
dbesc(str_replace('acct:','',$id)), dbesc(str_replace('acct:','',$id)),
dbesc($id) dbesc($id)
); );
}
if ($x) { if ($x) {
$best = Libzot::zot_record_preferred($x); $best = Libzot::zot_record_preferred($x);
} }
if ($best && $best['xchan_pubkey']) { if ($best && $best['xchan_pubkey']) {
return [ 'portable_id' => $best['xchan_hash'], 'public_key' => $best['xchan_pubkey'] , 'hubloc' => $best ]; return [ 'portable_id' => $best['xchan_hash'], 'public_key' => $best['xchan_pubkey'] , 'hubloc' => $best ];
}
} }
$wf = Webfinger::exec($id); $wf = Webfinger::exec($id);
@ -365,17 +372,16 @@ class HTTPSig {
dbesc(str_replace('acct:','',$id)), dbesc(str_replace('acct:','',$id)),
dbesc($id) dbesc($id)
); );
}
if ($x) { if ($x) {
$best = Libzot::zot_record_preferred($x); $best = Libzot::zot_record_preferred($x);
} }
if ($best && $best['xchan_pubkey']) { if ($best && $best['xchan_pubkey']) {
return [ 'portable_id' => $best['xchan_hash'], 'public_key' => $best['xchan_pubkey'] , 'hubloc' => $best ]; return [ 'portable_id' => $best['xchan_hash'], 'public_key' => $best['xchan_pubkey'] , 'hubloc' => $best ];
}
} }
$wf = Webfinger::exec($id); $wf = Webfinger::exec($id);
$key = [ 'portable_id' => '', 'public_key' => '', 'hubloc' => [] ]; $key = [ 'portable_id' => '', 'public_key' => '', 'hubloc' => [] ];