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

Version 0.9.7

Copyright (C) 2006-2007  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 internes */
/* et fichiers logos                              */
/**************************************************/

// Lecture d'un enregistrement fichiers internes
function lit_enr_fic($nom)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '$nom'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) > 0) // On a trouvé un ou plusieurs enregistrements
	{
		if (mysql_num_rows($rs2) == 1) // On a trouvé un seul enregistrement
		{
			if ($row2 = mysql_fetch_array($rs2))
				return $row2;
		}
		else
		{
			while ($row2 = mysql_fetch_array($rs2))
			{
				if ($nom==$row2["fic_nom"])
				{
					return $row2;
					break;
				}
			}
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Lecture d'un enregistrement fichiers internes (sur ID)
function lit_enr_ficid($id)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_id = '$id'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// MAJ Nb Téléchargements d'un fichier interne
function maj_nbtel_enr_fic($nom)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '$nom'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		$nbtel = $row2["fic_nbtel"];
		$nbtel++;
		
		$rech3 = "UPDATE `" .TBFIC. "` SET `fic_nbtel`='".$nbtel."'  WHERE `fic_nom` = '".$nom."'";
		$rs3 = mysql_query($rech3) or die(mysql_error());
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Donne l'id d'un enregistrement fichier
function donne_id_fic($nom)
{
	$no_id = -1;
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT fic_id FROM " .TBFIC. " WHERE fic_nom = '".$nom."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) > 0) // On a trouvé un ou plusieurs enregistrements
	{
		if (mysql_num_rows($rs2) == 1) // On a trouvé un seul enregistrement
		{
			if ($row2 = mysql_fetch_array($rs2))
				$no_id = $row2["fic_id"];
		}
		else
		{
			while ($row2 = mysql_fetch_array($rs2))
			{
				if ($nom==$row2["fic_nom"])
				{
					$no_id = $row2["fic_id"];
					break;
				}
			}
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $no_id;
}

// Donne le créateur du fichier/répertoire
function donne_createur_ficdir($nom)
{
	$createur = "inconnu";
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '".$nom."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) > 0) // On a trouvé un ou plusieurs enregistrements
	{
		if (mysql_num_rows($rs2) == 1) // On a trouvé un seul enregistrement
		{
			if ($row2 = mysql_fetch_array($rs2))
			{
				$createur = $row2["fic_codecrea"];
			}
		}
		else
		{
			while ($row2 = mysql_fetch_array($rs2))
			{
				if ($nom==$row2["fic_nom"])
				{
					$createur = $row2["fic_codecrea"];
					break;
				}
			}
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $createur;
}

// Donne le nombre de téléchargements du fichier/répertoire
function donne_nbtel_ficdir($nom)
{
	$nbtel = 0;
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '".$nom."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // On a trouvé un enregistrement
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			$nbtel = $row2["fic_nbtel"];
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbtel;
}

// Vérifie si ($user) est le créateur du fichier/répertoire (ou si c'est un administrateur)
function est_createur_ficdir($nom,$user)
{
	global
		$admin_code;

	if ($user == $admin_code) // L'admin peut tout faire
	{
		return true;
		break;
	}
				
	$est_createur = FALSE;
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '".$nom."' AND fic_codecrea = '".$user."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) > 0) // On a trouvé un enregistrement
	{
		$est_createur = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $est_createur;
}

// Donne le détail d'un enregistrement fichier
function donne_det_fic($nom)
{
	$detfic = "";
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT fic_detail FROM " .TBFIC. " WHERE fic_nom = '".$nom."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // On a trouvé un enregistrement
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			$detfic = $row2["fic_detail"];
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $detfic;
}

// Donne le nombre de hacks enregistrés
function nbhacks_ficlog()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFICLOG. " WHERE ficlog_type = '1'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbhacks=mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbhacks;
}

// Donne le nombre de leechs enregistrés
function nbleechs_ficlog()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFICLOG. " WHERE ficlog_type = '2'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbleechs=mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbleechs;
}


// Donne le nombre de "file not found" enregistrés
function nbnfounds_ficlog()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFICLOG. " WHERE ficlog_type = '3'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbnfounds=mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbnfounds;
}


/** Logos **/

function exist_logogif($code)
{
	global
		$root;

	$existe=FALSE;		
	$nomfic = $root."/".REP."/logos/log".strtolower($code);
	if (file_exists($nomfic.".gif"))
		$existe=TRUE;
	return $existe;
}

function exist_logopng($code)
{
	global
		$root;

	$existe=FALSE;		
	$nomfic = $root."/".REP."/logos/log".strtolower($code);
	if (file_exists($nomfic.".png"))
		$existe=TRUE;
	return $existe;
}

function exist_logo($code)
{
	global
		$root;

	$existe=FALSE;		
	$nomfic = $root."/".REP."/logos/log".strtolower($code);
	if ((file_exists($nomfic.".png")) AND (file_exists($nomfic.".gif")))
		$existe=TRUE;
	return $existe;
}

function exist_logo_allage()
{
	global
		$root;

	$existe=TRUE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	// Filtre sur agences ouvertes
	$rech2 = "SELECT * FROM ".TBAGE." WHERE `age_ouvert` = '1'";
	$rs2 = mysql_query($rech2);
	if (mysql_num_rows($rs2) <> 0)
	{
		while ($resultat = mysql_fetch_array($rs2))
		{
			extract($resultat);
			$nomfic = $root."/".REP."/logos/log".strtolower($age_code);
			if ((file_exists($nomfic.".png")==FALSE) OR (file_exists($nomfic.".gif")==FALSE))
			{
				$existe=FALSE;
				break;
			}
		}
	}
	return $existe;
}

?>