This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
43 lines (37 loc) · 1.32 KB
/
Copy pathapi.php
File metadata and controls
43 lines (37 loc) · 1.32 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
<?php
class Api {
private static $Rutas = [], $Cfg = [];
public static function cfg($K, $V) {
self::$Cfg[strtolower($K)] = $V;
}
public static function ruta($Url, $Callback) {
$Url = str_replace('/', '\/', $Url);
self::$Rutas[$Url] = $Callback;
}
public static function exe() {
header('Content-Type: application/json');
$Uri = self::$Cfg['mod_rewrite']
? substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME'])-9)
: substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME'])+1);
foreach(self::$Rutas as $Url => $Callback) {
if(preg_match("/^{$Url}$/i", $Uri, $Args)) {
array_shift($Args);
return call_user_func_array($Callback, array_values($Args));
}
}
}
public static function cors() {
if(isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if(isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if(isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit;
}
}
}