3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2014 *
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 if (!defined('_ECRIRE_INC_VERSION')) return;
16 // envoyer le navigateur sur une nouvelle adresse
17 // en evitant les attaques par la redirection (souvent indique par 1 $_GET)
19 // http://doc.spip.org/@redirige_par_entete
20 function redirige_par_entete($url, $equiv='', $status = 302) {
21 if (!in_array($status,array(301,302)))
24 $url = trim(strtr($url, "\n\r", " "));
25 # en theorie on devrait faire ca tout le temps, mais quand la chaine
26 # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne
28 $url = url_de_base().$url;
30 $url = self('&').$url;
31 # si profondeur non nulle et url relative, il faut la passer en absolue
32 if ($GLOBALS['profondeur_url']>(_DIR_RESTREINT?
1:2)
33 AND !preg_match(",^(\w+:)?//,",$url)){
34 include_spip("inc/filtres_mini");
35 $url = url_absolue($url);
38 if ($x = _request('transformer_xml'))
39 $url = parametre_url($url, 'transformer_xml', $x, '&');
41 if (defined('_AJAX') AND _AJAX
)
42 $url = parametre_url($url, 'var_ajax_redir', 1, '&');
44 // ne pas laisser passer n'importe quoi dans l'url
45 $url = str_replace(array('<','"'),array('<','"'),$url);
46 // interdire les url inline avec des pseudo-protocoles :
48 (preg_match(",data:,i",$url) AND preg_match("/base64\s*,/i",$url))
49 OR preg_match(",(javascript|mailto):,i",$url)
53 // Il n'y a que sous Apache que setcookie puis redirection fonctionne
54 include_spip('inc/cookie');
55 if ((!$equiv AND !spip_cookie_envoye()) OR ((strncmp("Apache", $_SERVER['SERVER_SOFTWARE'],6)==0) OR defined('_SERVER_APACHE'))) {
56 @header
("Location: " . $url);
59 @header
("Refresh: 0; url=" . $url);
60 $equiv = "<meta http-equiv='Refresh' content='0; url=$url'>";
62 include_spip('inc/lang');
65 echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">',"\n",
66 html_lang_attributes(),'
69 <title>HTTP '.$status.'</title>
72 <h1>HTTP '.$status.'</h1>
76 _T('navigateur_pas_redirige'),
79 spip_log("redirige $status: $url");
84 // http://doc.spip.org/@redirige_formulaire
85 function redirige_formulaire($url, $equiv = '', $format='message') {
88 AND !_request('var_ajax')) {
89 redirige_par_entete(str_replace('&','&',$url), $equiv);
91 // si c'est une ancre, fixer simplement le window.location.hash
92 elseif($format=='ajaxform' AND preg_match(',^#[0-9a-z\-_]+$,i',$url)) {
94 // on renvoie un lien masque qui sera traite par ajaxCallback.js
95 "<a href='$url' name='ajax_ancre' style='display:none;'>anchor</a>",
96 // et rien dans le message ok
100 // ne pas laisser passer n'importe quoi dans l'url
101 $url = str_replace(array('<','"'),array('<','"'),$url);
103 $url = strtr($url, "\n\r", " ");
104 # en theorie on devrait faire ca tout le temps, mais quand la chaine
105 # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne
107 $url = url_de_base().$url;
108 $url = str_replace('&','&',$url);
109 spip_log("redirige formulaire ajax: $url");
110 include_spip('inc/filtres');
111 if ($format=='ajaxform')
113 // on renvoie un lien masque qui sera traite par ajaxCallback.js
114 '<a href="'.quote_amp($url).'" name="ajax_redirect" style="display:none;">'._T('navigateur_pas_redirige').'</a>',
115 // et un message au cas ou
116 '<br /><a href="'.quote_amp($url).'">'._T('navigateur_pas_redirige').'</a>'
118 else // format message texte, tout en js inline
120 // ie poste les formulaires dans une iframe, il faut donc rediriger son parent
121 "<script type='text/javascript'>if (parent.window){parent.window.document.location.replace(\"$url\");} else {document.location.replace(\"$url\");}</script>"
122 . http_img_pack('searching.gif','')
124 . '<a href="'.quote_amp($url).'">'._T('navigateur_pas_redirige').'</a>';
128 // http://doc.spip.org/@redirige_url_ecrire
129 function redirige_url_ecrire($script='', $args='', $equiv='') {
130 return redirige_par_entete(generer_url_ecrire($script, $args, true), $equiv);
133 // http://doc.spip.org/@http_status
134 function http_status($status) {
135 global $REDIRECT_STATUS, $flag_sapi_name;
136 static $status_string = array(
138 204 => '204 No Content',
139 301 => '301 Moved Permanently',
141 304 => '304 Not Modified',
142 401 => '401 Unauthorized',
143 403 => '403 Forbidden',
144 404 => '404 Not Found',
145 503 => '503 Service Unavailable'
148 if ($REDIRECT_STATUS && $REDIRECT_STATUS == $status) return;
150 $php_cgi = ($flag_sapi_name AND preg_match(",cgi,i", @php_sapi_name
()));
152 header("Status: ".$status_string[$status]);
154 header("HTTP/1.0 ".$status_string[$status]);
157 // Retourne ce qui va bien pour que le navigateur ne mette pas la page en cache
158 // http://doc.spip.org/@http_no_cache
159 function http_no_cache() {
161 { spip_log("http_no_cache arrive trop tard"); return;}
162 $charset = empty($GLOBALS['meta']['charset']) ?
'utf-8' : $GLOBALS['meta']['charset'];
164 // selon http://developer.apple.com/internet/safari/faq.html#anchor5
165 // il faudrait aussi pour Safari
166 // header("Cache-Control: post-check=0, pre-check=0", false)
167 // mais ca ne respecte pas
168 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
170 header("Content-Type: text/html; charset=$charset");
171 header("Expires: 0");
172 header("Last-Modified: " .gmdate("D, d M Y H:i:s"). " GMT");
173 header("Cache-Control: no-cache, must-revalidate");
174 header("Pragma: no-cache");