<?php
//
// Si une version de SPIP est deja installee, exiger l'authentification FTP
//
if (file_exists("ecrire/inc_connect.php3") AND file_exists("ecrire/unpack.php3")) {
include ("ecrire/inc_version.php3");
include ("ecrire/inc_connect.php3");
include ("ecrire/inc_meta.php3");
include ("ecrire/inc_admin.php3");
$ok = true;
if (!$id_auteur) $ok = false;
else if (!verifier_action_auteur("unpack", $hash, $id_auteur)) $ok = false;
if (!$ok) {
@header("Location: ecrire/unpack.php3");
exit;
}
$charger = 'oui';
}
else {
function feed_globals($table) {
if (is_array($GLOBALS[$table])) {
reset($GLOBALS[$table]);
while (list($key, $val) = each($GLOBALS[$table])) {
$GLOBALS[$key] = $val;
}
}
}
feed_globals('HTTP_GET_VARS');
feed_globals('HTTP_POST_VARS');
feed_globals('HTTP_COOKIE_VARS');
feed_globals('HTTP_SERVER_VARS');
}
function debut_html($titre = "Installation du système de publication...") {
?>
<HTML>
<HTML>
<HEAD>
<TITLE><?php echo $titre; ?></TITLE>
<META HTTP-EQUIV="Expires" CONTENT="0">
<META HTTP-EQUIV="cache-control" CONTENT="no-cache,no-store">
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<style>
<!--
a {text-decoration: none; }
A:Hover {color:#FF9900; text-decoration: underline;}
.forml {width: 100%; background-color: #FFCC66; background-position: center bottom; float: none; color: #000000}
.formo {width: 100%; background-color: #970038; background-position: center bottom; float: none; color: #FFFFFF}
.fondl {background-color: #FFCC66; background-position: center bottom; float: none; color: #000000}
.fondo {background-color: #970038; background-position: center bottom; float: none; color: #FFFFFF}
.fondf {background-color: #FFFFFF; border-style: solid ; border-width: 1; border-color: #E86519; color: #E86519}
-->
</style>
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" link="#E86519" vlink="#6E003A" alink="#FF9900" TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
<BR><BR><BR>
<CENTER>
<TABLE WIDTH=450>
<TR><TD WIDTH=450>
<FONT FACE="Verdana,Arial,Helvetica,sans-serif" SIZE=4 COLOR="#970038"><B><?php
echo $titre;
?></B></FONT>
<FONT FACE="Georgia,Garamond,Times,serif" SIZE=3>
<?php
}
function fin_html() {
?>
</FONT>
</TD></TR></TABLE>
</CENTER>
</BODY>
</HTML>
<?php
}
function lire_short($f) {
global $_fread;
$bin = $_fread($f, 2);
$res = unpack('na', $bin);
return $res['a'];
}
function lire_long($f) {
global $_fread;
$bin = $_fread($f, 4);
$res = unpack('Na', $bin);
return $res['a'];
}
function lire_chaine($f) {
global $_fread;
$n = lire_long($f);
if (!$n) return '';
return $_fread($f, $n);
}
// Tester si le fichier est un fichier de configuration
// modifiable par le webmestre (squelettes, etc)
function test_fichier_config($fichier) {
// Fichiers graphiques modifiables (racine du site)
if (ereg("^[^/]*\.(gif|jpe?g|png)$", $fichier)) return true;
// Fichiers .php3 dans le repertoire de base
if (ereg("^([^/]*)\.php3$", $fichier, $regs)) {
$racine = $regs[1];
if ($racine == 'inc-urls') return true;
if (ereg("^(spip_|inc-)", $racine)) return false;
return true;
}
return false;
}
function unpacker_fichier($f) {
global $dir_base;
global $_fread;
global $chmod;
$chaine = $_fread($f, 4);
if ($chaine != 'spip') {
return false;
}
$chmod_dir = $chmod;
$chmod_file = $chmod & ~0111;
while (($fichier = lire_chaine($f)) != '') {
$n = lire_long($f);
$chemin = "$dir_base$fichier";
if (!$n) {
@mkdir($chemin, $chmod_dir);
@chmod($chemin, $chmod_dir);
}
else {
$bin = $_fread($f, $n);
if (!(test_fichier_config($fichier) AND file_exists($chemin))) {
$dest = fopen($chemin, "wb");
fwrite($dest, $bin, $n);
fclose($dest);
@chmod($chemin, $chmod_file);
}
}
}
return true;
}
//
// Verifier si la ZLib est utilisable
//
$gz = function_exists("gzopen");
if ($gz) {
$_fwrite = gzwrite;
$_fread = gzread;
$_fopen = gzopen;
$_fclose = gzclose;
}
else {
$_fwrite = fwrite;
$_fread = fread;
$_fopen = fopen;
$_fclose = fclose;
}
//
// Si le fichier est deja charge, le decompacter
//
if ($fichier AND file_exists($fichier)) {
$dir_base = "";
$f = $_fopen($fichier, "rb");
$ok = unpacker_fichier($f);
$_fclose($f);
@unlink($fichier);
if (!$ok) {
die ("<h4>Données incorrectes. Veuillez réessayer, ou utiliser l'installation manuelle.</h4>");
}
header("Location: ".$dir_base."ecrire/");
exit;
}
//
// Si pas encore fait, afficher la page de presentation
//
if ($charger != 'oui') {
debut_html("Téléchargement de SPIP");
echo "<P><B>Bienvenue dans la procédure d'installation automatique de SPIP.</B>";
echo "<P>Le système va d'abord vérifier les droits d'accès au répertoire courant, ";
echo "puis lancer le téléchargement des données SPIP à l'intérieur de ce répertoire.";
echo "<P>Veuillez appuyer sur le bouton suivant pour continuer.";
echo "<DIV ALIGN='right'>";
echo "<FORM ACTION='spip_loader.php3' METHOD='get'>";
echo "<INPUT TYPE='hidden' NAME='charger' VALUE='oui'>";
echo "<INPUT TYPE='submit' NAME='Valider' VALUE=\"Commencer l'installation >>\">";
echo "</FORM>";
fin_html();
exit;
}
//
// Gestion des droits d'acces
//
$ok = false;
$self = basename($PHP_SELF);
$uid = @fileowner('.');
$uid2 = @fileowner($self);
$gid = @filegroup('.');
$gid2 = @filegroup($self);
$perms = @fileperms($self);
// Comparer l'appartenance d'un fichier cree par PHP
// avec celle du script et du repertoire courant
@rmdir('test');
@unlink('test'); // effacer au cas ou
@touch('test');
if ($uid > 0 && $uid == $uid2 && @fileowner('test') == $uid)
$chmod = 0700;
else if ($gid > 0 && $gid == $gid2 && @filegroup('test') == $gid)
$chmod = 0770;
else
$chmod = 0777;
// Appliquer de plus les droits d'acces du script
if ($perms > 0) {
$perms = ($perms & 0777) | (($perms & 0444) >> 2);
$chmod |= $perms;
}
@unlink('test');
//echo "uids: $uid, $uid2<br>gids: $gid, $gid2<br>chmod: ".(($chmod & (7 << 9)) >> 9).(($chmod & (7 << 6)) >> 6).(($chmod & (7 << 3)) >> 3).($chmod & 7)."<br>";
// Verifier que les valeurs sont correctes
@mkdir('test', $chmod);
@chmod('test', $chmod);
$f = @fopen('test/test.php', 'w');
if ($f) {
@fputs($f, '<?php $ok = true; ?>');
@fclose($f);
@chmod('test/test.php', $chmod);
include('test/test.php');
}
@unlink('test/test.php');
@rmdir('test');
if (!$ok) {
debut_html("Téléchargement de SPIP");
echo "<BR><FONT FACE=\"Verdana,Arial,Helvetica,sans-serif\" SIZE=3>Préliminaire : ";
echo "<B>Régler les droits d'accès</B></FONT>";
echo "<P><B>Le répertoire courant n'est pas accessible en écriture.</B>";
echo "<P>Pour y remédier, utilisez votre client FTP afin de régler les droits d'accès ";
echo "à ce répertoire (répertoire d'installation de SPIP). ";
echo "La procédure est expliquée en détail dans le guide d'installation. Au choix :<BR>";
echo "<UL>";
echo "<LI><B>Si vous avez un client FTP graphique</B>, réglez les propriétés du répertoire courant ";
echo "afin qu'il soit accessible en écriture pour tous.<P>";
echo "<LI><B>Si votre client FTP est en mode texte</B>, changez le mode du répertoire à la valeur 777.<P>";
echo "<LI><B>Si vous avez un accès Telnet</B>, faites un <I>chmod 777 repertoire_courant</I>.<P>";
echo "</UL>";
echo "<P>Une fois cette manipulation effectuée, vous pourrez <B><A HREF='spip_loader.php3?charger=oui&hash=$hash&id_auteur=$id_auteur'>recharger cette page</A></B> ";
echo "afin de commencer le téléchargement puis l'installation.";
echo "<P>Si l'erreur persiste, vous devrez passer par la procédure d'installation classique ";
echo "(téléchargement de tous les fichiers par FTP).";
fin_html();
exit;
}
//
// Tenter le telechargement HTTP
//
$fichier = ".spip.bin";
if ($gz) $fichier .= ".gz";
$url = "http://www.spip.net/spip-dev/DISTRIB/$fichier";
// Tenter un chargement direct par fopen
$http = @fopen($url, "rb");
if (!$http) {
// En cas d'echec faire la requete HTTP a la main
for ($i = 0; $i < 5; $i++) {
$t = parse_url($url);
$host = $t['host'];
if (!($port = $t['port'])) $port = 80;
if (!($path = $t['path'])) $path = "/";
$http = @fsockopen($host, $port);
if (!$http) break;
@fputs($http, "GET $path HTTP/1.1\nHost: $host\n\n");
$status = 0;
$location = '';
// Passer les en-tetes (termines par une ligne vide)
$s = trim(fgets($http, 16384));
if (ereg('^HTTP/[0-9]+\.[0-9]+ ([0-9]+)', $s, $r)) $status = $r[1];
while ($s = trim(fgets($http, 16384))) {
if (ereg('^Location: (.*)', $s, $r)) {
$location = $r[1];
}
}
if ($status != 200) {
fclose($http);
unset($http);
}
if ($status >= 300 AND $status < 400 AND $location) $url = $location;
else break;
}
}
$n = 0;
if ($http) {
// Sauver le fichier telecharge
$f = fopen($fichier, "wb");
while (!feof($http)) {
$bin = fread($http, 1024);
fwrite($f, $bin);
}
fclose($f);
fclose($http);
// Passer a l'etape suivante (desarchivage)
header("Location: spip_loader.php3?fichier=$fichier&hash=$hash&id_auteur=$id_auteur&chmod=$chmod");
exit;
}
die ("<h4>Le chargement a échoué. Veuillez réessayer, ou utiliser l'installation manuelle.</h4>");
?>