0146614a3d9d22313970a5e4854c4f3531e22f16
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
14 * Gestion des headers et redirections
16 * @package SPIP\Core\Headers
19 if (!defined('_ECRIRE_INC_VERSION')) {
25 * Envoyer le navigateur sur une nouvelle adresse
27 * Le tout en évitant les attaques par la redirection (souvent indique par un `$_GET`)
31 * $redirect = parametre_url(urldecode(_request('redirect')),'id_article=' . $id_article);
32 * include_spip('inc/headers');
33 * redirige_par_entete($redirect);
36 * @param string $url URL de redirection
37 * @param string $equiv ?
38 * @param int $status Code de redirection (301 ou 302)
40 function redirige_par_entete($url, $equiv = '', $status = 302) {
41 if (!in_array($status, array(301, 302))) {
45 $url = trim(strtr($url, "\n\r", " "));
46 # si l'url de redirection est relative, on la passe en absolue
47 if (!preg_match(",^(\w+:)?//,", $url)) {
48 include_spip("inc/filtres_mini");
49 $url = url_absolue($url);
52 if ($x = _request('transformer_xml')) {
53 $url = parametre_url($url, 'transformer_xml', $x, '&');
56 if (defined('_AJAX') and _AJAX
) {
57 $url = parametre_url($url, 'var_ajax_redir', 1, '&');
60 // ne pas laisser passer n'importe quoi dans l'url
61 $url = str_replace(array('<', '"'), array('<', '"'), $url);
62 $url = str_replace(array("\r", "\n", ' '), array('%0D', '%0A', '%20'), $url);
63 while (strpos($url, '%0A') !== false) {
64 $url = str_replace('%0A', '', $url);
66 // interdire les url inline avec des pseudo-protocoles :
68 (preg_match(",data:,i", $url) and preg_match("/base64\s*,/i", $url))
69 or preg_match(",(javascript|mailto):,i", $url)
74 // Il n'y a que sous Apache que setcookie puis redirection fonctionne
75 include_spip('inc/cookie');
76 if ((!$equiv and !spip_cookie_envoye()) or (
77 (strncmp("Apache", $_SERVER['SERVER_SOFTWARE'], 6) == 0)
78 or (stripos($_SERVER['SERVER_SIGNATURE'], 'Apache') !== false)
79 or function_exists('apache_getenv')
80 or defined('_SERVER_APACHE')
83 @header
("Location: " . $url);
86 @header
("Refresh: 0; url=" . $url);
87 if (isset($GLOBALS['meta']['charset'])) {
88 @header
("Content-Type: text/html; charset=" . $GLOBALS['meta']['charset']);
90 $equiv = "<meta http-equiv='Refresh' content='0; url=$url'>";
92 include_spip('inc/lang');
96 echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">', "\n",
97 html_lang_attributes(), '
100 <title>HTTP ' . $status . '</title>
101 ' . ((isset($GLOBALS['meta']['charset'])) ?
'<meta http-equiv="Content-Type" content="text/html;charset=' . $GLOBALS['meta']['charset'] . '">' : '') . '
104 <h1>HTTP ' . $status . '</h1>
108 _T('navigateur_pas_redirige'),
109 '</a></body></html>';
111 spip_log("redirige $status: $url");
116 // http://code.spip.net/@redirige_formulaire
117 function redirige_formulaire($url, $equiv = '', $format = 'message') {
120 and !_request('var_ajax')
122 redirige_par_entete(str_replace('&', '&', $url), $equiv);
123 } // si c'est une ancre, fixer simplement le window.location.hash
124 elseif ($format == 'ajaxform' and preg_match(',^#[0-9a-z\-_]+$,i', $url)) {
126 // on renvoie un lien masque qui sera traite par ajaxCallback.js
127 "<a href='$url' name='ajax_ancre' style='display:none;'>anchor</a>",
128 // et rien dans le message ok
132 // ne pas laisser passer n'importe quoi dans l'url
133 $url = str_replace(array('<', '"'), array('<', '"'), $url);
135 $url = strtr($url, "\n\r", " ");
136 # en theorie on devrait faire ca tout le temps, mais quand la chaine
137 # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne
138 if ($url[0] == '?') {
139 $url = url_de_base() . $url;
141 $url = str_replace('&', '&', $url);
142 spip_log("redirige formulaire ajax: $url");
143 include_spip('inc/filtres');
144 if ($format == 'ajaxform') {
146 // on renvoie un lien masque qui sera traite par ajaxCallback.js
147 '<a href="' . quote_amp($url) . '" name="ajax_redirect" style="display:none;">' . _T('navigateur_pas_redirige') . '</a>',
148 // et un message au cas ou
149 '<br /><a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>'
151 } else // format message texte, tout en js inline
154 // ie poste les formulaires dans une iframe, il faut donc rediriger son parent
155 "<script type='text/javascript'>if (parent.window){parent.window.document.location.replace(\"$url\");} else {document.location.replace(\"$url\");}</script>"
156 . http_img_pack('searching.gif', '')
158 . '<a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>';
164 * Effectue une redirection par header PHP vers un script de l’interface privée
166 * @uses redirige_par_entete() Qui tue le script PHP.
169 * include_spip('inc/headers');
170 * redirige_url_ecrire('rubriques','id_rubrique=' . $id_rubrique);
173 * @param string $script
174 * Nom de la page privée (exec)
175 * @param string $args
176 * Arguments à transmettre. Exemple `etape=1&autre=oui`
177 * @param string $equiv
180 function redirige_url_ecrire($script = '', $args = '', $equiv = '') {
181 return redirige_par_entete(generer_url_ecrire($script, $args, true), $equiv);
185 * Renvoie au client le header HTTP avec le message correspondant au code indiqué.
187 * Ainsi `http_status(301)` enverra le message `301 Moved Permanently`.
189 * @link http://php.net/manual/fr/function.header.php Fonction header() de PHP utilisée ici
194 function http_status($status) {
196 static $status_string = array(
198 204 => '204 No Content',
199 301 => '301 Moved Permanently',
201 304 => '304 Not Modified',
202 401 => '401 Unauthorized',
203 403 => '403 Forbidden',
204 404 => '404 Not Found',
205 503 => '503 Service Unavailable'
208 if (!empty($GLOBALS['REDIRECT_STATUS']) && $GLOBALS['REDIRECT_STATUS'] == $status) {
212 $php_cgi = ($GLOBALS['flag_sapi_name'] and preg_match(",cgi,i", @php_sapi_name
()));
214 header("Status: " . $status_string[$status]);
216 header("HTTP/1.0 " . $status_string[$status]);
220 // Retourne ce qui va bien pour que le navigateur ne mette pas la page en cache
221 // http://code.spip.net/@http_no_cache
222 function http_no_cache() {
223 if (headers_sent()) {
224 spip_log("http_no_cache arrive trop tard");
228 $charset = empty($GLOBALS['meta']['charset']) ?
'utf-8' : $GLOBALS['meta']['charset'];
230 // selon http://developer.apple.com/internet/safari/faq.html#anchor5
231 // il faudrait aussi pour Safari
232 // header("Cache-Control: post-check=0, pre-check=0", false)
233 // mais ca ne respecte pas
234 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
236 header("Content-Type: text/html; charset=$charset");
237 header("Expires: 0");
238 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
239 header("Cache-Control: no-cache, must-revalidate");
240 header("Pragma: no-cache");