/protected/components/ezcomponents/MvcTools/src/request_parsers/http.php

https://github.com/kamarulismail/kamarul-playground · PHP · 269 lines · 173 code · 25 blank · 71 comment · 9 complexity · 8d9697c910f096d88bb7d2341d42305f MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcMvcHttpRequestParser class
  4. *
  5. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  6. * @license http://ez.no/licenses/new_bsd New BSD License
  7. * @version 1.1.3
  8. * @filesource
  9. * @package MvcTools
  10. */
  11. /**
  12. * Request parser that uses HTTP headers to populate an ezcMvcRequest object.
  13. *
  14. * @package MvcTools
  15. * @version 1.1.3
  16. * @mainclass
  17. */
  18. class ezcMvcHttpRequestParser extends ezcMvcRequestParser
  19. {
  20. /**
  21. * Uses the data from the superglobals.
  22. *
  23. * @return ezcMvcRequest
  24. */
  25. public function createRequest()
  26. {
  27. $this->request = $this->createRequestObject();
  28. $this->processStandardHeaders();
  29. $this->processAcceptHeaders();
  30. $this->processUserAgentHeaders();
  31. $this->processFiles();
  32. $this->processAuthVars();
  33. $this->processCookies();
  34. $this->request->raw = &$_SERVER;
  35. return $this->request;
  36. }
  37. /**
  38. * Creates and returns an ezcMvcRequest object.
  39. *
  40. * @return ezcMvcRequest
  41. */
  42. protected function createRequestObject()
  43. {
  44. return new ezcMvcRequest();
  45. }
  46. /**
  47. * Processes the basic HTTP auth variables is set
  48. */
  49. protected function processAuthVars()
  50. {
  51. $req = $this->request;
  52. if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) )
  53. {
  54. $req->authentication = new ezcMvcRequestAuthentication( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
  55. }
  56. }
  57. /**
  58. * Processes the standard headers that are not subdivided into other structs.
  59. */
  60. protected function processStandardHeaders()
  61. {
  62. $this->processProtocol();
  63. $this->processHost();
  64. $this->processDate();
  65. $this->processVariables();
  66. $this->processReferrer();
  67. $this->processUri();
  68. $this->processBody();
  69. $this->processRequestId();
  70. }
  71. /**
  72. * Processes the request protocol.
  73. */
  74. protected function processProtocol()
  75. {
  76. $req = $this->request;
  77. if ( isset( $_SERVER['REQUEST_METHOD'] ) )
  78. {
  79. switch ( $_SERVER['REQUEST_METHOD'] )
  80. {
  81. case 'POST':
  82. $req->protocol = 'http-post';
  83. break;
  84. case 'PUT':
  85. $req->protocol = 'http-put';
  86. break;
  87. case 'DELETE':
  88. $req->protocol = 'http-delete';
  89. break;
  90. default:
  91. $req->protocol = 'http-get';
  92. }
  93. }
  94. }
  95. /**
  96. * Processes the request host.
  97. */
  98. protected function processHost()
  99. {
  100. $this->request->host = isset( $_SERVER['HTTP_HOST'] )
  101. ? $_SERVER['HTTP_HOST']
  102. : (
  103. isset( $_SERVER['SERVER_NAME'] )
  104. ? $_SERVER['SERVER_NAME']
  105. : 'localhost.localdomain'
  106. );
  107. }
  108. /**
  109. * Processes the request date.
  110. */
  111. protected function processDate()
  112. {
  113. $this->request->date = isset( $_SERVER['REQUEST_TIME'] )
  114. ? new DateTime( "@{$_SERVER['REQUEST_TIME']}" )
  115. : new DateTime();
  116. }
  117. /**
  118. * Processes the request variables.
  119. */
  120. protected function processVariables()
  121. {
  122. $this->request->variables =& $_REQUEST;
  123. }
  124. /**
  125. * Processes the referrer.
  126. */
  127. protected function processReferrer()
  128. {
  129. $this->request->referrer = isset( $_SERVER['HTTP_REFERER'] )
  130. ? $_SERVER['HTTP_REFERER']
  131. : null;
  132. }
  133. /**
  134. * Processes the request URI.
  135. */
  136. protected function processUri()
  137. {
  138. $req = $this->request;
  139. $req->uri = isset( $_SERVER['REQUEST_URI'] )
  140. ? $_SERVER['REQUEST_URI']
  141. : '';
  142. // remove the query string from the URI
  143. $req->uri = preg_replace( '@\?.*$@', '', $req->uri );
  144. // url decode the uri
  145. $req->uri = urldecode( $req->uri );
  146. // remove the prefix from the URI
  147. $req->uri = preg_replace( '@^' . preg_quote( $this->properties['prefix'] ) . '@', '', $req->uri );
  148. }
  149. /**
  150. * Processes the request ID from host and URI.
  151. */
  152. protected function processRequestId()
  153. {
  154. $this->request->requestId = $this->request->host . $this->request->uri;
  155. }
  156. /**
  157. * Processes the request body for PUT requests.
  158. */
  159. protected function processBody()
  160. {
  161. $req = $this->request;
  162. if ( $req->protocol == 'http-put' )
  163. {
  164. $req->body = file_get_contents( "php://input" );
  165. }
  166. }
  167. /**
  168. * Proccesses the HTTP Accept headers into the ezcMvcRequestAccept struct.
  169. */
  170. protected function processAcceptHeaders()
  171. {
  172. $this->request->accept = new ezcMvcRequestAccept;
  173. $accept = $this->request->accept;
  174. $map = array(
  175. 'HTTP_ACCEPT' => 'types',
  176. 'HTTP_ACCEPT_CHARSET' => 'charsets',
  177. 'HTTP_ACCEPT_ENCODING' => 'encodings',
  178. 'HTTP_ACCEPT_LANGUAGE' => 'languages',
  179. );
  180. foreach ( $map as $var => $property )
  181. {
  182. if ( !isset( $_SERVER[$var] ) )
  183. {
  184. $accept->$property = array();
  185. continue;
  186. }
  187. $parts = explode( ',', $_SERVER[$var] );
  188. $tmpPriorities = array();
  189. foreach ( $parts as $part )
  190. {
  191. $priPart = explode( ';q=', $part );
  192. if ( count( $priPart ) == 2 )
  193. {
  194. $tmpPriorities[$priPart[0]] = $priPart[1];
  195. }
  196. else
  197. {
  198. $tmpPriorities[$part] = 1;
  199. }
  200. }
  201. asort( $tmpPriorities );
  202. $accept->$property = array_keys( array_reverse( $tmpPriorities ) );
  203. }
  204. }
  205. /**
  206. * Proccesses the User Agent header into the ezcMvcRequestUserAgent struct.
  207. */
  208. protected function processUserAgentHeaders()
  209. {
  210. $this->request->agent = new ezcMvcRequestUserAgent;
  211. $agent = $this->request->agent;
  212. $agent->agent = isset( $_SERVER['HTTP_USER_AGENT'] )
  213. ? $_SERVER['HTTP_USER_AGENT']
  214. : null;
  215. }
  216. /**
  217. * Processes uploaded files.
  218. */
  219. protected function processFiles()
  220. {
  221. foreach ( $_FILES as $name => $info )
  222. {
  223. $file = new ezcMvcRequestFile;
  224. $file->mimeType = $info['type'];
  225. $file->name = $info['name'];
  226. $file->size = $info['size'];
  227. $file->status = $info['error'];
  228. $file->tmpPath = $info['tmp_name'];
  229. $this->request->files[] = $file;
  230. }
  231. }
  232. /**
  233. * Process cookies
  234. */
  235. protected function processCookies()
  236. {
  237. foreach ( $_COOKIE as $name => $value )
  238. {
  239. $cookie = new ezcMvcRequestCookie( $name, $value );
  240. $this->request->cookies[] = $cookie;
  241. }
  242. }
  243. }
  244. ?>