Environment.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. /**
  10. * Server and execution environment information.
  11. *
  12. * @package Pimf
  13. * @author Gjero Krsteski <[email protected]>
  14. *
  15. * @property string X_REQUESTED_WITH It is sent by the Ajax functions of most major Frameworks
  16. * @property string HTTP Is the application running under HTTP protocol?
  17. * @property string HTTPS Is the application running under HTTPS protocol?
  18. * @property string SERVER_PROTOCOL Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0';
  19. * @property string CONTENT_LENGTH The Content-Length
  20. * @property string HOST The name of the server host under which the current script is executing.
  21. * @property string SERVER_NAME The name of the server host under which the current script is executing.
  22. * @property string SERVER_PORT Get the port
  23. * @property string PHP_SELF Filename of the currently executing script.
  24. * @property string SCRIPT_NAME Get Script Name (physical path)
  25. * @property string PATH_INFO Get Path Info (virtual path)
  26. * @property string X_FORWARDED_FOR Do on your machine is behind the proxy than us it instead of REMOTE_ADDR
  27. * @property string CLIENT_IP Get the client ip address
  28. * @property string REMOTE_ADDR The IP address from which the user is viewing the current page.
  29. * @property string HTTP_REFERER Get Referer - it cannot really be trusted.
  30. * @property string USER_AGENT Contents of the User-Agent from the current request, if there is one.
  31. * @property string HTTP_USER_AGENT Contents of the User-Agent: header from the current request, if there is one.
  32. * @property string REQUEST_URI The URI which was given in order to access this page; for instance, '/index.html'.
  33. * @property string REQUEST_METHOD Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.
  34. * @property string HTTP_IF_MODIFIED_SINCE Get request header from Apache even on PHP running as a CGI
  35. * @property string HTTP_IF_NONE_MATCH Get request header from Apache even on PHP running as a CGI
  36. */
  37. class Environment
  38. {
  39. /**
  40. * @var Param
  41. */
  42. private $envData;
  43. /**
  44. * @param array $envData Like $_SERVER
  45. */
  46. public function __construct(array $envData)
  47. {
  48. $this->envData = new Param($envData);
  49. }
  50. /**
  51. * @return Param
  52. */
  53. public function getData()
  54. {
  55. return $this->envData;
  56. }
  57. /**
  58. * @param $key
  59. *
  60. * @return string
  61. */
  62. public function __get($key)
  63. {
  64. return $this->envData->get($key);
  65. }
  66. /**
  67. * Is this an AJAX request?
  68. *
  69. * @return bool
  70. */
  71. public function isAjax()
  72. {
  73. return $this->X_REQUESTED_WITH === 'XMLHttpRequest';
  74. }
  75. /**
  76. * Is the application running under HTTP protocol?
  77. *
  78. * @return bool
  79. */
  80. public function isHttp()
  81. {
  82. return (bool)$this->HTTP;
  83. }
  84. /**
  85. * Is the application running under HTTPS protocol?
  86. *
  87. * @return bool
  88. */
  89. public function isHttps()
  90. {
  91. return $this->HTTPS === 'on';
  92. }
  93. /**
  94. * Get Host
  95. *
  96. * @return string
  97. */
  98. public function getHost()
  99. {
  100. if ($this->HOST) {
  101. if (strpos($this->HOST, ':') !== false) {
  102. $hostParts = explode(':', $this->HOST);
  103. return $hostParts[0];
  104. }
  105. return $this->HOST;
  106. }
  107. return $this->SERVER_NAME;
  108. }
  109. /**
  110. * Get Host with Port
  111. *
  112. * @return string
  113. */
  114. public function getHostWithPort()
  115. {
  116. return '' . $this->getHost() . ':' . $this->SERVER_PORT;
  117. }
  118. /**
  119. * Physical path + virtual path
  120. *
  121. * @return string
  122. */
  123. public function getPath()
  124. {
  125. return $this->SCRIPT_NAME . $this->PATH_INFO;
  126. }
  127. /**
  128. * Get remote IP
  129. *
  130. * @return string
  131. */
  132. public function getIp()
  133. {
  134. if ($this->X_FORWARDED_FOR) {
  135. return $this->X_FORWARDED_FOR;
  136. }
  137. if ($this->CLIENT_IP) {
  138. return $this->CLIENT_IP;
  139. }
  140. if ($this->SERVER_NAME) {
  141. return gethostbyname($this->SERVER_NAME);
  142. }
  143. return $this->REMOTE_ADDR;
  144. }
  145. /**
  146. * Get User Agent
  147. *
  148. * @return string|null
  149. */
  150. public function getUserAgent()
  151. {
  152. if ($this->USER_AGENT) {
  153. return $this->USER_AGENT;
  154. }
  155. if ($this->HTTP_USER_AGENT) {
  156. return $this->HTTP_USER_AGENT;
  157. }
  158. return null;
  159. }
  160. /**
  161. * Gives you the current page URL
  162. *
  163. * @return string
  164. */
  165. public function getUrl()
  166. {
  167. $protocol = strpos(strtolower($this->PATH_INFO), 'https') === false ? 'http' : 'https';
  168. return $protocol . '://' . $this->getHost();
  169. }
  170. /**
  171. * Try to get a request header.
  172. *
  173. * @param string $header
  174. *
  175. * @return array
  176. */
  177. public function getRequestHeader($header)
  178. {
  179. $header = str_replace('-', '_', strtoupper($header));
  180. $value = $this->{'HTTP_' . $header};
  181. if (!$value) {
  182. $headers = $this->getRequestHeaders();
  183. $value = !empty($headers[$header]) ? $headers[$header] : null;
  184. }
  185. return $value;
  186. }
  187. /**
  188. * Try to determine all request headers
  189. *
  190. * @return array
  191. */
  192. public function getRequestHeaders()
  193. {
  194. $headers = array();
  195. foreach ($this->envData->getAll() as $key => $value) {
  196. if ('HTTP_' === substr($key, 0, 5)) {
  197. $headers[substr($key, 5)] = $value;
  198. }
  199. }
  200. return $headers;
  201. }
  202. }