Bugfix.
[rock.divinelegy.git] / Services / Http / HttpRequest.php
1 <?php
2
3 namespace Services\Http;
4
5 use Services\Http\IHttpRequest;
6 use Services\Http\Util;
7
8 class HttpRequest implements IHttpRequest
9 {
10 const METHOD_GET = 'GET';
11 const METHOD_POST = 'POST';
12 const METHOD_PUT = 'PUT';
13 const METHOD_DELETE = 'DELETE';
14 const METHOD_HEAD = 'HEAD';
15
16 protected $_method;
17 protected $_cookies;
18 protected $_formDataMediaTypes = array('application/x-www-form-urlencoded');
19
20 public function __construct()
21 {
22 $this->_method = $_SERVER['REQUEST_METHOD'];
23
24 if(isset($_SERVER['HTTP_COOKIE'])) {
25 $this->_cookies = Util::parseCookieHeader($_SERVER['HTTP_COOKIE']);
26 }
27 }
28
29 public function getMethod()
30 {
31 return $this->_method;
32 }
33
34 public function isGet()
35 {
36 return $this->_method == self::METHOD_GET;
37 }
38
39 public function isPost()
40 {
41 return $this->_method == self::METHOD_POST;
42 }
43
44 public function isPut()
45 {
46 return $this->_method == self::METHOD_PUT;
47 }
48
49 public function isHead()
50 {
51 return $this->_method == self::METHOD_HEAD;
52 }
53
54 public function isDelete()
55 {
56 return $this->_method == self::METHOD_DELETE;
57 }
58
59 public function getContentType()
60 {
61 return isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : null;
62 }
63
64 public function isFormData()
65 {
66 return in_array($this->getContentType(), $this->_formDataMediaTypes);
67 }
68
69 public function get()
70 {
71 return !empty($_GET) ? $_GET : null;
72 }
73
74 public function getGetElement($key)
75 {
76 return isset($_GET[$key]) ? $_GET[$key] : null;
77 }
78
79 public function post()
80 {
81 return !empty($_POST) ? $_POST : null;
82 }
83
84 public function getPostElement($key)
85 {
86 return isset($_POST[$key]) ? $_POST[$key] : null;
87 }
88
89 public function put()
90 {
91 return $this->post();
92 }
93
94 public function getPutElement($key)
95 {
96 return $this->getPostElement($key);
97 }
98
99 public function delete()
100 {
101 return $this->post();
102 }
103
104 public function getDeleteElement($key)
105 {
106 return $this->getPostElement($key);
107 }
108
109 public function cookies()
110 {
111 return $this->_cookies;
112 }
113
114 public function getCookie($key)
115 {
116 return isset($this->_cookies[$key]) ? $this->_cookies[$key] : null;
117 }
118
119 public function getBody()
120 {
121 return @file_get_contents('php://input');
122 }
123 public function getHost()
124 {
125 if(isset($_SERVER['HTTP_HOST'])) {
126 $parts = explode(':', $_SERVER['HTTP_HOST']);
127 return $parts[0];
128 }
129
130 return $_SERVER['SERVER_NAME'];
131 }
132
133 public function getIp()
134 {
135 $keys = array('X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR', 'CLIENT_IP', 'REMOTE_ADDR');
136 foreach ($keys as $key) {
137 if(isset($_SERVER[$key])) {
138 return $_SERVER[$key];
139 }
140 }
141 }
142
143 public function getReferrer()
144 {
145 return $_SERVER['HTTP_REFERER'];
146 }
147
148 // some people like to spell it referer due to RFC 1945
149 public function getReferer()
150 {
151 return $this->getReferer();
152 }
153
154 public function getUserAgent()
155 {
156 return $_SERVER['HTTP_USER_AGENT'];
157 }
158
159 private function getPhysicalPath()
160 {
161 $scriptName = $_SERVER['SCRIPT_NAME']; // <-- "/foo/index.php"
162 $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"
163
164 // Physical path
165 if (strpos($requestUri, $scriptName) !== false) {
166 $physicalPath = $scriptName; // <-- Without rewriting
167 } else {
168 $physicalPath = str_replace('\\', '', dirname($scriptName)); // <-- With rewriting
169 }
170
171 return $physicalPath;
172 }
173
174 public function getPath()
175 {
176 $physicalPath = $this->getPhysicalPath();
177 $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"
178 $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; // <-- "test=abc" or ""
179
180 // Virtual path
181 $path = substr_replace($requestUri, '', 0, strlen($physicalPath)); // <-- Remove physical path
182 $path = str_replace('?' . $queryString, '', $path); // <-- Remove query string
183 $path = '/' . ltrim($path, '/'); // <-- Ensure leading slash
184 $path = rtrim($path, '/'); // remove / from the end of string
185
186 return $path;
187 }
188 }