1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/freshrss_ynh.git synced 2024-09-03 18:36:33 +02:00
freshrss_ynh/sources/lib/Minz/Request.php

236 lines
5.3 KiB
PHP
Raw Normal View History

2014-07-23 15:52:50 +02:00
<?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 = '';
2014-09-21 12:12:35 +02:00
private static $params = array();
2014-07-23 15:52:50 +02:00
private static $default_controller_name = 'index';
private static $default_action_name = 'index';
/**
* Getteurs
*/
2014-09-21 12:12:35 +02:00
public static function controllerName() {
2014-07-23 15:52:50 +02:00
return self::$controller_name;
}
2014-09-21 12:12:35 +02:00
public static function actionName() {
2014-07-23 15:52:50 +02:00
return self::$action_name;
}
2014-09-21 12:12:35 +02:00
public static function params() {
2014-07-23 15:52:50 +02:00
return self::$params;
}
2014-09-21 12:12:35 +02:00
public static function param($key, $default = false, $specialchars = false) {
if (isset(self::$params[$key])) {
2014-07-23 15:52:50 +02:00
$p = self::$params[$key];
2014-09-21 12:12:35 +02:00
if (is_object($p) || $specialchars) {
2014-07-23 15:52:50 +02:00
return $p;
} else {
2014-09-21 12:12:35 +02:00
return Minz_Helper::htmlspecialchars_utf8($p);
2014-07-23 15:52:50 +02:00
}
} else {
return $default;
}
}
2014-09-21 12:12:35 +02:00
public static function defaultControllerName() {
2014-07-23 15:52:50 +02:00
return self::$default_controller_name;
}
2014-09-21 12:12:35 +02:00
public static function defaultActionName() {
2014-07-23 15:52:50 +02:00
return self::$default_action_name;
}
2015-02-08 18:55:48 +01:00
public static function currentRequest() {
return array(
'c' => self::$controller_name,
'a' => self::$action_name,
'params' => self::$params,
);
}
2014-07-23 15:52:50 +02:00
/**
* Setteurs
*/
2014-09-21 12:12:35 +02:00
public static function _controllerName($controller_name) {
2014-07-23 15:52:50 +02:00
self::$controller_name = $controller_name;
}
2014-09-21 12:12:35 +02:00
public static function _actionName($action_name) {
2014-07-23 15:52:50 +02:00
self::$action_name = $action_name;
}
2014-09-21 12:12:35 +02:00
public static function _params($params) {
2014-07-23 15:52:50 +02:00
if (!is_array($params)) {
2014-09-21 12:12:35 +02:00
$params = array($params);
2014-07-23 15:52:50 +02:00
}
self::$params = $params;
}
2014-09-21 12:12:35 +02:00
public static function _param($key, $value = false) {
2014-07-23 15:52:50 +02:00
if ($value === false) {
2014-09-21 12:12:35 +02:00
unset(self::$params[$key]);
2014-07-23 15:52:50 +02:00
} else {
self::$params[$key] = $value;
}
}
/**
* Initialise la Request
*/
2014-09-21 12:12:35 +02:00
public static function init() {
self::magicQuotesOff();
2014-07-23 15:52:50 +02:00
}
/**
* Retourn le nom de domaine du site
*/
2014-09-21 12:12:35 +02:00
public static function getDomainName() {
2014-07-23 15:52:50 +02:00
return $_SERVER['HTTP_HOST'];
}
/**
* Détermine la base de l'url
* @return la base de l'url
*/
2014-09-21 12:12:35 +02:00
public static function getBaseUrl() {
2015-02-08 18:55:48 +01:00
$conf = Minz_Configuration::get('system');
$defaultBaseUrl = $conf->base_url;
2014-07-23 15:52:50 +02:00
if (!empty($defaultBaseUrl)) {
return $defaultBaseUrl;
} elseif (isset($_SERVER['REQUEST_URI'])) {
return dirname($_SERVER['REQUEST_URI']) . '/';
} else {
return '/';
}
}
/**
* Récupère l'URI de la requête
* @return l'URI
*/
2014-09-21 12:12:35 +02:00
public static function getURI() {
if (isset($_SERVER['REQUEST_URI'])) {
$base_url = self::getBaseUrl();
2014-07-23 15:52:50 +02:00
$uri = $_SERVER['REQUEST_URI'];
2014-09-21 12:12:35 +02:00
$len_base_url = strlen($base_url);
$real_uri = substr($uri, $len_base_url);
2014-07-23 15:52:50 +02:00
} else {
$real_uri = '';
}
return $real_uri;
}
/**
* 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
*/
2014-09-21 12:12:35 +02:00
public static function forward($url = array(), $redirect = false) {
2014-09-27 10:10:43 +02:00
if (!is_array($url)) {
header('Location: ' . $url);
exit();
}
2014-09-21 12:12:35 +02:00
$url = Minz_Url::checkUrl($url);
2014-07-23 15:52:50 +02:00
if ($redirect) {
2014-09-21 12:12:35 +02:00
header('Location: ' . Minz_Url::display($url, 'php'));
exit();
2014-07-23 15:52:50 +02:00
} else {
2014-09-21 12:12:35 +02:00
self::_controllerName($url['c']);
self::_actionName($url['a']);
self::_params(array_merge(
2014-07-23 15:52:50 +02:00
self::$params,
$url['params']
));
Minz_Dispatcher::reset();
}
}
2014-09-21 12:12:35 +02:00
/**
* 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);
}
2014-07-23 15:52:50 +02:00
/**
* 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
*/
2014-09-21 12:12:35 +02:00
public static function fetchGET($param = false, $default = false) {
2014-07-23 15:52:50 +02:00
if ($param === false) {
return $_GET;
2014-09-21 12:12:35 +02:00
} elseif (isset($_GET[$param])) {
2014-07-23 15:52:50 +02:00
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
*/
2014-09-21 12:12:35 +02:00
public static function fetchPOST($param = false, $default = false) {
2014-07-23 15:52:50 +02:00
if ($param === false) {
return $_POST;
2014-09-21 12:12:35 +02:00
} elseif (isset($_POST[$param])) {
2014-07-23 15:52:50 +02:00
return $_POST[$param];
} else {
return $default;
}
}
/**
* Méthode désactivant les magic_quotes pour les variables
* $_GET
* $_POST
* $_COOKIE
*/
2014-09-21 12:12:35 +02:00
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);
2014-07-23 15:52:50 +02:00
}
}
2014-09-21 12:12:35 +02:00
public static function isPost() {
return isset($_SERVER['REQUEST_METHOD']) &&
$_SERVER['REQUEST_METHOD'] === 'POST';
2014-07-23 15:52:50 +02:00
}
}