<?php

/* Setting up settings.php for constants storage */

//common variables
$sDbServer = isset($_POST['DB_SERVER'])?$_POST['DB_SERVER']:'localhost';
$sDbLogin = isset($_POST['DB_LOGIN'])?$_POST['DB_LOGIN']:'root';
$sDbName = isset($_POST['DB_NAME'])?$_POST['DB_NAME']:'dbqcm';
$sFirstName = isset($_POST['firstname'])?$_POST['firstname']:'';
$sLastName = isset($_POST['lastname'])?$_POST['lastname']:'';
$sFeedBackMail = isset($_POST['FEEDBACK_MAIL'])?$_POST['FEEDBACK_MAIL']:'';
$iMaxClass = isset($_POST['MAX_CLASS'])?$_POST['MAX_CLASS']:8;
$iMinGrade = isset($_POST['MIN_GRADE'])?$_POST['MIN_GRADE']:6;
$iMaxGrade = isset($_POST['MAX_GRADE'])?$_POST['MAX_GRADE']:3;

//process
if(isset($_POST['READY_TO_USE']))
{
	if(file_exists(SETTINGS_FILE))
	{
		unlink(SETTINGS_FILE);
	}
	$oConnection = @mysqli_connect($sDbServer, $sDbLogin, $_POST['DB_PASS']);
	if(!$oConnection)
	{
		addMessage(getError(array('Connexion impossible', 'V&eacute;rifier les identifiants')));
	}
	elseif($sFirstName=='' || $sLastName=='')
	{
		addMessage(getError('Informations incompl&egrave;tes'));
	}
	elseif($_POST['pass']!=$_POST['pass2'])
	{
		addMessage(getError('Les mots de passes ne sont pas les m&ecirc;mes'));
	} 	
	else
	{
		$asAdminInfo = array(array($sFirstName, $sLastName, '0', '0', encryptPassword($_POST['pass']), 1));
		if(install($oConnection, $sDbName, $asAdminInfo, isset($_POST['delete_db'])))
		{	
			//setting Constants into file
			$asConstants = array(	'DB_SERVER' => $_POST['DB_SERVER'], 
									'DB_LOGIN' => $_POST['DB_LOGIN'], 
									'DB_PASS' => $_POST['DB_PASS'],
									'DB_NAME' => $_POST['DB_NAME'],
									'FEEDBACK_MAIL' => $_POST['FEEDBACK_MAIL'],
									'MAX_CLASS' => $_POST['MAX_CLASS'],
									'MIN_GRADE' => $_POST['MIN_GRADE'],
									'MAX_GRADE' => $_POST['MAX_GRADE'],
									'READY_TO_USE' => $_POST['READY_TO_USE']);
			$sSettingsFile = '<?php '."\n".implodeAll($asConstants, '\', \'', "\n", 'define(\'', '\');')."\n".' ?>';
			if(file_put_contents(SETTINGS_FILE, $sSettingsFile, FILE_APPEND))
			{
				relocate(getSuccess('Installation termin&eacute;e'));
			}
			else
			{
				addMessage(getError(array('Cr&eacute;ation de fichier impossible', 'V&eacute;rifier les droits sur le dossier principal')));
			}
		}
	}
}
else
{
	logMeOut(false);
}

if(file_exists(SETTINGS_FILE))
{
	$sDbServer = DB_SERVER;
	$sDbLogin = DB_LOGIN;
	$sDbName = DB_NAME;
	$sFeedBackMail = FEEDBACK_MAIL;
	$iMaxClass = MAX_CLASS;
	$iMinGrade = MIN_GRADE;
	$iMaxGrade = MAX_GRADE;
}

//TODO getHtmlTable()
?>
<p>Bienvenue dans QcmMaker v<?php echo STABLE_VERSION; ?> !</p>
<p>Veuillez entrer les informations suivantes : </p>
<form method="post" action="?page=install">
	<fieldset class="rounded">
		<legend class="rounded">Informations server</legend>
		<table class="rounded">
			<tr>
				<td class="top-left-rounded">Adresse de votre server</td>
				<td class="top-right-rounded"><input type="text" name="DB_SERVER" value="<?php echo $sDbServer; ?>" /></td>
			</tr>
			<tr>
				<td>login MySql</td>
				<td><input type="text" name="DB_LOGIN" value="<?php echo $sDbLogin; ?>" /></td>
			</tr>
			<tr>
				<td>Mot-de-passe MySql</td>
				<td><input type="password" name="DB_PASS" /></td>
			</tr>
			<tr>
				<td>Nom de la base de donn&eacute;es</td>
				<td><input type="text" name="DB_NAME" value="<?php echo $sDbName; ?>" onchange="replaceInnerText('db_name', this.value);" /></td>
			</tr>
			<tr>
				<td class="bottom-left-rounded">Supprimer la base de donn&eacute;es '<span id="db_name"><?php echo $sDbName; ?></span>' (si existante)</td>
				<td class="bottom-right-rounded"><input type="checkbox" name="delete_db" /></td>
			</tr>
		</table>
	</fieldset>
	<fieldset class="rounded">
		<legend class="rounded">Informations administrateur</legend>
		<table class="rounded">
			<tr><th colspan="2" class="top-left-rounded top-right-rounded">Cr&eacute;ation du compte administrateur</th></tr>
			<tr>
				<td>Pr&eacute;nom</td>
				<td><input type="text" name="firstname" value="<?php echo $sFirstName; ?>" /></td>
			</tr>
			<tr>
				<td>Nom de famille</td>
				<td><input type="text" name="lastname" value="<?php echo $sLastName; ?>" /></td>
			</tr>
			<tr>
				<td>Mot de passe</td>
				<td><input type="password" name="pass" /></td>
			</tr>
			<tr>
				<td>Mot de passe (une nouvelle fois)</td>
				<td><input type="password" name="pass2" /></td>
			</tr>
			<tr>
				<td class="bottom-left-rounded">Adresse email de votre administrateur (optionel)</td>
				<td class="bottom-right-rounded"><input type="text" name="FEEDBACK_MAIL" value="<?php echo $sFeedBackMail; ?>" /></td>
			</tr>
		</table>
	</fieldset>
	<fieldset class="rounded">
		<legend class="rounded">Param&egrave;tres suppl&eacute;mentaires</legend>
		<input type="hidden" name="READY_TO_USE" value="1" />
		<table class="rounded">
			<tr>
				<td class="top-left-rounded">Nombre de classes maximum par section</td>
				<td class="top-right-rounded left"><input type="text" name="MAX_CLASS" value="<?php echo $iMaxClass; ?>" /></td>
			</tr>
			<tr>
				<td>Premi&egrave;re section concern&eacute;e</td>
				<td class="left"><input type="text" name="MIN_GRADE" value="<?php echo $iMinGrade; ?>" />&egrave;me</td>
			</tr>
			<tr>
				<td class="bottom-left-rounded">Derni&egrave;re section concern&eacute;e</td>
				<td class="bottom-right-rounded left"><input type="text" name="MAX_GRADE" value="<?php echo $iMaxGrade; ?>" />&egrave;me</td>
			</tr>
		</table>
	</fieldset>
	<p style="text-align:center;"><input style="margin-top:10px;" type="submit" value ="Valider" /></p>
</form>
