<?php

error_reporting(E_ALL ^ E_NOTICE);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// HamLogWeb
//
// Par F5AGL - GRIP 2007-2008
//
// Email : f5agl@zapgillou.fr
//
// Cette création est mise à disposition selon le Contrat Paternité-Pas d'Utilisation Commerciale-Pas de Modification 2.0 France 
// disponible en ligne http://creativecommons.org/licenses/by-nc-nd/2.0/fr/ 
// ou par courrier postal à Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
// 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fichier : index.php 
//
// Description : Gestionnaire d'installation de HamLogWeb
//
// Paramètres : 
//	
//	- mode     : complet ou maj
//      - site_www : adresse http du site HamLogWeb 	(ex: http://toto.free.fr/F5AGL)
//      - url      : adresse du serveur 		(ex: toto.free.fr)
//  	- path     : chemin d'installation de HamLogWeb (ex : /F5AGL)
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$mode		= $_GET['mode'];

$site_www	= $_GET['site_www'];
$url		= $_GET['url'];
$path		= $_GET['path'];

$db_host	= $_GET['db_host'];
$db_login	= $_GET['db_login'];
$db_psw		= $_GET['db_psw'];
$db_base	= $_GET['db_base'];
$db_table	= $_GET['db_table'];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title>HamLogWeb - Installation</title>
</head>

<body>
<div style="text-align: center;"><b><span
 style="font-family: Comic Sans MS; color: rgb(153, 0, 0);"></span></b><big
 style="font-family: Comic Sans MS; font-weight: bold; color: rgb(0, 0, 153);"><big><big><big>HamLog</big></big></big></big><big
 style="font-family: Comic Sans MS; color: rgb(102, 102, 102); font-weight: bold;"><big><big><big>Web</big></big></big></big><span
 style="font-family: Comic Sans MS; font-weight: bold;"><span
 style="color: rgb(153, 0, 0);"><br>
par F5AGL</span></span>
<div style="text-align: left;"><span
 style="font-family: Comic Sans MS; font-weight: bold;"><span
 style="color: rgb(153, 0, 0);"><br>
</span></span>
<div style="text-align: center;"><span
 style="font-family: Comic Sans MS; font-weight: bold;"><span
 style="color: rgb(153, 0, 0);"><?php 
if ($mode=="complet") { echo "INSTALLATION COMPLETE / FULL INSTALL"; }
if ($mode=="maj")     { echo "MISE A JOUR D'UN SITE EXISTANT / UPDATING AN EXISTING WEB SITE"; }
?></span></span>



<?php

function rmdir_r($path) {
  if (!is_dir($path)) {return false;}
  $stack = Array($path);
  while ($dir = array_pop($stack)) {
    if (@rmdir($dir)) {continue;}
    $stack[] = $dir;
    $dh = opendir($dir);
    while (($child = readdir($dh)) !== false) {
      if ($child[0] == '.') {continue;}
      $child = $dir . DIRECTORY_SEPARATOR . $child;
      if (is_dir($child)) {$stack[] = $child;}
      else {unlink($child);}
    }
  }
  return true;
}

function full_copy( $source, $target )
    {
        if ( is_dir( $source ) )
        {
            if (!(is_dir($target))) { @mkdir( $target ); };
           
            $d = dir( $source );
           
            while ( FALSE !== ( $entry = $d->read() ) )
            {
                if ( $entry == '.' || $entry == '..' )
                {
                    continue;
                }
               
                $Entry = $source . '/' . $entry;           
                if ( is_dir( $Entry ) )
                {
                    full_copy( $Entry, $target . '/' . $entry );
                    continue;
                }
                copy( $Entry, $target . '/' . $entry );
            }
           
            $d->close();
        }else
        {
            copy( $source, $target );
        }
    }

// ajout du / final si manquant
if (!(substr($site_www,strlen($site_www)-1,1)=="/"))
	{
	$site_www=$site_www."/";
	}

// chez free.fr, il faut créer un répertoire sessions à la racine du site sinon bug de session_start utilisé dans button.php pour le livre d'or
if (!((strstr($url,'free.fr'))==false))
	{
	@mkdir('/sessions', 0766); 
	}

// Création du fichier configmysql.php dans le répertoire /HLWTEMP
if ($mode=="complet")
	{
	$fs=fopen("./configmysql.php","r");
	$fd=fopen("./configmysql.tmp","w");

	while (!(feof($fs)))
		{
		$ligneavant=$ligne;
		$ligne=fgets($fs,4096);
		$reste=strstr($ligne,"=");
		if (!($reste==false))
			{
			$nomvar=trim(substr($ligne,1,strlen($ligne)-strlen($reste)-1));
			//$vartest=$HTTP_POST_VARS[$nomvar];
			$vartest=${$nomvar};
			//echo "<BR>".trim(substr($ligne,1,strlen($ligne)-strlen($reste)-1))." => ".$vartest;
			if ($vartest=="")
				{
				fwrite($fd,$ligne);
				}
			else
				{		
				fwrite($fd,substr($ligne,0,strlen($ligne)-strlen($reste))."=\"".$vartest."\";\r\n");
				}
			}
		else
			{
			fwrite($fd,$ligne);
			}
		}

	fclose($fs);
	fclose($fd);
	unlink("./configmysql.php");
	rename("./configmysql.tmp","./configmysql.php");
	}

// SI MISE A JOUR : on récupère les fichiers data et répertoires data dans le répertoire /HLWTEMP car ensuite on va copier tout HLWTEMP dans le
// répertoire HamLogWeb

if ($mode=="maj")
	{
	copy("../blog/config/prefs.php", 	"./blog/config/prefs.php");
	copy("../blog/config/user.php",		"./blog/config/user.php");
	copy("../blog/themes/F5AGL/style.css",	"./blog/themes/F5AGL/style.css");
	copy("../compteur.hlw", 		"./compteur.hlw");
	copy("../configmysql.php", 		"./configmysql.php");
	copy("../menu.sup", 			"./menu.sup");
	@copy("../DXCC_Contactes.lst", 		"./DXCC_Contactes.lst");

	}

@include("./configmysql.php");


// le passage de paramètre avec un include ne fonctionne pas, d'où cette ruse...
$_GET = array();
$_GET['install'] = 'oui';
@include("./config.php");

// Copie de /HLWTEMP dans le répertoire HamLogWeb

full_copy("../HLWTEMP", "../");

// Connexion au serveur MySQL
$link = mysql_connect($db_host,$db_login,$db_psw) or die("_Impossible de se connecter au serveur MySQL : " . mysql_error());

// Connexion à la base MySQL
$db_selected = mysql_select_db($db_base, $link); if (!$db_selected) { die ('_Impossible de sélectionner la base de données : ' . mysql_error()); }

if ($mode=="complet")
	{
	
	$sql = 'DROP TABLE `'.$db_table.'_DXCC`, `'.$db_table.'_LIVREDOR`, `'.$db_table.'_LOG`, `'.$db_table.'_QSL`, `'.$db_table.'_LISTELOG`';
	$result = mysql_query($sql);

	// Génération table LOG MySQL

	$sql = 'CREATE TABLE `'.$db_table.'_LOG` (	`QSO_DATE` date NOT NULL default \'0000-00-00\',
  							`TIME_ON` time NOT NULL default \'00:00:00\',
  							`CALL` varchar(15) NOT NULL default \'\',
  							`BAND` varchar(15) NOT NULL default \'\',
  							`MODE` varchar(10) NOT NULL default \'\',
  							`DXCC` varchar(5) NOT NULL default \'\',
  							`QSL_SENT` varchar(5) NOT NULL default \'\',
							`QSL_RCVD` varchar(5) NOT NULL default \'\',
							`LOG` varchar(15) NOT NULL default \'\',
							`NAME` varchar(20) NOT NULL default \'\',
							`QTH` varchar(50) NOT NULL default \'\'
						 ) TYPE=MyISAM;';

	$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error();  die (''); }

	// Génération table LIVREDOR MySQL

	$sql = 'CREATE TABLE `'.$db_table.'_LIVREDOR` (	 	`NOM` varchar(40) NOT NULL default \'\',
  								`DATE` date NOT NULL default \'0000-00-00\',
 								`TIME` time NOT NULL default \'00:00:00\',
  								`TEXTE` longtext NOT NULL
							) TYPE=MyISAM;';

	$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error(); die (''); }

	// Génération table QSL MySQL

	$sql = 'CREATE TABLE `'.$db_table.'_QSL` (	`DXCC` varchar(5) NOT NULL default \'\',
  							`CALL` varchar(15) NOT NULL default \'\',
							`QSL_DATE` date NOT NULL default \'0000-00-00\',
							`QSL_TIME` time NOT NULL default \'00:00:00\',
  							`NOMFIC` varchar(60) NOT NULL default \'\'
						 ) TYPE=MyISAM;';

	$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error(); die (''); }

	// Génération table LISTELOG MySQL

	$sql = 'CREATE TABLE `'.$db_table.'_LISTELOG` (	`INDICATIF` varchar(15) NOT NULL default \'\',
  							`AFFDATE` varchar(1) NOT NULL default \'\',
							`AFFTIME` varchar(1) NOT NULL default \'\',
							`DESCRIPT` varchar(100) NOT NULL default \'\'
							 ) TYPE=MyISAM;';

	$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error(); die (''); }

	}





// Génération ou mise à jour table DXCC MySQL

$sql = 'DROP TABLE `'.$db_table.'_DXCC`';
$result = mysql_query($sql); 

$sql = 'CREATE TABLE `'.$db_table.'_DXCC` (	`NUM` varchar(5) NOT NULL default \'\',
 						`UK` varchar(50) NOT NULL default \'\',
 						`ALL` varchar(4) NOT NULL default \'\',
 						`160M` varchar(4) NOT NULL default \'\',
 						`80M` varchar(4) NOT NULL default \'\',
 						`40M` varchar(4) NOT NULL default \'\',
 						`30M` varchar(4) NOT NULL default \'\',
 						`20M` varchar(4) NOT NULL default \'\',
 						`17M` varchar(4) NOT NULL default \'\',
 						`15M` varchar(4) NOT NULL default \'\',
 						`12M` varchar(4) NOT NULL default \'\',
 						`10M` varchar(4) NOT NULL default \'\',
 						`6M` varchar(4) NOT NULL default \'\',
 						`2M` varchar(4) NOT NULL default \'\'
					) TYPE=MyISAM;';

$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error(); }

$fs=fopen("./admin/dxcc.hlw","r");

while (!(feof($fs)))
	{
	$ligne		=fgets($fs,4096);
	$contree	=strstr($ligne, '@'); 
	$code		=substr($ligne,0,strlen($ligne)-strlen($contree));
	$contree	=trim(substr($contree,1,strlen($contree)-1));
	$sql		='INSERT INTO `'.$db_table.'_DXCC` VALUES (\''.$code.'\', \''.addslashes($contree).'\', \'----\', \'----\', \'----\', \'----\', \'----\', \'----\', \'----\', \'----\', \'----\', \'----\', \'----\', \'----\' );';
	$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error(); die (''); }
	}

fclose($fs);

// Chargement de la table par défaut CONFIG (table temporaire) - dans tous les cas maj ou complet

$sql = 'DROP TABLE `CONFIG`';
$result = mysql_query($sql); 

$fs=fopen("./CONFIG.sql","r");

while (!(feof($fs)))
	{
	$sql		=fgets($fs,4096);
	$result = mysql_query($sql); 
	}

fclose($fs);

// Si mise à jour alors on récupère les données existantes sur le site

if ($mode=="maj")
	{

	$sql = 'SELECT * FROM `CONFIG`';
	$result = mysql_query($sql); // if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error(); }

	while ($ligne=mysql_fetch_row($result))
		{
		$sql = 'SELECT * FROM `'.$db_table.'_CONFIG` WHERE `NOMVAR` LIKE \''.$ligne[1].'\'';
		$result1 = mysql_query($sql);
		if ($ligneancien=mysql_fetch_row($result1))
			{
			$sql = 'UPDATE `CONFIG` SET `VALEUR` = \''.addslashes($ligneancien[3]).'\', `OPTION2` = \''.addslashes($ligneancien[5]).'\', `OPTION3` = \''.addslashes($ligneancien[6]).'\', `OPTION4` = \''.addslashes($ligneancien[7]).'\', `OPTION5` = \''.addslashes($ligneancien[8]).'\', `ADMINFR` = \''.addslashes($ligne[9]).'\', `ADMINUK` = \''.addslashes($ligne[10]).'\', `ADMIN03` = \''.addslashes($ligne[11]).'\', `ADMIN04` = \''.addslashes($ligne[12]).'\', `ADMIN05` = \''.addslashes($ligne[13]).'\', `ADMIN06` = \''.addslashes($ligne[14]).'\' WHERE `NOMVAR` = \''.addslashes($ligne[1]).'\' LIMIT 1;';
			$result2 = mysql_query($sql); // if (!$result2) { echo "<BR>_Message système MySQL : " . mysql_error(); }	
			}
		}

	$sql = 'DROP TABLE `'.$db_table.'_CONFIG`';
	$result = mysql_query($sql); 
	}

$sql = 'ALTER TABLE `CONFIG` RENAME `'.$db_table.'_CONFIG`';;
$result = mysql_query($sql); 

$sql = 'UPDATE `'.$db_table.'_CONFIG` SET `VALEUR` = \''.$site_www.'\' WHERE `'.$db_table.'_CONFIG`.`NOMVAR` = \'site_www\' ';
$result = mysql_query($sql); 

$sql = 'UPDATE `'.$db_table.'_CONFIG` SET `VALEUR` = \''.$url.'\' WHERE `'.$db_table.'_CONFIG`.`NOMVAR` = \'url\' ';
$result = mysql_query($sql); 

$sql = 'UPDATE `'.$db_table.'_CONFIG` SET `VALEUR` = \''.$path.'\' WHERE `'.$db_table.'_CONFIG`.`NOMVAR` = \'path\' ';
$result = mysql_query($sql); 

$sql = 'SELECT * FROM `'.$db_table.'_CONFIG` WHERE `NOMVAR` LIKE \'progver\''; 

$result = @mysql_query($sql); 

$ligne=@mysql_fetch_row($result);

$ancienne_version=$ligne[3];

// ----------------------------------------------------------------------------------------------------------------------------
// ---------- Mise à jour depuis version < 0.035 ------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------


if (($ancienne_version<=34) and ($mode=="maj"))
	{
	// Génération table LISTELOG MySQL

	$sql = 'CREATE TABLE `'.$db_table.'_LISTELOG` (	`INDICATIF` varchar(15) NOT NULL default \'\',
  							`AFFDATE` varchar(1) NOT NULL default \'\',
							`AFFTIME` varchar(1) NOT NULL default \'\',
							`DESCRIPT` varchar(100) NOT NULL default \'\'
							 ) TYPE=MyISAM;';

	$result = @mysql_query($sql); 


	// Génération table LOG MySQL

	$sql = 'DROP TABLE `'.$db_table.'_LOG`';
	$result = mysql_query($sql);

	$sql = 'CREATE TABLE `'.$db_table.'_LOG` (	`QSO_DATE` date NOT NULL default \'0000-00-00\',
  							`TIME_ON` time NOT NULL default \'00:00:00\',
  							`CALL` varchar(15) NOT NULL default \'\',
  							`BAND` varchar(15) NOT NULL default \'\',
  							`MODE` varchar(10) NOT NULL default \'\',
  							`DXCC` varchar(5) NOT NULL default \'\',
  							`QSL_SENT` varchar(5) NOT NULL default \'\',
							`QSL_RCVD` varchar(5) NOT NULL default \'\',
							`LOG` varchar(15) NOT NULL default \'\',
							`NAME` varchar(20) NOT NULL default \'\',
							`QTH` varchar(20) NOT NULL default \'\'
						 ) TYPE=MyISAM;';

	$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error();  die (''); }

	// Génération table QSL MySQL


	$sql = 'DROP TABLE `'.$db_table.'_QSL`';
	$result = mysql_query($sql);

	$sql = 'CREATE TABLE `'.$db_table.'_QSL` (	`DXCC` varchar(5) NOT NULL default \'\',
  							`CALL` varchar(15) NOT NULL default \'\',
							`QSL_DATE` date NOT NULL default \'0000-00-00\',
							`QSL_TIME` time NOT NULL default \'00:00:00\',
  							`NOMFIC` varchar(60) NOT NULL default \'\'
						 ) TYPE=MyISAM;';

	$result = mysql_query($sql); if (!$result) { echo "<BR>_Message système MySQL : " . mysql_error(); die (''); }


	}

// ----------------------------------------------------------------------------------------------------------------------------
// ---------- Mise à jour depuis version < 0.039 ------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------

if (($ancienne_version<=38) and ($mode=="maj"))
	{
	// Passage du champs QTH de la table log à 50 caractères max au lieu de 20
	$sql = 'ALTER TABLE `'.$db_table.'_LOG` CHANGE `QTH` `QTH` VARCHAR(50)'; 
	$result = mysql_query($sql);
	}

$sql = 'UPDATE `'.$db_table.'_CONFIG` SET `VALEUR` = \''.$Prog_Ver.'\' WHERE `'.$db_table.'_CONFIG`.`NOMVAR` = \'progver\' ';

$result = mysql_query($sql); 


// fermeture session sur le serveur MySQL
mysql_close($link);

// Effacement de index.php, install.php, do_install.php et on renomme le fichier index.php final

unlink("../index.php");
unlink("../do_install.php");
unlink("../install.php");
rename("../index.temp","../index.php");

// Effacement de HLWTEMP 

// rmdir_r("../HLWTEMP");

// Affichage récapitulatif login / psw CDC et login / psw BLOG
?>

<div
 style="text-align: center; font-family: Comic Sans MS; font-weight: bold; color: rgb(0, 102, 0);"><span
 style="color: rgb(0, 153, 0);">INSTALLATION TERMINEE / INSTALL DONE<br>
<br>
</span>
<div style="text-align: left; margin-left: 40px;"><span
 style="color: rgb(0, 153, 0);"><span
 style="color: rgb(102, 0, 0);"><span
 style="color: rgb(0, 0, 0);">Il est recommand&eacute;
d'imprimer cette page. / Please print this page.</span><br style="color: rgb(0, 0, 0);">
<br style="color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0); font-weight: normal;"></span></span></span>
<table
 style="text-align: left; background-color: rgb(102, 102, 102); width: 70%;"
 border="1" cellpadding="0" cellspacing="1">
  <tbody>
    <tr>
      <td
 style="font-family: Helvetica,Arial,sans-serif; font-weight: bold; background-color: rgb(153, 153, 153);">Adresse
de votre HamLogWeb - Your HamLogWeb address</td>
      <td
 style="width: 40%; font-family: Helvetica,Arial,sans-serif; background-color: rgb(153, 153, 153);">
      <div style="margin-left: 40px;"><?php echo $site_www; ?></div>
      </td>
    </tr>
    <tr>
      <td
 style="font-family: Helvetica,Arial,sans-serif; font-weight: bold; background-color: rgb(153, 153, 153);">Centre
de configuration - Adresse / Center of Configuration - Address</td>
      <td
 style="font-family: Helvetica,Arial,sans-serif; background-color: rgb(153, 153, 153);">
      <div style="margin-left: 40px;"><?php echo $site_www."admin"; ?></div>
      </td>
    </tr>
    <tr>
      <td
 style="width: 30%; font-family: Helvetica,Arial,sans-serif; font-weight: bold; background-color: rgb(153, 153, 153);">Centre
de configuration - Mot de passe / Center of configuration - Password</td>
      <td
 style="font-family: Helvetica,Arial,sans-serif; background-color: rgb(153, 153, 153);">
      <div style="margin-left: 40px;"><?php echo $popeye; ?></div>
      </td>
    </tr>
<?php if ($mode=="complet")
{ ?>
    <tr>
      <td
 style="width: 30%; font-family: Helvetica,Arial,sans-serif; font-weight: bold; background-color: rgb(153, 153, 153);">Blog
- Login</td>
      <td
 style="font-family: Helvetica,Arial,sans-serif; background-color: rgb(153, 153, 153);">
      <div style="margin-left: 40px;">user</div>
      </td>
    </tr>
    <tr>
      <td
 style="width: 30%; font-family: Helvetica,Arial,sans-serif; font-weight: bold; background-color: rgb(153, 153, 153);">Blog
- Mot de passe / Password</td>
      <td
 style="font-family: Helvetica,Arial,sans-serif; background-color: rgb(153, 153, 153);">
      <div style="margin-left: 40px;">password</div>
      </td>
    </tr>
<?php } ?>
  </tbody>
</table>
<br>
<span style="color: rgb(0, 0, 0); font-weight: normal;">Lisez
la documentation. / Read documentation.</span><br
 style="color: rgb(0, 0, 0); font-weight: normal;">
<br style="color: rgb(0, 0, 0); font-weight: normal;">
<span style="color: rgb(0, 0, 0); font-weight: normal;">Et
rendez-vous sur <a href="http://www.zapgillou.fr/hamlogweb/">http://www.zapgillou.fr/hamlogweb/</a>
pour faire part de vos interrogations, remarques, critiques,
propositions d'&eacute;volutions, etc...<br>
<br style="color: rgb(0, 0, 0); font-weight: normal;">
<span style="color: rgb(0, 0, 0); font-weight: normal;">You are welcome on <a href="http://www.zapgillou.fr/hamlogweb/">http://www.zapgillou.fr/hamlogweb/</a>
in order to tell us your questions, bugs reports, etc...<br>
<br>
73 QRO,<br>
<br>
Gilles, F5AGL.</span><span style="color: rgb(0, 153, 0);"><span
 style="color: rgb(102, 0, 0);"><span
 style="color: rgb(0, 0, 0); font-weight: normal;"></span></span></span><span
 style="color: rgb(0, 153, 0);"></span></div>
</div>


<hr style="width: 100%; height: 2px;">
<div style="text-align: center;"><small><span
 style="font-family: Comic Sans MS; font-weight: bold;"><span
 style="color: rgb(0, 0, 153);">HamLog</span><span
 style="color: rgb(102, 102, 102);">Web</span></span>&nbsp;-
par <a href="mailto:hamweb@zapgillou.fr">F5AGL</a><br>
Distribu&eacute; sous&nbsp;<a
 href="http://creativecommons.org/licenses/by-nc-nd/2.0/fr/deed.en">Creative
Commons Licence</a><br>
<small><small><small><small><small><small>&nbsp;</small></small></small></small></small></small></small></div>
<div style="text-align: center;">
<a href="http://www.php.net/"><img src="./img/php.png"
 style="border: 0pt none ;"></a> <a
 href="http://www.nvu.com/"><img src="./img/nvu.gif"
 style="border: 0pt none ;"></a> <a
 href="http://www.getfirefox.com/"><img
 src="./img/firefox.gif" style="border: 0pt none ;"></a>
<a rel="license"
 href="http://creativecommons.org/licenses/by-nc-nd/2.0/fr/"><img
 alt="Creative Commons License" style="border-width: 0pt;"
 src="http://i.creativecommons.org/l/by-nc-nd/2.0/fr/80x15.png"></a></div>
</body>
</html>

		