2021-11-25 14:52:37 +00:00
|
|
|
<?php
|
|
|
|
|
2021-11-25 19:00:18 +00:00
|
|
|
function get_request_json() {
|
|
|
|
return @json_decode(file_get_contents('php://input'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function unquote($text) {
|
|
|
|
$text = str_replace("'", "", $text);
|
|
|
|
$text = str_replace('"', '', $text);
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:43:35 +00:00
|
|
|
# HTTP result => JSON
|
|
|
|
function hres_json($code, $status, $message) {
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
return hres(json_encode([
|
|
|
|
'status' => $status,
|
|
|
|
'message' => $message
|
|
|
|
]), $code);
|
|
|
|
}
|
|
|
|
|
|
|
|
# HTTP result
|
|
|
|
function hres($message, $code=200) {
|
|
|
|
http_response_code($code);
|
|
|
|
die($message);
|
|
|
|
}
|
|
|
|
|
2021-11-25 14:52:37 +00:00
|
|
|
# Support instance aliases!
|
|
|
|
function resolve_instance($txt) {
|
|
|
|
$txt = preg_replace('/^https?:\/\//', '', strtolower(trim($txt)));
|
|
|
|
if (!preg_match('/^[a-zA-Z0-9\-\.]+$/', $txt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://'.$txt.'/.well-known/host-meta');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, 1);
|
|
|
|
$output = curl_exec($ch);
|
|
|
|
$headers = explode("\n", $output);
|
|
|
|
if (count($headers) === 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$name = false;
|
|
|
|
$httpret = trim($headers[0]);
|
|
|
|
$httpret = explode(' ', $httpret);
|
|
|
|
if (count($httpret) !== 2)
|
|
|
|
return false;
|
|
|
|
$httpret = intval($httpret[1]);
|
|
|
|
|
|
|
|
# search for location header
|
|
|
|
if (in_array($httpret, [301, 302])) {
|
|
|
|
foreach ($headers as $header) {
|
|
|
|
$header = strtolower(trim($header));
|
|
|
|
if (str_starts_with($header, 'location:')) {
|
|
|
|
$url = trim(preg_replace('/^location:/','',$header));
|
|
|
|
$url = parse_url($url);
|
|
|
|
if (isset($url['host'])) {
|
|
|
|
$name = $url['host'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($httpret === 200) {
|
|
|
|
$name = $txt;
|
|
|
|
}
|
|
|
|
if ($name === false)
|
|
|
|
return $name;
|
|
|
|
|
|
|
|
return 'https://'.$name;
|
|
|
|
}
|
|
|
|
|