-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHRO.php
281 lines (250 loc) · 8 KB
/
PHRO.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* PHRO is PHP Route / Router Library
* A PHP library for defining and handling HTTP routes in PHP applications.
*/
class PHRO {
/**
* Array to store server URL segments.
* @var array
*/
private static $server_url = [];
/**
* HTTP request method.
* @var string
*/
private static $server_method;
/**
* Callback function to execute when a route is matched.
* @var callable
*/
private static $callback;
/**
* Flag indicating whether a route has been matched.
* @var bool
*/
private static $matched = false;
/**
* Array to store URL parameters.
* @var array
*/
private static $params = [];
/**
* Regular expression pattern to trim URL segments.
* @var string
*/
private static $trim = '/\^$/';
/**
* Default home URL for routes.
* @var string
*/
private static $default_home_url;
/**
* Array to store all defined routes.
* @var array
*/
private static $routes = [];
/**
* Initializes the PHRO object with default home URL.
*
* @param string $default_home_url Default home URL for routes.
* @return void
*/
public static function initialize($default_home_url = ''){
self::$default_home_url = $default_home_url;
$url = trim($_SERVER['REQUEST_URI'], self::$trim);
self::$server_method = strtolower($_SERVER['REQUEST_METHOD']);
self::$server_url = explode('/', $url);
}
/**
* Get the root URL for the application.
*
* @return string The base URL.
*/
public static function root(){
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domain = $_SERVER['HTTP_HOST'];
return rtrim($protocol . $domain . self::$default_home_url, '/');
}
/**
* Define a route for GET method.
*
* @param string $url Route URL pattern.
* @param callable $callback Callback function to execute when route is matched.
* @return void
*/
public static function get($url, $callback){
self::match('get', $url, $callback);
}
/**
* Define a route for POST method.
*
* @param string $url Route URL pattern.
* @param callable $callback Callback function to execute when route is matched.
* @return void
*/
public static function post($url, $callback){
self::match('post', $url, $callback);
}
/**
* Define a route for PUT method.
*
* @param string $url Route URL pattern.
* @param callable $callback Callback function to execute when route is matched.
* @return void
*/
public static function put($url, $callback){
self::match('put', $url, $callback);
}
/**
* Define a route for PATCH method.
*
* @param string $url Route URL pattern.
* @param callable $callback Callback function to execute when route is matched.
* @return void
*/
public static function patch($url, $callback){
self::match('patch', $url, $callback);
}
/**
* Define a route for DELETE method.
*
* @param string $url Route URL pattern.
* @param callable $callback Callback function to execute when route is matched.
* @return void
*/
public static function delete($url, $callback){
self::match('delete', $url, $callback);
}
/**
* Define a route for custom HTTP method.
*
* @param string $method Custom HTTP method.
* @param string $url Route URL pattern.
* @param callable $callback Callback function to execute when route is matched.
* @return void
*/
public static function add($method, $url, $callback){
self::match(strtolower($method), $url, $callback);
}
/**
* Match the route URL pattern with the current request URL.
*
* @param string $method HTTP method.
* @param string $url Route URL pattern.
* @param callable $callback Callback function to execute when route is matched.
* @return void
*/
private static function match($method, $url, $callback){
self::$routes[] = [
'short' => $url,
'method' => strtoupper($method),
'link' => self::root() . '/' . trim($url, '/')
];
if(self::$matched){
return;
}
$url = trim(self::$default_home_url.$url, self::$trim);
$current_url = explode('/', $url);
$url_length = count($current_url);
if($method != self::$server_method){
return;
}
if($url_length != count(self::$server_url)){
return;
}
$matched = true;
for($i = 0; $i < $url_length; $i++){
if($current_url[$i] == self::$server_url[$i]){
continue;
}
if(isset($current_url[$i][0]) && $current_url[$i][0] == '@'){
self::$params[substr($current_url[$i], 1)] = self::$server_url[$i];
continue;
}
$matched = false;
break;
}
if($matched){
self::$callback = $callback;
self::$matched = true;
}
}
/**
* Fetch IP information from multiple sources with fallbacks.
*
* @return array
*/
public static function fetchIPInfo() {
$urls = [
"http://ip-api.com/json/?fields=status,message,country,countryCode,region,regionName,city,lat,lon,zip,timezone,isp,org,as,mobile,proxy,query",
"http://ip-api.com/json/",
"https://ipinfo.io/json/",
"https://freegeoip.app/json/",
"https://api.ipbase.com/v1/json/",
"https://api.ipify.org/?format=json"
];
$timeout = 0.8;
foreach ($urls as $url) {
try {
$response = self::getHTTPResponse($url, $timeout);
$data = json_decode($response, true);
if (json_last_error() === JSON_ERROR_NONE && isset($data['status']) && $data['status'] === 'success') {
self::$params = array_merge(self::$params, $data);
return $data;
}
} catch (Exception $e) {
}
}
return [];
}
/**
* Perform an HTTP GET request with a specified timeout.
*
* @param string $url The URL to fetch data from.
* @param int $timeout The timeout duration in seconds.
* @return string The response body.
* @throws Exception If the request fails or times out.
*/
private static function getHTTPResponse($url, $timeout) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$response = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return $response;
}
/**
* Get all defined routes with full link and method.
*
* @return array All routes with link and method.
*/
public static function routes() {
return self::$routes;
}
/**
* Listen for incoming HTTP requests and execute matching route callback.
*
* @param callable|null $not_found_callback Callback function to execute when no route is matched.
* @return void
*/
public static function listen($not_found_callback = null){
if(!self::$matched){
if ($not_found_callback !== null && is_callable($not_found_callback)) {
call_user_func($not_found_callback);
} else {
header("HTTP/1.1 404 Not Found");
}
return;
}
self::fetchIPInfo();
self::$params = array_merge(self::$params, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES, $_REQUEST);
call_user_func(self::$callback, self::$params);
}
}
?>