Environment.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <[email protected]>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.2.0
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Slim;
  34. /**
  35. * Environment
  36. *
  37. * This class creates and returns a key/value array of common
  38. * environment variables for the current HTTP request.
  39. *
  40. * This is a singleton class; derived environment variables will
  41. * be common across multiple Slim applications.
  42. *
  43. * This class matches the Rack (Ruby) specification as closely
  44. * as possible. More information available below.
  45. *
  46. * @package Slim
  47. * @author Josh Lockhart
  48. * @since 1.6.0
  49. */
  50. class Environment implements \ArrayAccess, \IteratorAggregate
  51. {
  52. /**
  53. * @var array
  54. */
  55. protected $properties;
  56. /**
  57. * @var \Slim\Environment
  58. */
  59. protected static $environment;
  60. /**
  61. * Get environment instance (singleton)
  62. *
  63. * This creates and/or returns an environment instance (singleton)
  64. * derived from $_SERVER variables. You may override the global server
  65. * variables by using `\Slim\Environment::mock()` instead.
  66. *
  67. * @param bool $refresh Refresh properties using global server variables?
  68. * @return \Slim\Environment
  69. */
  70. public static function getInstance($refresh = false)
  71. {
  72. if (is_null(self::$environment) || $refresh) {
  73. self::$environment = new self();
  74. }
  75. return self::$environment;
  76. }
  77. /**
  78. * Get mock environment instance
  79. *
  80. * @param array $userSettings
  81. * @return \Slim\Environment
  82. */
  83. public static function mock($userSettings = array())
  84. {
  85. self::$environment = new self(array_merge(array(
  86. 'REQUEST_METHOD' => 'GET',
  87. 'SCRIPT_NAME' => '',
  88. 'PATH_INFO' => '',
  89. 'QUERY_STRING' => '',
  90. 'SERVER_NAME' => 'localhost',
  91. 'SERVER_PORT' => 80,
  92. 'ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  93. 'ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
  94. 'ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
  95. 'USER_AGENT' => 'Slim Framework',
  96. 'REMOTE_ADDR' => '127.0.0.1',
  97. 'slim.url_scheme' => 'http',
  98. 'slim.input' => '',
  99. 'slim.errors' => @fopen('php://stderr', 'w')
  100. ), $userSettings));
  101. return self::$environment;
  102. }
  103. /**
  104. * Constructor (private access)
  105. *
  106. * @param array|null $settings If present, these are used instead of global server variables
  107. */
  108. private function __construct($settings = null)
  109. {
  110. if ($settings) {
  111. $this->properties = $settings;
  112. } else {
  113. $env = array();
  114. //The HTTP request method
  115. $env['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD'];
  116. //The IP
  117. $env['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
  118. /**
  119. * Application paths
  120. *
  121. * This derives two paths: SCRIPT_NAME and PATH_INFO. The SCRIPT_NAME
  122. * is the real, physical path to the application, be it in the root
  123. * directory or a subdirectory of the public document root. The PATH_INFO is the
  124. * virtual path to the requested resource within the application context.
  125. *
  126. * With htaccess, the SCRIPT_NAME will be an absolute path (without file name);
  127. * if not using htaccess, it will also include the file name. If it is "/",
  128. * it is set to an empty string (since it cannot have a trailing slash).
  129. *
  130. * The PATH_INFO will be an absolute path with a leading slash; this will be
  131. * used for application routing.
  132. */
  133. if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {
  134. $env['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME']; //Without URL rewrite
  135. } else {
  136. $env['SCRIPT_NAME'] = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']) ); //With URL rewrite
  137. }
  138. $env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));
  139. if (strpos($env['PATH_INFO'], '?') !== false) {
  140. $env['PATH_INFO'] = substr_replace($env['PATH_INFO'], '', strpos($env['PATH_INFO'], '?')); //query string is not removed automatically
  141. }
  142. $env['SCRIPT_NAME'] = rtrim($env['SCRIPT_NAME'], '/');
  143. $env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/');
  144. //The portion of the request URI following the '?'
  145. $env['QUERY_STRING'] = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
  146. //Name of server host that is running the script
  147. $env['SERVER_NAME'] = $_SERVER['SERVER_NAME'];
  148. //Number of server port that is running the script
  149. $env['SERVER_PORT'] = $_SERVER['SERVER_PORT'];
  150. //HTTP request headers
  151. $specialHeaders = array('CONTENT_TYPE', 'CONTENT_LENGTH', 'PHP_AUTH_USER', 'PHP_AUTH_PW', 'PHP_AUTH_DIGEST', 'AUTH_TYPE');
  152. foreach ($_SERVER as $key => $value) {
  153. $value = is_string($value) ? trim($value) : $value;
  154. if (strpos($key, 'HTTP_') === 0) {
  155. $env[substr($key, 5)] = $value;
  156. } elseif (strpos($key, 'X_') === 0 || in_array($key, $specialHeaders)) {
  157. $env[$key] = $value;
  158. }
  159. }
  160. //Is the application running under HTTPS or HTTP protocol?
  161. $env['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https';
  162. //Input stream (readable one time only; not available for mutipart/form-data requests)
  163. $rawInput = @file_get_contents('php://input');
  164. if (!$rawInput) {
  165. $rawInput = '';
  166. }
  167. $env['slim.input'] = $rawInput;
  168. //Error stream
  169. $env['slim.errors'] = fopen('php://stderr', 'w');
  170. $this->properties = $env;
  171. }
  172. }
  173. /**
  174. * Array Access: Offset Exists
  175. */
  176. public function offsetExists($offset)
  177. {
  178. return isset($this->properties[$offset]);
  179. }
  180. /**
  181. * Array Access: Offset Get
  182. */
  183. public function offsetGet($offset)
  184. {
  185. if (isset($this->properties[$offset])) {
  186. return $this->properties[$offset];
  187. } else {
  188. return null;
  189. }
  190. }
  191. /**
  192. * Array Access: Offset Set
  193. */
  194. public function offsetSet($offset, $value)
  195. {
  196. $this->properties[$offset] = $value;
  197. }
  198. /**
  199. * Array Access: Offset Unset
  200. */
  201. public function offsetUnset($offset)
  202. {
  203. unset($this->properties[$offset]);
  204. }
  205. /**
  206. * IteratorAggregate
  207. *
  208. * @return \ArrayIterator
  209. */
  210. public function getIterator()
  211. {
  212. return new \ArrayIterator($this->properties);
  213. }
  214. }