public function __construct($request) { // Get the token from $request which has been set in the headers $this->token = $request['token']; // Get the args which is simply the url without domain and ... $args = $request['args']; // This will check if user is authenticated, if not Auth will throw a Error(7) and kills the page Auth::authenticate($this->token); // Get the all arguments from url $this->args = explode('/', rtrim($args, '/')); // Get the Controller name $this->endpoint = ucfirst($this->args[0]); // always the first one is our endpoint , E.g : api/v1/ -> products // Do a loop on all arguments to find ids and popo names foreach ($this->args as $arg) { // Look for an id , either mongo id , or product id ! if (is_mongo_id($arg)) { $this->id = $arg; continue; // continue if the condition is met , go next loop } // Check if there is popo with this arg in popo folder if (popo_exists($this->endpoint, uc_first($arg))) { $this->popo = uc_first($arg); } } // Request type $this->request_type = $this->get_request_method(); // PUT and DELETE can be hidden inside of an POST request , check them : if ($this->request_type == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) { if ($_SERVER['HTTP_X_HTTP_METHOD'] == "DELETE") { $this->request_type = 'DELETE'; } else { if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') { $this->request_type = 'PUT'; } } } // Get all inputs $this->input = @file_get_contents('php://input'); $this->input = json_decode($this->input); // Check if request method is either POST or PUT and if yes , check if input is empty or not validate_input($this->input, $this->request_type); // Get params from GET , if is set if (isset($_GET)) { $this->params = $_GET; // first param is like : /produtcs/34534543 , So we dont need it array_shift($this->params); } // Get params from POST , if is set if (isset($_POST)) { foreach ($_POST as $k => $v) { $this->params[$k] = $v; } } // Define the protocol $this->protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https" : "http"; }
/** * Method : load_popo * will include the specified popo name in the API , if it exist in the popo folder */ function load_popo($endpoint, $popo) { if (popo_exists($endpoint, $popo)) { $filename = str_replace("/Functions", "", dirname(__FILE__)); $filename .= '/Popo/' . $endpoint . $popo . '.php'; include_once $filename; } else { new Error(3); } }