| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- /**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
- /**
- * Request représente la requête http
- */
- class Minz_Request {
- private static $controller_name = '';
- private static $action_name = '';
- private static $params = array();
- private static $default_controller_name = 'index';
- private static $default_action_name = 'index';
- /**
- * Getteurs
- */
- public static function controllerName() {
- return self::$controller_name;
- }
- public static function actionName() {
- return self::$action_name;
- }
- public static function params() {
- return self::$params;
- }
- public static function param($key, $default = false, $specialchars = false) {
- if (isset(self::$params[$key])) {
- $p = self::$params[$key];
- if (is_object($p) || $specialchars) {
- return $p;
- } else {
- return Minz_Helper::htmlspecialchars_utf8($p);
- }
- } else {
- return $default;
- }
- }
- public static function defaultControllerName() {
- return self::$default_controller_name;
- }
- public static function defaultActionName() {
- return self::$default_action_name;
- }
- public static function currentRequest() {
- return array(
- 'c' => self::$controller_name,
- 'a' => self::$action_name,
- 'params' => self::$params,
- );
- }
- /**
- * Setteurs
- */
- public static function _controllerName($controller_name) {
- self::$controller_name = $controller_name;
- }
- public static function _actionName($action_name) {
- self::$action_name = $action_name;
- }
- public static function _params($params) {
- if (!is_array($params)) {
- $params = array($params);
- }
- self::$params = $params;
- }
- public static function _param($key, $value = false) {
- if ($value === false) {
- unset(self::$params[$key]);
- } else {
- self::$params[$key] = $value;
- }
- }
- /**
- * Initialise la Request
- */
- public static function init() {
- self::magicQuotesOff();
- }
- /**
- * Return true if the request is over HTTPS, false otherwise (HTTP)
- */
- public static function isHttps() {
- if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
- return strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https';
- } else {
- return isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
- }
- }
- /**
- * Try to guess the base URL from $_SERVER information
- *
- * @return the base url (e.g. http://example.com/)
- */
- public static function guessBaseUrl() {
- $url = 'http';
- $https = self::isHttps();
- if (!empty($_SERVER['HTTP_HOST'])) {
- $host = $_SERVER['HTTP_HOST'];
- } elseif (!empty($_SERVER['SERVER_NAME'])) {
- $host = $_SERVER['SERVER_NAME'];
- } else {
- $host = 'localhost';
- }
- if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) {
- $port = intval($_SERVER['HTTP_X_FORWARDED_PORT']);
- } elseif (!empty($_SERVER['SERVER_PORT'])) {
- $port = intval($_SERVER['SERVER_PORT']);
- } else {
- $port = $https ? 443 : 80;
- }
- if ($https) {
- $url .= 's://' . $host . ($port == 443 ? '' : ':' . $port);
- } else {
- $url .= '://' . $host . ($port == 80 ? '' : ':' . $port);
- }
- if (isset($_SERVER['REQUEST_URI'])) {
- $path = $_SERVER['REQUEST_URI'];
- $url .= substr($path, -1) === '/' ? substr($path, 0, -1) : dirname($path);
- }
- return filter_var($url, FILTER_SANITIZE_URL);
- }
- /**
- * Return the base_url from configuration and add a suffix if given.
- *
- * @return the base_url with a suffix.
- */
- public static function getBaseUrl() {
- $conf = Minz_Configuration::get('system');
- $url = rtrim($conf->base_url, '/\\');
- return filter_var($url, FILTER_SANITIZE_URL);
- }
- /**
- * Relance une requête
- * @param $url l'url vers laquelle est relancée la requête
- * @param $redirect si vrai, force la redirection http
- * > sinon, le dispatcher recharge en interne
- */
- public static function forward($url = array(), $redirect = false) {
- if (!is_array($url)) {
- header('Location: ' . $url);
- exit();
- }
- $url = Minz_Url::checkUrl($url);
- if ($redirect) {
- header('Location: ' . Minz_Url::display($url, 'php'));
- exit();
- } else {
- self::_controllerName($url['c']);
- self::_actionName($url['a']);
- self::_params(array_merge(
- self::$params,
- $url['params']
- ));
- Minz_Dispatcher::reset();
- }
- }
- /**
- * Wrappers good notifications + redirection
- * @param $msg notification content
- * @param $url url array to where we should be forwarded
- */
- public static function good($msg, $url = array()) {
- Minz_Session::_param('notification', array(
- 'type' => 'good',
- 'content' => $msg
- ));
- Minz_Request::forward($url, true);
- }
- public static function bad($msg, $url = array()) {
- Minz_Session::_param('notification', array(
- 'type' => 'bad',
- 'content' => $msg
- ));
- Minz_Request::forward($url, true);
- }
- /**
- * Permet de récupérer une variable de type $_GET
- * @param $param nom de la variable
- * @param $default valeur par défaut à attribuer à la variable
- * @return $_GET[$param]
- * $_GET si $param = false
- * $default si $_GET[$param] n'existe pas
- */
- public static function fetchGET($param = false, $default = false) {
- if ($param === false) {
- return $_GET;
- } elseif (isset($_GET[$param])) {
- return $_GET[$param];
- } else {
- return $default;
- }
- }
- /**
- * Permet de récupérer une variable de type $_POST
- * @param $param nom de la variable
- * @param $default valeur par défaut à attribuer à la variable
- * @return $_POST[$param]
- * $_POST si $param = false
- * $default si $_POST[$param] n'existe pas
- */
- public static function fetchPOST($param = false, $default = false) {
- if ($param === false) {
- return $_POST;
- } elseif (isset($_POST[$param])) {
- return $_POST[$param];
- } else {
- return $default;
- }
- }
- /**
- * Méthode désactivant les magic_quotes pour les variables
- * $_GET
- * $_POST
- * $_COOKIE
- */
- private static function magicQuotesOff() {
- if (get_magic_quotes_gpc()) {
- $_GET = Minz_Helper::stripslashes_r($_GET);
- $_POST = Minz_Helper::stripslashes_r($_POST);
- $_COOKIE = Minz_Helper::stripslashes_r($_COOKIE);
- }
- }
- public static function isPost() {
- return isset($_SERVER['REQUEST_METHOD']) &&
- $_SERVER['REQUEST_METHOD'] === 'POST';
- }
- }
|