<?php

		session_start();

      	        $_SESSION['database']="local";
	
	        if($_SESSION['database']=="local")
	          {
	             $db_host    = "localhost";
	             $db_user   = "root";
	     	     $db_pass   = "";
     	     	     $db_name   = "boutique";

	  	  }
	
		if($_SESSION['database']=="public")
		  {
	  	     $db_host    = "sql2";
	   	     $db_user   = "analogde";
	 	     $db_pass   = "rainbow";
    		     //$db_type   = "mysql";
	     	     $db_name   = "analogde";
	 	  }

		if(strpos($_POST, "<")) die("Hacking!");

		$compteur = 0;
		
		//$f = fopen("visu.txt", "w");
		
		while($champ = $_POST["tab".strval($compteur) ])
		     {
		     	$champ =  addslashes($champ);
		     	
		     	if($compteur == 0)
		     	  { $f = fopen($champ, "w");
		     	  }	
		     	
		     	if($compteur != 0)
		     	  {		     	
		     	  	
		     	  	if($compteur == 1)
			  	  { $nom_categorie = $champ ;
			  	  }
		     	  	
		     	  	if($compteur != 1)
		     	  	  {
		     	  	
					if($compteur == 2)
			  	  	{ $chaine = $champ ;
			  	  	}	

					if($compteur != 2)
				  	{ $chaine = $chaine ."," .$champ  ;
				  	}	
				  
    			        	fputs($f, $champ."<br>");
  				}
  
		     	 }
    			
    			$compteur ++;

    
		    }

		fclose($f);
		
		
		// creation de la categorie et stockage de la chaine dans le champ descriptif
		
		$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	 	mysql_select_db($db_name, $connexion);
		
		//$requete = "UPDATE categorie SET descriptif = '$chaine'  WHERE table_article = '$nom_categorie' ";
 		//$result = mysql_query($requete);
 		
 		$req = "INSERT INTO categorie (id,table_article,filtre,descriptif) VALUES('','$nom_categorie','','$chaine')";
  	        mysql_query($req); 	
		
		$morceau = explode(",", $chaine);
		
		$motif_titre = '(*)';
		$remplacement = "";
		
		$tableau = array();
		
		for($i=0; $i<=count($morceau)-1; $i++)
	  	   {   
		      	     // il s'agit d'un critére de filtrage -  recherche le wildcard
		      	     if (preg_match('`\*`', $morceau[$i]))
			  	{  $str = str_replace($motif_titre, $remplacement, $morceau[$i]); 
			           echo $str;
			           echo "<br>";
			           $tableau[]= $str; 
			        }
		   }	
	     
	     	print_r($tableau);
	     	
	     	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	 	mysql_select_db($db_name, $connexion);
	     	
	     	$champ = "";
	     	
	     	for($i=0; $i<=count($tableau)-1; $i++)
	           {  
	           	if($i == 0)
	           	  {$chaine = $tableau[$i] ." varchar(30) NOT NULL,";
			  }
			if($i != 0)
			  {$chaine = $chaine ." " .$tableau[$i] ." varchar(30) NOT NULL,";
			  }
			
	           }
	           
	           
		$chaine = strtolower($chaine);
	           
	           
		$p1 = " CREATE TABLE $nom_categorie (
  		id tinyint(4) unsigned NOT NULL AUTO_INCREMENT,
  		id_catalogue varchar(20) NOT NULL,
  		designation varchar(50) NOT NULL DEFAULT '',
  		reference varchar(20) NOT NULL,
  		marque varchar(20) NOT NULL,
  		fournisseur varchar(20) NOT NULL,
  		dispo varchar(20) NOT NULL,
  		prix_ttc decimal(7,2) NOT NULL,
  		prix_ht decimal(7,2) NOT NULL,
  		remise decimal(7,2) NOT NULL,
		tva decimal(5,2) NOT NULL,
  		descriptif blob NOT NULL,
  		stock varchar(20) NOT NULL,
  		status varchar(20) NOT NULL,
  		heure varchar(20) NOT NULL,
		date varchar(20) NOT NULL, "; 
  	 	$p2 = " PRIMARY KEY (id) ) ";

	       
	        $requete = $p1 .$chaine .$p2; 	       
	       
		mysql_query($requete);	        
	     		
	     		
  		 
	
//if(preg_match("\*\",$morceau[$i]))
//if (preg_match('`\$`', $morceau[$i]))

  		 
	     
		
?>



		  		