summaryrefslogtreecommitdiff
path: root/lib/utils.php
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-12-08 22:17:05 +0500
committerilotterytea <iltsu@alright.party>2025-12-08 22:17:05 +0500
commit95800ffe216a83bc0eba994ecc53ed22860fe90e (patch)
tree69f1bcb85e63a5fc0fcbc6d70eb56e22940fd6fd /lib/utils.php
parent57472eab3c7b035392c6a5aa240593ecaa7d1ccf (diff)
upd: include paths
Diffstat (limited to 'lib/utils.php')
-rw-r--r--lib/utils.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/utils.php b/lib/utils.php
new file mode 100644
index 0000000..87d96c6
--- /dev/null
+++ b/lib/utils.php
@@ -0,0 +1,68 @@
+<?php
+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";
+ $output = "";
+
+ for ($i = 0; $i < $length; $i++) {
+ $charindex = random_int(0, strlen($chars) - 1);
+ $output .= $chars[$charindex];
+ }
+
+ return $output;
+}
+
+function str_safe(string $s, int|null $max_length, bool $remove_new_lines = true): string
+{
+ $output = $s;
+
+ if ($remove_new_lines) {
+ $output = str_replace(PHP_EOL, "", $output);
+ }
+
+ $output = htmlspecialchars($output);
+ $output = strip_tags($output);
+
+ if ($max_length) {
+ $output = substr($output, 0, $max_length);
+ }
+
+ $output = trim($output);
+
+ return $output;
+}
+
+function format_timestamp(int $timestamp_secs)
+{
+ $days = (int) floor($timestamp_secs / (60.0 * 60.0 * 24.0));
+ $hours = (int) floor(round($timestamp_secs / (60 * 60)) % 24);
+ $minutes = (int) floor(round($timestamp_secs % (60 * 60)) / 60);
+ $seconds = (int) floor($timestamp_secs % 60);
+
+ if ($days == 0 && $hours == 0 && $minutes == 0) {
+ return "$seconds second" . ($seconds > 1 ? "s" : "");
+ } else if ($days == 0 && $hours == 0) {
+ return "$minutes minute" . ($minutes > 1 ? "s" : "");
+ } else if ($days == 0) {
+ return "$hours hour" . ($hours > 1 ? "s" : "");
+ } else {
+ return "$days day" . ($days > 1 ? "s" : "");
+ }
+}
+
+function clamp(int $current, int $min, int $max): int
+{
+ return max($min, min($max, $current));
+}
+
+function in_range(float $value, float $min, float $max): bool
+{
+ return $min <= $value && $value <= $max;
+} \ No newline at end of file