<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.6
Copyright (C) 2006  Sylvain FATOME

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 (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
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr
****************************************************************************************/

session_start(); 
?>
<?php if ((@$HTTP_SESSION_VARS["etat_log"] <> "connecte") or ((@$HTTP_SESSION_VARS["niv_log"] <> 9) AND (@$HTTP_SESSION_VARS["niv_log"] <> 5))) header("Location: ../index.php") ?>
<?php include ("../inc/config.inc.php") ?>
<?php include ("../inc/func.inc.php") ?>
<?php include ("../inc/func_log.inc.php") ?>
<?php include ("../inc/func_txt.inc.php") ?>
<?php include ("../inc/fic.inc.php") ?>
<?php require_once ("../classes/xpm/smtp.php"); ?>
<?php include ("../inc/mail.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);
?>
<?php
	 	// Variables Formulaire
		$titre = strip_tags(trim(@$_POST["titre"]));
		$creation = strip_tags(trim(@$_POST["creation"]));

		$code = strip_tags(trim(@$_POST["code"]));
		$pass = strip_tags(trim(@$_POST["pass"]));
		if ($creation == 1)
			$passcrypt = md5($pass);
						
		$nom = str_majuscules(strip_tags(trim(@$_POST["nom"])));
		$rcs = str_majuscules(strip_tags(trim(@$_POST["rcs"])));
		$tcom = str_majuscules(strip_tags(trim(@$_POST["tcom"])));
		$adr1 = str_majuscules(strip_tags(trim(@$_POST["adr1"])));
		$adr2 = str_majuscules(strip_tags(trim(@$_POST["adr2"])));
		$dep = strip_tags(trim(@$_POST["dep"]));
		$ville = str_majuscules(strip_tags(trim(@$_POST["ville"])));
		$pays = str_majuscules(strip_tags(trim(@$_POST["pays"])));
		$telf = strip_tags(trim(@$_POST["telf"]));
		$txttelf = strip_tags(trim(@$_POST["txttelf"]));
		$fax = strip_tags(trim(@$_POST["fax"]));
		$txtfax = strip_tags(trim(@$_POST["txtfax"]));
		$web = strip_tags(trim(@$_POST["web"]));
		$heures = strip_tags(trim(@$_POST["heures"]));
		$email = strtolower(strip_tags(trim(@$_POST["email"])));
		$emaild = strtolower(strip_tags(trim(@$_POST["emaild"])));
		$emailc = strtolower(strip_tags(trim(@$_POST["emailc"])));
		$emailt = strtolower(strip_tags(trim(@$_POST["emailt"])));
		$emailf = strtolower(strip_tags(trim(@$_POST["emailf"])));
		$emaili = strtolower(strip_tags(trim(@$_POST["emaili"])));
		$emailrh = strtolower(strip_tags(trim(@$_POST["emailrh"])));
		$nota = strip_tags(trim(@$_POST["nota"]));

		$retourmnu = FALSE;
		$cree = FALSE;

		// MAJ de la fiche centre de gestion
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rsge = "SELECT * FROM " .TBCG;
		$rssge = mysql_query($rsge) or die(mysql_error());

		if (mysql_num_rows($rssge) == 1) // Uniquement UN SEUL enregistrement !
		{
			if ($row = mysql_fetch_array($rssge)) 
			{
				$rech = "UPDATE `" .TBCG. "` SET `cge_datemod`='".$datea."',`cge_nom`='".$nom."', `cge_rcs`='".$rcs."', `cge_tcom`='".$tcom."', `cge_adr1`='".$adr1."', `cge_adr2`='".$adr2."', `cge_ville`='".$ville."', `cge_pays`='".$pays."', `cge_telf`='".$telf."', `cge_txttelf`='".$txttelf."', `cge_fax`='".$fax."', `cge_txtfax`='".$txtfax."', `cge_email`='".$email."', `cge_emaild`='".$emaild."', `cge_emailc`='".$emailc."', `cge_emailt`='".$emailt."', `cge_emailf`='".$emailf."', `cge_emaili`='".$emaili."',`cge_emailrh`='".$emailrh."', `cge_heures`='".$heures."', `cge_web`='".$web."', `cge_nota`='".$nota."'  WHERE `cge_id` = '".$row["cge_id"]."'";
				$rs = mysql_query($rech) or die(mysql_error());
				$msg = "Fiche modifiée avec succès !";
				$retourmnu = TRUE;
			}
		}
		
		if (mysql_num_rows($rssge) == 0) // N'existe pas, donc créé
		{
			$rech = "INSERT INTO " .TBCG;
			$rech .= " (cge_datemod,cge_code,cge_pass,cge_nom,cge_rcs,cge_tcom,cge_adr1,cge_adr2,cge_dep,cge_ville,cge_pays,cge_telf,cge_txttelf,cge_fax,cge_txtfax,cge_email,cge_emaild,cge_emailc,cge_emailt,cge_emailf,cge_emaili,cge_emailrh,cge_heures,cge_web,cge_nota) VALUES";
			$rech .= " ('$datea','$code','$passcrypt','$nom','$rcs','$tcom','$adr1','$adr2','$dep','$ville','$pays','$telf','$txttelf','$fax','$txtfax','$email','$emaild','$emailc','$emailt','$emailf','$emaili','$emailrh','$heures','$web','$nota')";
			$rs = mysql_query($rech) or die(mysql_error());
			$msg = "Fiche créée avec succès !";
			$retourmnu = TRUE;
			$cree = TRUE;

			// Ajout du compte pour connexion
			// Niveau par défaut : 5
			// Actif par défaut
				
			$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP
			$rech2 = "INSERT INTO " .TBPW. "(pw_code,pw_pass,pw_level,pw_lastsuccess,pw_lastip,pw_actif) VALUES ('$code','$passcrypt','5','$datehcrea','$log_ip','1')";
			$rs2 = mysql_query($rech2) or die(mysql_error());

		}
		
		mysql_free_result($rssge);
		mysql_close($conn);

		if ($cree == TRUE)
		{
			// Envoi d'email vers l'agence
			envoi_mail("Création du siège","cdg","cre","xxx","cdg",$codea,$pass,"",TRUE);
		}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Fiche Centre de Gestion</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->
<link href="../inc/menu.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour affichages -->
<script language="JavaScript" src="../inc/aff.js"></script>

</head>
<body>

<?php 
	include_once ("../inc/entete.php"); 
?>


<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>

<?PHP if ($retourmnu == FALSE) { ?>
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"><a href="cge_maj.php"><img src="../img/liste.gif" width="33" height="34" border="0" align="abddle"></a> <a href="cge_maj.php" class="annot_gras_it">Retour &agrave; la saisie </a></div></td>
		<td width="7%">&nbsp;</td>
		<td width="43%"><a href="index.php"><em><img src="../img/index.gif" width="48" height="48" border="0" align="abddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
<?PHP } else {
	if ($creation == 1)
	{
?>	
	<p align="center" class="annot_std_it"><span class="annot_std_gras">Pensez &agrave; t&eacute;l&eacute;charger le logo du centre de gestion ! </span></p>
<?PHP
	}
?>
	<p align="center" class="annot_std_it">Retour à l'index dans 2 secondes...</p>
	<script type="text/javascript">
	window.setTimeout("location=('index.php');",2000)
	</script>
<?PHP } ?>

<?php 
	include_once ("pied.php")
?>

