streams/Code/Lib/Zotfinger.php

93 lines
2.9 KiB
PHP
Raw Normal View History

2018-05-18 10:09:38 +00:00
<?php
2022-02-16 04:08:28 +00:00
namespace Code\Lib;
2018-05-18 10:09:38 +00:00
2022-02-16 04:08:28 +00:00
use Code\Web\HTTPSig;
use Code\Lib\Channel;
use Code\Lib\Url;
2022-01-25 01:26:12 +00:00
2021-12-02 23:02:31 +00:00
class Zotfinger
{
public static function exec($resource, $channel = null, $verify = true, $recurse = true)
2021-12-02 23:02:31 +00:00
{
if (!$resource) {
return false;
}
$m = parse_url($resource);
if ($m['host'] !== punify($m['host'])) {
2022-08-21 00:15:14 +00:00
$resource = str_replace($m['host'], punify($m['host']), $resource);
2021-12-02 23:02:31 +00:00
$m['host'] = punify($m['host']);
}
$data = json_encode(['zot_token' => random_string()]);
2023-03-19 09:44:18 +00:00
$accepts = Libzot::getAccepts();
2021-12-02 23:02:31 +00:00
if ($channel && $m) {
$headers = [
2023-03-19 08:18:54 +00:00
'Accept' => $accepts,
'Content-Type' => 'application/x-nomad+json',
2021-12-02 23:02:31 +00:00
'X-Zot-Token' => random_string(),
'Digest' => HTTPSig::generate_digest_header($data),
'Host' => $m['host'],
'(request-target)' => 'post ' . get_request_string($resource)
];
2022-01-25 01:26:12 +00:00
$h = HTTPSig::create_sig($headers, $channel['channel_prvkey'], Channel::url($channel), false);
}
else {
2023-03-19 08:18:54 +00:00
$h = ['Accept: ' . $accepts];
2021-12-02 23:02:31 +00:00
}
$result = [];
$x = Url::post($resource, $data, ['headers' => $h]);
2021-12-02 23:02:31 +00:00
if (in_array(intval($x['return_code']), [ 404, 410 ]) && $recurse) {
// The resource has been deleted or doesn't exist at this location.
// Try to find another nomadic resource for this channel and return that.
// First, see if there's a hubloc for this site. Fetch that record to
// obtain the nomadic identity hash. Then use that to find any additional
// nomadic locations.
$h = Activity::get_actor_hublocs($resource, 'nomad');
if ($h) {
// mark this location deleted
hubloc_delete($h[0]);
$hubs = Activity::get_actor_hublocs($h[0]['hubloc_hash']);
if ($hubs) {
foreach ($hubs as $hub) {
if ($hub['hubloc_id_url'] !== $resource and !$hub['hubloc_deleted']) {
2022-02-05 22:07:10 +00:00
$rzf = self::exec($hub['hubloc_id_url'],$channel,$verify);
if ($rzf) {
return $rzf;
}
}
}
}
}
}
2021-12-02 23:02:31 +00:00
if ($x['success']) {
if ($verify) {
$result['signature'] = HTTPSig::verify($x, EMPTY_STR, 'zot6');
}
$result['data'] = json_decode($x['body'], true);
if ($result['data'] && is_array($result['data']) && array_key_exists('encrypted', $result['data']) && $result['data']['encrypted']) {
$result['data'] = json_decode(Crypto::unencapsulate($result['data'], get_config('system', 'prvkey')), true);
}
return $result;
}
return false;
}
2021-12-03 03:01:39 +00:00
}