Request.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace KarmaFW\Http;
  3. use \KarmaFW\Routing\Route;
  4. // TODO: a remplacer par ou rendre compatible avec GuzzleHttp\Psr7\Request
  5. class Request
  6. {
  7. protected $method = null;
  8. protected $url = null;
  9. protected $protocol = null;
  10. protected $route = null;
  11. protected $client_ip = null;
  12. protected $user_agent = null;
  13. public $GET = null;
  14. public $POST = null;
  15. public $COOKIE = null;
  16. public $SESSION = null;
  17. public $ENV = null;
  18. public $FILES = null;
  19. public $SERVER = null;
  20. //public function __construct($url=null, $method=null)
  21. public function __construct($method, $url, array $headers=[], $body=null, $version='1.1')
  22. {
  23. $this->url = $url;
  24. $this->method = strtoupper($method);
  25. $this->protocol = $version;
  26. //$this->setHeaders($headers);
  27. //print_r($_SERVER); exit;
  28. }
  29. public static function createFromArgv()
  30. {
  31. // TODO
  32. $request = new self(null, null);
  33. $request->GET = isset($_GET) ? $_GET : [];
  34. $request->POST = isset($_POST) ? $_POST : [];
  35. $request->COOKIE = isset($_COOKIE) ? $_COOKIE : [];
  36. $request->SESSION = isset($_SESSION) ? $_SESSION : [];
  37. $request->ENV = isset($_ENV) ? $_ENV : [];
  38. $request->FILES = isset($_FILES) ? $_FILES : [];
  39. $request->SERVER = isset($_SERVER) ? $_SERVER : [];
  40. $client_ip = null;
  41. $request->setClientIp($client_ip);
  42. return $request;
  43. }
  44. public static function createFromGlobals()
  45. {
  46. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;
  47. //$url = isset($_SERVER['REQUEST_URI']) ? explode("?", $_SERVER['REQUEST_URI'])[0] : null;
  48. $url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
  49. $request = new self($method, $url);
  50. /*
  51. $request->setGet(isset($_GET) ? $_GET : []);
  52. $request->setPost(isset($_POST) ? $_POST : []);
  53. $request->setCookie(isset($_COOKIE) ? $_COOKIE : []);
  54. $request->setSession(isset($_SESSION) ? $_SESSION : []);
  55. $request->setEnv(isset($_ENV) ? $_ENV : []);
  56. $request->setFiles(isset($_FILES) ? $_FILES : []);
  57. $request->setServer(isset($_SERVER) ? $_SERVER : []);
  58. */
  59. $request->GET = isset($_GET) ? $_GET : [];
  60. $request->POST = isset($_POST) ? $_POST : [];
  61. $request->COOKIE = isset($_COOKIE) ? $_COOKIE : [];
  62. $request->SESSION = isset($_SESSION) ? $_SESSION : [];
  63. $request->ENV = isset($_ENV) ? $_ENV : [];
  64. $request->FILES = isset($_FILES) ? $_FILES : [];
  65. $request->SERVER = isset($_SERVER) ? $_SERVER : [];
  66. // Set Server name (if behind a proxy)
  67. if (! empty($request->SERVER['HTTP_X_FORWARDED_HOST'])) {
  68. // if "ProxyPreserveHost On" is not set in apache
  69. $request->SERVER['HTTP_HOST'] = $request->SERVER['HTTP_X_FORWARDED_HOST'];
  70. $request->SERVER['SERVER_NAME'] = $request->SERVER['HTTP_X_FORWARDED_HOST'];
  71. }
  72. // Set Client User-Agent
  73. $user_agent = isset($request->SERVER['HTTP_USER_AGENT']) ? $request->SERVER['HTTP_USER_AGENT'] : null;
  74. $request->setUserAgent($user_agent);
  75. // Set Client IP
  76. $client_ip = null;
  77. if (! empty($request->SERVER['REMOTE_ADDR'])) {
  78. $client_ip = $request->SERVER['REMOTE_ADDR'];
  79. }
  80. if (! empty($request->SERVER['HTTP_X_FORWARDED_FOR'])) {
  81. $client_ip = $request->SERVER['HTTP_X_FORWARDED_FOR'];
  82. }
  83. $request->setClientIp($client_ip);
  84. return $request;
  85. }
  86. public function getUrl()
  87. {
  88. return $this->url;
  89. }
  90. public function getMethod()
  91. {
  92. return $this->method;
  93. }
  94. public function getClientIp()
  95. {
  96. return $this->client_ip;
  97. }
  98. public function setClientIp($client_ip)
  99. {
  100. $this->client_ip = $client_ip;
  101. }
  102. public function getUserAgent()
  103. {
  104. return $this->user_agent;
  105. }
  106. public function setUserAgent($user_agent)
  107. {
  108. $this->user_agent = $user_agent;
  109. }
  110. public function getRoute()
  111. {
  112. return $this->route;
  113. }
  114. public function setRoute(Route $route)
  115. {
  116. $this->route = $route;
  117. }
  118. public function isSecure()
  119. {
  120. return (! empty($this->SERVER['HTTPS']) && $this->SERVER['HTTPS'] == 'On')
  121. || (! empty($this->SERVER['REQUEST_SCHEME']) && $this->SERVER['REQUEST_SCHEME'] == 'https')
  122. || (! empty($this->SERVER['HTTP_X_FORWARDED_HTTPS']) && $this->SERVER['HTTP_X_FORWARDED_HTTPS'] == 'On')
  123. || (! empty($this->SERVER['HTTP_X_FORWARDED_SCHEME']) && $this->SERVER['HTTP_X_FORWARDED_SCHEME'] == 'https');
  124. }
  125. /*
  126. public function setUrl($url)
  127. {
  128. $this->url = $url;
  129. }
  130. public function setMethod($method)
  131. {
  132. $this->method = $method;
  133. }
  134. public function getGet()
  135. {
  136. return is_null($key) ? $this->GET : (isset($this->GET[$key]) ? $this->GET[$key] : null);
  137. }
  138. public function setGet($GET)
  139. {
  140. $this->GET = $GET;
  141. }
  142. public function getPost()
  143. {
  144. return is_null($key) ? $this->POST : (isset($this->POST[$key]) ? $this->POST[$key] : null);
  145. }
  146. public function setPost($POST)
  147. {
  148. $this->POST = $POST;
  149. }
  150. public function getCookie()
  151. {
  152. return is_null($key) ? $this->COOKIE : (isset($this->COOKIE[$key]) ? $this->COOKIE[$key] : null);
  153. }
  154. public function setCookie($COOKIE)
  155. {
  156. $this->COOKIE = $COOKIE;
  157. }
  158. public function getSession()
  159. {
  160. return is_null($key) ? $this->SESSION : (isset($this->SESSION[$key]) ? $this->SESSION[$key] : null);
  161. }
  162. public function setSession($SESSION)
  163. {
  164. $this->SESSION = $SESSION;
  165. }
  166. public function getEnv()
  167. {
  168. return is_null($key) ? $this->ENV : (isset($this->ENV[$key]) ? $this->ENV[$key] : null);
  169. }
  170. public function setEnv($ENV)
  171. {
  172. $this->ENV = $ENV;
  173. }
  174. public function getFiles()
  175. {
  176. return is_null($key) ? $this->FILES : (isset($this->FILES[$key]) ? $this->FILES[$key] : null);
  177. }
  178. public function setFiles($FILES)
  179. {
  180. $this->FILES = $FILES;
  181. }
  182. public function getServer($key=null)
  183. {
  184. return is_null($key) ? $this->SERVER : (isset($this->SERVER[$key]) ? $this->SERVER[$key] : null);
  185. }
  186. public function setServer($SERVER)
  187. {
  188. $this->SERVER = $SERVER;
  189. }
  190. */
  191. }