[SPIP] v3.2.11 -> v3.2.12
[lhc/web/www.git] / www / ecrire / inc / headers.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13 /**
14 * Gestion des headers et redirections
15 *
16 * @package SPIP\Core\Headers
17 **/
18
19 if (!defined('_ECRIRE_INC_VERSION')) {
20 return;
21 }
22
23
24 /**
25 * Envoyer le navigateur sur une nouvelle adresse
26 *
27 * Le tout en évitant les attaques par la redirection (souvent indique par un `$_GET`)
28 *
29 * @example
30 * ```
31 * $redirect = parametre_url(urldecode(_request('redirect')),'id_article=' . $id_article);
32 * include_spip('inc/headers');
33 * redirige_par_entete($redirect);
34 * ```
35 *
36 * @param string $url URL de redirection
37 * @param string $equiv ?
38 * @param int $status Code de redirection (301 ou 302)
39 **/
40 function redirige_par_entete($url, $equiv = '', $status = 302) {
41 if (!in_array($status, array(301, 302))) {
42 $status = 302;
43 }
44
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);
50 }
51
52 if ($x = _request('transformer_xml')) {
53 $url = parametre_url($url, 'transformer_xml', $x, '&');
54 }
55
56 if (defined('_AJAX') and _AJAX) {
57 $url = parametre_url($url, 'var_ajax_redir', 1, '&');
58 }
59
60 // ne pas laisser passer n'importe quoi dans l'url
61 $url = str_replace(array('<', '"'), array('&lt;', '&quot;'), $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);
65 }
66 // interdire les url inline avec des pseudo-protocoles :
67 if (
68 (preg_match(",data:,i", $url) and preg_match("/base64\s*,/i", $url))
69 or preg_match(",(javascript|mailto):,i", $url)
70 ) {
71 $url = "./";
72 }
73
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')
81 )
82 ) {
83 @header("Location: " . $url);
84 $equiv = "";
85 } else {
86 @header("Refresh: 0; url=" . $url);
87 if (isset($GLOBALS['meta']['charset'])) {
88 @header("Content-Type: text/html; charset=" . $GLOBALS['meta']['charset']);
89 }
90 $equiv = "<meta http-equiv='Refresh' content='0; url=$url'>";
91 }
92 include_spip('inc/lang');
93 if ($status != 302) {
94 http_status($status);
95 }
96 echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">', "\n",
97 html_lang_attributes(), '
98 <head>',
99 $equiv, '
100 <title>HTTP ' . $status . '</title>
101 ' . ((isset($GLOBALS['meta']['charset'])) ? '<meta http-equiv="Content-Type" content="text/html;charset=' . $GLOBALS['meta']['charset'] . '">' : '') . '
102 </head>
103 <body>
104 <h1>HTTP ' . $status . '</h1>
105 <a href="',
106 quote_amp($url),
107 '">',
108 _T('navigateur_pas_redirige'),
109 '</a></body></html>';
110
111 spip_log("redirige $status: $url");
112
113 exit;
114 }
115
116 // http://code.spip.net/@redirige_formulaire
117 function redirige_formulaire($url, $equiv = '', $format = 'message') {
118 if (!_AJAX
119 and !headers_sent()
120 and !_request('var_ajax')
121 ) {
122 redirige_par_entete(str_replace('&amp;', '&', $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)) {
125 return array(
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
129 ''
130 );
131 } else {
132 // ne pas laisser passer n'importe quoi dans l'url
133 $url = str_replace(array('<', '"'), array('&lt;', '&quot;'), $url);
134
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;
140 }
141 $url = str_replace('&amp;', '&', $url);
142 spip_log("redirige formulaire ajax: $url");
143 include_spip('inc/filtres');
144 if ($format == 'ajaxform') {
145 return array(
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>'
150 );
151 } else // format message texte, tout en js inline
152 {
153 return
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', '')
157 . '<br />'
158 . '<a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>';
159 }
160 }
161 }
162
163 /**
164 * Effectue une redirection par header PHP vers un script de l’interface privée
165 *
166 * @uses redirige_par_entete() Qui tue le script PHP.
167 * @example
168 * ```
169 * include_spip('inc/headers');
170 * redirige_url_ecrire('rubriques','id_rubrique=' . $id_rubrique);
171 * ```
172 *
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
178 * @return void
179 **/
180 function redirige_url_ecrire($script = '', $args = '', $equiv = '') {
181 return redirige_par_entete(generer_url_ecrire($script, $args, true), $equiv);
182 }
183
184 /**
185 * Renvoie au client le header HTTP avec le message correspondant au code indiqué.
186 *
187 * Ainsi `http_status(301)` enverra le message `301 Moved Permanently`.
188 *
189 * @link https://www.php.net/manual/fr/function.http-response-code.php
190 * @uses http_response_code()
191 *
192 * @param int $status
193 * Code d'erreur
194 **/
195 function http_status($status) {
196 http_response_code($status);
197 }
198
199 // Retourne ce qui va bien pour que le navigateur ne mette pas la page en cache
200 // http://code.spip.net/@http_no_cache
201 function http_no_cache() {
202 if (headers_sent()) {
203 spip_log("http_no_cache arrive trop tard");
204
205 return;
206 }
207 $charset = empty($GLOBALS['meta']['charset']) ? 'utf-8' : $GLOBALS['meta']['charset'];
208
209 // selon http://developer.apple.com/internet/safari/faq.html#anchor5
210 // il faudrait aussi pour Safari
211 // header("Cache-Control: post-check=0, pre-check=0", false)
212 // mais ca ne respecte pas
213 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
214
215 header("Content-Type: text/html; charset=$charset");
216 header("Expires: 0");
217 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
218 header("Cache-Control: no-cache, must-revalidate");
219 header("Pragma: no-cache");
220 }