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

Version 0.9.9

Copyright (C) 2006-2009  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

Web : http://smitst.homelinux.com
****************************************************************************************/

session_start(); 
if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
include_once("../inc/config.inc.php");
include_once("../inc/func_log.inc.php");
if (CheckURLLog(TRUE)==FALSE) header("Location: ../index.php");
if (CheckNivLog("9")==FALSE) header("Location: ../index.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_ope.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../inc/mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$categorie = $_GET["cat"];

$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));
$code = strip_tags(trim(@$_POST["code"]));

$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP

$retourliste = FALSE;

if (@$_POST["init"] == html_entity_decode($but_reinit))
{
	$nouvmdp = cremdp(FALSE); // Création d'un nouveau de passe (envoyé)
	$nouvmdpcrypte = md5($nouvmdp); // Cryptage du nouveau mot de passe (stocké)
	
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	switch ($categorie)
	{
		// MAJ de la fiche centre de gestion
		case "siege" :
			$rech = "SELECT * FROM " .TBCG;
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract($row);
					$code = $cge_code;
					$upd = "UPDATE `" .TBCG. "` SET `cge_pass`='".$nouvmdpcrypte."'  WHERE `cge_id` = '".$cge_id."'";
					$rs2 = mysql_query($upd) or die(mysql_error());
					$msg = $txt_fiche_mdp_reinit;
					$retourliste = TRUE;
				}
			}
			break;

		// MAJ de la fiche Agence
		case "age" :
			$rech = "SELECT * FROM " .TBAGE. " WHERE `age_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract($row);
					$upd = "UPDATE `" .TBAGE. "` SET `age_pass`='".$nouvmdpcrypte."'  WHERE `age_id` = '".$age_id."'";
					$rs2 = mysql_query($upd) or die(mysql_error());
					$msg = $txt_fiche_mdp_reinit;
					$retourliste = TRUE;
				}
			}
			break;

		// MAJ de la fiche Technicien
		case "tec" :
			$rech = "SELECT * FROM " .TBTEC. " WHERE `tec_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract($row);
					$upd = "UPDATE `" .TBTEC. "` SET `tec_pass`='".$nouvmdpcrypte."'  WHERE `tec_id` = '".$tec_id."'";
					$rs2 = mysql_query($upd) or die(mysql_error());
					$msg = $txt_fiche_mdp_reinit;
					$retourliste = TRUE;
				}
			}
			break;

		// MAJ de la fiche Client
		case "cli" :
			$rech = "SELECT * FROM " .TBCLI. " WHERE `cli_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract($row);
					$upd = "UPDATE `" .TBCLI. "` SET `cli_pass`='".$nouvmdpcrypte."'  WHERE `cli_id` = '".$cli_id."'";
					$rs2 = mysql_query($upd) or die(mysql_error());
					$msg = $txt_fiche_mdp_reinit;
					$retourliste = TRUE;
				}
			}
			
			break;
			
		// MAJ de la fiche Opérateur
		case "ope" :
			$rech = "SELECT * FROM " .TBOPE. " WHERE `ope_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract($row);
					$upd = "UPDATE `" .TBOPE. "` SET `ope_pass`='".$nouvmdpcrypte."'  WHERE `ope_id` = '".$ope_id."'";
					$rs2 = mysql_query($upd) or die(mysql_error());
					$msg = $txt_fiche_mdp_reinit;
					$retourliste = TRUE;
				}
			}
			break;

		// MAJ de la fiche Donneur d'ordres
		case "do" :
			$rech = "SELECT * FROM " .TBDO. " WHERE `do_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract($row);
					$upd = "UPDATE `" .TBDO. "` SET `do_pass`='".$nouvmdpcrypte."'  WHERE `do_id` = '".$do_id."'";
					$rs2 = mysql_query($upd) or die(mysql_error());
					$msg = $txt_fiche_mdp_reinit;
					$retourliste = TRUE;
				}
			}
			break;

		// MAJ de la fiche Sous-traitant
		case "st" :
			$rech = "SELECT * FROM " .TBST. " WHERE `st_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract($row);
					$upd = "UPDATE `" .TBST. "` SET `st_pass`='".$nouvmdpcrypte."'  WHERE `st_id` = '".$st_id."'";
					$rs2 = mysql_query($upd) or die(mysql_error());
					$msg = $txt_fiche_mdp_reinit;
					$retourliste = TRUE;
				}
			}
			break;
	}
	if ($retourliste == TRUE)
	{
		// Modification du compte de connexion
		$rech = "UPDATE `" .TBPW.  "` SET `pw_pass`='".$nouvmdpcrypte."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
	}
	mysql_close($conn);

	if ($retourliste == TRUE)
	{
		switch ($categorie)
		{
			// Envoi de l'email vers le siège
			case "siege" :
				envoi_mail("","Mise à jour mot de passe","cdg","mdp","xxx","cdg",$code,$nouvmdp,"",TRUE);
				break;
			// Envoi de l'email vers l'agence
			case "age" :
				envoi_mail("","Mise à jour mot de passe","age","mdp","xxx","age",$code,$nouvmdp,"",TRUE);
				break;
			// Envoi de l'email vers le technicien
			case "tec" :
				envoi_mail("","Mise à jour mot de passe","tec","mdp","xxx","tec",$code,$nouvmdp,"",TRUE);
				break;
			// Envoi de l'email vers le client
			case "cli" :
				envoi_mail("","Mise à jour mot de passe","cli","mdp","xxx","cli",$code,$nouvmdp,"",TRUE);
				break;
			// Envoi de l'email vers l'opérateur
			case "ope" :
				envoi_mail("","Mise à jour mot de passe","ope","mdp","xxx","ope",$code,$nouvmdp,"",TRUE);
				break;
			// Envoi de l'email vers le donneur d'ordres
			case "do" :
				envoi_mail("","Mise à jour mot de passe","do","mdp","xxx","do",$code,$nouvmdp,"",TRUE);
				break;
			// Envoi de l'email vers le sous-traitant
			case "st" :
				envoi_mail("","Mise à jour mot de passe","st","mdp","xxx","st",$code,$nouvmdp,"",TRUE);
				break;
		}
	}
}



if ((@$_POST["init"] == html_entity_decode($but_desact)) OR (@$_POST["init"] == html_entity_decode($but_react)))
{
	$actif = 1;
	if (@$_POST["init"] == html_entity_decode($but_desact))
		$actif = 0;
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if (($categorie=="age") OR ($categorie=="tec") OR ($categorie=="cli") OR ($categorie=="ope") OR ($categorie=="do") OR ($categorie=="st"))
	{
		// MAJ du compte de connexion
		$rech = "UPDATE `" .TBPW.  "` SET `pw_actif`='".$actif."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
		if ($actif==0)
			$msg = $txt_fiche_mdp_desac;
		else
			$msg = $txt_fiche_mdp_reac;
		$retourliste = TRUE;
	}
	mysql_close($conn);
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : <?PHP echo $title_mdp; ?></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 -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

</head>
<body>
<?PHP
include_once("../inc/entete.inc.php"); 
?>
<p align="center"><span class="titrepage"><?PHP echo $titre; ?></span></p>
<p align="center">&nbsp;</p>
<p align="center">
<?PHP 
if ($retourliste==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
echo $msg; 
echo '</span>';
?>
</p>
<?PHP
aff_ret_idx_auto("index.php",2000);
include_once("pied.php");
?>
