PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/flight/net/Request.php

https://github.com/apostolp/flight
PHP | 143 lines | 78 code | 15 blank | 50 comment | 12 complexity | dee25a4897771fee6df708ff0b53de62 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Flight: An extensible micro-framework.
  4. *
  5. * @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
  6. * @license http://www.opensource.org/licenses/mit-license.php
  7. */
  8. namespace flight\net;
  9. use flight\util\Collection;
  10. /**
  11. * The Request class represents an HTTP request. Data from
  12. * all the super globals $_GET, $_POST, $_COOKIE, and $_FILES
  13. * are stored and accessible via the Request object.
  14. *
  15. * The default request properties are:
  16. * url - The URL being requested
  17. * base - The parent subdirectory of the URL
  18. * method - The request method (GET, POST, PUT, DELETE)
  19. * referrer - The referrer URL
  20. * ip - IP address of the client
  21. * ajax - Whether the request is an AJAX request
  22. * scheme - The server protocol (http, https)
  23. * user_agent - Browser information
  24. * body - Raw data from the request body
  25. * type - The content type
  26. * length - The content length
  27. * query - Query string parameters
  28. * data - Post parameters
  29. * cookies - Cookie parameters
  30. * files - Uploaded files
  31. */
  32. class Request
  33. {
  34. /**
  35. * Constructor.
  36. *
  37. * @param array $config Request configuration
  38. */
  39. public function __construct($config = array())
  40. {
  41. // Default properties
  42. if (empty($config)) {
  43. $config = array(
  44. 'url' => getenv('REQUEST_URI') ? : '/',
  45. 'base' => str_replace('\\', '/', dirname(getenv('SCRIPT_NAME'))),
  46. 'method' => getenv('REQUEST_METHOD') ?: 'GET',
  47. 'referrer' => getenv('HTTP_REFERER') ?: '',
  48. 'ip' => getenv('REMOTE_ADDR') ?: '',
  49. 'ajax' => getenv('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest',
  50. 'scheme' => getenv('SERVER_PROTOCOL') ? : 'HTTP/1.1',
  51. 'user_agent' => getenv('HTTP_USER_AGENT') ? : '',
  52. 'body' => file_get_contents('php://input'),
  53. 'type' => getenv('CONTENT_TYPE') ? : '',
  54. 'length' => getenv('CONTENT_LENGTH') ? : 0,
  55. 'query' => new Collection($_GET),
  56. 'data' => new Collection($_POST),
  57. 'cookies' => new Collection($_COOKIE),
  58. 'files' => new Collection($_FILES),
  59. 'secure' => getenv('HTTPS') && getenv('HTTPS') != 'off',
  60. 'accept' => getenv('HTTP_ACCEPT') ?: '',
  61. 'proxy_ip' => $this->getProxyIpAddress()
  62. );
  63. }
  64. $this->init($config);
  65. }
  66. /**
  67. * Initialize request properties.
  68. *
  69. * @param array $properties Array of request properties
  70. */
  71. public function init($properties)
  72. {
  73. foreach ($properties as $name => $value) {
  74. $this->$name = $value;
  75. }
  76. if ($this->base != '/' && strlen($this->base) > 0 && strpos($this->url, $this->base) === 0) {
  77. $this->url = substr($this->url, strlen($this->base));
  78. }
  79. if (empty($this->url)) {
  80. $this->url = '/';
  81. }
  82. else {
  83. $_GET += self::parseQuery($this->url);
  84. $this->query->setData($_GET);
  85. }
  86. }
  87. /**
  88. * Parse query parameters from a URL.
  89. *
  90. * @param string $url URL string
  91. * @return array Query parameters
  92. */
  93. public static function parseQuery($url)
  94. {
  95. $params = array();
  96. $args = parse_url($url);
  97. if (isset($args['query'])) {
  98. parse_str($args['query'], $params);
  99. }
  100. return $params;
  101. }
  102. /**
  103. * Gets the real remote IP address.
  104. *
  105. * @return string IP address
  106. */
  107. private function getProxyIpAddress()
  108. {
  109. static $forwarded = array(
  110. 'HTTP_CLIENT_IP',
  111. 'HTTP_X_FORWARDED_FOR',
  112. 'HTTP_X_FORWARDED',
  113. 'HTTP_X_CLUSTER_CLIENT_IP',
  114. 'HTTP_FORWARDED_FOR',
  115. 'HTTP_FORWARDED'
  116. );
  117. $flags = \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE;
  118. foreach ($forwarded as $key) {
  119. if (array_key_exists($key, $_SERVER)) {
  120. sscanf($_SERVER[$key], '%[^,]', $ip);
  121. if (filter_var($ip, \FILTER_VALIDATE_IP, $flags) !== false) {
  122. return $ip;
  123. }
  124. }
  125. }
  126. return '';
  127. }
  128. }