Exemplo n.º 1
0
function redirige_par_entete($url, $equiv = '', $status = 302)
{
    if (!in_array($status, array(301, 302))) {
        $status = 302;
    }
    $url = trim(strtr($url, "\n\r", "  "));
    # en theorie on devrait faire ca tout le temps, mais quand la chaine
    # commence par ? c'est imperatif, sinon l'url finale n'est pas la bonne
    if ($url[0] == '?') {
        $url = url_de_base() . $url;
    }
    if ($url[0] == '#') {
        $url = self('&') . $url;
    }
    # si profondeur non nulle et url relative, il faut la passer en absolue
    if ($GLOBALS['profondeur_url'] > (_DIR_RESTREINT ? 1 : 2) and !preg_match(",^(\\w+:)?//,", $url)) {
        include_spip("inc/filtres_mini");
        $url = url_absolue($url);
    }
    if ($x = _request('transformer_xml')) {
        $url = parametre_url($url, 'transformer_xml', $x, '&');
    }
    if (defined('_AJAX') and _AJAX) {
        $url = parametre_url($url, 'var_ajax_redir', 1, '&');
    }
    // ne pas laisser passer n'importe quoi dans l'url
    $url = str_replace(array('<', '"'), array('&lt;', '&quot;'), $url);
    // interdire les url inline avec des pseudo-protocoles :
    if (preg_match(",data:,i", $url) and preg_match("/base64\\s*,/i", $url) or preg_match(",(javascript|mailto):,i", $url)) {
        $url = "./";
    }
    // Il n'y a que sous Apache que setcookie puis redirection fonctionne
    include_spip('inc/cookie');
    if (!$equiv and !spip_cookie_envoye() or (strncmp("Apache", $_SERVER['SERVER_SOFTWARE'], 6) == 0 or defined('_SERVER_APACHE'))) {
        @header("Location: " . $url);
        $equiv = "";
    } else {
        @header("Refresh: 0; url=" . $url);
        $equiv = "<meta http-equiv='Refresh' content='0; url={$url}'>";
    }
    include_spip('inc/lang');
    if ($status != 302) {
        http_status($status);
    }
    echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">', "\n", html_lang_attributes(), '
<head>', $equiv, '
<title>HTTP ' . $status . '</title>
</head>
<body>
<h1>HTTP ' . $status . '</h1>
<a href="', quote_amp($url), '">', _T('navigateur_pas_redirige'), '</a></body></html>';
    spip_log("redirige {$status}: {$url}");
    exit;
}
Exemplo n.º 2
0
/**
 * Envoyer le navigateur sur une nouvelle adresse
 *
 * Le tout en évitant les attaques par la redirection (souvent indique par un `$_GET`)
 *
 * @example
 *     ```
 *     $redirect = parametre_url(urldecode(_request('redirect')),'id_article=' . $id_article);
 *     include_spip('inc/headers');
 *     redirige_par_entete($redirect);
 *     ```
 *
 * @param string $url URL de redirection
 * @param string $equiv ?
 * @param int $status Code de redirection (301 ou 302)
 **/
function redirige_par_entete($url, $equiv = '', $status = 302)
{
    if (!in_array($status, array(301, 302))) {
        $status = 302;
    }
    $url = trim(strtr($url, "\n\r", "  "));
    # si l'url de redirection est relative, on la passe en absolue
    if (!preg_match(",^(\\w+:)?//,", $url)) {
        include_spip("inc/filtres_mini");
        $url = url_absolue($url);
    }
    if ($x = _request('transformer_xml')) {
        $url = parametre_url($url, 'transformer_xml', $x, '&');
    }
    if (defined('_AJAX') and _AJAX) {
        $url = parametre_url($url, 'var_ajax_redir', 1, '&');
    }
    // ne pas laisser passer n'importe quoi dans l'url
    $url = str_replace(array('<', '"'), array('&lt;', '&quot;'), $url);
    // interdire les url inline avec des pseudo-protocoles :
    if (preg_match(",data:,i", $url) and preg_match("/base64\\s*,/i", $url) or preg_match(",(javascript|mailto):,i", $url)) {
        $url = "./";
    }
    // Il n'y a que sous Apache que setcookie puis redirection fonctionne
    include_spip('inc/cookie');
    if (!$equiv and !spip_cookie_envoye() or (strncmp("Apache", $_SERVER['SERVER_SOFTWARE'], 6) == 0 or defined('_SERVER_APACHE'))) {
        @header("Location: " . $url);
        $equiv = "";
    } else {
        @header("Refresh: 0; url=" . $url);
        if (isset($GLOBALS['meta']['charset'])) {
            @header("Content-Type: text/html; charset=" . $GLOBALS['meta']['charset']);
        }
        $equiv = "<meta http-equiv='Refresh' content='0; url={$url}'>";
    }
    include_spip('inc/lang');
    if ($status != 302) {
        http_status($status);
    }
    echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">', "\n", html_lang_attributes(), '
<head>', $equiv, '
<title>HTTP ' . $status . '</title>
' . (isset($GLOBALS['meta']['charset']) ? '<meta http-equiv="Content-Type" content="text/html;charset=' . $GLOBALS['meta']['charset'] . '">' : '') . '
</head>
<body>
<h1>HTTP ' . $status . '</h1>
<a href="', quote_amp($url), '">', _T('navigateur_pas_redirige'), '</a></body></html>';
    spip_log("redirige {$status}: {$url}");
    exit;
}
Exemplo n.º 3
0
function spip_setcookie($name = '', $value = '', $expire = 0, $path = 'AUTO', $domain = '', $secure = '')
{
    $name = preg_replace('/^spip_/', $GLOBALS['cookie_prefix'] . '_', $name);
    if ($path == 'AUTO') {
        $path = defined('_COOKIE_PATH') ? _COOKIE_PATH : preg_replace(',^\\w+://[^/]*,', '', url_de_base());
    }
    if (!$domain and defined('_COOKIE_DOMAIN')) {
        $domain = _COOKIE_DOMAIN;
    }
    #spip_log("cookie('$name', '$value', '$expire', '$path', '$domain', '$secure', '$httponly'");
    // liste des cookies en httponly (a passer en define si besoin)
    $httponly = in_array($name, explode(' ', 'spip_session'));
    $a = ($httponly and strnatcmp(phpversion(), '5.2.0') >= 0) ? @setcookie($name, $value, $expire, $path, $domain, $secure, $httponly) : ($secure ? @setcookie($name, $value, $expire, $path, $domain, $secure) : ($domain ? @setcookie($name, $value, $expire, $path, $domain) : ($path ? @setcookie($name, $value, $expire, $path) : ($expire ? @setcookie($name, $value, $expire) : @setcookie($name, $value)))));
    spip_cookie_envoye(true);
    return $a;
}