<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Alain Pomirol
(CATICE Académie de Bordeaux)

This file is part of GiBii/Melia.

GiBii/Melia is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

GiBii/Melia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GiBii/Melia; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

****************************************************************************/

include("../include/vars_adm.php");
include("../include/util.inc.php");
include("../include/utiljs.php");
include("update.inc.php");


//Passage 1 : affiche un ou 2 boutons radio (correctif ou distrib)
//----------------------------------------------------------------
function affradio($typemaj) { 	//'cor' ou 'dis'
if ($typemaj=='cor') {
	$msgradio='le correctif';
	$fmsg=$_SESSION['cor_msg'];
	$fact=$_SESSION['cor_fic'];
	}
else {
	$msgradio='la distrib';
	$fmsg=$_SESSION['dis_msg'];
	$fact=$_SESSION['dis_fic'];
	}
if (!$fact) return;

$tf=explode('/',$fact);
$fbase=$tf[count($tf)-1];
print "<tr><td>";
print "<input type=\"radio\" name=\"typemaj\" value=\"$typemaj\"> $msgradio <b>$fbase</b><br />";
print "</td><td>";
$texte="&nbsp";
if ($fmsg) {
	$lignes=@file_get_contents($fmsg);
	if ($lignes!==false) $texte=nl2br($lignes);
	}
print $texte;
print "</td>";
print "</tr>";
}


//Télécharge le fichier $fic, le copie localement et retourne son nom
//-------------------------------------------------------------------
function copielocale($fic) {
$tfic=explode('/',$fic);
$ficbase=$tfic[count($tfic)-1];

print "<p>Chargement de $ficbase</p>";
$ch=@file_get_contents($fic);
if ($ch===false) erreur("$fic inaccessible."); 

$flocal=LOCAL_REP_ZIP.$ficbase;
$fp=fopen($flocal,'w');
fwrite($fp,$ch);
fclose($fp);
return $flocal;
}


//A partir du nom de fichier zip $flocal, extrait l'arbo dans gibii
//-----------------------------------------------------------------
function extraire_dans_gibii($flocal) {
$tf=explode('/',$flocal);	
$f=$tf[count($tf)-1];		//bla.zip
$tf=explode('.',$f);		
$f=$tf[count($tf)-2];		//bla
$repextract=LOCAL_REP_ZIP.$f;	//../data/bla
creertousrep($repextract.'/');	//../data/bla/

$archive = new PclZip($flocal);
$list=$archive->listContent();
//dbg('list');

//recherche de l'arbo (en principe, c'est soit directement sadmin 
//soit b2i/sadmin, par exemple. Dans le 2e cas, il faut éliminer b2i/
for($i=0; $i<count($list); $i++) {
	$entree=$list[$i];
	if (!$entree['folder']) continue;
	$rep=$entree['stored_filename'];
	//print "rep=$rep, ";
	foreach(array(	'sadmin','admin','backup','data','eleve','gestion','images','include',
					'incorporation','install','langues','login','outils','prof','themes') 
				  as $rbase) {
		$rang=strpos($rep,$rbase);
		//print "$rbase,rang=$rang<br>";
		if ($rang!==false) {
			if ($rang==0) $enlever='';
			else $enlever=substr($rep,0,$rang-1);
			break;
			}
		}
	if (isset($enlever)) break;
	}
//print "enlever=$enlever<br><br><br>";

$list = $archive->extract('..',$enlever);
return $list;
}

//====================================================================================
connexion("$db");
opensession("Sadmin",false);
if ($sess_login=='admstats') {
	aneantir_session();
	goto("./index.php");
	}
$PHP_SELF=basename($_SERVER['PHP_SELF']);
//print_r($_SESSION);exit();

print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
print "<html><head>\n";
print "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\n";
print "<title>Mise à jour de $MODULE</title>\n";
print "<link rel=\"stylesheet\" href=\"../include/general.css\">\n";
print "</head>";
print "<body class=\"arial_sadmin\">\n";

if (!isset($envoi)) {
	//passage 1
	//---------
	if (!$maj_existe) print "Il n'existe pas de mise à jour pour votre version.</body></html>";
	
	print "<h2 align=\"center\">Mise à jour de $MODULE</h2><hr />\n";
	print "<p>&nbsp;</p>\n";
	print "<p>$MODULE est actuellement en version $VERSION_GIBII.</p>\n";
	print "<p>Vous pouvez mettre à jour avec :</p>\n";
	print "<form name=\"form1\" method=\"post\" action=\"$PHP_SELF\">";
	print "<table>\n";
	affradio('cor');
	affradio('dis');
	print "</table><p>&nbsp;</p><p>";
	print "<input type=\"submit\" name=\"envoi\" value=\"Mettre à jour\">";
	print "</p></form></body></html>";
	exit();
	}

//Passage 2 : traitement du formulaire
//------------------------------------
//POST a récupéré typemaj='cor' ou 'dis'
$fic=$_SESSION[$typemaj.'_fic']; //nom complet du fichier correctif ou distrib
$act=$_SESSION[$typemaj.'_act']; //version proposée par la maj

include("../include/pclzip.lib.php");
$flocal=copielocale($fic); //recopie le zip en local pour pouvoir le décompresser
//print "flocal=$flocal<br>";	//c:\temp\bla.zip
$liste=extraire_dans_gibii($flocal);
dbg('list');

lancer_maj(); //s'il n'y en a pas, passe à index2.php (menu sadmin)

?>
