Location: PHPKode > projects > Alliances Manager > modelos/ModeloApp.php
<?php 
/*
 *  Alliances Manager (Gestionador de Alianzas) - Travian
 *  Copyright (C) 2008-12  Viva Mayer, Francisco Mesías <hide@address.com>
 *
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class ModeloApp
{
	public function instalar()
	{
		$mysql_host = $_POST['BD_host'];
		$mysql_user = $_POST['BD_user'];
		$mysql_pass = $_POST['BD_pass'];
		$mysql_database = $_POST['BD_name'];
		$mysql_pref = $_POST['BD_pref'];
		
		// Parámetros para la aplicación
		$perte_grupo = $_POST['AP_grupo'];
		
		// Datos del Usuario Administrador pasados del formulario
		$admin_name = $_POST['admin_name'];
		$admin_pass = $_POST['admin_pass'];
		$admin_raza = $_POST['admin_raza'];
	
		// Verificamos que la conexión a la Base de Datos funcione correctamente
		@mysql_connect($mysql_host, $mysql_user, $mysql_pass) OR die(mysql_error()."<br/><center><a href=\"instalador.php\">Volver a intentar<a></center>");
		mysql_select_db($mysql_database) OR die(mysql_error()."<br/><center><a href=\"index.php\">Volver a intentar<a></center>");
		
		// Cargando estructura de la Base de Datos
		$sql = "CREATE TABLE `".$mysql_pref."grupo` (
 					`id` smallint(5) unsigned NOT NULL auto_increment,
					`nombre` char(50) default NULL,
					`descripcion` tinytext,
					`x1` smallint(3) default NULL,
					`y1` smallint(3) default NULL,
					`x2` smallint(3) default NULL,
					`y2` smallint(3) default NULL,
				PRIMARY KEY  (`id`)
				) ENGINE=InnoDB DEFAULT CHARSET=latin1";
		mysql_query($sql) or die ('Invalid query GRUPO: '.mysql_error());
		
		$sql = "CREATE TABLE `".$mysql_pref."tropas` (
					`cod` smallint(2) unsigned NOT NULL,
					`att` smallint(3) unsigned default NULL,
					`defi` smallint(3) unsigned default NULL,
					`defc` smallint(3) unsigned default NULL,
					`velocidad` smallint(2) unsigned default NULL,
					`consumo` smallint(1) unsigned default NULL,
				PRIMARY KEY  (`cod`)
				) ENGINE=InnoDB DEFAULT CHARSET=latin1";
		mysql_query($sql) or die ('Invalid query TROPAS: '.mysql_error());
	
		$sql = "INSERT INTO `".$mysql_pref."tropas` (`cod`, `att`, `defi`, `defc`, `velocidad`, `consumo`) VALUES 
			(10,40,35,50,6,1), 
			(11,30,65,35,5,1),
			(12,70,40,25,7,1),
			(13,0,20,10,16,2),
			(14,120,65,50,14,3),
			(15,180,80,105,10,4),
			(16,60,30,75,4,3),
			(17,75,60,10,3,6),
			(18,50,40,20,4,5),
			(19,0,80,80,5,1),
			(20,40,20,5,7,1),
			(21,10,35,60,7,1),
			(22,60,30,30,6,1),
			(23,0,20,10,9,1),
			(24,55,100,40,10,2),
			(25,150,50,75,9,3),
			(26,65,30,80,4,3),
			(27,50,60,10,3,6),
			(28,40,60,40,4,4),
			(29,10,80,80,5,1),
			(30,15,40,50,7,1),
			(31,65,35,20,6,1),
			(32,0,20,10,17,2),
			(33,90,25,40,19,2),
			(34,40,115,55,16,2),
			(35,140,50,165,13,3),
			(36,50,30,105,4,3),
			(37,70,45,10,3,6),
			(38,40,50,50,5,4),
			(39,0,80,80,5,1)";
		mysql_query($sql) or die ('Invalid query INSERT TROPAS: '.mysql_error());

		$sql = "CREATE TABLE `".$mysql_pref."usuario` (
 					`nombre` varchar(20) NOT NULL,
		 			`pass` varchar(15) NOT NULL,
 					`raza` tinyint(1) default NULL,
 					`registro` date default '0000-00-00',
 					`last_login` date default '0000-00-00',
		 			`last_change_aldea` date default '0000-00-00',
 					`last_change_tropa` date default '0000-00-00',
		 			`estado` enum('Activo','Inactivo') default 'Inactivo',
 					`grupo` smallint(5) unsigned default NULL,
	 			PRIMARY KEY  (`nombre`),
				KEY `posicion_FK` (`grupo`),
				CONSTRAINT `usuario_ibfk_1` FOREIGN KEY (`grupo`) 
					REFERENCES `".$mysql_pref."grupo` (`id`) 
						ON DELETE SET NULL 
						ON UPDATE CASCADE
				) ENGINE=InnoDB DEFAULT CHARSET=latin1";
		mysql_query($sql) or die ('Invalid query USUARIO: '.mysql_error());
			
		$sql = "CREATE TABLE `".$mysql_pref."aldea` (
				`x` smallint(3) NOT NULL,
				`y` smallint(3) NOT NULL,
				`usuario_nombre` varchar(20) NOT NULL,
				`nombre` varchar(35) default NULL,
				`tipo` enum(
							'Normal',
							'Madera',
							'Barro',
							'Hierro',
							'15c',
							'9c') default 'Normal',
				`es_capital` tinyint(1) default '0',
				`es_principal` tinyint(1) default '0',
				`descripcion` enum(
									'Ofensiva',
									'Defensiva',
									'Productora',
									'Mixta') default NULL,
				`residencia` tinyint(2) unsigned default NULL,
				`muralla` tinyint(2) unsigned default NULL,
				`granero` mediumint(8) unsigned default NULL,
				PRIMARY KEY  (`x`,`y`),
				KEY `aldea_FKIndex1` (`usuario_nombre`),
				CONSTRAINT `aldea_ibfk_1` FOREIGN KEY (`usuario_nombre`) 
					REFERENCES `".$mysql_pref."usuario` (`nombre`) 
						ON DELETE CASCADE 
						ON UPDATE CASCADE
				) ENGINE=InnoDB DEFAULT CHARSET=latin1";
		mysql_query($sql) or die ('Invalid query ALDEA: '.mysql_error());
		
		$sql = "CREATE TABLE `".$mysql_pref."permiso` (
				`usuario_nombre` varchar(20) NOT NULL,
 				`ver_tropas` tinyint(1) unsigned NOT NULL default '0',
	 			`gestion` tinyint(1) unsigned NOT NULL default '0',
 				`buscar` tinyint(1) unsigned NOT NULL default '0',
				`grupo` tinyint(1) unsigned NOT NULL default '0',
 				PRIMARY KEY  (`usuario_nombre`),
	 			KEY `permiso_FK` (`usuario_nombre`),
 				CONSTRAINT `permiso_ibfk_1` FOREIGN KEY (`usuario_nombre`) 
					REFERENCES `".$mysql_pref."usuario` (`nombre`) 
						ON DELETE CASCADE 
						ON UPDATE CASCADE
			) ENGINE=InnoDB DEFAULT CHARSET=latin1";

		mysql_query($sql) or die ('Invalid query PERMISOS: '.mysql_error());

		$sql = "CREATE TABLE `".$mysql_pref."tropas_aldea` (
 				`idtropa` smallint(2) unsigned NOT NULL,
 				`oy` smallint(3) NOT NULL,
	 			`ox` smallint(3) NOT NULL,
 				`cantidad` mediumint(8),
				 PRIMARY KEY  (`idtropa`,`oy`,`ox`),
				 KEY `tropas_FK` (`idtropa`),
				 KEY `aldea_FK` (`ox`,`oy`),
				 CONSTRAINT `tropas_aldea_ibfk_1` FOREIGN KEY (`idtropa`) 
					REFERENCES `".$mysql_pref."tropas` (`cod`) 
						ON DELETE CASCADE 
						ON UPDATE CASCADE,
				 CONSTRAINT `tropas_aldea_ibfk_2` FOREIGN KEY (`ox`, `oy`) 
					REFERENCES `".$mysql_pref."aldea` (`x`, `y`) 
						ON DELETE CASCADE 
						ON UPDATE CASCADE
				) ENGINE=InnoDB DEFAULT CHARSET=latin1";
		$error = mysql_query($sql) or die ('Invalid query TROPAS_ALDEA: '.mysql_error());
	
		$sql = "CREATE TABLE `".$mysql_pref."refuerzos` (
 				`ox` smallint(3) NOT NULL,
 				`oy` smallint(3) NOT NULL,
	 			`dx` smallint(3) NOT NULL,
 				`dy` smallint(3) NOT NULL,
 				`idtropa` smallint(2) unsigned NOT NULL,
				`cantidad` mediumint(8) default NULL,
 				PRIMARY KEY  (`ox`,`oy`,`dx`,`dy`,`idtropa`),
 				KEY `tropa_refuerzo_FK` (`idtropa`),
 				KEY `aldea_origen_FK` (`ox`,`oy`),
	 			CONSTRAINT `refuerzos_ibfk_1` 
					FOREIGN KEY (`idtropa`) REFERENCES `".$mysql_pref."tropas` (`cod`) 
						ON DELETE NO ACTION 
						ON UPDATE CASCADE,
	 			CONSTRAINT `refuerzos_ibfk_2` 
					FOREIGN KEY (`ox`, `oy`) REFERENCES `".$mysql_pref."aldea` (`x`, `y`) 
						ON DELETE CASCADE 
						ON UPDATE CASCADE
				) ENGINE=InnoDB DEFAULT CHARSET=latin1";
		mysql_query($sql) or die ('Invalid query REFUERZOS: '.mysql_error());
		
		//Insertando nuevo usuario
		$sql = "INSERT INTO `".$mysql_pref."usuario` (`nombre`, `pass`, `raza`, `registro`, `estado`, `grupo`) VALUES (\"".$admin_name."\",\"".$admin_pass."\",\"".$admin_raza."\", NOW(),1, NULL)";
		mysql_query($sql) or die ('Invalid query INSERT USUARIO: '.mysql_error());
		
		// Otorgando permisos de Administrador
		$sql = "INSERT INTO `".$mysql_pref."permiso` (`usuario_nombre` , `ver_tropas` , `gestion` , `buscar`, `grupo`) VALUES ('".$admin_name."', 2, 2, 1, 1)";
		mysql_query($sql) or die ('Invalid query INSERT PERMISOS: '.mysql_error());
		
		// Guardando los datos para las conexiones
		$datei = fopen("config.php","w");
	
		$temp =  "<?php \n";
		fwrite($datei, $temp);
		$temp =  "\$config = Config::singleton();\n\n";
		fwrite($datei, $temp);
	
		$temp =  "\$config->set('CarpetaControl', 'control/');\n";
		fwrite($datei, $temp);
		$temp =  "\$config->set('CarpetaModelos', 'modelos/');\n";
		fwrite($datei, $temp);
		$temp =  "\$config->set('CarpetaVistas', 'vistas/');\n";
		fwrite($datei, $temp);
		
		$temp =  "\$config->set('dbhost', '".$mysql_host."');\n";
		fwrite($datei, $temp);
		$temp =  "\$config->set('dbname', '".$mysql_database."');\n";
		fwrite($datei, $temp);
		$temp =  "\$config->set('dbuser', '".$mysql_user."');\n";
		fwrite($datei, $temp);
		$temp =  "\$config->set('dbpass', '".$mysql_pass."');\n";
		fwrite($datei, $temp);
		$temp =  "\$config->set('PREFIX', '".$mysql_pref."');\n\n";
		fwrite($datei, $temp);
		
		$temp =  "\$config->set('perte_grupo', ".$perte_grupo.");\n\n";
		fwrite($datei, $temp);
		
		$temp =  "define('INSTALADO',1);\n";
		fwrite($datei, $temp);
		$temp =  "?>";
		fwrite($datei, $temp);
		
		fclose($datei);
	
		return false;
		
	}
	
	public function ExtraerFaq($name, &$xslDoc, &$DocDatos)
	{
		$xslDoc = new DOMDocument();
		$xslDoc->load("libs/PantillaAyuda.xsl");
		
		$xmlDoc = new DOMDocument();
		$xmlDoc->load("idioma/ayuda.xml");
		$xmlDoc->validate();
		
		$DomNodeList = $xmlDoc->getElementsByTagName("sobre");
		
		//---------------------------
		$output = ''; $i = 0;
	    $docaux = new DOMDocument();
	
	    while ( $node = $DomNodeList->item($i) ) 
		{
			// import node
	        $domNode = $docaux->importNode($node, true);
			
			if ($domNode->attributes->getNamedItem("desc")->nodeValue == $name)
				// append node
		        $docaux->appendChild($domNode);
			$i++;
	    }
	    $output = $docaux->saveXML();
	    $output = print_r($output, 1);
		//---------------------------
		
		$DocDatos = new DOMDocument();
		$DocDatos->loadXML($output);
		unset($xmlDoc, $output, $DomNodeList, $domNode, $node, $docaux);
	}
} // FIN CLASE
?>
Return current item: Alliances Manager