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
|
<?php
/**
* @class Request
*/
class Request {
public $restful, $method, $controller, $action, $id, $params;
public function __construct($params) {
$this->restful = (isset($params["restful"])) ? $params["restful"] : false;
$this->method = $_SERVER["REQUEST_METHOD"];
$this->parseRequest();
}
public function isRestful() {
return $this->restful;
}
protected function parseRequest() {
if ($this->method == 'PUT') { // <-- Have to jump through hoops to get PUT data
$raw = '';
$httpContent = fopen('php://input', 'r');
while ($kb = fread($httpContent, 1024)) {
$raw .= $kb;
}
fclose($httpContent);
$params = array();
parse_str($raw, $params);
if (isset($params['data'])) {
$this->params = json_decode(stripslashes($params['data']));
} else {
$params = json_decode(stripslashes($raw));
$this->params = $params->data;
}
} else {
// grab JSON data if there...
$this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;
if (isset($_REQUEST['data'])) {
$this->params = json_decode(stripslashes($_REQUEST['data']));
} else {
$raw = '';
$httpContent = fopen('php://input', 'r');
while ($kb = fread($httpContent, 1024)) {
$raw .= $kb;
}
$params = json_decode(stripslashes($raw));
$this->params = $params->data;
}
}
// Quickndirty PATH_INFO parser
if (isset($_SERVER["PATH_INFO"])){
$cai = '/^\/([a-z]+\w)\/([a-z]+\w)\/([0-9]+)$/'; // /controller/action/id
$ca = '/^\/([a-z]+\w)\/([a-z]+)$/'; // /controller/action
$ci = '/^\/([a-z]+\w)\/([0-9]+)$/'; // /controller/id
$c = '/^\/([a-z]+\w)$/'; // /controller
$i = '/^\/([0-9]+)$/'; // /id
$matches = array();
if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
$this->controller = $matches[1];
$this->action = $matches[2];
$this->id = $matches[3];
} else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
$this->controller = $matches[1];
$this->action = $matches[2];
} else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
$this->controller = $matches[1];
$this->id = $matches[2];
} else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
$this->controller = $matches[1];
} else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
$this->id = $matches[1];
}
}
}
}
|