Request.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * Try to guess the base URL from $_SERVER information
  82. *
  83. * @return the base url (e.g. http://example.com/)
  84. */
  85. public static function guessBaseUrl() {
  86. $url = 'http';
  87. $host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
  88. $port = empty($_SERVER['SERVER_PORT']) ? 80 : $_SERVER['SERVER_PORT'];
  89. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
  90. $url .= 's://' . $host . ($port == 443 ? '' : ':' . $port);
  91. } else {
  92. $url .= '://' . $host . ($port == 80 ? '' : ':' . $port);
  93. }
  94. if (isset($_SERVER['REQUEST_URI'])) {
  95. $path = $_SERVER['REQUEST_URI'];
  96. $url .= substr($path, -1) === '/' ? substr($path, 0, -1) : dirname($path);
  97. }
  98. return filter_var($url, FILTER_SANITIZE_URL);
  99. }
  100. /**
  101. * Return the base_url from configuration and add a suffix if given.
  102. *
  103. * @param $base_url_suffix a string to add at base_url (default: empty string)
  104. * @return the base_url with a suffix.
  105. */
  106. public static function getBaseUrl($base_url_suffix = '') {
  107. $conf = Minz_Configuration::get('system');
  108. $url = rtrim($conf->base_url, '/\\') . $base_url_suffix;
  109. return filter_var($url, FILTER_SANITIZE_URL);
  110. }
  111. /**
  112. * Relance une requête
  113. * @param $url l'url vers laquelle est relancée la requête
  114. * @param $redirect si vrai, force la redirection http
  115. * > sinon, le dispatcher recharge en interne
  116. */
  117. public static function forward($url = array(), $redirect = false) {
  118. if (!is_array($url)) {
  119. header('Location: ' . $url);
  120. exit();
  121. }
  122. $url = Minz_Url::checkUrl($url);
  123. if ($redirect) {
  124. header('Location: ' . Minz_Url::display($url, 'php'));
  125. exit();
  126. } else {
  127. self::_controllerName($url['c']);
  128. self::_actionName($url['a']);
  129. self::_params(array_merge(
  130. self::$params,
  131. $url['params']
  132. ));
  133. Minz_Dispatcher::reset();
  134. }
  135. }
  136. /**
  137. * Wrappers good notifications + redirection
  138. * @param $msg notification content
  139. * @param $url url array to where we should be forwarded
  140. */
  141. public static function good($msg, $url = array()) {
  142. Minz_Session::_param('notification', array(
  143. 'type' => 'good',
  144. 'content' => $msg
  145. ));
  146. Minz_Request::forward($url, true);
  147. }
  148. public static function bad($msg, $url = array()) {
  149. Minz_Session::_param('notification', array(
  150. 'type' => 'bad',
  151. 'content' => $msg
  152. ));
  153. Minz_Request::forward($url, true);
  154. }
  155. /**
  156. * Permet de récupérer une variable de type $_GET
  157. * @param $param nom de la variable
  158. * @param $default valeur par défaut à attribuer à la variable
  159. * @return $_GET[$param]
  160. * $_GET si $param = false
  161. * $default si $_GET[$param] n'existe pas
  162. */
  163. public static function fetchGET($param = false, $default = false) {
  164. if ($param === false) {
  165. return $_GET;
  166. } elseif (isset($_GET[$param])) {
  167. return $_GET[$param];
  168. } else {
  169. return $default;
  170. }
  171. }
  172. /**
  173. * Permet de récupérer une variable de type $_POST
  174. * @param $param nom de la variable
  175. * @param $default valeur par défaut à attribuer à la variable
  176. * @return $_POST[$param]
  177. * $_POST si $param = false
  178. * $default si $_POST[$param] n'existe pas
  179. */
  180. public static function fetchPOST($param = false, $default = false) {
  181. if ($param === false) {
  182. return $_POST;
  183. } elseif (isset($_POST[$param])) {
  184. return $_POST[$param];
  185. } else {
  186. return $default;
  187. }
  188. }
  189. /**
  190. * Méthode désactivant les magic_quotes pour les variables
  191. * $_GET
  192. * $_POST
  193. * $_COOKIE
  194. */
  195. private static function magicQuotesOff() {
  196. if (get_magic_quotes_gpc()) {
  197. $_GET = Minz_Helper::stripslashes_r($_GET);
  198. $_POST = Minz_Helper::stripslashes_r($_POST);
  199. $_COOKIE = Minz_Helper::stripslashes_r($_COOKIE);
  200. }
  201. }
  202. public static function isPost() {
  203. return isset($_SERVER['REQUEST_METHOD']) &&
  204. $_SERVER['REQUEST_METHOD'] === 'POST';
  205. }
  206. }