Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / examples / restful / remote / lib / request.php
1 <?php
2
3 /**
4  * @class Request
5  */
6 class Request {
7     public $restful, $method, $controller, $action, $id, $params;
8
9     public function __construct($params) {
10         $this->restful = (isset($params["restful"])) ? $params["restful"] : false;
11         $this->method = $_SERVER["REQUEST_METHOD"];
12         $this->parseRequest();
13     }
14     public function isRestful() {
15         return $this->restful;
16     }
17     protected function parseRequest() {
18         if ($this->method == 'PUT') {   // <-- Have to jump through hoops to get PUT data
19             $raw  = '';
20             $httpContent = fopen('php://input', 'r');
21             while ($kb = fread($httpContent, 1024)) {
22                 $raw .= $kb;
23             }
24             fclose($httpContent);
25             $params = array();
26             parse_str($raw, $params);
27             $this->id = (isset($params['id'])) ? $params['id'] : null;
28             $this->params = (isset($params['data'])) ? json_decode(stripslashes($params['data']), true) : null;
29         } else {
30             // grab JSON data if there...
31             $this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data']), true) : null;
32             $this->id = (isset($_REQUEST['id'])) ? json_decode(stripslashes($_REQUEST['id']), true) : null;
33         }
34         // Quickndirty PATH_INFO parser
35         if (isset($_SERVER["PATH_INFO"])){
36             $cai = '/^\/([a-z]+\w)\/([a-z]+\w)\/([0-9]+)$/';  // /controller/action/id
37             $ca =  '/^\/([a-z]+\w)\/([a-z]+)$/';              // /controller/action
38             $ci = '/^\/([a-z]+\w)\/([0-9]+)$/';               // /controller/id
39             $c =  '/^\/([a-z]+\w)$/';                             // /controller
40             $i =  '/^\/([0-9]+)$/';                             // /id
41             $matches = array();
42             if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
43                 $this->controller = $matches[1];
44                 $this->action = $matches[2];
45                 $this->id = $matches[3];
46             } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
47                 $this->controller = $matches[1];
48                 $this->action = $matches[2];
49             } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
50                 $this->controller = $matches[1];
51                 $this->id = $matches[2];
52             } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
53                 $this->controller = $matches[1];
54             } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
55                 $this->id = $matches[1];
56             }
57         }
58     }
59 }
60