$request_uri = $_SERVER["REQUEST_URI"]; $n = strpos($request_uri, "?"); if($n) { $base_uri = substr($request_uri, 0, $n); $params = substr($request_uri, $n+1); } else { $base_uri = $request_uri; $params = ""; } $uri_params = substr ($request_uri, strpos ($request_uri, "?") + 1); $uri_params = str_replace("?", "&", $uri_params); parse_str ($uri_params, $request_vars0) ; // Init/load/unify request prameters if(!isset($request_vars)) { $request_vars = array(); } if(!isset($body_params)) { $body_params = array(); } if(isset($_POST)) { $request_vars = array_merge ($request_vars, $_POST) ; } $saved_request_vars = $request_vars; if(isset($request_vars0)) { $request_vars = array_merge ($request_vars, $request_vars0) ; } $method = $HTTP_SERVER_VARS["REQUEST_METHOD"]; switch($method) { case "GET": case "HEAD": // do nothing break; case "DELETE": break; default: $request_body = file_get_contents("php://input"); if($request_body) { $body_params = (array)json_decode($request_body); if($body_params) { foreach($body_params as $name => $v) { if(!is_object($v) && !is_array($v)) { $request_vars[$name] = $v; } } } } } // Assume we use /// // or // // or / // plus optional URL params after '?' if(preg_match("/^\\/([a-z\_]+)(\\/[\\d_a-z]+)*/", $base_uri) ) { $matches = explode("/", $base_uri); // [0] is empty due to leading '/' $obj = $matches[1]; $opt = ""; $xid = ""; //var_dump($matches); if(count($matches) > 3) { $xid = $matches[2]; $opt = $matches[3]; } else { if(isset($matches[2])) { $xid = $matches[2]; } } //echo "opt: $opt
"; // validate before dispatch switch($obj) { case "users": case "events": case "photos": // OK break; default: $err_str = "Bad object name '".$obj."'"; } if(isset($err_str)) { fail_req("400 Bad Request", $err_str); exit; } require_once("api/".$obj.".php")); } else { header("HTTP/1.1 404 Page Not Found"); echo "ERROR 404, Page Not Found"; } if(isset($r)) { return_js_reply($r); } /********************/ function return_js_reply($r) { global $json_flags; $rstr = json_encode($r, $json_flags); $hash = md5($rstr); cache_ctl_if_match($hash); cache_ctl(CACHE_PRESET, $hash); header("HTTP/1.1 200 OK"); header("Content-Type: application/json; charset=utf-8"); header("Content-Length: ".strlen($rstr)); echo $rstr; } // end return_js_reply() function fail_req($http_err, $err_str) { global $json_flags; suppress_cache(); if(!$http_err) { $http_err = "400 Bad Request"; } header("HTTP/1.1 ".$http_err); $r = array("error_description" => $err_str); $rstr = json_encode($r, $json_flags); header("Content-Type: application/json; charset=utf-8"); header("Content-Length: ".strlen($rstr)); echo $rstr; exit; } // end fail_req()