-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
141 lines (125 loc) · 3.78 KB
/
helpers.php
File metadata and controls
141 lines (125 loc) · 3.78 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
use App\Helpers\Response;
if (!function_exists('response')) {
function response() {
return new class {
public function json(array $data, int $status = 200, bool $return = false) {
Response::json($data, $status, $return);
exit;
}
public function file(string $filePath, array $headers = []) {
Response::file($filePath, $headers);
exit;
}
};
}
}
if (!function_exists('sanitize_input')) {
/**
* Sanitize user input to prevent XSS attacks
*/
function sanitize_input(string $input): string {
return htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
}
}
if (!function_exists('array_flatten')) {
/**
* Flatten a multi-dimensional array
*/
function array_flatten(array $array): array {
$result = [];
array_walk_recursive($array, function ($value) use (&$result) {
$result[] = $value;
});
return $result;
}
}
if (!function_exists('matchRoute')) {
/**
* Route matching function with support for dynamic parameters
*/
function matchRoute(array $routes, string $method, string $uri): ?array
{
if (!isset($routes[$method])) return null;
foreach ($routes[$method] as $route => $handler) {
// Replace route parameters with regex to capture values
$routePattern = preg_replace('/\{[a-zA-Z0-9_]+\}/', '([a-zA-Z0-9_]+)', $route);
$routePattern = str_replace('/', '\/', $routePattern);
$routePattern = "/^" . $routePattern . "$/";
if (preg_match($routePattern, $uri, $matches)) {
array_shift($matches); // Remove full match from matches
return ['handler' => $handler, 'params' => $matches];
}
}
return null;
}
}
if (!function_exists('handleException')) {
/**
* Send a JSON response with the erorr code.
*/
function handleException(\Exception $e)
{
$statusCode = $e->getCode() ?: 500;
Response::json(['error' => $e->getMessage(), $statusCode]);
}
}
if (!function_exists('validateInput')) {
/**
* Determine whether a variable is empty
*/
function validateInput(array $data, array $requiredFields)
{
$errors = [];
foreach ($requiredFields as $field) {
if (empty($data[$field])) {
$errors[$field] = "Missing field: $field";
}
}
return $errors;
}
}
if (!function_exists('sanitize')) {
/**
* Convert special characters to HTML entities
*/
function sanitize(mixed $input): string
{
return htmlspecialchars(strip_tags($input));
}
}
if (!function_exists('validateEmail')) {
/**
* Filters a variable with a specified filter
*/
function validateEmail(string $email): mixed
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
}
if (!function_exists('hashPassword')) {
/**
* Hash Password Algo
*/
function hashPassword(string $password): string
{
// CRYPT_BLOWFISH
return password_hash($password, PASSWORD_BCRYPT, ["option" => 8]);
}
}
if (!function_exists('sanitize_sensitive_data')) {
/**
* Removes sensitive credentials by replacing them with a placeholder.
*
* @param array|string $data Data containing sensitive information.
* @param string $placeholder The value to replace sensitive data with.
* @return array|string The sanitized data.
*/
function sanitize_sensitive_data(array|string $data, string $placeholder = '***'): array|string
{
if (is_array($data)) {
return array_map(fn($value) => $placeholder, $data);
}
return $placeholder;
}
}