-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticator.php
More file actions
153 lines (107 loc) · 3.14 KB
/
authenticator.php
File metadata and controls
153 lines (107 loc) · 3.14 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
142
143
144
145
146
147
148
149
150
151
152
153
<?php
function terminator($message = 'An error occurred', $statusCode = 400)
{
http_response_code($statusCode);
die($message);
}
function successResponse($message, $statusCode = 200)
{
http_response_code($statusCode);
die($message);
}
function successResponseObj($message)
{
http_response_code(200);
die(json_encode($message));
}
function validatePrice($str)
{
$num = str_replace(',', '', $str);
if ($num) {
return $num;
}
throw new Exception('Invalid price');
}
function validatePriceNull($str)
{
$pattern = '/^\d{1,3}(?:,\d{3})*$/';
if (preg_match($pattern, $str)) {
return $str;
} else {
return null;
}
}
function validateLocation($string)
{
$pattern = '/^([A-Za-z]+), ([A-Za-z]+), ([A-Za-z]+), ([A-Za-z]+)$/';
if (preg_match($pattern, strtolower($string), $matches)) {
return $matches;
}
return null;
}
function parseInputJson()
{
$postData = file_get_contents('php://input');
$data = json_decode($postData, true);
if (json_last_error() !== JSON_ERROR_NONE) {
terminator('Error parsing data');
}
if (!is_array($data)) {
terminator('Invalid data format');
}
return $data;
}
function generateNumericOTP($n = 6)
{
$generator = "1357902468";
$result = "";
for ($i = 1; $i <= $n; $i++) {
$result .= substr($generator, (rand() % (strlen($generator))), 1);
}
return $result;
}
function generateAlphaNumericCode($n = 6)
{
$generator = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$result = "";
for ($i = 1; $i <= $n; $i++) {
$result .= substr($generator, (rand() % (strlen($generator))), 1);
}
return $result;
}
function generateSecurePassword($length = 12): string {
return substr(bin2hex(random_bytes($length)), 0, $length);
}
function httpRequest(array $options): AuthResponse
{
$ch = curl_init();
// Extract the values from the $options array
$url = $options['url'] ?? '';
$method = strtoupper($options['method'] ?? 'POST');
$body = $options['body'] ?? [];
$headers = $options['headers'] ?? [];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HEADER, true);
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
// Handle POST request
if ($method === 'POST' && !empty($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
} elseif ($method === 'GET' && !empty($body)) {
$url .= '?' . http_build_query($body);
curl_setopt($ch, CURLOPT_URL, $url);
}
$response = curl_exec($ch);
if (curl_errno($ch)) {
return new AuthResponse(curl_getinfo($ch, CURLINFO_HTTP_CODE), 'Error: ' . curl_error($ch));
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$bodyContent = substr($response, $headerSize);
// Close cURL session
curl_close($ch);
return new AuthResponse($statusCode, $bodyContent);
}