|
1 | 1 | <?php
|
2 |
| -//get uri |
3 |
| -$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . |
4 |
| -"://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
5 |
| -$path = parse_url($url, PHP_URL_PATH); |
6 |
| -//remove string from last "/" to first character |
7 |
| -$path = trim(ltrim(strchr($path,"/"),"/")); |
8 |
| -$path = ltrim(substr($path,strposX($path,"/",1)),"/"); |
9 |
| - |
10 |
| -$sql = new Sql(); |
11 |
| - |
12 |
| -function strposX($haystack, $needle, $number = 0) |
13 |
| -{ |
14 |
| - return strpos($haystack, $needle, |
15 |
| - $number > 1 ? |
16 |
| - strposX($haystack, $needle, $number - 1) + strlen($needle) : 0 |
17 |
| - ); |
18 |
| -} |
| 2 | +$path = $_SERVER['REQUEST_URI']; |
| 3 | +//get $path except first "/" |
| 4 | +$path = substr($path, 1); |
| 5 | +//remove string from start to second "/" |
| 6 | +$path = substr($path, strpos($path, "/") + 1); |
| 7 | + |
| 8 | +$method = $_SERVER['REQUEST_METHOD']; |
19 | 9 |
|
20 |
| -function api($api=null){ |
| 10 | +function api($routeName,$api,$method){ |
21 | 11 | global $path;
|
22 |
| - if($api!=null) $path = $api; |
23 |
| - if(isset($_POST)) extract($_POST); |
24 |
| - if(isset($_GET)) extract($_GET); |
| 12 | + $route = $routeName; |
| 13 | + //for request with parameters in uri |
| 14 | + if(strpos($routeName,"{")){ |
| 15 | + //get routeName without parameters |
| 16 | + $tempRoute = substr($routeName,0,strpos($routeName,"{")+1); |
| 17 | + $route = substr($tempRoute,0,strlen($tempRoute)-1); |
| 18 | + //get param keys |
| 19 | + $paramKeys = substr($routeName,strpos($routeName,"{")+1); |
| 20 | + $paramKeys = substr($paramKeys,0,strlen($paramKeys)-1); |
| 21 | + $paramKeys = explode(",",$paramKeys); |
| 22 | + |
| 23 | + |
| 24 | + //check if routename exists in request uri |
| 25 | + if(str_contains($path,$route)){ |
| 26 | + $pos = strrpos($route,"/"); |
| 27 | + $paramValues = substr($path,$pos+1); |
| 28 | + $paramValues = explode("/",$paramValues); |
| 29 | + if(count($paramKeys) != count($paramValues)) |
| 30 | + notFound(); |
| 31 | + $params = array_combine($paramKeys,$paramValues); |
| 32 | + $path = substr($path,0,strlen($route)-1); |
| 33 | + $route = substr($tempRoute,0,strlen($tempRoute)-2); |
| 34 | + extract($params); |
| 35 | + } |
| 36 | + } |
| 37 | + if($route != $path) |
| 38 | + return; |
| 39 | + $path = $api; |
| 40 | + extract($method); |
25 | 41 | if(!file_exists("api/$path.php")){
|
26 | 42 | if(!file_exists("api/$path/index.php")){
|
27 |
| - header("HTTP/1.1 404 Not Found"); |
28 |
| - exit("URL not found"); |
| 43 | + notFound(); |
29 | 44 | }
|
30 | 45 | $path = $path."/index";
|
31 | 46 | }
|
32 |
| - global $sql; |
33 |
| - header('Cache-Control: max-age=86400'); |
34 |
| - header("Access-Control-Allow-Origin: *"); |
35 |
| - header("Content-Type: application/json; charset=utf-8"); |
| 47 | + |
36 | 48 | include "api/$path.php";
|
| 49 | + exit(); |
37 | 50 | }
|
38 | 51 |
|
39 |
| -function route($routeName,$api){ |
40 |
| - global $path; |
41 |
| - if($routeName==$path){ |
42 |
| - api($api); |
43 |
| - exit(); |
44 |
| - } |
45 |
| - } |
| 52 | +function post($routeName,$api){ |
| 53 | + global $method; |
| 54 | + if($method != "POST") return; |
| 55 | + api($routeName,$api,$_POST); |
| 56 | +} |
| 57 | + |
| 58 | +function get($routeName,$api){ |
| 59 | + global $method; |
| 60 | + if($method != "GET") return; |
| 61 | + api($routeName,$api,$_GET); |
| 62 | +} |
| 63 | + |
| 64 | +function put($routeName,$api){ |
| 65 | + global $method; |
| 66 | + if($method != "PUT") return; |
| 67 | + $_PUT = json_decode(file_get_contents('php://input'),true); |
| 68 | + api($routeName,$api,$_PUT); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +function notFound(){ |
| 73 | + header("HTTP/1.1 404 Not Found"); |
| 74 | + exit("URL not found"); |
| 75 | +} |
46 | 76 |
|
47 | 77 |
|
0 commit comments