Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[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
28             if (isset($params['data'])) {
29                 $this->params =  json_decode(stripslashes($params['data']));
30             } else {
31                 $params = json_decode(stripslashes($raw));
32                 $this->params = $params->data;
33             }
34         } else {
35             // grab JSON data if there...
36             $this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;
37
38             if (isset($_REQUEST['data'])) {
39                 $this->params =  json_decode(stripslashes($_REQUEST['data']));
40             } else {
41                 $raw  = '';
42                 $httpContent = fopen('php://input', 'r');
43                 while ($kb = fread($httpContent, 1024)) {
44                     $raw .= $kb;
45                 }
46                 $params = json_decode(stripslashes($raw));
47                 $this->params = $params->data;
48             }
49
50         }
51         // Quickndirty PATH_INFO parser
52         if (isset($_SERVER["PATH_INFO"])){
53             $cai = '/^\/([a-z]+\w)\/([a-z]+\w)\/([0-9]+)$/';  // /controller/action/id
54             $ca =  '/^\/([a-z]+\w)\/([a-z]+)$/';              // /controller/action
55             $ci = '/^\/([a-z]+\w)\/([0-9]+)$/';               // /controller/id
56             $c =  '/^\/([a-z]+\w)$/';                             // /controller
57             $i =  '/^\/([0-9]+)$/';                             // /id
58             $matches = array();
59             if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
60                 $this->controller = $matches[1];
61                 $this->action = $matches[2];
62                 $this->id = $matches[3];
63             } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
64                 $this->controller = $matches[1];
65                 $this->action = $matches[2];
66             } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
67                 $this->controller = $matches[1];
68                 $this->id = $matches[2];
69             } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
70                 $this->controller = $matches[1];
71             } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
72                 $this->id = $matches[1];
73             }
74         }
75     }
76 }
77