Files
matrix-register-bot/functions.php
2018-02-23 17:43:08 +01:00

52 lines
1.1 KiB
PHP

<?php
function abort($msg="Unknown")
{
header("403 Forbidden");
$response = array(
"success" => false,
"message" => $msg
);
echo json_encode($response);
print("\n");
exit();
}
function exec_curl_request($handle)
{
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not want to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
throw new Exception("Server responds: '" . $response['error'] . "'");
} else {
$response = json_decode($response, true);
if (isset($response["event_id"])) {
$response = true;
} else {
$response = false;
}
}
return $response;
}
?>