2018-05-18 10:09:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zotlabs\Lib;
|
|
|
|
|
2018-06-20 02:33:50 +00:00
|
|
|
use Zotlabs\Web\HTTPSig;
|
2018-05-18 10:09:38 +00:00
|
|
|
|
2021-12-02 23:02:31 +00:00
|
|
|
class Zotfinger
|
|
|
|
{
|
|
|
|
|
|
|
|
public static function exec($resource, $channel = null, $verify = true)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$resource) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$m = parse_url($resource);
|
|
|
|
|
|
|
|
if ($m['host'] !== punify($m['host'])) {
|
|
|
|
$url = str_replace($m['host'], punify($m['host']), $url);
|
|
|
|
$m['host'] = punify($m['host']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = json_encode(['zot_token' => random_string()]);
|
|
|
|
|
|
|
|
if ($channel && $m) {
|
|
|
|
$headers = [
|
|
|
|
'Accept' => 'application/x-zot+json',
|
|
|
|
'Content-Type' => 'application/x-zot+json',
|
|
|
|
'X-Zot-Token' => random_string(),
|
|
|
|
'Digest' => HTTPSig::generate_digest_header($data),
|
|
|
|
'Host' => $m['host'],
|
|
|
|
'(request-target)' => 'post ' . get_request_string($resource)
|
|
|
|
];
|
|
|
|
$h = HTTPSig::create_sig($headers, $channel['channel_prvkey'], channel_url($channel), false);
|
|
|
|
} else {
|
|
|
|
$h = ['Accept: application/x-zot+json'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
$redirects = 0;
|
|
|
|
$x = z_post_url($resource, $data, $redirects, ['headers' => $h]);
|
|
|
|
|
|
|
|
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
|
|
|
}
|