Request.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * Request représente la requête http
  8. */
  9. class Minz_Request {
  10. private static $controller_name = '';
  11. private static $action_name = '';
  12. private static $params = array();
  13. private static $default_controller_name = 'index';
  14. private static $default_action_name = 'index';
  15. /**
  16. * Getteurs
  17. */
  18. public static function controllerName() {
  19. return self::$controller_name;
  20. }
  21. public static function actionName() {
  22. return self::$action_name;
  23. }
  24. public static function params() {
  25. return self::$params;
  26. }
  27. public static function param($key, $default = false, $specialchars = false) {
  28. if (isset(self::$params[$key])) {
  29. $p = self::$params[$key];
  30. if (is_object($p) || $specialchars) {
  31. return $p;
  32. } else {
  33. return Minz_Helper::htmlspecialchars_utf8($p);
  34. }
  35. } else {
  36. return $default;
  37. }
  38. }
  39. public static function defaultControllerName() {
  40. return self::$default_controller_name;
  41. }
  42. public static function defaultActionName() {
  43. return self::$default_action_name;
  44. }
  45. public static function currentRequest() {
  46. return array(
  47. 'c' => self::$controller_name,
  48. 'a' => self::$action_name,
  49. 'params' => self::$params,
  50. );
  51. }
  52. /**
  53. * Setteurs
  54. */
  55. public static function _controllerName($controller_name) {
  56. self::$controller_name = $controller_name;
  57. }
  58. public static function _actionName($action_name) {
  59. self::$action_name = $action_name;
  60. }
  61. public static function _params($params) {
  62. if (!is_array($params)) {
  63. $params = array($params);
  64. }
  65. self::$params = $params;
  66. }
  67. public static function _param($key, $value = false) {
  68. if ($value === false) {
  69. unset(self::$params[$key]);
  70. } else {
  71. self::$params[$key] = $value;
  72. }
  73. }
  74. /**
  75. * Initialise la Request
  76. */
  77. public static function init() {
  78. self::magicQuotesOff();
  79. }
  80. /**
  81. * Return true if the request is over HTTPS, false otherwise (HTTP)
  82. */
  83. public static function isHttps() {
  84. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  85. return strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https';
  86. } else {
  87. return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
  88. }
  89. }
  90. /**
  91. * Try to guess the base URL from $_SERVER information
  92. *
  93. * @return the base url (e.g. http://example.com/)
  94. */
  95. public static function guessBaseUrl() {
  96. $url = 'http';
  97. $https = self::isHttps();
  98. if (!empty($_SERVER['HTTP_HOST'])) {
  99. $host = $_SERVER['HTTP_HOST'];
  100. } elseif (!empty($_SERVER['SERVER_NAME'])) {
  101. $host = $_SERVER['SERVER_NAME'];
  102. } else {
  103. $host = 'localhost';
  104. }
  105. if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) {
  106. $port = intval($_SERVER['HTTP_X_FORWARDED_PORT']);
  107. } elseif (!empty($_SERVER['SERVER_PORT'])) {
  108. $port = intval($_SERVER['SERVER_PORT']);
  109. } else {
  110. $port = $https ? 443 : 80;
  111. }
  112. if ($https) {
  113. $url .= 's://' . $host . ($port == 443 ? '' : ':' . $port);
  114. } else {
  115. $url .= '://' . $host . ($port == 80 ? '' : ':' . $port);
  116. }
  117. if (isset($_SERVER['REQUEST_URI'])) {
  118. $path = $_SERVER['REQUEST_URI'];
  119. $url .= substr($path, -1) === '/' ? substr($path, 0, -1) : dirname($path);
  120. }
  121. return filter_var($url, FILTER_SANITIZE_URL);
  122. }
  123. /**
  124. * Return the base_url from configuration and add a suffix if given.
  125. *
  126. * @return the base_url with a suffix.
  127. */
  128. public static function getBaseUrl() {
  129. $conf = Minz_Configuration::get('system');
  130. $url = rtrim($conf->base_url, '/\\');
  131. return filter_var($url, FILTER_SANITIZE_URL);
  132. }
  133. /**
  134. * Relance une requête
  135. * @param $url l'url vers laquelle est relancée la requête
  136. * @param $redirect si vrai, force la redirection http
  137. * > sinon, le dispatcher recharge en interne
  138. */
  139. public static function forward($url = array(), $redirect = false) {
  140. if (!is_array($url)) {
  141. header('Location: ' . $url);
  142. exit();
  143. }
  144. $url = Minz_Url::checkUrl($url);
  145. if ($redirect) {
  146. header('Location: ' . Minz_Url::display($url, 'php'));
  147. exit();
  148. } else {
  149. self::_controllerName($url['c']);
  150. self::_actionName($url['a']);
  151. self::_params(array_merge(
  152. self::$params,
  153. $url['params']
  154. ));
  155. Minz_Dispatcher::reset();
  156. }
  157. }
  158. /**
  159. * Wrappers good notifications + redirection
  160. * @param $msg notification content
  161. * @param $url url array to where we should be forwarded
  162. */
  163. public static function good($msg, $url = array()) {
  164. Minz_Session::_param('notification', array(
  165. 'type' => 'good',
  166. 'content' => $msg
  167. ));
  168. Minz_Request::forward($url, true);
  169. }
  170. public static function bad($msg, $url = array()) {
  171. Minz_Session::_param('notification', array(
  172. 'type' => 'bad',
  173. 'content' => $msg
  174. ));
  175. Minz_Request::forward($url, true);
  176. }
  177. /**
  178. * Permet de récupérer une variable de type $_GET
  179. * @param $param nom de la variable
  180. * @param $default valeur par défaut à attribuer à la variable
  181. * @return $_GET[$param]
  182. * $_GET si $param = false
  183. * $default si $_GET[$param] n'existe pas
  184. */
  185. public static function fetchGET($param = false, $default = false) {
  186. if ($param === false) {
  187. return $_GET;
  188. } elseif (isset($_GET[$param])) {
  189. return $_GET[$param];
  190. } else {
  191. return $default;
  192. }
  193. }
  194. /**
  195. * Permet de récupérer une variable de type $_POST
  196. * @param $param nom de la variable
  197. * @param $default valeur par défaut à attribuer à la variable
  198. * @return $_POST[$param]
  199. * $_POST si $param = false
  200. * $default si $_POST[$param] n'existe pas
  201. */
  202. public static function fetchPOST($param = false, $default = false) {
  203. if ($param === false) {
  204. return $_POST;
  205. } elseif (isset($_POST[$param])) {
  206. return $_POST[$param];
  207. } else {
  208. return $default;
  209. }
  210. }
  211. /**
  212. * Méthode désactivant les magic_quotes pour les variables
  213. * $_GET
  214. * $_POST
  215. * $_COOKIE
  216. */
  217. private static function magicQuotesOff() {
  218. if (get_magic_quotes_gpc()) {
  219. $_GET = Minz_Helper::stripslashes_r($_GET);
  220. $_POST = Minz_Helper::stripslashes_r($_POST);
  221. $_COOKIE = Minz_Helper::stripslashes_r($_COOKIE);
  222. }
  223. }
  224. public static function isPost() {
  225. return isset($_SERVER['REQUEST_METHOD']) &&
  226. $_SERVER['REQUEST_METHOD'] === 'POST';
  227. }
  228. }