[SPIP][PLUGINS] v3.0-->v3.2
[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-2017 *
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 // interdire les url inline avec des pseudo-protocoles :
63 if (
64 (preg_match(",data:,i", $url) and preg_match("/base64\s*,/i", $url))
65 or preg_match(",(javascript|mailto):,i", $url)
66 ) {
67 $url = "./";
68 }
69
70 // Il n'y a que sous Apache que setcookie puis redirection fonctionne
71 include_spip('inc/cookie');
72 if ((!$equiv and !spip_cookie_envoye()) or ((strncmp("Apache", $_SERVER['SERVER_SOFTWARE'],
73 6) == 0) or defined('_SERVER_APACHE'))
74 ) {
75 @header("Location: " . $url);
76 $equiv = "";
77 } else {
78 @header("Refresh: 0; url=" . $url);
79 if (isset($GLOBALS['meta']['charset'])) {
80 @header("Content-Type: text/html; charset=" . $GLOBALS['meta']['charset']);
81 }
82 $equiv = "<meta http-equiv='Refresh' content='0; url=$url'>";
83 }
84 include_spip('inc/lang');
85 if ($status != 302) {
86 http_status($status);
87 }
88 echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">', "\n",
89 html_lang_attributes(), '
90 <head>',
91 $equiv, '
92 <title>HTTP ' . $status . '</title>
93 ' . ((isset($GLOBALS['meta']['charset'])) ? '<meta http-equiv="Content-Type" content="text/html;charset=' . $GLOBALS['meta']['charset'] . '">' : '') . '
94 </head>
95 <body>
96 <h1>HTTP ' . $status . '</h1>
97 <a href="',
98 quote_amp($url),
99 '">',
100 _T('navigateur_pas_redirige'),
101 '</a></body></html>';
102
103 spip_log("redirige $status: $url");
104
105 exit;
106 }
107
108 // http://code.spip.net/@redirige_formulaire
109 function redirige_formulaire($url, $equiv = '', $format = 'message') {
110 if (!_AJAX
111 and !headers_sent()
112 and !_request('var_ajax')
113 ) {
114 redirige_par_entete(str_replace('&amp;', '&', $url), $equiv);
115 } // si c'est une ancre, fixer simplement le window.location.hash
116 elseif ($format == 'ajaxform' and preg_match(',^#[0-9a-z\-_]+$,i', $url)) {
117 return array(
118 // on renvoie un lien masque qui sera traite par ajaxCallback.js
119 "<a href='$url' name='ajax_ancre' style='display:none;'>anchor</a>",
120 // et rien dans le message ok
121 ''
122 );
123 } else {
124 // ne pas laisser passer n'importe quoi dans l'url
125 $url = str_replace(array('<', '"'), array('&lt;', '&quot;'), $url);
126
127 $url = strtr($url, "\n\r", " ");
128 # en theorie on devrait faire ca tout le temps, mais quand la chaine
129 # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne
130 if ($url[0] == '?') {
131 $url = url_de_base() . $url;
132 }
133 $url = str_replace('&amp;', '&', $url);
134 spip_log("redirige formulaire ajax: $url");
135 include_spip('inc/filtres');
136 if ($format == 'ajaxform') {
137 return array(
138 // on renvoie un lien masque qui sera traite par ajaxCallback.js
139 '<a href="' . quote_amp($url) . '" name="ajax_redirect" style="display:none;">' . _T('navigateur_pas_redirige') . '</a>',
140 // et un message au cas ou
141 '<br /><a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>'
142 );
143 } else // format message texte, tout en js inline
144 {
145 return
146 // ie poste les formulaires dans une iframe, il faut donc rediriger son parent
147 "<script type='text/javascript'>if (parent.window){parent.window.document.location.replace(\"$url\");} else {document.location.replace(\"$url\");}</script>"
148 . http_img_pack('searching.gif', '')
149 . '<br />'
150 . '<a href="' . quote_amp($url) . '">' . _T('navigateur_pas_redirige') . '</a>';
151 }
152 }
153 }
154
155 /**
156 * Effectue une redirection par header PHP vers un script de l’interface privée
157 *
158 * @uses redirige_par_entete() Qui tue le script PHP.
159 * @example
160 * ```
161 * include_spip('inc/headers');
162 * redirige_url_ecrire('rubriques','id_rubrique=' . $id_rubrique);
163 * ```
164 *
165 * @param string $script
166 * Nom de la page privée (exec)
167 * @param string $args
168 * Arguments à transmettre. Exemple `etape=1&autre=oui`
169 * @param string $equiv
170 * @return void
171 **/
172 function redirige_url_ecrire($script = '', $args = '', $equiv = '') {
173 return redirige_par_entete(generer_url_ecrire($script, $args, true), $equiv);
174 }
175
176 /**
177 * Renvoie au client le header HTTP avec le message correspondant au code indiqué.
178 *
179 * Ainsi `http_status(301)` enverra le message `301 Moved Permanently`.
180 *
181 * @link http://php.net/manual/fr/function.header.php Fonction header() de PHP utilisée ici
182 *
183 * @param int $status
184 * Code d'erreur
185 **/
186 function http_status($status) {
187
188 static $status_string = array(
189 200 => '200 OK',
190 204 => '204 No Content',
191 301 => '301 Moved Permanently',
192 302 => '302 Found',
193 304 => '304 Not Modified',
194 401 => '401 Unauthorized',
195 403 => '403 Forbidden',
196 404 => '404 Not Found',
197 503 => '503 Service Unavailable'
198 );
199
200 if (!empty($GLOBALS['REDIRECT_STATUS']) && $GLOBALS['REDIRECT_STATUS'] == $status) {
201 return;
202 }
203
204 $php_cgi = ($GLOBALS['flag_sapi_name'] and preg_match(",cgi,i", @php_sapi_name()));
205 if ($php_cgi) {
206 header("Status: " . $status_string[$status]);
207 } else {
208 header("HTTP/1.0 " . $status_string[$status]);
209 }
210 }
211
212 // Retourne ce qui va bien pour que le navigateur ne mette pas la page en cache
213 // http://code.spip.net/@http_no_cache
214 function http_no_cache() {
215 if (headers_sent()) {
216 spip_log("http_no_cache arrive trop tard");
217
218 return;
219 }
220 $charset = empty($GLOBALS['meta']['charset']) ? 'utf-8' : $GLOBALS['meta']['charset'];
221
222 // selon http://developer.apple.com/internet/safari/faq.html#anchor5
223 // il faudrait aussi pour Safari
224 // header("Cache-Control: post-check=0, pre-check=0", false)
225 // mais ca ne respecte pas
226 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
227
228 header("Content-Type: text/html; charset=$charset");
229 header("Expires: 0");
230 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
231 header("Cache-Control: no-cache, must-revalidate");
232 header("Pragma: no-cache");
233 }