<?php

////////////////////////////////// version actual (la que se va a instalar).
$currentversion = '2013.1';

error_reporting (0);

///////////////////////////////// verificar si es una instalacion nueva o una actualizaci�n.

/// leer archivo config.php
if (file_exists('../config.php')) {
	// si existe config, verificar que tenga contenido
	$contenidodeconfig = file_get_contents('../config.php');
	if (preg_match('/db__name/i',$contenidodeconfig)){
		// es un update
		include_once('../config.php');

		$db_link=@mysql_connect($db__server,$db__user,$db__password);
		if(!@mysql_select_db($db__name,$db_link)){
			echo "ERROR 11<br>
			<img src='flag-english.png' border='1'> Impossible to connect to the database.
			<br>
			<img src='flag-spanish.png' border='1'> Fall&oacute; la conexi&oacute;n a la base de datos.";
			echo '
			<form method="post" action="';
			echo $PHP_SELF;
			echo '?step=3&lang='.$_REQUEST[lang].'&type='.$_REQUEST[type].'">
			<p align="center"><input class="btnformyellow" type="submit" value="'.ucfirst(_RETURN).'"></p>
			</form>';
			exit;
		}
		@mysql_query("SET NAMES 'utf8'");
		$query = 'SELECT * FROM hd_config WHERE ref="55" AND variable="this_version"';
		$result = mysql_query ($query);
		while ($data = mysql_fetch_array ($result))			{
			$versionanterior = $data['valor'];
		}
		if (!$versionanterior) {$versionanterior = 'unknow';}
	}else{
		// es una instalacion
		$versionanterior = null;
	}
}else{
	// si no existe config, es una instalacion nueva
	$versionanterior = null;
}


function verifylangs() {
	if (!$db__name) {include('../config.php');}
	$db_link = mysql_connect ($db__server, $db__user, $db__password);
	if (!mysql_select_db($db__name,$db_link)){
		echo "ERROR 12<br><img src='flag-english.png' border='1'> Impossible to connect to the database.
		<br><img src='flag-spanish.png' border='1'> Fall&oacute; la conexi&oacute;n a la base de datos.";
		echo '
		<form method="post" action="';
		echo $PHP_SELF;
		echo '?step=3&lang='.$_REQUEST[lang].'&type='.$_REQUEST[type].'">
		<p align="center"><input class="btnformyellow" type="submit" value="'.ucfirst(_RETURN).'"></p>
		</form>';
		exit;
	}
	@mysql_query("SET NAMES 'utf8'");
	$existe_en = 0; $existe_es = 0;
	$result = mysql_query("SHOW COLUMNS FROM hd_languages");
	if (mysql_num_rows($result) > 0) {
		while ($row = mysql_fetch_assoc($result)) {
			if ($row[Field]=='en'){$existe_en = 1;}
			if ($row[Field]=='es'){$existe_es = 1;}
		}
	}
	if ($existe_en == 0) {mysql_query("ALTER TABLE `hd_languages` ADD `en` TEXT NULL",$db_link);}
	if ($existe_es == 0) {mysql_query("ALTER TABLE `hd_languages` ADD `es` TEXT NULL",$db_link);}
}

function query($query,$type) {
	$result = mysql_query($query);
	$error = mysql_error();
	global $query_count;
	global $queries_used;
	$query_count++;
	$queries_used .= $query.'<br>';
	if (!empty($error)) {
		ob_end_clean();
		$errno = mysql_errno();
		die("ERROR 13 <br>Help Desk was unable to successfully run a MySQL query.<br>MySQL Returned this error: <b>$error</b> Error number: <b>$errno</b><br>The query that caused this error was: <b>$query</b>");
	}
	if ($type == 0) {
		return $result;
	}
	if ($type == 1) {
		$array = mysql_fetch_array($result);
		return $array;
	}
	if ($type == 2) {
		$array = mysql_num_rows($result);
		return $array;
	}
	if ($type == 3) {
		return mysql_insert_id();
	}
	if ($type == 4) {
		return mysql_result($result,0);
	}
	@mysql_free_result($result);
}


function mysql_dump($sqlfile){
	if (!$db__name) {include('../config.php');}
	$db_link=mysql_connect($db__server,$db__user,$db__password);
	if(!mysql_select_db($db__name,$db_link)){
		if ($sqlfile == 'database.sql') {
			$output = '';
			$fp = fopen ('../config.php', 'w');
			fwrite ($fp, $output);
			fclose ($fp);
		}
		echo "<strong>ERROR 14: "._PASS_INCORRECT."</strong><hr>
		<img src='flag-english.png' border='1'> Impossible to connect to the database.
		<br>
		<img src='flag-spanish.png' border='1'> Fall&oacute; la conexi&oacute;n a la base de datos.<hr>";
		echo "<br><strong>db__server:</strong> ".$db__server;
		echo "<br><strong>db__user:</strong> ".$db__user;
		echo "<br><strong>db__name:</strong> ".$db__name;
		echo "<br><strong>db__password:</strong> ".$db__password;
		echo "<hr>";
		echo '
		<form method="post" action="';
		echo $PHP_SELF;
		echo '?step=3&lang='.$_REQUEST[lang].'&type='.$_REQUEST[type].'">
		<p align="center"><input class="btnformyellow" type="submit" value="'.ucfirst(_RETURN).'"></p>
		</form>';
		exit;
	}
	@mysql_query("SET NAMES 'utf8'");

	$file_content = file_get_contents($sqlfile);
	$file_content2 = explode('-- --------------------------------------------------------',$file_content);
	foreach($file_content2 as $sql_line){
		if (!mysql_query($sql_line)) {echo '<hr><b>ERROR 15:</b><br>'.mysql_error().'<br><b>SQL_LINE:</b> '.$sql_line; exit;}
	}
	echo "<br><font color='green' class='sizeextrabold'><b>"._FILE." ".$sqlfile." "._INSERTED_CORRECTLY_INTO_THE_DATABASE."</b></font>";
}


if ($_REQUEST['lang']=='spanish'){
	define("_SYSTEM_REQUERIMENTS","Requerimientos del sistema");
	define("_INSTALL_UPDATE","Instalaci&oacute;n o actualizaci&oacute;n");
	define("_PHP_VERSION","Versi&oacute;n de PHP");
	define("_OK","Correcto");
	define("_CONTINUE","Continuar");
	define("_PHP_ERROR","Por favor actualice PHP al menos a la versi&oacute;n 5.3.0");
	define("_MYSQL_ERROR","MySQL no disponible");
	define("_MB_STRING_ERROR","La extensión mbstring no está instalada en su servidor");
	define("_PERMISSIONS","Permisos");
	define("_FILE","Archivo");
	define("_FOLDER","Carpeta");
	define("_CONFIG_ERROR","Por favor fije permisos 777 (CHMOD 777) al archivo config.php");
	define("_ERRORLOG_ERROR","Por favor fije permisos 777 (CHMOD 777) al archivo attachments/<strong>error.log</strong>");
	define("_ATTACHMENT_ERROR","Por favor fije permisos 777 (CHMOD 777) a la carpeta attachments");
	define("_DOCS_ERROR","Por favor fije permisos 777 (CHMOD 777) a la carpeta helpdeskdocs");
	define("_DOWNLOAD_ERROR","Por favor fije permisos 777 (CHMOD 777) a la carpeta download");
	define("_INSTALL_ERROR","<b>Se detectaron errores.</b><br>Por favor corrija los errores que se detallan arriba y recargue la p&aacute;gina");
	define("_INSTALLATION_TYPE","Elija el tipo de instalaci&oacute;n");
	define("_VERSION","versi&oacute;n");
	define("_NEW_INSTALL","Instalaci&oacute;n nueva");
	define("_UPDATE_FROM","Actualizar desde");
	define("_TO","a");
	define("_ALREADY_UPDATED","YA ACTUALIZADO. La versi&oacute;n instalada es la ".$currentversion);
	define("_BEGIN_INSTALLATION","Comenzar la instalaci&oacute;n");
	define("_DATABASE_CONNECTION","Conexi&oacute;n a la bases de datos");
	define("_DATABASE_HOST","Servidor de la base de datos");
	define("_DATABASE_NAME","Nombre de la base de datos");
	define("_DATABASE_USERNAME","Usuario de la base de datos");
	define("_DATABASE_PASSWORD","Contrasena de la base de datos");
	define("_NOTHING_TO_DO","nada para hacer");
	define("_INSTALLATION_COMPLETE","Instalaci&oacute;n completa");
	define("_STEPS_TO_FOLLOW","Pasos a seguir");
	define("_DELETE_THE_INSTALL_FOLDER","Elimine la carpeta INSTALL");
	define("_LOGIN_TO_THE_ADMIN_AREA","Ingrese al &aacute;rea administrativa");
	define("_THE_USER_IS_ADMIN_THE_PASSWORD_IS_ADMIN","El <b>Usuario</b> es <b>admin</b>. La <b>contrase&ntilde;a</b> es <b>admin</b>.");
	define("_ENTER_VIA_FTP_AND_DELETE_THE_INSTALL_FOLDER","Ingrese mediante FTP y elimine la carpeta install.");
	define("_WRONG","incorrecto/a");
	define("_HERE","AQUI");
	define("_STATUS","ESTADO");
	define("_RETURN","VOLVER");
	define("_CLICK_HERE_TO_HELP","Click aqu&iacute; para ayuda");
	define("_INSERTED_CORRECTLY_INTO_THE_DATABASE","insertado correctamente en la base de datos");
	define("_GO_TO_CONFIG_FIRST_SETUP","vaya a <b>Configuraci&oacute;n</b> > <b>Configuraci&oacute;n inicial</b>");
	define("_CONFIG_RENAMED","Si es la primera instalacion, renombre el archivo <strong>config_dist.php</strong> a <strong>config.php</strong>");
	define("_PASS_INCORRECT","Contraseña con caracteres no permitidos. Cambie la contraseña del usuario en el panel de hosting");
}else{
	define("_SYSTEM_REQUERIMENTS","System requirements");
	define("_INSTALL_UPDATE","Install or update");
	define("_PHP_VERSION","PHP version");
	define("_OK","OK");
	define("_CONTINUE","Continue");
	define("_PHP_ERROR","Please update PHP to at least version 5.3.0");
	define("_MYSQL_ERROR","MySQL not available");
	define("_MB_STRING_ERROR","The extension mbstring is not installed in your server");
	define("_PERMISSIONS","Permissions");
	define("_FILE","File");
	define("_FOLDER","Folder");
	define("_CONFIG_ERROR","Please set permissions for config.php file to 777 (CHMOD 777)");
	define("_ERRORLOG_ERROR","Please set permissions for attachments/<strong>error.log</strong> file to 777 (CHMOD 777)");
	define("_ATTACHMENT_ERROR","Please set permissions for attachments folder to 777 (CHMOD 777)");
	define("_DOCS_ERROR","Please set permissions for helpdeskdocs folder to 777 (CHMOD 777)");
	define("_DOWNLOAD_ERROR","Please set permissions for download folder to 777 (CHMOD 777)");
	define("_INSTALL_ERROR","<b>Errors were detected.</b><br>Please correct the errors as described above and reload the page");
	define("_INSTALLATION_TYPE","Choose installation type");
	define("_VERSION","version");
	define("_NEW_INSTALL","New install");
	define("_UPDATE_FROM","Update from");
	define("_TO","to");
	define("_ALREADY_UPDATED","ALREADY UPDATED. The installed version is ".$currentversion);
	define("_BEGIN_INSTALLATION","Begin installation");
	define("_DATABASE_CONNECTION","Database Connection");
	define("_DATABASE_HOST","Database Host");
	define("_DATABASE_NAME","Database Name");
	define("_DATABASE_USERNAME","Database Username");
	define("_DATABASE_PASSWORD","Database Password");
	define("_NOTHING_TO_DO","Nothing to do");
	define("_INSTALLATION_COMPLETE","Installation complete");
	define("_STEPS_TO_FOLLOW","Steps to follow");
	define("_DELETE_THE_INSTALL_FOLDER","Delete the INSTALL folder");
	define("_LOGIN_TO_THE_ADMIN_AREA","Login to the admin area");
	define("_THE_USER_IS_ADMIN_THE_PASSWORD_IS_ADMIN","The <b>User</b> is <b>admin</b>. The <b>password</b> is <b>admin</b>.");
	define("_ENTER_VIA_FTP_AND_DELETE_THE_INSTALL_FOLDER","Enter via FTP and delete the install folder.");
	define("_WRONG","wrong");
	define("_HERE","HERE");
	define("_STATUS","STATUS");
	define("_RETURN","RETURN");
	define("_CLICK_HERE_TO_HELP","Click here to help");
	define("_INSERTED_CORRECTLY_INTO_THE_DATABASE","inserted correctly into the database");
	define("_GO_TO_CONFIG_FIRST_SETUP","go to <b>Config</b> > <b>First Setup</b>");
	define("_CONFIG_RENAMED","If is the first installation, rename the file <strong>config_dist.php</strong> to <strong>config.php</strong>");
	define("_PASS_INCORRECT","Password with illegal characters. Change the password in the hosting panel");
}

?>
<html>
<head>
<title>METROHelp Desk</title>
       <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1.0, maximum-scale=1">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

        <meta name="description" content="<?= $config[81][1] ?>" />
        <meta name="keywords" content="<?= $config[83][1] ?>" />
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />

        <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>

        <link rel="stylesheet" type="text/css" href="../content/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="../content/css/bootstrap-responsive.css">
        <link rel="stylesheet" type="text/css" href="../content/css/bootmetro.css">
        <link rel="stylesheet" type="text/css" href="../content/css/bootmetro-tiles.css">
        <link rel="stylesheet" type="text/css" href="../content/css/bootmetro-charms.css">
        <link rel="stylesheet" type="text/css" href="../content/css/metro-ui-semilight.css">
        <link rel="stylesheet" type="text/css" href="../content/css/icomoon.css">

        <link rel="shortcut icon" href="../content/ico/favicon.ico">
        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="../content/ico/apple-touch-icon-144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="../content/ico/apple-touch-icon-114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="../content/ico/apple-touch-icon-72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="../content/ico/apple-touch-icon-57-precomposed.png">

        <script src="../scripts/modernizr.min.js"></script>


        <script type="text/javascript" src="../js/bootstrap.js"></script>

        <script type="text/javascript" src="../js/jquery.js"></script>
        <script type="text/javascript" src="../js/jquery-ui.custom.min.js"></script>
        <script type="text/javascript" src="../js/btip.js"></script>

</head>
<body data-accent="blue">
<br><br>

<div class="container">
	<div class="row">




<table class="table table-bordered">
<tr>
<th width=45% colspan=2><font size=5>METRO Help Desk - <?php echo _INSTALL_UPDATE; ?></font></th>
</tr>

<tr><td valign="top" align="center">


<?php
$step = $_REQUEST['step'];
$type = $_REQUEST['type'];


/////////////////111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
/////////////////
///////////////////////////////////////////////////////////////////////////// SELECCIONAR IDIOMA //////////////////////////////////////////////
if ($step == '')	{
	///////////////////////// seleccionar idioma

	echo '<br><br>
	<p><img src="flag-english.png"> <b><font size=4>Select language for installation process</font></b> <img src="flag-english.png"></p>
	<p><img src="flag-spanish.png"> <b><font size=4>Seleccione el lenguaje para el proceso de instalacion</font></b> <img src="flag-spanish.png"></p>
	<br><br>';

	echo '
	<p align=center>
	<input class="sizeextrabold" type="submit" value="ENGLISH" onClick="window.location=\'';
	echo $PHP_SELF;
	echo '?step=2&lang=english\'">
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input class="sizeextrabold" type="submit" value="ESPA&ntilde;OL" onClick="window.location=\'';
	echo $PHP_SELF;
	echo '?step=2&lang=spanish\'">';
}



/////////////////222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
/////////////////
/////////////////////////////////////////////////////////////////////////// CHEQUEAR REQUERIMIENTOS Y TIPO DE INSTALACION /////////////////////
if ($step == '2')	{


	//////////////////////// verificar permisos, etc
        ?>
	<table class="table table-striped">

            <thead>
                <tr>
		<th><?= strtoupper(_SYSTEM_REQUERIMENTS)?></th>
		<th><?= strtoupper(_STATUS)?></th>
		</tr>
            </thead>
            <tbody>
		<tr>
                    <td width="75%"><?php echo _PHP_VERSION.': '.phpversion (); ?></td>
        <?php
	if ('5.3.0' <= phpversion ())		{
		echo '<td><font color="green"><B>'._OK.'</B></font></td>';
	}else{
		echo '<td><font color="red">'._PHP_ERROR.'</font></td>';
		$error = '1';
	}
        ?>
	</tr>

	<tr>
            <td>MySQL</td>
            <?php
	if (function_exists ('mysql_connect'))		{
		echo '<td><font color="green"><B>'._OK.'</B></font></td>';
	}else{
		echo '<td><font color="red">'._MYSQL_ERROR.'</font></td>';
		$error = '1';
	}
        ?>
	</tr>

	<tr>
            <td>mbstring</td>
           <?php
	if (function_exists ('mb_eregi'))		{
		echo '<td><font color="green"><B>'._OK.'</B></font></td>';
	}else{
		echo '<td><font color="red">'._MB_STRING_ERROR.'</font></td>';
		$error = '1';
	}
        ?>
	</tr>
            </tbody>
        </table>

	<table class="table table-striped">
            <thead>
                <tr>
		<th><?= strtoupper(_PERMISSIONS)?></th>
		<th><?= strtoupper(_STATUS)?></th>
		</tr>
            </thead>
            <tbody>

	<tr>
            <td width="75%"><?= _FILE ?> <b>config.php</b> (<?= _CONFIG_RENAMED ?>)</td>

            <?php
	if (is_writable ('../config.php'))		{
		echo '<td><font color="green"><B>'._OK.'</B></font></td>';
	}else{
		echo '<td><font color="red">'._CONFIG_ERROR.'</font>';
		echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='theme/filezilla.jpg' target='_blank' onClick=\"window.open(this.href, this.target, 'width=500, height=294, left=20, top=20, scrollbars=1'); return false;\"><b>|"._CLICK_HERE_TO_HELP."|</b></a>&nbsp;&nbsp;";
		echo '</td>';
		$error = '1';
	}
        ?>
	</tr>

	<tr>
            <td><?= _FOLDER ?> <b>attachments</b></td>

            <?php
	if (is_writable ('../attachments/'))		{
		echo '<td><font color="green"><B>'._OK.'</B></font></td>';
	}else{
		echo '<td><font color="red">'._ATTACHMENT_ERROR.'</font>';
		echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='theme/filezilla.jpg' target='_blank' onClick=\"window.open(this.href, this.target, 'width=500, height=294, left=20, top=20, scrollbars=1'); return false;\"><b>|"._CLICK_HERE_TO_HELP."|</b></a>&nbsp;&nbsp;";
		echo '</td>';
		$error = '1';
	}
        ?>
	</tr>


	<tr><td><?= _FILE ?> <b>attachments/error.log</b></td>
            <?php
	if (is_writable ('../attachments/error.log'))		{
		echo '<td><font color="green"><B>'._OK.'</B></font></td>';
	}else{
		echo '<td><font color="red">'._ERRORLOG_ERROR.'</font>';
		echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='theme/filezilla.jpg' target='_blank' onClick=\"window.open(this.href, this.target, 'width=500, height=294, left=20, top=20, scrollbars=1'); return false;\"><b>|"._CLICK_HERE_TO_HELP."|</b></a>&nbsp;&nbsp;";
		echo '</td>';
		$error = '1';
	}
        ?>
	</tr>


	<tr><td><?= _FOLDER ?> <b>download</b></td>
            <?php
	if (is_writable ('../download/'))		{
		echo '<td><font color="green"><B>'._OK.'</B></font></td>';
	}else{
		echo '<td><font color="red">'._DOWNLOAD_ERROR.'</font>';
		echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='theme/filezilla.jpg' target='_blank' onClick=\"window.open(this.href, this.target, 'width=500, height=294, left=20, top=20, scrollbars=1'); return false;\"><b>|"._CLICK_HERE_TO_HELP."|</b></a>&nbsp;&nbsp;";
		echo '</td>';
		$error = '1';
	}
        ?>
	</tr>
            </tbody>
        </table>

	<table>

            <?php
	if ($error == '1')	{
		echo '<tr>
			<td colspan="2" align="center" bgcolor="red"><font color="#ffffff" size="5">
			'._INSTALL_ERROR.'
			</font>
			</td>
			</tr>
			';
	}else{
		echo '	<tr>
			<td colspan=2 align=center bgcolor="#FFFF99"><b>'.strtoupper(_INSTALLATION_TYPE).'</b></td>
			</tr>';

		echo '	<tr>
			<td colspan=2>';

		if ($versionanterior <> $currentversion) {
			echo '<form method="post" action="';
			echo $PHP_SELF;
			echo '?step=3&lang='.$_REQUEST[lang].'"><p align="center">';

			echo '<select name="type" style="font-size:20px;" class="input-xxlarge">';

			echo '<option value="newinstall"';
			if ($versionanterior == null)	{echo ' selected';}else{echo ' disabled';}
			echo ' style="font-size:20px;"> '._NEW_INSTALL.' - '._VERSION.' '.$currentversion.'</option>';
                        
			echo '<br><option value="update201210"';
			if ($versionanterior == '2012.10')	{echo ' selected';}elseif($versionanterior == 'unknow'){echo '';}else{echo ' disabled';}
			echo ' style="font-size:20px;"> '._UPDATE_FROM.' '._VERSION.' 2012.10 '._TO.' '._VERSION.' '.$currentversion.'</option>';



			////////////// Agregar antes de esto lo de la ultima version

			echo '</select>';

			echo '</p><p align="center"><input class="btnformgreen" type="submit" value="'._BEGIN_INSTALLATION.'"></p></form>';
		}else{
			echo '<font size="5" color="red"><b>'._ALREADY_UPDATED.'</b><font>';
		}
		echo '</td>';
	}
		echo '</tr>';
        ?>
        </table>
        <?php
}



/////////////////3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
/////////////////
/////////////////////////////////////////////////////////////////// DATOS DE LA DB ///////////////////////////////////////////////////////
if ($step == '3')	{
	echo '<form method="post" action="';
	echo $PHP_SELF;
	echo '?step=4&lang='.$_REQUEST[lang].'&type='.$_REQUEST[type].'">

	<p><b>'.ucfirst(_DATABASE_CONNECTION).'</b></p>

	<table class="table table-striped">
		<tr>
			<td width="50%">'.ucfirst(_DATABASE_HOST).'</td>
			<td><input type="text" name="dbhost" size="20" value="'.$db__server.'"> (localhost OR server IP)</td>
		</tr>
		<tr>
			<td>'.ucfirst(_DATABASE_NAME).'</td>
			<td><input type="text" name="dbname" size="20" value="'.$db__name.'"></td>
		</tr>
		<tr>
			<td>'.ucfirst(_DATABASE_USERNAME).'</td>
			<td><input type="text" name="dbusername" size="20" value="'.$db__user.'"></td>
		</tr>
		<tr>
			<td>'.ucfirst(_DATABASE_PASSWORD).'</td>
			<td><input type="password" name="dbpassword" size="20" value="'.$db__password.'"></td>
		</tr>
	</table>

	<p align="center"><input class="btnformgreen" type="submit" value="'.ucfirst(_CONTINUE).'"></p>
	</form>';
}



/////////////////444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
/////////////////
///////////////////////////////////////////////////// PROCESO DE INSTALACION O ACTUALIZACION ///////////////////////////////////////////////
if ($step == '4')	{
	if ($_REQUEST['dbhost'] == '')	{echo '<br>'.ucfirst(_DATABASE_HOST).' '._WRONG; exit ();}
	if ($_REQUEST['dbname'] == '')	{echo '<br>'.ucfirst(_DATABASE_NAME).' '._WRONG; exit ();}
	if ($_REQUEST['dbusername'] == '')	{echo '<br>'.ucfirst(_DATABASE_USERNAME).' '._WRONG; exit ();}
	if ($_REQUEST['dbpassword'] == '')	{echo '<br>'.ucfirst(_DATABASE_PASSWORD).' '._WRONG; exit ();}

	$db_link = @mysql_connect ($_REQUEST['dbhost'], $_REQUEST['dbusername'], $_REQUEST['dbpassword']);
	if (!@mysql_select_db($_REQUEST['dbname'],$db_link)){
		echo "ERROR 40<br>
		<img src='flag-english.png' border='1'> Impossible to connect to the database. &nbsp;&nbsp;&nbsp;
		<img src='flag-spanish.png' border='1'> Fall&oacute; la conexi&oacute;n a la base de datos.";
		echo '
		<form method="post" action="';
		echo $PHP_SELF;
		echo '?step=3&lang='.$_REQUEST[lang].'&type='.$_REQUEST[type].'">
		<p align="center"><input class="btnformyellow" type="submit" value="'.ucfirst(_RETURN).'"></p>
		</form>';
		exit;
	}else{
		@mysql_query("SET NAMES 'utf8'");

		// convertir la DB a utf8
		if (!mysql_query("ALTER DATABASE `".$_REQUEST['dbname']."` CHARACTER SET utf8 COLLATE utf8_general_ci")) {
			print ("ERROR 41<br>".mysql_error()); exit;
		}

		$output = '<?php
$db__server = "' . $_REQUEST['dbhost'] . '";
$db__user = "' . $_REQUEST['dbusername'] . '";
$db__password = "' . $_REQUEST['dbpassword'] . '";
$db__name = "' . $_REQUEST['dbname'] . '";
?>';

		$fp = fopen ('../config.php', 'w');
		fwrite ($fp, $output);
		fclose ($fp);
		include_once '../config.php';

	}
	//	echo 'type: '.$_REQUEST['type'];

	if ($_REQUEST['type'] == 'newinstall' AND !$versionanterior) {
		mysql_dump ('database.sql');
	}elseif ($_REQUEST['type'] == 'update201210') {
		verifylangs(); mysql_dump ('update-from-2012-10-to-2013-1.sql');
//		verifylangs(); mysql_dump ('update-from-2011-9-to-2012-1.sql'); updateto20121();
//	}elseif ($_REQUEST['type'] == 'update20119') {
//		verifylangs(); mysql_dump ('update-from-2011-9-to-2012-1.sql'); updateto20121();
	}else{
		echo '<br>'.ucfirst(_NOTHING_TO_DO); exit;
	}

	echo '
	<form method="post" action="';
	echo $PHP_SELF;
	echo '?step=5&lang='.$_REQUEST[lang].'&type='.$_REQUEST[type].'">
	<p align="center"><input class="btnformgreen" type="submit" value="'.ucfirst(_CONTINUE).'"></p>
	</form>';

}



/////////////////555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555
/////////////////
////////////////////////////////////////////////////////////////// INSTRUCCIONES FINALES /////////////////////////////////////////////////////////
if ($step == '5')	{

	echo '<br><br>
	<p align="left"><b><font size="6">'.ucfirst(_INSTALLATION_COMPLETE).'</font></b></p>

	<p align="left"><font size="4">'.ucfirst(_STEPS_TO_FOLLOW).':</font></p>

	<p align="left"><font size="3"><b>1- '.ucfirst(_DELETE_THE_INSTALL_FOLDER).'</b></font>&nbsp;&nbsp;&nbsp;'._ENTER_VIA_FTP_AND_DELETE_THE_INSTALL_FOLDER.'</p>

	<p align="left"><font size="3"><b>2- '.ucfirst(_LOGIN_TO_THE_ADMIN_AREA).'</b>&nbsp;&nbsp;&nbsp;<a href="../index.php"><b>'._HERE.'</b></a>';
	if ($_REQUEST['type'] == 'newinstall') {
		echo '&nbsp;&nbsp;&nbsp;'._THE_USER_IS_ADMIN_THE_PASSWORD_IS_ADMIN.'';
	}
	echo '</font></p>';
	echo '<p align="left"><font size="3"><b>3- '.ucfirst(_GO_TO_CONFIG_FIRST_SETUP).'</b></font></p>';

}

?>
</td>
        </tr>

      </table>
    </div>
    </div>
</body>
</html>


