<?php
/**
* MiniClick
* Gestionnaire de tableau collaboratif
* Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
*
* 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 2 of the License, or
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
* @package miniclick
* @version 5.0.0
*/

//nouvelle app
include (MINICLICK_INSTALL_PATH."includes/connection.php");
include(MINICLICK_INSTALL_PATH."engine/miniclic/miniclick.class.php");
if ((isset($_GET["newapp"])) && (isset($_GET["id"]))){
	//recuperation des donnees de la ligne (si donnees vide -> erreur)
	$sql_query="SELECT Nom, Host, Db, Role, User_Id FROM APP WHERE App_Id=".$_GET["id"];
	$rq=mysql_query($sql_query);
	$app=mysql_fetch_object($rq);
	if (($app->Nom!="") && ($app->Host!="") && ($app->Db!="") && ($app->Role!="") && ($app->User_Id!="")){
		$sql_admin="SELECT User_Id, Passwd FROM USER WHERE Login='Admin' OR Login='admin'";
		$rq_admin=mysql_query($sql_admin);
		$admin=mysql_fetch_object($rq_admin);
		//verification existence du dossier (si existence -> avertissement)
		if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom)){
			mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom);
			echo "Creation d'un dossier apps/".$app->Nom."<br>";
		}
		if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/css")){
			mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/css");
			echo "Creation d'un dossier apps/".$app->Nom."/css<br>";
		}
		if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/img")){
			mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/img");
			echo "Creation d'un dossier apps/".$app->Nom."/img<br>";
		}
		if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/modules")){
			mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/modules");
			echo "Creation d'un dossier apps/".$app->Nom."/modules<br>";			
		}
		if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/templates")){
			mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/templates");
			echo "Creation d'un dossier apps/".$app->Nom."/templates<br>";
		}
		if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/patchs")){
			mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/patchs");
			echo "Creation d'un dossier apps/".$app->Nom."/patchs<br>";
		}
		if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/scripts")){
			mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/scripts");
			echo "Creation d'un dossier apps/".$app->Nom."/scripts<br>";
		}
		if (!is_file(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/app_config.php")){
			$config = fopen(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/app_config.php","w+");
			fclose($config);
			chmod (MINICLICK_INSTALL_PATH."apps/".$app->Nom."/app_config.php", 0755);
		}
		if (!is_file(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/default.php")){
			$defaut = fopen(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/default.php","w+");
			fputs($defaut,'<?php session_start(); echo "Bonjour:".$_SESSION[\'role\'];
			?>');
			fclose($defaut);
			chmod (MINICLICK_INSTALL_PATH."apps/".$app->Nom."/default.php", 0755);
		}
		$tables = mysql_list_tables("MINICLICK");
		//Creation de la bdd
		echo "CREATE DATABASE `".$app->Db."`<br>";
		mysql_query("CREATE DATABASE `".$app->Db."`");
		//connexion a l'host et a la bddd 
		$base = mysql_select_db($app->Db);
		if(!$base){
      			 die("Impossible de se connecter la base ".$app->Db);
		}
		//creation des tables de miniclick et de sa config par defaut (verifier execution requete et afficher) DUPLIQUER SAMPLE
		while ($donnees = mysql_fetch_array($tables))  {
			$table=trim($donnees[0]);
			if ($table!="") {
				//echo "Sauvegarde de la table $table en cours... \n";
				$Resultat = mysql_query("SHOW CREATE TABLE SAMPLE.$table");
				if ($Resultat) {

					//creation de la table
					$tableau = mysql_fetch_array($Resultat);
					$tableau[1].= ";";
					//si la table n'existe pas 
					$return=mysql_query($tableau[1]);
					if ($return==true){
						echo "Creation de la table $table <br>";
					}
	
					//recherche des donnees de la table dans SAMPLE
					$req_table = mysql_query("SELECT * FROM SAMPLE.$table");
					$nbr_champs = mysql_num_fields($req_table);
					$nb_lig = mysql_num_rows($req_table);
					while ($ligne = mysql_fetch_array($req_table)) {
						//verification de l existence de la ligne
						$sql_exi="SELECT ".ucfirst(strtolower($table))."_Id FROM ".$app->Db.".".$table." WHERE ";
						for ($i=0; $i<=$nbr_champs-1; $i++) {
							$nom_champ=mysql_field_name($req_table,$i);
							if (($nom_champ=="Table_Id")||($nom_champ=="Colonne_Id")){
								$sql_exi.=$nom_champ."='".$ligne[$i]."' AND ";
							}
							
						}
						$sql_exi = substr($sql_exi,0,-4);
						echo $sql_exi."<br>";
						$creates = mysql_query($sql_exi);

              					if(!$create = mysql_fetch_object($creates)){
							$insertion = "";
							$sql = "INSERT INTO ".$app->Db.".$table (";
							$val = " VALUES (";
							for ($i=0; $i<=$nbr_champs-1; $i++) {
								$nom_champ=mysql_field_name($req_table,$i);
								if ($nom_champ!=(ucfirst(strtolower($table))."_Id")){
									$sql .= "`".$nom_champ."`,";
									$val .= "'".mysql_real_escape_string($ligne[$i])."',";
								}
							}
							$sql = substr($sql,0,-1);
							$val = substr($val,0,-1);
							$sql .= ")";
							$val .= ");";
							$insertion =$sql.$val;
							echo $insertion."<br>";
							$return=mysql_query($insertion);
							if ($return==true){
							echo "Mise a jour des donnees de $table <br>";
							}
						}
					}
					
					
				}
			}
		}
		//creation utilisateur ADMIN
		$sql_exi_admin="SELECT User_Id FROM USER WHERE Login='Admin' OR Login='admin'";
		$rq_exi_admin=mysql_query($sql_exi_admin);
		$nb_admin=mysql_num_rows($rq_exi_admin);
		if ($nb_admin<1){
			mysql_query("INSERT INTO `USER` ( `User_Id` , `Login` , `Passwd` , `Nom` , `Mail` )VALUES ('', 'Admin', '".$admin->Passwd."', 'Admin', '');");
			echo "Creation de l'utilisateur Admin<br>";
		}
		
	}else{
		echo "Informations sur l'application incomplètes ";
	}
}

//supprimer un user d'une application
if ((isset($_GET["suppapp"])) && (isset($_GET["id"]))){
	//recuperation des donnees de la ligne (si donnees vide -> erreur)
	$sql_query="SELECT APP.Nom, APP.Host, APP.Db, APP.Role, APP.User_Id, USER.Login, USER.Passwd FROM APP, USER WHERE USER.User_Id=APP.User_Id AND App_Id=".$_GET["id"];
	//echo $sql_query;
	$rq=mysql_query($sql_query);
	$app=mysql_fetch_object($rq);
	if (($app->Nom!="") && ($app->Host!="") && ($app->Db!="") && ($app->Role!="") && ($app->User_Id!="")){
		$base = mysql_select_db($app->Db);
		$sql_user="SELECT USER.User_Id FROM USER WHERE User_id=".$app->User_Id;
		//echo "SELECT USER.User_Id FROM USER WHERE Passwd='".$app->Passwd."' AND Login='".$app->Login."'";
		$rq_user=mysql_query($sql_user);
		while($user=mysql_fetch_object($rq_user)){
			//suppresion du user dans la base client
			//suppresion des access
			$sql_del="DELETE FROM ".$app->Db.".USER WHERE User_id= '".$user->User_Id."'";
			//echo $sql_del."<br>";
			mysql_query($sql_del);
			echo "Suppresion de l'utilisateur dans  ".$app->Db."<br>";
			//creation des access de tous les modules par defaut dans la base ient
			$sql_mod="DELETE FROM ".$app->Db.".ACCESS WHERE User_id= '".$user->User_Id."'";
			//echo $sql_mod."<br>";
			mysql_query($sql_mod);
			echo "Suppresion des access de l'utilisateur dans  ".$app->Db."<br>";
		}
		$base = mysql_select_db("MINICLICK");
		$sql_mod="DELETE FROM APP WHERE App_id= '".$_GET["id"]."' AND User_id=".$app->User_Id;
		//echo $sql_mod."<br>";
		mysql_query($sql_mod);
		echo "Suppresion de l'app de l'utilisateur dans MINICLICK<br>";	
	}else{
		echo "Informations sur l'application incomplètes ";
	}
}
?>
