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
|
|
|
|
|
|
|
class Zotfinger {
|
|
|
|
|
2020-07-15 23:29:17 +00:00
|
|
|
static function exec($resource,$channel = null,$verify = true) {
|
2018-05-18 10:09:38 +00:00
|
|
|
|
2020-07-06 06:18:31 +00:00
|
|
|
if (! $resource) {
|
2018-05-18 10:09:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 04:08:57 +00:00
|
|
|
$m = parse_url($resource);
|
2021-04-29 03:29:46 +00:00
|
|
|
|
|
|
|
if ($m['host'] !== punify($m['host'])) {
|
|
|
|
$url = str_replace($m['host'],punify($m['host']),$url);
|
|
|
|
$m['host'] = punify($m['host']);
|
|
|
|
}
|
2018-10-10 04:08:57 +00:00
|
|
|
|
|
|
|
$data = json_encode([ 'zot_token' => random_string() ]);
|
|
|
|
|
2020-07-06 06:18:31 +00:00
|
|
|
if ($channel && $m) {
|
2018-10-10 04:08:57 +00:00
|
|
|
|
2018-06-20 02:33:50 +00:00
|
|
|
$headers = [
|
2018-10-11 00:58:51 +00:00
|
|
|
'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)
|
2018-06-20 02:33:50 +00:00
|
|
|
];
|
|
|
|
$h = HTTPSig::create_sig($headers,$channel['channel_prvkey'],channel_url($channel),false);
|
2018-05-24 04:48:19 +00:00
|
|
|
}
|
2018-06-04 04:23:34 +00:00
|
|
|
else {
|
|
|
|
$h = [ 'Accept: application/x-zot+json' ];
|
|
|
|
}
|
|
|
|
|
2018-05-18 10:09:38 +00:00
|
|
|
$result = [];
|
|
|
|
|
|
|
|
$redirects = 0;
|
2018-10-10 04:08:57 +00:00
|
|
|
$x = z_post_url($resource,$data,$redirects, [ 'headers' => $h ] );
|
2018-05-24 04:48:19 +00:00
|
|
|
|
2020-07-06 06:18:31 +00:00
|
|
|
if ($x['success']) {
|
2019-12-26 01:11:41 +00:00
|
|
|
|
2020-07-15 23:29:17 +00:00
|
|
|
if ($verify) {
|
|
|
|
$result['signature'] = HTTPSig::verify($x, EMPTY_STR, 'zot6');
|
|
|
|
}
|
2018-07-26 02:56:24 +00:00
|
|
|
|
2018-05-18 10:09:38 +00:00
|
|
|
$result['data'] = json_decode($x['body'],true);
|
2018-05-24 04:48:19 +00:00
|
|
|
|
2020-07-06 06:18:31 +00:00
|
|
|
if ($result['data'] && is_array($result['data']) && array_key_exists('encrypted',$result['data']) && $result['data']['encrypted']) {
|
2018-12-17 04:37:43 +00:00
|
|
|
$result['data'] = json_decode(Crypto::unencapsulate($result['data'],get_config('system','prvkey')),true);
|
2018-05-24 04:48:19 +00:00
|
|
|
}
|
2018-05-29 02:42:40 +00:00
|
|
|
|
2018-05-18 10:09:38 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|