hres and hres_json for responses

master
Bofh 1 year ago
parent 3ad1d22f46
commit 96d6b5f083

@ -3,12 +3,8 @@
$instance = resolve_instance($_GET['instance'] ?? '');
if ($instance === false) {
http_response_code(400);
die(json_encode([
'status' => 'error',
'message' => lr('error.instance_not_exists',
'Instance does not exist or is incorrect.')
]));
hres_json(400, ERR, lr('error.instance_not_exists',
'Instance does not exist or is incorrect.'));
}
$app_name = conf('app_name', 'RealFan');
@ -33,12 +29,8 @@ curl_close($ch);
# result must be JSON
$result = @json_decode($result);
if ($result === null) {
http_response_code(500);
die(json_encode([
'status' => 'error',
'message' => lr('error.instance_failed_or_not_mastodon',
'Instance failed to create an authorization App. Is it an instance?')
]));
hres_json(500, ERR, lr('error.instance_failed_or_not_mastodon',
'Instance failed to create an authorization App. Is it an instance?'));
}
# create the URL for OAUTH
@ -48,7 +40,4 @@ $query_string = http_build_query([
'response_type' => 'code',
'scope' => $scopes
]);
echo json_encode([
'status' => 'ok',
'message' => $instance.'/oauth/autorize?'.$query_string
]);
hres_json(200, OK, $instance.'/oauth/authorize?'.$query_string);

@ -1,5 +1,20 @@
<?php
# 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);
}
# Support instance aliases!
function resolve_instance($txt) {
$txt = preg_replace('/^https?:\/\//', '', strtolower(trim($txt)));

@ -1,5 +1,8 @@
<?php
define('OK', 'ok');
define('ERR', 'err');
$env = 'prod';
if (getenv('ENV') !== false && in_array(getenv('ENV'), ['prod', 'dev']))
$env = getenv('ENV');

Loading…
Cancel
Save