4
0

Request.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 $headers = array();
  14. private static $default_controller_name = 'index';
  15. private static $default_action_name = 'index';
  16. /**
  17. * Getteurs
  18. */
  19. public static function controllerName() {
  20. return self::$controller_name;
  21. }
  22. public static function actionName() {
  23. return self::$action_name;
  24. }
  25. public static function params() {
  26. return self::$params;
  27. }
  28. public static function param($key, $default = false, $specialchars = false) {
  29. if (isset(self::$params[$key])) {
  30. $p = self::$params[$key];
  31. if (is_object($p) || $specialchars) {
  32. return $p;
  33. } else {
  34. return Minz_Helper::htmlspecialchars_utf8($p);
  35. }
  36. } else {
  37. return $default;
  38. }
  39. }
  40. public static function paramTernary($key) {
  41. if (isset(self::$params[$key])) {
  42. $p = self::$params[$key];
  43. $tp = trim($p);
  44. if ($p === null || $tp === '' || $tp === 'null') {
  45. return null;
  46. } elseif ($p == false || $tp == '0' || $tp === 'false' || $tp === 'no') {
  47. return false;
  48. }
  49. return true;
  50. }
  51. return null;
  52. }
  53. public static function paramBoolean($key) {
  54. if (null === $value = self::paramTernary($key)) {
  55. return false;
  56. }
  57. return $value;
  58. }
  59. public static function defaultControllerName() {
  60. return self::$default_controller_name;
  61. }
  62. public static function defaultActionName() {
  63. return self::$default_action_name;
  64. }
  65. public static function currentRequest() {
  66. return array(
  67. 'c' => self::$controller_name,
  68. 'a' => self::$action_name,
  69. 'params' => self::$params,
  70. );
  71. }
  72. /**
  73. * Setteurs
  74. */
  75. public static function _controllerName($controller_name) {
  76. self::$controller_name = $controller_name;
  77. }
  78. public static function _actionName($action_name) {
  79. self::$action_name = $action_name;
  80. }
  81. public static function _params($params) {
  82. if (!is_array($params)) {
  83. $params = array($params);
  84. }
  85. self::$params = $params;
  86. }
  87. public static function _param($key, $value = false) {
  88. if ($value === false) {
  89. unset(self::$params[$key]);
  90. } else {
  91. self::$params[$key] = $value;
  92. }
  93. }
  94. /**
  95. * Initialise la Request
  96. */
  97. public static function init() {
  98. static::$headers = $_SERVER;
  99. self::initJSON();
  100. }
  101. public static function is($controller_name, $action_name) {
  102. return (
  103. self::$controller_name === $controller_name &&
  104. self::$action_name === $action_name
  105. );
  106. }
  107. /**
  108. * Return true if the request is over HTTPS, false otherwise (HTTP)
  109. *
  110. * @return boolean
  111. */
  112. public static function isHttps() {
  113. $header = static::getHeader('HTTP_X_FORWARDED_PROTO');
  114. if (null !== $header) {
  115. return 'https' === strtolower($header);
  116. }
  117. return 'on' === static::getHeader('HTTPS');
  118. }
  119. /**
  120. * Try to guess the base URL from $_SERVER information
  121. *
  122. * @return the base url (e.g. http://example.com/)
  123. */
  124. public static function guessBaseUrl() {
  125. $protocol = static::extractProtocol();
  126. $host = static::extractHost();
  127. $port = static::extractPortForUrl();
  128. $prefix = static::extractPrefix();
  129. $path = static::extractPath();
  130. return filter_var("{$protocol}://{$host}{$port}{$prefix}{$path}", FILTER_SANITIZE_URL);
  131. }
  132. /**
  133. * @return string
  134. */
  135. private static function extractProtocol() {
  136. if (static::isHttps()) {
  137. return 'https';
  138. }
  139. return 'http';
  140. }
  141. /**
  142. * @return string
  143. */
  144. private static function extractHost() {
  145. if (null !== $host = static::getHeader('HTTP_X_FORWARDED_HOST')) {
  146. return parse_url("http://{$host}", PHP_URL_HOST);
  147. }
  148. if (null !== $host = static::getHeader('HTTP_HOST')) {
  149. // Might contain a port number, and mind IPv6 addresses
  150. return parse_url("http://{$host}", PHP_URL_HOST);
  151. }
  152. if (null !== $host = static::getHeader('SERVER_NAME')) {
  153. return $host;
  154. }
  155. return 'localhost';
  156. }
  157. /**
  158. * @return integer
  159. */
  160. private static function extractPort() {
  161. if (null !== $port = static::getHeader('HTTP_X_FORWARDED_PORT')) {
  162. return intval($port);
  163. }
  164. if (null !== $proto = static::getHeader('HTTP_X_FORWARDED_PROTO')) {
  165. return 'https' === strtolower($proto) ? 443 : 80;
  166. }
  167. if (null !== $port = static::getHeader('SERVER_PORT')) {
  168. return intval($port);
  169. }
  170. return static::isHttps() ? 443 : 80;
  171. }
  172. /**
  173. * @return string
  174. */
  175. private static function extractPortForUrl() {
  176. if (static::isHttps() && 443 !== $port = static::extractPort()) {
  177. return ":{$port}";
  178. }
  179. if (!static::isHttps() && 80 !== $port = static::extractPort()) {
  180. return ":{$port}";
  181. }
  182. return '';
  183. }
  184. /**
  185. * @return string
  186. */
  187. private static function extractPrefix() {
  188. if (null !== $prefix = static::getHeader('HTTP_X_FORWARDED_PREFIX')) {
  189. return rtrim($prefix, '/ ');
  190. }
  191. return '';
  192. }
  193. /**
  194. * @return string
  195. */
  196. private static function extractPath() {
  197. if (null !== $path = static::getHeader('REQUEST_URI')) {
  198. return '/' === substr($path, -1) ? substr($path, 0, -1) : dirname($path);
  199. }
  200. return '';
  201. }
  202. /**
  203. * Return the base_url from configuration and add a suffix if given.
  204. *
  205. * @return the base_url with a suffix.
  206. */
  207. public static function getBaseUrl() {
  208. $conf = Minz_Configuration::get('system');
  209. $url = rtrim($conf->base_url, '/\\');
  210. return filter_var($url, FILTER_SANITIZE_URL);
  211. }
  212. /**
  213. * Relance une requête
  214. * @param $url l'url vers laquelle est relancée la requête
  215. * @param $redirect si vrai, force la redirection http
  216. * > sinon, le dispatcher recharge en interne
  217. */
  218. public static function forward($url = array(), $redirect = false) {
  219. if (!is_array($url)) {
  220. header('Location: ' . $url);
  221. exit();
  222. }
  223. $url = Minz_Url::checkUrl($url);
  224. if ($redirect) {
  225. header('Location: ' . Minz_Url::display($url, 'php'));
  226. exit();
  227. } else {
  228. self::_controllerName($url['c']);
  229. self::_actionName($url['a']);
  230. self::_params(array_merge(
  231. self::$params,
  232. $url['params']
  233. ));
  234. Minz_Dispatcher::reset();
  235. }
  236. }
  237. /**
  238. * Wrappers good notifications + redirection
  239. * @param $msg notification content
  240. * @param $url url array to where we should be forwarded
  241. */
  242. public static function good($msg, $url = array()) {
  243. Minz_Session::_param('notification', array(
  244. 'type' => 'good',
  245. 'content' => $msg
  246. ));
  247. Minz_Request::forward($url, true);
  248. }
  249. public static function bad($msg, $url = array()) {
  250. Minz_Session::_param('notification', array(
  251. 'type' => 'bad',
  252. 'content' => $msg
  253. ));
  254. Minz_Request::forward($url, true);
  255. }
  256. /**
  257. * Permet de récupérer une variable de type $_GET
  258. * @param $param nom de la variable
  259. * @param $default valeur par défaut à attribuer à la variable
  260. * @return $_GET[$param]
  261. * $_GET si $param = false
  262. * $default si $_GET[$param] n'existe pas
  263. */
  264. public static function fetchGET($param = false, $default = false) {
  265. if (false === $param) {
  266. return $_GET;
  267. }
  268. if (isset($_GET[$param])) {
  269. return $_GET[$param];
  270. }
  271. return $default;
  272. }
  273. /**
  274. * Allows receiving POST data as application/json
  275. */
  276. private static function initJSON() {
  277. if ('application/json' !== static::extractContentType()) {
  278. return;
  279. }
  280. if ('' === $ORIGINAL_INPUT = file_get_contents('php://input', false, null, 0, 1048576)) {
  281. return;
  282. }
  283. if (null === $json = json_decode($ORIGINAL_INPUT, true)) {
  284. return;
  285. }
  286. foreach ($json as $k => $v) {
  287. if (!isset($_POST[$k])) {
  288. $_POST[$k] = $v;
  289. }
  290. }
  291. }
  292. /**
  293. * @return string
  294. */
  295. private static function extractContentType() {
  296. return strtolower(trim(static::getHeader('CONTENT_TYPE')));
  297. }
  298. /**
  299. * Permet de récupérer une variable de type $_POST
  300. * @param $param nom de la variable
  301. * @param $default valeur par défaut à attribuer à la variable
  302. * @return $_POST[$param]
  303. * $_POST si $param = false
  304. * $default si $_POST[$param] n'existe pas
  305. */
  306. public static function fetchPOST($param = false, $default = false) {
  307. if (false === $param) {
  308. return $_POST;
  309. }
  310. if (isset($_POST[$param])) {
  311. return $_POST[$param];
  312. }
  313. return $default;
  314. }
  315. /**
  316. * @return mixed
  317. */
  318. public static function getHeader($header, $default = null) {
  319. if (isset(static::$headers[$header])) {
  320. return static::$headers[$header];
  321. }
  322. return $default;
  323. }
  324. /**
  325. * @return boolean
  326. */
  327. public static function isPost() {
  328. return 'POST' === static::getHeader('REQUEST_METHOD');
  329. }
  330. /**
  331. * @return array
  332. */
  333. public static function getPreferredLanguage() {
  334. if (preg_match_all('/(^|,)\s*(?P<lang>[^;,]+)/', static::getHeader('HTTP_ACCEPT_LANGUAGE'), $matches)) {
  335. return $matches['lang'];
  336. }
  337. return array('en');
  338. }
  339. }