<?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
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

/*************************************************/
/* Fonctions pour les accès aux fichiers Humains */
/*************************************************/

/** Civilités **/

// Vérification du nombre de civilités
function nb_civ()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCIV;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Lecture d'un enregistrement civilité
function lit_enr_civ($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBCIV. " WHERE civ_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

/** Fonctions **/

// Vérification du nombre de fonctions
function nb_fonc()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBFONC;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Lecture d'un enregistrement fonction
function lit_enr_fonc($code,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM " .TBFONC. " WHERE fonc_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Retourne le prénom, le nom, et le niveau utilisateur d'un compte à partir de son code
// Pour  les opérateurs téléphoniques, les techniciens agences, les techniciens donneurs d'ordres, les techniciens sous-traitants
// Pour les clients, les agences, les donneurs d'ordres et les sous-traitants, seul le nom de société est retourné
// Pour le centre de gestion, le mot "centre de gestion" est retourné
// Pour l'administrateur, le mot "administrateur" est retourné
function prenom_nom_compte($code,$open)
{
	$tab_pn=array();
	$tab_pn["prenom"]="";
	$tab_pn["nom"]="";
	$tab_pn["level"]=0;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rexist= "SELECT pw_level FROM `" .TBPW. "` WHERE `pw_code` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row=mysql_fetch_array($rs2))
		{
			extract($row);
			switch (intval($pw_level))
			{
				case 1 :
					$rexist2= "SELECT cli_type,cli_ste,cli_nom,cli_prenom FROM `" .TBCLI. "` WHERE `cli_code` = '$code'";
					$rs3 = mysql_query($rexist2) or die(mysql_error());
					if (mysql_num_rows($rs3) <> 0)
					{
						if ($row2=mysql_fetch_array($rs3))
						{
							extract($row2);
							if ($cli_type==1)
							{
								$tab_pn["prenom"]=$cli_prenom;
								$tab_pn["nom"]=$cli_nom;
							}
							else
							{
								$tab_pn["nom"]=$cli_ste;
							}
							$tab_pn["level"]=$pw_level;
						}
					}
					break;
				case 2 :
					$rexist2= "SELECT ope_prenom,ope_nom FROM `" .TBOPE. "` WHERE `ope_code` = '$code'";
					$rs3 = mysql_query($rexist2) or die(mysql_error());
					if (mysql_num_rows($rs3) <> 0)
					{
						if ($row2=mysql_fetch_array($rs3))
						{
							extract($row2);
							$tab_pn["prenom"]=$ope_prenom;
							$tab_pn["nom"]=$ope_nom;
							$tab_pn["level"]=$pw_level;
						}
					}
					break;
				case 3 :
				case 7 :
				case 11 :
					$rexist2= "SELECT tec_prenom,tec_nom FROM `" .TBTEC. "` WHERE `tec_code` = '$code'";
					$rs3 = mysql_query($rexist2) or die(mysql_error());
					if (mysql_num_rows($rs3) <> 0)
					{
						if ($row2=mysql_fetch_array($rs3))
						{
							extract($row2);
							$tab_pn["prenom"]=$tec_prenom;
							$tab_pn["nom"]=$tec_nom;
							$tab_pn["level"]=$pw_level;
						}
					}
					break;
				case 4 :
					$rexist2= "SELECT age_nom FROM `" .TBAGE. "` WHERE `age_code` = '$code'";
					$rs3 = mysql_query($rexist2) or die(mysql_error());
					if (mysql_num_rows($rs3) <> 0)
					{
						if ($row2=mysql_fetch_array($rs3))
						{
							extract($row2);
							$tab_pn["nom"]=$age_nom;
							$tab_pn["level"]=$pw_level;
						}
					}
					break;
				case 5 :
					$tab_pn["nom"]="Centre de gestion";
					$tab_pn["level"]=$pw_level;
					break;
				case 6 :
					$rexist2= "SELECT do_nom FROM `" .TBDO. "` WHERE `do_code` = '$code'";
					$rs3 = mysql_query($rexist2) or die(mysql_error());
					if (mysql_num_rows($rs3) <> 0)
					{
						if ($row2=mysql_fetch_array($rs3))
						{
							extract($row2);
							$tab_pn["nom"]=$do_nom;
							$tab_pn["level"]=$pw_level;
						}
					}
					break;
				case 9 :
					$tab_pn["nom"]="Administrateur";
					$tab_pn["level"]=$pw_level;
					break;
				case 10 :
					$rexist2= "SELECT st_nom FROM `" .TBST. "` WHERE `st_code` = '$code'";
					$rs3 = mysql_query($rexist2) or die(mysql_error());
					if (mysql_num_rows($rs3) <> 0)
					{
						if ($row2=mysql_fetch_array($rs3))
						{
							extract($row2);
							$tab_pn["nom"]=$st_nom;
							$tab_pn["level"]=$pw_level;
						}
					}
					break;
			}
		}
	}
	if ($open==TRUE)
	{
		mysql_free_result($rs2);
		mysql_close($conn2);
	}
	
	return $tab_pn;
}

?>