summaryrefslogtreecommitdiff
path: root/src/utils.php
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-04-22 13:45:47 +0500
committerilotterytea <iltsu@alright.party>2025-04-22 14:13:28 +0500
commitaad9ffc2c02ccce9098558503e130e4cc9fc35b3 (patch)
treed32f070060a5e51381812d6bfb9e9e04b5637a8c /src/utils.php
parent460d12ab7bb93daa6b03300fefdfccefb6d32d01 (diff)
feat: CLIENT_REQUIRES_JSON constant, json_response and clamp method
Diffstat (limited to 'src/utils.php')
-rw-r--r--src/utils.php13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/utils.php b/src/utils.php
index 4954580..325603b 100644
--- a/src/utils.php
+++ b/src/utils.php
@@ -1,4 +1,13 @@
<?php
+define("CLIENT_REQUIRES_JSON", isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json");
+
+function json_response(mixed $response, int $status = 200)
+{
+ http_response_code($status);
+ header("Content-Type: application/json");
+ echo json_encode($response);
+}
+
function generate_random_string(int $length): string
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -48,4 +57,8 @@ function format_timestamp(int $timestamp_secs)
} else {
return "$days day" . ($days > 1 ? "s" : "");
}
+}
+function clamp(int $current, int $min, int $max): int
+{
+ return max($min, min($max, $current));
} \ No newline at end of file