<?php
///////////////////////////////////////////////
//                @lex Guestbook             //
//      by Soulard Alexis (c) 2002 - 2006    //
//         http://www.alexphpteam.com/       //
//             all right reserved            //
///////////////////////////////////////////////

function alex_livre_sid(){
    mt_srand((double)microtime()*1000000); 
    return md5(mt_rand(0,9999999));
}

// -----

function redirection_aguest3($pagego){
	echo '<script language="javascript" type="text/javascript">';
	echo "document.location.href=\"".$pagego."\"";
	echo "</script>";
	die();
}

// -----

function noaccents($chaine){ 
	return (strtr($chaine, "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ", "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn")); 
}

// -----

function nettoyer_car($chaine){
	return eregi_replace("[+]|[.]|[,]|[*]|[;]|[:]", "", $chaine); 
}

// -----

function nettoyer_var_post(){
	global $_POST;

	if (!get_magic_quotes_gpc()){
		while (list ($key, $val) = each ($_POST))
			if (!is_array($val))
				$_POST[$key] = addslashes($val);

		while (list ($key, $val) = each ($_GET))
			if (!is_array($val))
				$_GET[$key] = addslashes($val);
	}
}

// -----

function set_options(){
	global $chem_absolu, $alex_livre_version, $chem_modelixe;

	$chem_modelixe = $chem_absolu."templates/modelixe/";
}

// -----

function template_type($title, $tab = true, $chemin = "../"){
	global $echo_html, $alex_livre_version, $chem_absolu, $f_lang, $chem_template, $SCRIPT_ENCODAGE;

	$echo_html -> MxBloc("bloc1", "modify", $chem_absolu.$chem_template."/header.htm");
	$echo_html -> MxText("bloc1.chem_template", $chem_absolu.$chem_template);
	$echo_html -> MxText("bloc1.title", "@lex Guestbook ".$alex_livre_version." - ".$title);
	$echo_html -> MxText("bloc1.SCRIPT_ENCODAGE", $SCRIPT_ENCODAGE);

	if ($tab){
		$echo_html -> MxBloc("bloc2", "modify", $chem_absolu.$chem_template."tab_haut.htm");
		$echo_html -> MxText("bloc2.chem_absolu", $chemin);
		$echo_html -> MxBloc("bloc3", "modify", $chem_absolu.$chem_template."alexphpteam.htm");
		$echo_html -> MxText("bloc3.alexphpteam pub", $f_lang['alexphpteam pub']);
		$echo_html -> MxBloc("bloc4", "modify", $chem_absolu.$chem_template."tab_bas.htm");
	}
}

// -----

function read_rep($nomrep, $type = "file"){
	global $alex_livre_ext;

	$dossier = opendir($nomrep);
	$nbfic = 0;

	while ($Fichier = readdir($dossier)){
		if ($Fichier != "." && $Fichier != ".."){
			if ((strpos($Fichier, ".".$alex_livre_ext) && $type == "file" && $Fichier != "index.php") || ($type == "rep" && is_dir($nomrep.$Fichier))){
				$nbfic = $nbfic + 1;
				$nomfic[$nbfic] = str_replace(".".$alex_livre_ext, "", $Fichier); 
			}
		} 
	}
	closedir($dossier);

	return $nomfic;
}

// -----

function generate_options($tab_options, $egalite, $url=""){
	$chaine_options = "";

	for ($i=1; $i<=count($tab_options); $i++){
		if ($tab_options[$i]){
			if ($tab_options[$i] == $egalite)
				$select = "selected";
			else
				$select = "";

			$chaine_options .= "<option value=\"".$url.$tab_options[$i]."\"$select>".$tab_options[$i]."</option>\n";
		}
	}

	return $chaine_options;
}

// -----

function choose_checked($var_config, $type){
	if (($var_config && $type == "1") or (!$var_config && $type == "0"))
		return " checked";
}

// -----

function message_javascript($message, $echo = true){
	if ($echo){
		echo "<script language=\"javascript\" type=\"text/javascript\">\n";
		echo "alert(\"".$message."\");\n";
		echo "</script>\n";
	}
	else{
		return "<script language=\"javascript\" type=\"text/javascript\">\nalert(\"".$message."\");\n</script>\n";
	}
}

// -----

function attribuer_val($variable){
	global $_POST;

	if ($_POST[$variable] == "true")
		return true;
	else
		return false;
}

// -----

function read_page($page){
	$fd = @fopen($page, "r");
	$contents = @fread($fd, filesize ($page));
	@fclose($fd);

	return $contents;
}

// -----

function generate_langue(&$tab, $rajout = ""){
	global $echo_html, $f_lang;

	for ($i = 1; $i <= count($tab); $i++){
		$echo_html -> MxText($rajout.$tab[$i], @$f_lang[$rajout.$tab[$i]]);
	}
}

// -----

function generate_radio(&$tab){
	global $echo_html, $f_lang;

	for ($i = 1; $i <= count($tab); $i++){
		global ${$tab[$i]};

		$echo_html -> MxText("value_".$tab[$i]."1", choose_checked(${$tab[$i]}, "1"));
		$echo_html -> MxText("value_".$tab[$i]."0", choose_checked(${$tab[$i]}, "0"));
	}
}

// -----

function generer_liste_admin(){
	global $tab_champs_admin;

	$tab_champs_admin = array(
		1 => "receive_email",
		"modif_options",
		"gestion_skins",
		"gestion_messages",
		"gestion_censure",
		"gestion_ip",
		"gestion_smileys",
		"gestion_admin",
		"gestion_reponse_auto",
		"gestion_bdd"
	);
}

// -----

function transform_bool($var){
	if ($var == "1")
		return true;
	else 
		return false;
}

// -----

function verif_page($page){
	global $tab_champs_admin;

	for ($i=1; $i<=count($tab_champs_admin); $i++){
		global ${"user_".$tab_champs_admin[$i]};
	}

	if (!${"user_".$page}){
		header("Location: erreur.php");
		exit;
	}
}

// -----

function ajZeroAguest($nbre){
	if (strlen($nbre) < 2)
		return "0".$nbre;
	else
		return $nbre;
}
// -----

function transform_date($timestamp){
	global $format_date;
	$my_format = $format_date;

	if (strpos($format_date, "/"))
		$separation = "/";
	else
		$separation = "-";
	 
	$tab_format_date = explode($separation, $my_format);
	$date = getdate($timestamp);
	 
	$my_format = str_replace("d", ajZeroAguest($date['mday']), $my_format);
	$my_format = str_replace("m", ajZeroAguest($date['mon']), $my_format);
	return $my_format = str_replace("Y", $date['year'], $my_format);
}

// -----

function transform_heure($timestamp){
	$time_message = getdate($timestamp);
	return ajZeroAguest($time_message['hours']).":". ajZeroAguest($time_message['minutes']);
}

// -----

function replace_censure_smileys(&$texte, $censure=true){
	global $chem_absolu, $nb_champs_alex_livre_censure, $alex_livre_censure_texte_censure, $alex_livre_censure_texte_remplace, $nb_champs_alex_livre_smileys, $alex_livre_smileys_smiley, $alex_livre_smileys_car_replace, $CHEM_COMPLET, $AJOUTER_SAUT_LIGNE, $boite_java, $value_maxi_message;

	if (isset($boite_java)){
		if (strlen($texte) > $value_maxi_message)
			$texte = substr($texte, 0, $value_maxi_message)."...";
	}

	//mots censurés
	if ($censure){
		for ($i = 1; $i <= $nb_champs_alex_livre_censure; $i++){
			$texte = eregi_replace($alex_livre_censure_texte_censure[$i], $alex_livre_censure_texte_remplace[$i], $texte);
		}
	}

	//smileys
	for ($i = 1; $i <= $nb_champs_alex_livre_smileys; $i++){
		$lastPos = strrpos($alex_livre_smileys_smiley[$i], ".");
		$texte = str_replace($alex_livre_smileys_car_replace[$i], "<img src=\"".((isset($CHEM_COMPLET)) ? $CHEM_COMPLET : $chem_absolu)."images/smileys/".$alex_livre_smileys_smiley[$i]."\" alt=\"\" title=\"".ucfirst(str_replace("_", " ", substr($alex_livre_smileys_smiley[$i], 0, $lastPos)))."\"  style=\"border: 0px; vertical-align: middle\" />", $texte);
	}

	//urls
	if (!isset($CHEM_COMPLET))
		$texte = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>", $texte);

	// saut de ligne
	if (!isset($AJOUTER_SAUT_LIGNE) || (isset($AJOUTER_SAUT_LIGNE) && $AJOUTER_SAUT_LIGNE))
		$texte = nl2br($texte);

	//encodage des urls pour les rendre invisibles des moteurs de recherche (ca permet d'éviter aux spammeurs
	//d'être tentés de spammer inutilement ;o)
	if (!isset($CHEM_COMPLET)){
		$pos = 0;
		while ($pos = strpos(".".$texte, "<a ")){
			$endPos = strpos(".".$texte, "</a>");
			$chaineReplace = substr($texte, $pos-1, $endPos-$pos+4);
			$texte = substr($texte, 0, $pos-1).encodeTxt($chaineReplace).substr($texte, $endPos+3);
		}
	}

	return $texte;	
}

// -----

function cut(&$string, $separation = ' '){ 
	global $cut_mots;

	return preg_replace('/([^ ]{'.$cut_mots.'})/si','\\1'.$separation, $string); 
}

// -----

function ip_bannie(&$ip_bannie, &$ip_visiteur){
	$alex_livre_ip_ip_bannie = explode(".", $ip_bannie);
	$alex_livre_ip_ip_visiteur = explode(".", $ip_visiteur);

	$nb_ok = 0;
	for ($i = 0; $i < 4; $i++){
		if ($alex_livre_ip_ip_bannie[$i] == $alex_livre_ip_ip_visiteur[$i] or $alex_livre_ip_ip_bannie[$i] == "*")
			$nb_ok++;
	}

	if ($nb_ok == 4)
		return true;
	else
		return false;
}

// -----

function arrondir($chiffre){
	$tab_chiffres = explode(".", $chiffre);
	if (strpos($chiffre, "."))
		$chiffre = $tab_chiffres[0].".".substr($tab_chiffres[1], 0, 1);

	return $chiffre;
}

// -----

function  encodeEmail($email){
	global $chem_absolu, $f_lang, $alex_livre_messages_nom, $alex_livre_messages_email, $i;

	if (eregi ('([^?]+)([?].*)', $email, $temp))
		$email = $temp[1]; 
	
	$tout = addslashes($temp[2]);
	$email = preg_replace( '|@|',"'+'@'+'" ,$email);
	$email = "var e='$email';" ;
	
	if ($tout)
		$email .= "var f='$tout';";

	$email .= "document.write('<a href=\"mailto:'+e";
	
	if ($tout)
		$email .= "+f";

	$email .= "+'\" title=\"'+e+'\">";
	$email .= "<img src=\"".$chem_absolu."images/mail.gif\" alt=\"\" title=\"".$f_lang['send_mail_to']." ".addslashes($alex_livre_messages_nom[$i])." [".addslashes($alex_livre_messages_email[$i])."]\" style=\"border: 0px\" width=\"12\" height=\"10\" />";	
	$email .= "</a>')";
	$email = chunk_split (bin2hex($email ),2, '%');
	$email = '%'. substr($email ,0, strlen($email )-1);
	$email = chunk_split ($email, 54,"'+\n'" );
	$email = substr($email ,0, strlen($email )-4);
	$email = "<script type=\"text/javascript\" language=\"javascript\">\n<!--\neval(unescape( \n'$email'));\n//-->\n</script>"; 
	
	return $email; 
}

// -----

function  encodeTxt($html){
	global $chem_absolu;
	$html = addslashes($html);

	$url = "document.write('".$html."')";
	$url = chunk_split (bin2hex($url ),2, '%');
	$url = '%'. substr($url ,0, strlen($url)-1);
	$url = chunk_split ($url, 54,"'+\n'" );
	$url = substr($url, 0, strlen($url)-4);
	$url = "<script type=\"text/javascript\" language=\"javascript\">\n<!--\neval(unescape( \n'".$url."'));\n//-->\n</script>"; 
	
	return $url; 
}

// -----

function verifMailSyntaxe($email) {
  return( ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
               '@'.
               '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
               '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
               $email));
}

// -----

function save_options($langue = "francais", $skin = "AguestNew", $session = "1800", $mail = "true", $temps_post = "60", $date = "d/m/Y", $ok_mail = "true", $ok_url = "true", $ok_pays = "true", $ok_note = "true", $ok_moteur = "true", $nb_pages = "10", $cut_mots = "75", $url_recharger = "index.php", $var_url = "test=#test", $admin_valide_messages = "false", $maxi_car = "1500", $admin_add_code_securite = "false", $chem_dossier_fonts = "", $color_txt_code = "#000000", $color_background_code = "#FFFFFF", $admin_envoyer_remerciement = 0, $nb_max_smileys=20){
	global $f_lang, $write_object;

	$write_object = new write_files();
	$write_object -> save_donnees("<?php");
	$write_object -> save_donnees("/* ".$f_lang['options_generales']." */");
	$write_object -> save_donnees("\$f_language = \"".$langue."\";");
	$write_object -> save_donnees("\$skin_use = \"".$skin."\";");	
	$write_object -> save_donnees("\$f_temps_session = \"".$session."\";");
	$write_object -> save_donnees("\$send_mail = ".$mail.";");
	$write_object -> save_donnees("\$temps_ip_post = \"".$temps_post."\";");
	$write_object -> save_donnees("\$format_date = \"".$date."\";");
	$write_object -> save_donnees("\$admin_valide_messages = ".$admin_valide_messages.";");
	$write_object -> save_donnees("\$admin_envoyer_remerciement = ".$admin_envoyer_remerciement.";");
	$write_object -> save_donnees("\$admin_add_code_securite = ".$admin_add_code_securite.";");
	$write_object -> save_donnees("\$chem_dossier_fonts = \"".$chem_dossier_fonts."\";");
	$write_object -> save_donnees("\$color_txt_code = \"".$color_txt_code."\";");
	$write_object -> save_donnees("\$color_background_code = \"".$color_background_code."\";\n");

	$write_object -> save_donnees("/* ".$f_lang['options_modules']." */");
	$write_object -> save_donnees("\$ok_mail = ".$ok_mail.";");
	$write_object -> save_donnees("\$ok_url = ".$ok_url.";");
	$write_object -> save_donnees("\$ok_pays = ".$ok_pays.";");
	$write_object -> save_donnees("\$ok_note = ".$ok_note.";");
	$write_object -> save_donnees("\$ok_aff_moteur = ".$ok_moteur.";\n");

	$write_object -> save_donnees("/* ".$f_lang['options_tech']." */");
	$write_object -> save_donnees("\$nb_pages = \"".$nb_pages."\";");
	$write_object -> save_donnees("\$cut_mots = \"".$cut_mots."\";");
	$write_object -> save_donnees("\$maxi_car = \"".$maxi_car."\";");
	$write_object -> save_donnees("\$nb_max_smileys = \"".$nb_max_smileys."\";");
	$write_object -> save_donnees("\$url_recharger = \"".$url_recharger."\";");
	$write_object -> save_donnees("\$extension_url = \"".$var_url."\";");
	$write_object -> save_donnees("?>", false); //<?php
}

// -----

function globalValuesTemplate($chemBloc=""){
	global $_GET, $echo_html, $f_lang, $url_recharger;

	$echo_html -> MxText($chemBloc."pageReload", @$url_recharger);
	$echo_html -> MxText($chemBloc."debut", @$_GET['debut']);
	$echo_html -> MxText($chemBloc."lang", @$_GET['lang']);
	$echo_html -> MxText($chemBloc."skin", @$_GET['skin']);
	$echo_html -> MxText($chemBloc."seeAdd", @$_GET['seeAdd']);
	$echo_html -> MxText($chemBloc."seeNotes", @$_GET['seeNotes']);
	$echo_html -> MxText($chemBloc."seeMess", @$_GET['seeMess']);
	$echo_html -> MxText($chemBloc."addSignature", @$f_lang['addSignature']);
	$echo_html -> MxText($chemBloc."seeMessages", @$f_lang['seeMessages']);
}

//----------

function generatePwdGuest3($chrs=10){ 
	$pwd = ""; 
	mt_srand ((double) microtime() * 1000000);
	while (strlen($pwd)<$chrs){ 
		$chr = chr(mt_rand (0,255));
		if (eregi("^[a-z0-9]$", $chr))
			$pwd = $pwd.$chr; 
	}; 
	return $pwd; 
}

//----------

function dertimner_path_fonts(){ 
	global $chem_dossier_fonts, $_SERVER;
	$chemin_use = '';

	// on est sous windows...
	if (!trim($chem_dossier_fonts) && isset($_SERVER['WINDIR'])){
		$chemin_use = str_replace("\\\\", "\\", $_SERVER['WINDIR']."\fonts\\");
	}
	else if (!trim($chem_dossier_fonts)){
		$chemin_use = eregi_replace("(admin|genImg)/(options|index).php", "", $_SERVER['SCRIPT_FILENAME'])."fonts/";
	}
	else
		$chemin_use = $chem_dossier_fonts;

	return $chemin_use;
}

//----------

function generer_chaine_code_securite($nb_lettres = 4){
	// Caractères autorisés
	$tab_car = array(1=>"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

	$x = 10;
	$chaine = '';
	for ($i=1; $i<=$nb_lettres; $i++){
		$value  = rand(1, 23); // lettre
		$chaine .= $tab_car[$value];
	}

	return $chaine;
}

//----------

function isolercolor_hexa($color){
	$color = str_replace("#", "", $color);
	$tab = array();
	$tab[0] = hexdec(substr($color, 0, 2));
	$tab[1] = hexdec(substr($color, 2, 2));
	$tab[2] = hexdec(substr($color, 4, 2));

	return $tab;
}

//----------

function envoyer_mail($email, $objet, $message, $entetemail){
	@mail($email, $objet, $message, $entetemail);
}

//----------

function detectHost($ip){
	return trim(strtolower(@gethostbyaddr($ip)));
}

//----------

function detectCountry($host, $lang){
	global $FLAGS_LANG;

	// recherche d'un domaine existant
	$domain = substr($host, strrpos($host, '.') + 1);
	
	if (strlen($domain) != 2){
		// recherche d'un second domaine possible. Ex : "ca" dans ".ca.com"
		$host = substr($host, 0, strrpos($host, '.') );
		$domain = substr($host, strrpos($host, '.'));
	}
	
	if (isset($FLAGS_LANG[$domain]))
		return $domain;
	else{
		$offcomma = strpos(str_replace('cs', 'cz', $lang), ',');
	
		if ($offcomma == 2)	{
			$domain =  substr( $lang , 0, 2);
			if (isset($FLAGS_LANG[$domain]))
				return $domain;
		}

		$off = strpos($lang, '-');

		if ($off!==false){
			$domain = substr($lang, $off+1, 2);
			
			if(isset($FLAGS_LANG[$domain]))
				return $domain;
		}
		
		// catch the first extension Ex: "fr" in "fr,ar-ma"
		$domain = substr($lang, 0, 2);
		
		if (isset($FLAGS_LANG[$domain]))
			return $domain;
	}
	
	return "xx";
}

//----------

function defautHtmlBoite(){
	return "<table cellspacing=\"0\" cellpadding=\"0\" style=\"border: 0px; width: 100%\">\n[BOUCLE]\n <tr style=\"vertical-align: top\">\n  <td style=\"width: 150px\">[DATE]</td>\n  <td style=\"width: 140px\">[PSEUDO]</td>\n  <td style=\"width: 40px\">[NOTE]/10</td>\n  <td><td><a href=\"[URL]\">[MESSAGE]</a></td></td>\n </tr>\n[/BOUCLE]\n</table>";
}

//----------

function supSpace($txt){
	return str_replace("	", " ", $txt);
}
//----------

function findHost(){
	global $_SERVER;

	$path = eregi_replace("admin/|boiteJava/", "", $_SERVER['PHP_SELF']);
	$pos = strrpos($path, "/");

	return "http://".$_SERVER['SERVER_NAME'].substr($path, 0, $pos+1);
}

//----------

function extension($file){
	 return strtolower(substr(strrchr($file, "."), 1));
}

//----------

function replace_car_html(&$txt){
	return str_replace('<', '&lt;', str_replace('>', '&gt;', $txt));
}
?>