PageRenderTime 32ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/ext-4.0.7/examples/restful/remote/lib/request.php

https://bitbucket.org/srogerf/javascript
PHP | 79 lines | 68 code | 6 blank | 5 comment | 20 complexity | 3ae38692a64164af68d5e42ef5267cfd MD5 | raw file
 1<?php
 2
 3/**
 4 * @class Request
 5 */
 6class 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;
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                if ($params) {
48                    $this->params = $params;
49                }
50            }
51
52        }
53        // Quickndirty PATH_INFO parser
54        if (isset($_SERVER["PATH_INFO"])){
55            $cai = '/^\/([a-z]+\w)\/([a-z]+\w)\/([0-9]+)$/';  // /controller/action/id
56            $ca =  '/^\/([a-z]+\w)\/([a-z]+)$/';              // /controller/action
57            $ci = '/^\/([a-z]+\w)\/([0-9]+)$/';               // /controller/id
58            $c =  '/^\/([a-z]+\w)$/';                             // /controller
59            $i =  '/^\/([0-9]+)$/';                             // /id
60            $matches = array();
61            if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
62                $this->controller = $matches[1];
63                $this->action = $matches[2];
64                $this->id = $matches[3];
65            } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
66                $this->controller = $matches[1];
67                $this->action = $matches[2];
68            } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
69                $this->controller = $matches[1];
70                $this->id = $matches[2];
71            } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
72                $this->controller = $matches[1];
73            } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
74                $this->id = $matches[1];
75            }
76        }
77    }
78}
79