<?php

/************************************************************************/
/* Eleonline                                                            */
/* ============================================                         */
/*  Installer was based on Joomla Installer                             */
/************************************************************************/

error_reporting (E_ALL); 

// Set flag that this is a parent file
define( "_VALID_MOS", 1 );

// Include common.php
require_once( 'common.php' );
require_once( './includes/database.php' );

$DBhostname = mosGetParam( $_POST, 'DBhostname', '' );
$DBuserName = mosGetParam( $_POST, 'DBuserName', '' );
$DBpassword = mosGetParam( $_POST, 'DBpassword', '' );
$DBname  	= mosGetParam( $_POST, 'DBname', '' );
$DBPrefix  	= mosGetParam( $_POST, 'DBPrefix', 'soraldo' );

$sitename  	= mosGetParam( $_POST, 'sitename', '' );
$nomecomune = mosGetParam( $_POST, 'nomecomune', '' ) ;
$istat =  mosGetParam( $_POST, 'istat', '' ) ;
$Capoluogo = mosGetParam( $_POST, 'Capoluogo', '' );
$Lingua = mosGetParam( $_POST, 'Lingua', '' );
$Multicomune = mosGetParam( $_POST, 'Multicomune', '' );
$Replica = mosGetParam( $_POST, 'Replica', '' );

//tema
$tema = mosGetParam( $_POST, 'tema', '' );
$sceltatema = mosGetParam( $_POST, 'sceltatema', '' );
$blocco = mosGetParam( $_POST, 'blocco', '' );
$flash = mosGetParam( $_POST, 'flash', '' );
// d'hondt

$Limite = mosGetParam( $_POST, 'Limite', '' );


$adminName = mosGetParam( $_POST, 'adminName', '');
$adminEmail = mosGetParam( $_POST, 'adminEmail', '');
$CreateUSer = intval( mosGetParam( $_POST, 'CreateUSer', '' ) );
$siteUrl  	= mosGetParam( $_POST, 'siteUrl', '' );
$absolutePath = mosGetParam( $_POST, 'absolutePath', '' );
$adminPassword = mosGetParam( $_POST, 'adminPassword', '');

if ((trim($adminEmail== "")) || (preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $adminEmail )==false)) {

	echo "<form name=\"stepBack\" method=\"post\" action=\"install3.php\">
		<input type=\"hidden\" name=\"DBhostname\" value=\"$DBhostname\" />
		<input type=\"hidden\" name=\"DBuserName\" value=\"$DBuserName\" />
		<input type=\"hidden\" name=\"DBpassword\" value=\"$DBpassword\" />
		<input type=\"hidden\" name=\"DBname\" value=\"$DBname\" />
		<input type=\"hidden\" name=\"DBPrefix\" value=\"$DBPrefix\" />	
	
		<input type=\"hidden\" name=\"DBcreated\" value=\"1\" />
		<input type=\"hidden\" name=\"sitename\" value=\"$sitename\" />";


	echo "	<input type=\"hidden\" name=\"nomecomune\" value=\"$nomecomune\" />
		<input type=\"hidden\" name=\"istat\" value=\"$istat\"/>
		<input type=\"hidden\" name=\"Capoluogo\" value= \"$Capoluogo\" />
		<input type=\"hidden\" name=\"Lingua\" value= \"$Lingua\" />
		<input type=\"hidden\" name=\"Multicomune\" value=\"$Multicomune\" />
		<input type=\"hidden\" name=\"Replica\" value=\"$Replica\" />
		
		<input type=\"hidden\" name=\"tema\" value=\"$tema\" />
		<input type=\"hidden\" name=\"sceltatema\" value=\"$sceltatema\" />
		<input type=\"hidden\" name=\"blocco\" value=\"$blocco\" />
		<input type=\"hidden\" name=\"flash\" value= \"$flash\" />
		
		
		<input type=\"hidden\" name=\"Limite\" value=\"$Linite\" />";
		
				
		echo "Indirizzo Email non valido";
		
		
	echo "	<input type=\"hidden\" name=\"adminName\" value=\"$adminName\" />
		<input type=\"hidden\" name=\"adminEmail\" value=\"$adminEmail\" />
		
		<input type=\"hidden\" name=\"siteUrl\" value=\"$siteUrl\" />
		<input type=\"hidden\" name=\"absolutePath\" value=\"$absolutePath\" />
		</form>";
	echo "<script>alert('Devi inserire un indirizzo email valido.'); document.stepBack.submit(); </script>";
	return;
}

if($DBhostname && $DBuserName && $DBname) {
	$configArray['DBhostname']	= $DBhostname;
	$configArray['DBuserName']	= $DBuserName;
	$configArray['DBpassword']	= $DBpassword;
	$configArray['DBname']	 	= $DBname;
	$configArray['DBPrefix']	= $DBPrefix;
} else {
	echo "<form name=\"stepBack\" method=\"post\" action=\"install3.php\">
		<input type=\"hidden\" name=\"DBhostname\" value=\"$DBhostname\" />
		<input type=\"hidden\" name=\"DBuserName\" value=\"$DBuserName\" />
		<input type=\"hidden\" name=\"DBpassword\" value=\"$DBpassword\" />
		<input type=\"hidden\" name=\"DBname\" value=\"$DBname\" />
		<input type=\"hidden\" name=\"DBPrefix\" value=\"$DBPrefix\" />
		
		<input type=\"hidden\" name=\"DBcreated\" value=\"1\" />
		<input type=\"hidden\" name=\"sitename\" value=\"$sitename\" />";
		
	echo '	<input type="hidden" name="nomecomune" value="<?php echo "$nomecomune"; ?>" />
		<input type="hidden" name="istat" value="<?php echo "$istat"; ?>" />
		<input type="hidden" name="Capoluogo" value="<?php echo "$Capoluogo"; ?>" />
		<input type="hidden" name="Lingua" value="<?php echo "$Lingua"; ?>" />
		<input type="hidden" name="Multicomune" value="<?php echo "$Multicomune"; ?>" />
		<input type="hidden" name="Replica" value="<?php echo "$Replica"; ?>" />
		
		<input type="hidden" name="tema" value="<?php echo "$tema"; ?>" />
		<input type="hidden" name="sceltatema" value="<?php echo "$sceltatema"; ?>" />
		<input type="hidden" name="blocco" value="<?php echo "$blocco"; ?>" />
		<input type="hidden" name="flash" value="<?php echo "$flash"; ?>" />
		
		
		<input type="hidden" name="Limite" value="<?php echo "$Linite"; ?>" />';
		
					
		
		
		
	echo "	<input type=\"hidden\" name=\"adminName\" value=\"$adminName\" />
		<input type=\"hidden\" name=\"adminEmail\" value=\"$adminEmail\" />
		
		<input type=\"hidden\" name=\"siteUrl\" value=\"$siteUrl\" />
		<input type=\"hidden\" name=\"absolutePath\" value=\"$absolutePath\" />
		</form>";

	echo "<script>alert('The database details provided are incorrect and/or empty'); document.stepBack.submit(); </script>";
	return;
}

if ($sitename) {
	if (!get_magic_quotes_gpc()) {
		$configArray['sitename'] = addslashes($sitename);
	} else {
		$configArray['sitename'] = $sitename;
	}
} else {
	echo "<form name=\"stepBack\" method=\"post\" action=\"install3.php\">
		<input type=\"hidden\" name=\"DBhostname\" value=\"$DBhostname\" />
		<input type=\"hidden\" name=\"DBuserName\" value=\"$DBuserName\" />
		<input type=\"hidden\" name=\"DBpassword\" value=\"$DBpassword\" />
		<input type=\"hidden\" name=\"DBname\" value=\"$DBname\" />
		<input type=\"hidden\" name=\"DBPrefix\" value=\"$DBPrefix\" />
		
		<input type=\"hidden\" name=\"DBcreated\" value=\"1\" />
		<input type=\"hidden\" name=\"sitename\" value=\"$sitename\" />";
		
	echo '	<input type="hidden" name="nomecomune" value="<?php echo "$nomecomune"; ?>" />
		<input type="hidden" name="istat" value="<?php echo "$istat"; ?>" />
		<input type="hidden" name="Lingua" value="<?php echo "$Lingua"; ?>" />
		<input type="hidden" name="Capoluogo" value="<?php echo "$Capoluogo"; ?>" />
		<input type="hidden" name="Multicomune" value="<?php echo "$Multicomune"; ?>" />
		<input type="hidden" name="Replica" value="<?php echo "$Replica"; ?>" />
		
		<input type="hidden" name="tema" value="<?php echo "$tema"; ?>" />
		<input type="hidden" name="sceltatema" value="<?php echo "$sceltatema"; ?>" />
		<input type="hidden" name="blocco" value="<?php echo "$blocco"; ?>" />
		<input type="hidden" name="flash" value="<?php echo "$flash"; ?>" />
		
		
		<input type="hidden" name="Limite" value="<?php echo "$Linite"; ?>" />
		';
				
		
	echo "	<input type=\"hidden\" name=\"adminName\" value=\"$adminName\" />
		<input type=\"hidden\" name=\"adminEmail\" value=\"$adminEmail\" />
		
		<input type=\"hidden\" name=\"siteUrl\" value=\"$siteUrl\" />
		<input type=\"hidden\" name=\"absolutePath\" value=\"$absolutePath\" />
		</form>";

	echo "<script>alert('The sitename has not been provided'); document.stepBack2.submit();</script>";
	return;
}

if (file_exists( '../client/config.php' )) {
	$canWrite = is_writable( '../client/config.php' );
} else {
	$canWrite = is_writable( '../client' );
}

// admin
if (file_exists( '../client/config.php' )) {
	$canWrite2 = is_writable( '../admin/config.php' );
} else {
	$canWrite2 = is_writable( '../admin' );
}



if ($siteUrl) {
	$configArray['siteUrl']=$siteUrl;
	// Fix for Windows
	$absolutePath= str_replace("\\\\","/", $absolutePath);
	$configArray['absolutePath']=$absolutePath;


#########################################################################
# Config visualizzazione
$config = "<?php\n";
$config .= "\n";

$config .= "/************************************************************************/\n";
$config .= "/* Eleonline - Raccolta e diffusione dei dati elettorali                */\n";
$config .= "/* by Roberto Gigli & Luciano Apolito                                   */\n";
$config .= "/* http://www.eleonline.it                                              */\n";
$config .= "/* info@eleonline.it  luciano@aniene.net rgigli@libero.it               */\n";
$config .= "/************************************************************************/\n";
$config .= "\n";
$config .= "\n";
$config .= "if (stristr(\"config.php\",\$_SERVER['PHP_SELF'])) {\n";
$config .= "        Header(\"Location: index.php\");\n";
$config .= "	die();\n";
$config .= "}\n";
$config .= "\n";
$config .= "\n";
$config .= "/* Variabili di configurazione accesso db */\n";
$config .= "\$dbhost = \"{$configArray['DBhostname']}\";\n";
$config .= "\$dbuname = \"{$configArray['DBuserName']}\";\n";
$config .= "\$dbpass = \"{$configArray['DBpassword']}\";\n";
$config .= "\$dbname = \"{$configArray['DBname']}\";\n";
$config .= "\$prefix = \"{$configArray['DBPrefix']}\";\n";
$config .= "\$dbtype = \"MySQL\";\n";




$config .= "\n";
$config .= "ini_set('display_errors',0);\n";
$config .= "?>\n";


#####################################################################################
# config amministrazione
#####################################################################################

$config_adm = "<?php\n";
$config_adm .= "\n";

$config_adm .= "/************************************************************************/\n";
$config_adm .= "/* Eleonline - Raccolta e diffusione dei dati elettorali                */\n";
$config_adm .= "/* by Roberto Gigli & Luciano Apolito                                   */\n";
$config_adm .= "/* http://www.eleonline.it                                              */\n";
$config_adm .= "/* info@eleonline.it  luciano@aniene.net rgigli@libero.it               */\n";
$config_adm .= "/************************************************************************/\n";
$config_adm .= "\n";
$config_adm .= "\n";
$config_adm .= "if (stristr(\"config.php\",\$_SERVER['PHP_SELF'])) {\n";
$config_adm .= "        Header(\"Location: index.php\");\n";
$config_adm .= "	die();\n";
$config_adm .= "}\n";
$config_adm .= "\n";
$config_adm .= "\n";
$config_adm .= "\n";
$config_adm .= "\$dbhost = \"{$configArray['DBhostname']}\";\n";
$config_adm .= "\$dbuname = \"{$configArray['DBuserName']}\";\n";
$config_adm .= "\$dbpass = \"{$configArray['DBpassword']}\";\n";
$config_adm .= "\$dbname = \"{$configArray['DBname']}\";\n";
$config_adm .= "\$prefix = \"{$configArray['DBPrefix']}\";\n";
$config_adm .= "\$dbtype = \"MySQL\";\n";
$config_adm .= "ini_set('display_errors',0);\n";
$config_adm .= "?>\n";











	if ($canWrite && ($fp = fopen("../client/config.php", "w"))) {
		fputs( $fp, $config, strlen( $config ) );
		fclose( $fp );
	} else {
		$canWrite = false;
	}
	
	if ($canWrite2 && ($fp = fopen("../admin/config.php", "w"))) {
		fputs( $fp, $config_adm, strlen( $config_adm ) );
		fclose( $fp );
	} else {
		$canWrite2 = false;
	}




	$database = new database( $DBhostname, $DBuserName, $DBpassword, $DBname );
	$nullDate = $database->getNullDate();

	// create the admin user
	$cryptpass = md5( $adminPassword );
	//$query = "INSERT INTO {$configArray['DBPrefix']}_authors VALUES ('$adminName', '$adminName', '0', '$adminEmail', '$cryptpass','1','0','0','1','it')";
	$query = "UPDATE {$configArray['DBPrefix']}_authors SET aid='$adminName', name='$adminName', email='$adminEmail', pwd= '$cryptpass' WHERE aid='suser'";
	$database->setQuery( $query );
	$database->query();
	
	// create Comune
	
	$query = "INSERT INTO {$configArray['DBPrefix']}_ele_comuni VALUES ('$istat', '$nomecomune', '','','','','$Limite','$Capoluogo','','','')";
	
	$database->setQuery( $query );
	$database->query();
	
	$query = "UPDATE {$configArray['DBPrefix']}_config SET sitename='$sitename', siteurl='$siteUrl', adminmail='$adminEmail', tema='$tema', foot='',language='$Lingua',blocco='$blocco',fileout='$Replica',Versione='2.0 beta', siteistat='$istat',multicomune='$Multicomune',flash='$flash', tema_on='$sceltatema'   WHERE sitename=''";
	

	$database->setQuery( $query );
	$database->query();


	// touch config table
	$date = date("F Y");
	$query = "UPDATE {$configArray['DBPrefix']}_config SET sitename='$sitename', nukeurl='$siteUrl', startdate='$date', adminmail='$adminEmail', backend_title='$sitename', notify_email='$adminEmail'";
	$database->setQuery( $query );
	$database->query();

} else {
?>
	<form action="install3.php" method="post" name="stepBack3" id="stepBack3">
	  <input type="hidden" name="DBhostname" value="<?php echo $DBhostname;?>" />
	  <input type="hidden" name="DBusername" value="<?php echo $DBuserName;?>" />
	  <input type="hidden" name="DBpassword" value="<?php echo $DBpassword;?>" />
	  <input type="hidden" name="DBname" value="<?php echo $DBname;?>" />
	  <input type="hidden" name="DBcreated" value="1" />
	  <input type="hidden" name="sitename" value="<?php echo $sitename;?>" />
	  <input type="hidden" name="adminName" value="$adminName" />
	  <input type="hidden" name="adminEmail" value="$adminEmail" />
	  <input type="hidden" name="CreateUSer" value="$CreateUSer" />
	  <input type="hidden" name="siteUrl" value="$siteUrl" />
	  <input type="hidden" name="absolutePath" value="$absolutePath" />
	  <?php 
	  echo '<input type="hidden" name="nomecomune" value="<?php echo "$nomecomune"; ?>" />
		<input type="hidden" name="istat" value="<?php echo "$istat"; ?>" />
		<input type="hidden" name="Capoluogo" value="<?php echo "$Capoluogo"; ?>" />
		<input type="hidden" name="Lingua" value="<?php echo "$Lingua"; ?>" />
		<input type="hidden" name="Multicomune" value="<?php echo "$Multicomune"; ?>" />
		<input type="hidden" name="Replica" value="<?php echo "$Replica"; ?>" />
		
		<input type="hidden" name="tema" value="<?php echo "$tema"; ?>" />
		<input type="hidden" name="sceltatema" value="<?php echo "$sceltatema"; ?>" />
		<input type="hidden" name="blocco" value="<?php echo "$blocco"; ?>" />
		<input type="hidden" name="flash" value="<?php echo "$flash"; ?>" />
		
		<input type="hidden" name="Limite" value="<?php echo "$Linite"; ?>" />
		';
	?>			
	</form>
	<script>alert('The site url has not been provided'); document.stepBack3.submit();</script>
<?php
}
include("header.php");
?>
<div id="ctr" align="center">
	<form action="dummy" name="form" id="form">
	<div class="install">
		<div id="stepbar">
			<div class="step-off">inizio</div>
			<div class="step-off">licenza</div>
			<div class="step-off">passo 1</div>
			<div class="step-off">passo 2</div>
			<div class="step-off">passo 3</div>
			<div class="step-on">passo 4</div>
		</div>
		<div id="right">
			<div id="step">passo 4</div>
			<div class="far-right">
				<input class="button" type="button" name="runSite" value="Guarda il Sito"
<?php
				if ($siteUrl) {
					echo "onClick=\"window.location.href='$siteUrl/index.php' \"";
				} else {
					echo "onClick=\"window.location.href='".$configArray['siteURL']."/index.php' \"";
				}
?>/>
				<input class="button" type="button" name="Admin" value="Amministrazione"
<?php
				if ($siteUrl) {
					echo "onClick=\"window.location.href='$siteUrl/../admin' \"";
				} else {
					echo "onClick=\"window.location.href='".$configArray['siteURL']."/../admin' \"";
				}
?>/>
			</div>
			<div class="clr"></div>
			<h1>Congratulazioni! EleOnLine e' installato</h1>
			<div class="install-text">
				<p>Clicca sul pulsante "Guarda il Sito" per visitare la parte in Visualizzazione oppure su "Amministrazione" per entrare nel pannello di ammnistrazione.</p>
			</div>
			<div class="install-form">
				<div class="form-block">
					<table width="100%">
						<tr><td class="error" align="center">Una volta configurato e testato il sistema<br/>
e prima di avviare il servizio online<br/>
ricordati di effettuare una politica di sicurezza<br/>
come la separazione della parte di visualizzazione (client)<br/>
da quella di amministrazione (admin)<br/>
od anche l'utenza del database pubblico in sola lettura<br/> 
e altri accorgimenti che riterrai necessarie<br/>



</td></tr>
						<tr><td align="center"><h5>Dettagli Login Amministrazione</h5></td></tr>
						<tr><td align="center" class="notice"><b>Username : <?php echo $adminName; ?></b></td></tr>
						<tr><td align="center" class="notice"><b>Password : <?php echo $adminPassword; ?></b></td></tr>
						<tr><td>&nbsp;</td></tr>
						<tr><td align="right">&nbsp;</td></tr>
<!-- client -->						
<?php						if (!$canWrite) { ?>
						<tr>
							<td class="small">
							<h1>config.php per visualizzazione</h1>
							<font color="#FF0000"><b>ATTENZIONE:</b></font> Il tuo file di configurazione o la cartella non sono scrivibili,
								o c'è stato un problema nella creazione del file di configurazione. Clicca nella area di testo per evidenziare
								tutto il codice. Crea un nuovo file chiamato <b>config.php</b> e copialo nella cartella <b>client</b>.
							</td>
						</tr>
						
						
						<tr>
							<td align="center">
								<textarea rows="15" cols="48" name="configcode" onClick="javascript:this.form.configcode.focus();this.form.configcode.select();" ><?php echo htmlspecialchars( $config );?></textarea>
							</td>
						</tr>
<?php						} ?>

<!-- admin -->
<?php						if (!$canWrite2) { ?>
						<tr>
							<td class="small">
							<h1>config.php per amministrazione</h1>
							<font color="#FF0000"><b>ATTENZIONE:</b></font> Il tuo file di configurazione o la cartella non sono scrivibili,
								o c'è stato un problema nella creazione del file di configurazione. Clicca nella area di testo per evidenziare
								tutto il codice. Crea un nuovo file chiamato <strong>config.php</strong> e copialo nella cartella <b>admin</b>.
							</td>
						</tr>
						
						
						<tr>
							<td align="center">
								<textarea rows="15" cols="48" name="configcode" onClick="javascript:this.form.configcode.focus();this.form.configcode.select();" ><?php echo htmlspecialchars( $config_adm );?></textarea>
							</td>
						</tr>
<?php						} ?>




						<tr><td class="small"><?php /*echo $chmod_report*/; ?></td></tr>
					</table>
				</div>
			</div>
			<div id="break"></div>
		</div>
		<div class="clr"></div>
	</div>
	</form>
</div>
<div class="clr"></div>
<div class="ctr">
	
</div>
<?php include("footer.php"); ?>
</div>
</html>
