]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * @class Request\r | |
5 | */\r | |
6 | class Request {\r | |
7 | public $restful, $method, $controller, $action, $id, $params;\r | |
8 | \r | |
9 | public function __construct($params) {\r | |
10 | $this->restful = (isset($params["restful"])) ? $params["restful"] : false;\r | |
11 | $this->method = $_SERVER["REQUEST_METHOD"];\r | |
12 | $this->parseRequest();\r | |
13 | }\r | |
14 | public function isRestful() {\r | |
15 | return $this->restful;\r | |
16 | }\r | |
17 | protected function parseRequest() {\r | |
18 | if ($this->method == 'PUT') { // <-- Have to jump through hoops to get PUT data\r | |
19 | $raw = '';\r | |
20 | $httpContent = fopen('php://input', 'r');\r | |
21 | while ($kb = fread($httpContent, 1024)) {\r | |
22 | $raw .= $kb;\r | |
23 | }\r | |
24 | fclose($httpContent);\r | |
25 | $params = array();\r | |
26 | parse_str($raw, $params);\r | |
27 | \r | |
28 | if (isset($params['data'])) {\r | |
29 | $this->params = json_decode(stripslashes($params['data']));\r | |
30 | } else {\r | |
31 | $params = json_decode(stripslashes($raw));\r | |
32 | $this->params = $params;\r | |
33 | }\r | |
34 | } else {\r | |
35 | // grab JSON data if there...\r | |
36 | $this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;\r | |
37 | \r | |
38 | if (isset($_REQUEST['data'])) {\r | |
39 | $this->params = json_decode(stripslashes($_REQUEST['data']));\r | |
40 | } else {\r | |
41 | $raw = '';\r | |
42 | $httpContent = fopen('php://input', 'r');\r | |
43 | while ($kb = fread($httpContent, 1024)) {\r | |
44 | $raw .= $kb;\r | |
45 | }\r | |
46 | $params = json_decode(stripslashes($raw));\r | |
47 | if ($params) {\r | |
48 | $this->params = $params;\r | |
49 | }\r | |
50 | }\r | |
51 | \r | |
52 | }\r | |
53 | // Quickndirty PATH_INFO parser\r | |
54 | if (isset($_SERVER["PATH_INFO"])){\r | |
55 | $cai = '/^\/([a-z]+\w)\/([a-z]+\w)\/([0-9]+)$/'; // /controller/action/id\r | |
56 | $ca = '/^\/([a-z]+\w)\/([a-z]+)$/'; // /controller/action\r | |
57 | $ci = '/^\/([a-z]+\w)\/([0-9]+)$/'; // /controller/id\r | |
58 | $c = '/^\/([a-z]+\w)$/'; // /controller\r | |
59 | $i = '/^\/([0-9]+)$/'; // /id\r | |
60 | $matches = array();\r | |
61 | if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {\r | |
62 | $this->controller = $matches[1];\r | |
63 | $this->action = $matches[2];\r | |
64 | $this->id = $matches[3];\r | |
65 | } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {\r | |
66 | $this->controller = $matches[1];\r | |
67 | $this->action = $matches[2];\r | |
68 | } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {\r | |
69 | $this->controller = $matches[1];\r | |
70 | $this->id = $matches[2];\r | |
71 | } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {\r | |
72 | $this->controller = $matches[1];\r | |
73 | } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {\r | |
74 | $this->id = $matches[1];\r | |
75 | }\r | |
76 | }\r | |
77 | }\r | |
78 | }\r | |
79 | \r |