Request.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 paramTernary($key) {
  40. if (isset(self::$params[$key])) {
  41. $p = self::$params[$key];
  42. $tp = trim($p);
  43. if ($p === null || $tp === '' || $tp === 'null') {
  44. return null;
  45. } elseif ($p == false || $tp == '0' || $tp === 'false' || $tp === 'no') {
  46. return false;
  47. }
  48. return true;
  49. }
  50. return null;
  51. }
  52. public static function paramBoolean($key) {
  53. if (null === $value = self::paramTernary($key)) {
  54. return false;
  55. }
  56. return $value;
  57. }
  58. public static function defaultControllerName() {
  59. return self::$default_controller_name;
  60. }
  61. public static function defaultActionName() {
  62. return self::$default_action_name;
  63. }
  64. public static function currentRequest() {
  65. return array(
  66. 'c' => self::$controller_name,
  67. 'a' => self::$action_name,
  68. 'params' => self::$params,
  69. );
  70. }
  71. /**
  72. * Setteurs
  73. */
  74. public static function _controllerName($controller_name) {
  75. self::$controller_name = $controller_name;
  76. }
  77. public static function _actionName($action_name) {
  78. self::$action_name = $action_name;
  79. }
  80. public static function _params($params) {
  81. if (!is_array($params)) {
  82. $params = array($params);
  83. }
  84. self::$params = $params;
  85. }
  86. public static function _param($key, $value = false) {
  87. if ($value === false) {
  88. unset(self::$params[$key]);
  89. } else {
  90. self::$params[$key] = $value;
  91. }
  92. }
  93. /**
  94. * Initialise la Request
  95. */
  96. public static function init() {
  97. self::initJSON();
  98. }
  99. public static function is($controller_name, $action_name) {
  100. return (
  101. self::$controller_name === $controller_name &&
  102. self::$action_name === $action_name
  103. );
  104. }
  105. /**
  106. * Return true if the request is over HTTPS, false otherwise (HTTP)
  107. */
  108. public static function isHttps() {
  109. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  110. return strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https';
  111. } else {
  112. return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
  113. }
  114. }
  115. /**
  116. * Try to guess the base URL from $_SERVER information
  117. *
  118. * @return the base url (e.g. http://example.com/)
  119. */
  120. public static function guessBaseUrl() {
  121. $url = 'http';
  122. $https = self::isHttps();
  123. if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  124. $host = parse_url('http://' . $_SERVER['HTTP_X_FORWARDED_HOST'], PHP_URL_HOST);
  125. } elseif (!empty($_SERVER['HTTP_HOST'])) {
  126. //Might contain a port number, and mind IPv6 addresses
  127. $host = parse_url('http://' . $_SERVER['HTTP_HOST'], PHP_URL_HOST);
  128. } elseif (!empty($_SERVER['SERVER_NAME'])) {
  129. $host = $_SERVER['SERVER_NAME'];
  130. } else {
  131. $host = 'localhost';
  132. }
  133. if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) {
  134. $port = intval($_SERVER['HTTP_X_FORWARDED_PORT']);
  135. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
  136. $port = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https' ? 443 : 80;
  137. } elseif (!empty($_SERVER['SERVER_PORT'])) {
  138. $port = intval($_SERVER['SERVER_PORT']);
  139. } else {
  140. $port = $https ? 443 : 80;
  141. }
  142. if ($https) {
  143. $url .= 's://' . $host . ($port == 443 ? '' : ':' . $port);
  144. } else {
  145. $url .= '://' . $host . ($port == 80 ? '' : ':' . $port);
  146. }
  147. if (!empty($_SERVER['HTTP_X_FORWARDED_PREFIX'])) {
  148. $url .= rtrim($_SERVER['HTTP_X_FORWARDED_PREFIX'], '/ ');
  149. }
  150. if (isset($_SERVER['REQUEST_URI'])) {
  151. $path = $_SERVER['REQUEST_URI'];
  152. $url .= substr($path, -1) === '/' ? substr($path, 0, -1) : dirname($path);
  153. }
  154. return filter_var($url, FILTER_SANITIZE_URL);
  155. }
  156. /**
  157. * Return the base_url from configuration and add a suffix if given.
  158. *
  159. * @return the base_url with a suffix.
  160. */
  161. public static function getBaseUrl() {
  162. $conf = Minz_Configuration::get('system');
  163. $url = rtrim($conf->base_url, '/\\');
  164. return filter_var($url, FILTER_SANITIZE_URL);
  165. }
  166. /**
  167. * Relance une requête
  168. * @param $url l'url vers laquelle est relancée la requête
  169. * @param $redirect si vrai, force la redirection http
  170. * > sinon, le dispatcher recharge en interne
  171. */
  172. public static function forward($url = array(), $redirect = false) {
  173. if (!is_array($url)) {
  174. header('Location: ' . $url);
  175. exit();
  176. }
  177. $url = Minz_Url::checkUrl($url);
  178. if ($redirect) {
  179. header('Location: ' . Minz_Url::display($url, 'php'));
  180. exit();
  181. } else {
  182. self::_controllerName($url['c']);
  183. self::_actionName($url['a']);
  184. self::_params(array_merge(
  185. self::$params,
  186. $url['params']
  187. ));
  188. Minz_Dispatcher::reset();
  189. }
  190. }
  191. /**
  192. * Wrappers good notifications + redirection
  193. * @param $msg notification content
  194. * @param $url url array to where we should be forwarded
  195. */
  196. public static function good($msg, $url = array()) {
  197. Minz_Session::_param('notification', array(
  198. 'type' => 'good',
  199. 'content' => $msg
  200. ));
  201. Minz_Request::forward($url, true);
  202. }
  203. public static function bad($msg, $url = array()) {
  204. Minz_Session::_param('notification', array(
  205. 'type' => 'bad',
  206. 'content' => $msg
  207. ));
  208. Minz_Request::forward($url, true);
  209. }
  210. /**
  211. * Permet de récupérer une variable de type $_GET
  212. * @param $param nom de la variable
  213. * @param $default valeur par défaut à attribuer à la variable
  214. * @return $_GET[$param]
  215. * $_GET si $param = false
  216. * $default si $_GET[$param] n'existe pas
  217. */
  218. public static function fetchGET($param = false, $default = false) {
  219. if ($param === false) {
  220. return $_GET;
  221. } elseif (isset($_GET[$param])) {
  222. return $_GET[$param];
  223. } else {
  224. return $default;
  225. }
  226. }
  227. /**
  228. * Allows receiving POST data as application/json
  229. */
  230. private static function initJSON() {
  231. $contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
  232. if ($contentType == '') { //PHP < 5.3.16
  233. $contentType = isset($_SERVER['HTTP_CONTENT_TYPE']) ? $_SERVER['HTTP_CONTENT_TYPE'] : '';
  234. }
  235. $contentType = strtolower(trim($contentType));
  236. if ($contentType === 'application/json') {
  237. $ORIGINAL_INPUT = file_get_contents('php://input', false, null, 0, 1048576);
  238. if ($ORIGINAL_INPUT != '') {
  239. $json = json_decode($ORIGINAL_INPUT, true);
  240. if ($json != null) {
  241. foreach ($json as $k => $v) {
  242. if (!isset($_POST[$k])) {
  243. $_POST[$k] = $v;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. /**
  251. * Permet de récupérer une variable de type $_POST
  252. * @param $param nom de la variable
  253. * @param $default valeur par défaut à attribuer à la variable
  254. * @return $_POST[$param]
  255. * $_POST si $param = false
  256. * $default si $_POST[$param] n'existe pas
  257. */
  258. public static function fetchPOST($param = false, $default = false) {
  259. if ($param === false) {
  260. return $_POST;
  261. } elseif (isset($_POST[$param])) {
  262. return $_POST[$param];
  263. } else {
  264. return $default;
  265. }
  266. }
  267. public static function isPost() {
  268. return isset($_SERVER['REQUEST_METHOD']) &&
  269. $_SERVER['REQUEST_METHOD'] === 'POST';
  270. }
  271. }