-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.php
More file actions
103 lines (88 loc) · 2.34 KB
/
utils.php
File metadata and controls
103 lines (88 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// Set default timezone
date_default_timezone_set("Asia/Jakarta");
// Get route
function get_route()
{
return basename($_SERVER["PHP_SELF"]);
}
// Redirect to error page
function redirect_error()
{
if (get_route() !== "error.php") header("Location: error.php");
}
// Check if authenticated
function is_authenticated()
{
return
isset($_SESSION["is_authenticated"]) && $_SESSION["is_authenticated"]
&& isset($_SESSION["username"]) && $_SESSION["username"]
&& isset($_SESSION["is_admin"]);
}
// Logout
function logout()
{
$route = get_route();
session_destroy();
if ($route !== "index.php" && !in_array($route, UNAUTHENTICATED_ROUTES) && !in_array($route, API_ROUTES)) {
header("Location: index.php");
exit;
} else {
session_start();
}
}
// Clean data
function clean_data($data)
{
$data = trim($data);
$data = stripslashes($data);
return htmlspecialchars($data);
}
// Check if empty string
function is_empty($string)
{
return strlen($string) === 0;
}
// Empty error message
function empty_error($name)
{
$name = ucfirst($name);
return "$name must be filled.";
}
// Convert to camel case
function to_camel_case($inputString)
{
// Replace invalid characters with underscores
$inputString = preg_replace('/[^a-zA-Z0-9]+/', '_', $inputString);
// Replace spaces with underscores
$inputString = str_replace(' ', '_', $inputString);
// Convert to lowercase and remove underscores between words
$inputString = strtolower($inputString);
$inputString = str_replace('_', '', ucwords($inputString, '_'));
return lcfirst($inputString);
}
// Greet according to time of day
function greet()
{
$current_hour = date("H");
if ($current_hour >= 5 && $current_hour < 12) {
$greeting = "Wake up to delicious flavors";
} elseif ($current_hour >= 12 && $current_hour < 18) {
$greeting = "Lunchtime elegance awaits";
} elseif ($current_hour >= 18 && $current_hour < 24) {
$greeting = "Dine under the stars";
} else {
$greeting = "Wishing you a restful night";
}
return $greeting;
}
// Convert to currency
function to_currency($price)
{
$formatted_price = number_format($price, 2, ',', '.');
return 'IDR ' . $formatted_price;
}
function checkSameDate($date)
{
return $date === date("Y-m-d");
}