## Apache Zeta Components Manual :: File Source for http.php

## Source for file http.php

Documentation is available at http.php

1. <?php
2. /**
3. * File containing the ezcMvcHttpRequestParser class
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package MvcTools
26. */
28. /**
29. * Request parser that uses HTTP headers to populate an ezcMvcRequest object.
30. *
31. * @package MvcTools
32. * @version //autogentag//
33. * @mainclass
34. */
35. class ezcMvcHttpRequestParser extends ezcMvcRequestParser
36. {
37. /**
38. * Uses the data from the superglobals.
39. *
40. * @return ezcMvcRequest
41. */
42. public function createRequest ()
43. {
44. $this-> request = $this-> createRequestObject ();
45. $this-> processStandardHeaders ();
46. $this-> processAcceptHeaders ();
47. $this-> processUserAgentHeaders ();
48. $this-> processFiles ();
49. $this-> processAuthVars ();
50. $this-> processCookies ();
52. $this-> request ->raw = &$_SERVER;
54. return $this-> request ;
55. }
57. /**
58. * Creates and returns an ezcMvcRequest object.
59. *
60. * @return ezcMvcRequest
61. */
62. protected function createRequestObject ()
63. {
64. return new ezcMvcRequest ();
65. }
67. /**
68. * Processes the basic HTTP auth variables is set
69. */
70. protected function processAuthVars ()
71. {
72. $req = $this-> request ;
73. if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) )
74. {
75. $req->authentication = new ezcMvcRequestAuthentication ( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
76. }
77. }
79. /**
80. * Processes the standard headers that are not subdivided into other structs.
81. */
82. protected function processStandardHeaders ()
83. {
84. $this-> processProtocol ();
85. $this-> processHost ();
86. $this-> processDate ();
87. $this-> processVariables ();
88. $this-> processReferrer ();
89. $this-> processUri ();
90. $this-> processBody ();
91. $this-> processRequestId ();
92. }
94. /**
95. * Processes the request protocol.
96. */
97. protected function processProtocol ()
98. {
99. $req = $this-> request ;
101. if ( isset( $_SERVER['REQUEST_METHOD'] ) )
102. {
103. switch ( $_SERVER['REQUEST_METHOD'] )
104. {
105. case 'POST':
106. $req->protocol = 'http-post';
107. break;
108. case 'PUT':
109. $req->protocol = 'http-put';
110. break;
111. case 'DELETE':
112. $req->protocol = 'http-delete';
113. break;
114. default:
115. $req->protocol = 'http-get';
116. }
117. }
118. }
120. /**
121. * Processes the request host.
122. */
123. protected function processHost ()
124. {
125. $this-> request ->host = isset( $_SERVER['HTTP_HOST'] )
126. ? $_SERVER['HTTP_HOST']
127. : (
128. isset( $_SERVER['SERVER_NAME'] )
129. ? $_SERVER['SERVER_NAME']
130. : 'localhost.localdomain'
131. );
132. }
134. /**
135. * Processes the request date.
136. */
137. protected function processDate ()
138. {
139. $this-> request ->date = isset( $_SERVER['REQUEST_TIME'] )
140. ? new DateTime( "@{$_SERVER['REQUEST_TIME']}" )
141. : new DateTime();
142. }
144. /**
145. * Processes the request variables.
146. */
147. protected function processVariables ()
148. {
149. $this-> request ->variables =& $_REQUEST;
150. }
152. /**
153. * Processes the referrer.
154. */
155. protected function processReferrer ()
156. {
157. $this-> request ->referrer = isset( $_SERVER['HTTP_REFERER'] )
158. ? $_SERVER['HTTP_REFERER']
159. : null;
160. }
162. /**
163. * Processes the request URI.
164. */
165. protected function processUri ()
166. {
167. $req = $this-> request ;
169. $req->uri = isset( $_SERVER['REQUEST_URI'] )
170. ? $_SERVER['REQUEST_URI']
171. : '';
172. // remove the query string from the URI
173. $req->uri = preg_replace ( '@\?.*$@', '', $req->uri );
174. // url decode the uri
175. $req->uri = urldecode ( $req->uri );
176. // remove the prefix from the URI
177. $req->uri = preg_replace ( '@^' . preg_quote ( $this-> properties ['prefix'] ) . '@', '', $req->uri );
178. }
180. /**
181. * Processes the request ID from host and URI.
182. */
183. protected function processRequestId ()
184. {
185. $this-> request ->requestId = $this-> request ->host . $this-> request ->uri;
186. }
188. /**
189. * Processes the request body for PUT requests.
190. */
191. protected function processBody ()
192. {
193. $req = $this-> request ;
195. if ( $req->protocol == 'http-put' )
196. {
197. $req->body = file_get_contents ( "php://input" );
198. }
199. }
201. /**
202. * Proccesses the HTTP Accept headers into the ezcMvcRequestAccept struct.
203. */
204. protected function processAcceptHeaders ()
205. {
206. $this-> request ->accept = new ezcMvcRequestAccept ;
207. $accept = $this-> request ->accept;
209. $map = array(
210. 'HTTP_ACCEPT' => 'types',
211. 'HTTP_ACCEPT_CHARSET' => 'charsets',
212. 'HTTP_ACCEPT_ENCODING' => 'encodings',
213. 'HTTP_ACCEPT_LANGUAGE' => 'languages',
214. );
216. foreach ( $map as $var => $property )
217. {
218. if ( !isset( $_SERVER[$var] ) )
219. {
220. $accept->$property = array();
221. continue;
222. }
223. $parts = explode ( ',', $_SERVER[$var] );
224. $tmpPriorities = array();
225. foreach ( $parts as $part )
226. {
227. $priPart = explode ( ';q=', $part );
228. if ( count ( $priPart ) == 2 )
229. {
230. $tmpPriorities[$priPart[0]] = $priPart[1];
231. }
232. else
233. {
234. $tmpPriorities[$part] = 1;
235. }
236. }
237. asort ( $tmpPriorities );
238. $accept->$property = array_keys ( array_reverse ( $tmpPriorities ) );
239. }
240. }
242. /**
243. * Proccesses the User Agent header into the ezcMvcRequestUserAgent struct.
244. */
245. protected function processUserAgentHeaders ()
246. {
247. $this-> request ->agent = new ezcMvcRequestUserAgent ;
248. $agent = $this-> request ->agent;
250. $agent->agent = isset( $_SERVER['HTTP_USER_AGENT'] )
251. ? $_SERVER['HTTP_USER_AGENT']
252. : null;
253. }
255. /**
256. * Processes uploaded files.
257. */
258. protected function processFiles ()
259. {
260. foreach ( $_FILES as $name => $info )
261. {
262. $file = new ezcMvcRequestFile ;
263. $file->mimeType = $info['type'];
264. $file->name = $info['name'];
265. $file->size = $info['size'];
266. $file->status = $info['error'];
267. $file->tmpPath = $info['tmp_name'];
269. $this-> request ->files[] = $file;
270. }
271. }
273. /**
274. * Process cookies
275. */
276. protected function processCookies ()
277. {
278. foreach ( $_COOKIE as $name => $value )
279. {
280. $cookie = new ezcMvcRequestCookie ( $name, $value );
281. $this-> request ->cookies[] = $cookie;
282. }
283. }
284. }
285. ?>

Documentation generated by phpDocumentor 1.4.3