<?php

// Correction du traitement des variables GPC
if (get_magic_quotes_gpc())
{
	function stripslashes_deep($value)
	{
		$value = is_array($value) ? array_map('stripslashes_deep', $value) : trim(stripslashes($value));
		return $value;
	}

	$_POST = array_map('stripslashes_deep', $_POST);
	$_GET = array_map('stripslashes_deep', $_GET);
	$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

function select($name, $array, $selected)
{
	$out = "";

	if ($c = count($array))
	{
		$out .= "<select name=\"" . $name . "\" id=\"" . $name . "\" size=\"1\">\n";
		$out .= "<option value=\"\">-- Choisir --</option>\n";

		foreach ($array as $k => $v)
		{
			$out .= "<option value=\"" . $k . "\"" . ($k == $selected ? " selected=selected" : "") . ">" . $v . "</option>\n";
		}
		$out .= "</select>";
	}

	return $out;
}

function getFields($fields, $requestOnly = false)
{
	$vars = array();
	$methodType = array ("get", "post", "request");

	foreach ($fields AS $name => $options)
	{
		if (in_array($options[0], $methodType) === false)
			die ("Illegal method type used for field " . $name . " : " . $options[0]);

		if ($requestOnly === true)
			$method = "_REQUEST";
		else
			$method = "_" . strtoupper($options[0]);

		switch ($options[1])
		{
			case "integer":

				$vars[$name] = (isset($GLOBALS[$method][$name]) && is_numeric($GLOBALS[$method][$name]) ? intval($GLOBALS[$method][$name]) : $options[2]);
				break;

			case "float":

				$vars[$name] = (isset($GLOBALS[$method][$name]) && is_numeric($GLOBALS[$method][$name]) ? floatval($GLOBALS[$method][$name]) : $options[2]);
				break;

			case "string":

				$vars[$name] = (isset($GLOBALS[$method][$name]) ? trim($GLOBALS[$method][$name]) : $options[2]);
				break;

			case "array":

				$vars[$name] = (isset($GLOBALS[$method][$name]) && is_array($GLOBALS[$method][$name]) ? $GLOBALS[$method][$name] : $options[2]);
				break;

			case "boolean":

				$vars[$name] = (isset($GLOBALS[$method][$name]) ? $GLOBALS[$method][$name] : $options[2]);
				break;

			default:
				die ("Illegal method type used for field " . $name . " : " . $options[1]);
		}
	}

	// Insert into $GLOBALS
	foreach ($vars AS $var => $value)
		$GLOBALS[$var] = $value;

//	return $vars;
}

function select_rosace($name, $array, $selected)
{
	$out = "";

	if ($c = count($array))
	{
		$out .= "<select name=\"" . $name . "\" id=\"" . $name . "\" size=\"1\">\n";

		foreach ($array as $k => $v)
			$out .= "<option value=\"" . $k . "\"" . ($k == $selected ? " selected=selected" : "") . ">" . $v . "</option>\n";

		$out .= "</select>";
	}

	return $out;
}

function format_date($date)
{
	return ereg_replace("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", "\\3/\\2/\\1", $date);
}

function sql_date($date)
{
	return ereg_replace("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", "\\3-\\2-\\1", $date);
}

function comptaDate($date)
{
	return ereg_replace("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", "\\3*\\2*\\1", $date);
}

function check_date($jour, $mois, $annee)
{
	settype($jour, "integer");
	settype($mois, "integer");
	settype($annee, "integer");

	if (strlen($jour) > 0 && strlen($mois) > 0 && strlen($annee) == 4 && checkdate($mois, $jour, $annee))
		return true;

	return false;
}

// Affiche un tableau de façon lisible
function parray($array)
{
	print("<pre>"); print_r($array); print("</pre>");
}

// Formate une chaine avant insertion dans la base de donnée
function sqldata($data)
{
	return addslashes(nl2br(htmlspecialchars(trim(strip_tags($data)))));
}

// Enleve les <br> et <br /> pour les formulaires
function nobr($texte)
{
	return preg_replace( '!<br.*>!iU', "", $texte);
}

function br2nl($texte)
{
	return preg_replace( '!<br.*>!iU', "\r\n", $texte);
}

// Converti les caracteres HTML
function unhtmlspecialchars($string)
{
	$string = str_replace ( '&amp;', '&', $string );
	$string = str_replace ( '&#039;', '\'', $string );
	$string = str_replace ( '&quot;', '"', $string );
	$string = str_replace ( '&lt;', '<', $string );
	$string = str_replace ( '&gt;', '>', $string );
	$string = str_replace ( '&rsquo;', '\'', $string );
	$string = str_replace ( '&laquo;', '«', $string );
	$string = str_replace ( '&eacute;', 'é', $string );
	$string = str_replace ( '&agrave;', 'à', $string );
	$string = str_replace ( '&nbsp;', ' ', $string );
	$string = str_replace ( '&acirc;', 'â', $string );
	$string = str_replace ( '&egrave;', 'è', $string );
	$string = str_replace ( '&raquo;', '»', $string );
	$string = str_replace ( '&icirc;', 'î', $string );
	$string = str_replace ( '&ccedil;', 'ç', $string );
	$string = str_replace ( '&ecirc;', 'ê', $string );
	$string = str_replace ( '&ordm;', 'º', $string );
	$string = str_replace ( '&ordm;', 'º', $string );
	$string = str_replace ( '&ndash;', '-', $string );
	$string = str_replace ( '&middot;', '·', $string );
	$string = str_replace ( '&ocirc;', 'ô', $string );
	$string = str_replace ( '&oelig;', 'Oe', $string );
	$string = str_replace ( '&hellip;', '...', $string );

	return $string;
}

function req()
{
	return "<span class=\"error\">*</span>";
}

// Transforme les <br /> en <br>
function br($texte) {
	return preg_replace( '!<br.*>!iU', "<br>", $texte);
}

// Vérifie la validité d'une adresse mail
function check_mail($email)
{
	if (eregi("^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$", $email))
		return true;
	else
		return false;
}

function _get()
{
	$f_args = func_get_args();

	for ($i = 0; $i < func_num_args(); $i++)
	{
		global ${$f_args[$i]};
		${$f_args[$i]} = (isset($_GET[$f_args[$i]]) && trim($_GET[$f_args[$i]]) != "" ? $_GET[$f_args[ $i]] : "");
	}
}

function _post()
{
	$f_args = func_get_args();

	for ($i = 0; $i < func_num_args(); $i++)
	{
		global ${$f_args[$i]};
		${$f_args[$i]} = (isset($_POST[$f_args[ $i]]) && trim($_POST[$f_args[$i]]) != "" ? $_POST[$f_args[ $i]] : "");
	}
}

function _post_array()
{
	$f_args = func_get_args();

	for ($i = 0; $i < func_num_args(); $i++)
	{
		global ${$f_args[$i]};
		${$f_args[$i]} = (isset($_POST[$f_args[ $i]]) ? $_POST[$f_args[ $i]] : "");
	}
}

function _array()
{
	$f_args = func_get_args();
	$array = $f_args[0];

	for ($i = 1; $i < func_num_args(); $i++)
	{
		global ${$f_args[$i]};
		${$f_args[$i]} = (isset($array[$f_args[ $i]]) ? $array[$f_args[ $i]] : "");
	}
}

function _request()
{
	$f_args = func_get_args();

	for ($i = 0; $i < func_num_args(); $i++)
	{
		global ${$f_args[$i]};
		${$f_args[$i]} = (isset($_REQUEST[$f_args[ $i]]) && trim($_REQUEST[$f_args[$i]]) != "" ? $_REQUEST[$f_args[ $i]] : "");
	}
}

function in_multi_array($needle, $haystack)
{
	$in_multi_array = false;
	if (in_array($needle, $haystack))
		$in_multi_array = true;
	else
	{
		foreach ($haystack AS $key => $val)
		{
			if (is_array($val))
			{
				if (in_multi_array($needle, $val))
				{
					$in_multi_array = true;
					break;
				}
			}
		}
	}
	return $in_multi_array;
}

function find_in_array($key, $value, $array)
{
    if (is_array($array))
    {
        foreach ($array AS $t_key => $t_value)
        {
            if (is_array($t_value))
            {
                if (find_in_array($key, $value, $t_value))
                    return true;
            }
            else
            {
                if ($t_key == $key && $t_value == $value)
                    return true;
            }
        }
    }

    return false;
}

function display_infobox()
{
	global $infobox;
	echo "<div id=\"info-box\">\n";
	echo "<table>\n";

	if (count($infobox))
	{
		$i = 0;
		foreach ($infobox as $infotype => $info)
		{
			echo "<tr" . ($i % 2 == 0 ? " class=\"inter\"" : "") . ">";
			echo "<td>" . $infotype . "</td>";
			echo "<td>: " . $info . "</td>";
			echo "</tr>";
			$i++;
		}
	}
	echo "</table>\n";
	echo "</div>\n";
}

function calcul_execution()
{
	list($msec, $sec) = explode(" ", microtime());
	return ((float) $sec + (float) $msec) * 1000000;
}

$queryCount = 0;
// Exécute une requete
function query($q)
{
	global $queryCount;

	$r = mysql_query($q);
	if ($r === false)
		die ("Invalid query: " . mysql_error() . "<br />Query: " . $q);

	$queryCount++;
	return $r;
}

function queryCheck($q)
{
	$r = query($q);

	if (mysql_num_rows($r))
		return true;

	return false;
}

// Exécute une requete et renvoie les résultats dans un tableau
function queryList($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res[] = $n;
	}

	return $res;
}

// Exécute une requete et renvoie le premier champ des résultats dans un tableau
function queryListOne($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res[] = $n[0];
	}

	return $res;
}

// Exécute une requete et renvoie les résultats dans un tableau associatif (champ0 => champ1)
function queryAssoc($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res[$n[0]] = $n[1];
	}

	return $res;
}

// Exécute une requete et renvoie le résultat dans un tableau
function queryOne($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res = $n;
	}

	return $res;
}

// Exécute une requete et renvoie le résultat
function queryOneField($q)
{
	$res = "";
	$r = query($q);

	if (mysql_num_rows($r))
		list($res) = mysql_fetch_array($r);

	return $res;
}

// Affiche un tableau en concervant son formatage
function printVar($array)
{
	echo "<pre style=\"border: 1px solid black; text-align: left; font-size: 9px\">\n";
	print_r($array);
	echo "</pre>\n";
}

?>