<?
$_key				= $_GET['_key'];
$_key				= $_POST['_key'];

if (!$SITE['lib_membre']) {	

	$SITE['lib_membre'] = 1;

	function membre_de_confiance($id) {
		
		$fin = 0;
		
		if ($id == 6509) 	{ $fin = 1; }		// populassite
		else if ($id == 1) 	{ $fin = 1; } 		// programmeur
		else if ($id == 8925)	{ $fin = 1; } 		// guillaumeromain
		
		return $fin;
	}
	
	function actualiser_ip_membre ($id) {
	
		$table 	 = 'membres';
		$where 	 = "(id = '$id')";
	
		$ip 	 = $_SERVER['REMOTE_ADDR'];
	
		$requete = "UPDATE $table SET ip = '$ip' WHERE $where";
		$num 	 = requete_sql($requete);
	
		return 1;
	}
	
	function compter_la_nouvelle_inscription() {
	
		$todaydate = date_courante_sql();
	
		requete_sql("UPDATE inscriptions_du_jour SET nb = nb + 1 WHERE (date = '$todaydate')");
	
		return 1;
	}
	
	function identifier_membre ($login, $password) {
	global $majeur, $confirmation, $email;
	
		$table 			= 'membres'; 
	
		$md5_new_cookie 	= ($password != $_COOKIE['password_cook']);
	
		if ((($md5_new_cookie) and (!($_SESSION['mise_a_jour'])))
		|| (($password != $_SESSION['password']) and ($md5_new_cookie) and ($_SESSION['mise_a_jour']))) {
	
			 $password 	= md5($password);
		}
	
		$where 			= "(login like '$login') and (password = '$password')";

		if (nb_results($table, $where) > 0) {
	
			$requete 	= "SELECT id, porno, confirmation, email FROM $table WHERE $where";
			$num 		= requete_sql($requete);
	
			$id 	 	= $num[1]['id'];
			$porno 		= $num[1]['porno'];
			$confirmation	= $num[1]['confirmation'];
			$email		= $num[1]['email'];
	
			$majeur	 	= $porno; // variable globale
	
			setcookie("login_cook","",time()-100,"/");
			setcookie("login_cook",$login,time()+3600*24*365,"/");
	
			setcookie("porno_cook","",time()-100,"/");
			setcookie("porno_cook",$porno,time()+3600*24*365,"/");
	
			if (isset($_POST['auto_connect'])) {
	
				setcookie("password_cook","",time()-100,"/");
				setcookie("password_cook",$password,time()+3600*24*365,"/");
			}
	
			actualiser_ip_membre ($id);

			return $id;
	
		} else return 0;
	}

	function diff($n1,$n2) { 

		$tablo 			= array($n1,$n2,abs($n1-$n2)); 
 
		sort($tablo,SORT_NUMERIC); 
        
		return $tablo; 
	} 

	function PGCD($n1,$n2) { 

		$tablo			= diff($n1,$n2); 
           
		if($tablo[0] > 0) {

			return PGCD($tablo[0],$tablo[1]); 

		} else {

			return $tablo[1]; 
        	} 
	} 

	function rapport_irreductible($n1,$n2) {

		$ret_pgcd		= PGCD($n1,$n2);

		$new_n1 		= round($n1 / $ret_pgcd);
		$new_n2 		= round($n2 / $ret_pgcd);

		$tab['n1']		= $new_n1;
		$tab['n2']		= $new_n2;

		return $tab;
	}

	function rapport_irreductible_den_max($n1,$n2,$den_max) {

		$ret_pgcd		= PGCD($n1,$n2);

		$new_n1 		= round($n1 / $ret_pgcd);
		$new_n2 		= round($n2 / $ret_pgcd);

		$total			= $new_n1 + $new_n2;

		if ($total > $den_max) {

			$rapport 	= $total / $den_max;

			$total		= $den_max;

			$new_n1		= round($new_n1 / $rapport);
			$new_n1		= round($new_n1 / 5) * 5;

			$new_n2		= round($new_n2 / $rapport);
			$new_n2		= round($new_n2 / 5) * 5;

			$ret_pgcd2	= pgcd($new_n1,$new_n2);

			$new_n1 	= round($new_n1 / $ret_pgcd2);
			$new_n2 	= round($new_n2 / $ret_pgcd2);
		}

		$tab['n1']		= $new_n1;
		$tab['n2']		= $new_n2;

		return $tab;
	}

	function membre_forum_existant ($id) {
	
		$table = 'IPBmembers'; 
		$where = "(id = '$id')";

		$ret = (nb_results($table,$where) > 0);

		return $ret;
	}

	function membre_existant ($id) {

		$table = 'membres'; 
		$where = "(id = '$id')";

		$ret = (nb_results($table,$where) > 0);

		return $ret;
	}
	
	function retourner_partie_existante ($id) {

		$table = 'parties'; 
		$where = "(id_membre_blanc = '$id')";

		$ret = (nb_results($table,$where) > 0);

		return $ret;
	}
	
	function retourner_partie_invitee ($id_noir) {

		$where = "(partie_en_cours = 0) and (id_membre_noir = '$id_noir')";

		$num = requete_sql("SELECT id_membre_blanc FROM parties WHERE {$where}");

		return $num;
	}
	
	function retourner_partie_en_cours ($id) {

		$table = 'parties'; 
		$where = "(partie_en_cours = 1) and ((id_membre_blanc = '$id') or (id_membre_noir = '$id'))";
		
		$ret = (nb_results($table,$where) > 0);

		return $ret;
	}
	
	function retourner_id_partie ($id) {

		$where = "(partie_en_cours = 1) and ((id_membre_blanc = '$id') or (id_membre_noir = '$id'))";
		
		$num	 = requete_sql("SELECT id_partie FROM parties WHERE {$where} ORDER BY id_partie DESC LIMIT 1");

		return $num[1]['id_partie'];
	}
	
	function retourner_id_partie_invitee ($id_blanc, $id_noir) {

		$where = "(partie_en_cours = 0) and ((id_membre_blanc = '$id_blanc') or (id_membre_noir = '$id_noir'))";
		
		$num	 = requete_sql("SELECT id_partie FROM parties WHERE {$where} ORDER BY id_partie DESC LIMIT 1");

		return $num[1]['id_partie'];
	}
	
	function pseudo_existant ($pseudo) {

		$table = 'membres'; 
		$where = "(pseudo = '{$pseudo}')";

		$ret = (nb_results($table,$where) > 0);

		return $ret;
	}
	
	function email_existant ($email) {

		$table = 'membres'; 
		$where = "(email = '{$email}')";

		$ret = (nb_results($table,$where) > 0);

		return $ret;
	}

	function accepter_le_defi ($id_defieur, $id_defie, $key) {
		
		if ($id_defieur && $id_defie && $key) {

			$table = 'defieurs'; 

			$where = "(id_defieur = '$id_defieur') and (id_defie = '$id_defie') and (_key = '$key')";

			$ret   = (nb_results($table,$where) > 0);

			if ($ret) {

				return 1;
			
			} else {

				return 0;
			}
		}
	}

	function ajouter_euros_cumul ($euros) {
	global $SITE;

		requete_sql("UPDATE chiffre_daffaire_journalier SET euros = euros + $euros ORDER BY id DESC LIMIT 1");

		return 1;
	}

	function retourner_euros_mois() {

		$todaydate 		= getdate(time() + $SITE['decalage_horaire']);
	
		$tm 			= $todaydate['mon'];
		$td 			= $todaydate['mday'];
		$ty 			= $todaydate['year'];

		if ($tm < 10) 		{ $tm = "0".$tm; }
		if ($td < 10) 		{ $td = "0".$td; }

		$date_deb		= "{$ty}-{$tm}-01";

		if ($tm < 12) {

			$post_ty 	= $ty;
			$post_tm 	= $tm + 1;

		} else {

			$post_ty 	= $ty + 1;
			$post_tm 	= 1;
		}

		if ($post_tm < 10) 	{ $post_tm = "0".$post_tm; }

		$date_fin		= "{$post_ty}-{$post_tm}-01";
		
		$num			= requete_sql("SELECT sum(euros) as total FROM `chiffre_daffaire_journalier` WHERE (date >= '{$date_deb}') and (date < '{$date_fin}')");

		$ret 			= $num[1]['total'];

		return $ret;
	}


	function retourner_euros_mois_complet() {

		$todaydate 		= getdate(time() + $SITE['decalage_horaire']);
	
		$tm 			= $todaydate['mon'];
		$td 			= $todaydate['mday'];
		$ty 			= $todaydate['year'];

		if ($tm < 10) 		{ $tm = "0".$tm; }
		if ($td < 10) 		{ $td = "0".$td; }

		$date_deb		= "{$ty}-{$tm}-01";

		if ($tm < 12) {

			$post_ty 	= $ty;
			$post_tm 	= $tm + 1;

		} else {

			$post_ty 	= $ty + 1;
			$post_tm 	= 1;
		}

		if ($post_tm < 10) 	{ $post_tm = "0".$post_tm; }

		$date_fin		= "{$post_ty}-{$post_tm}-01";
		
		$num			= requete_sql("SELECT count(*) as nb_jours FROM `chiffre_daffaire_journalier` WHERE (date >= '{$date_deb}') and (date < '{$date_fin}')");

		$nb_jours		= $num[1]['nb_jours'];
	
		$ret			= (retourner_euros_mois() / $nb_jours) * 30;

		return $ret;
	}

	function ajouter_pts_sur_defi ($mise_defie, $id_defieur, $id_defie, $_key) {

		$table 		= 'defieurs'; 
		$where 		= "(id_defieur = '$id_defieur') and (id_defie = '$id_defie') and (_key = '$_key')";

		requete_sql("UPDATE defieurs SET mise_defie = $mise_defie WHERE $where");
	}

	function creer_new_key_of_sql ($id_defieur, $id_defie, $_key) {
	global $new_key;

		if (accepter_le_defi ($id_defieur, $id_defie, $_key)) {

			$table 		= 'defieurs'; 
		
			$where 		= "(id_defieur = '$id_defieur') and (id_defie = '$id_defie') and (_key = '$_key')";

			$new_key	= clef_defi2($id_defieur, $id_defie);

			requete_sql("UPDATE defieurs SET _key = '$new_key' WHERE $where");

			return $new_key;

		} else {

			return 0;
		}
	}

	function creer_deja_envoye_mail ($id_defieur, $id_defie, $_key) {

		if (accepter_le_defi ($id_defieur, $id_defie, $_key)) {

			$table 		= 'defieurs'; 
		
			$where 		= "(id_defieur = '$id_defieur') and (id_defie = '$id_defie') and (_key = '$_key')";

			requete_sql("UPDATE defieurs SET deja_envoye = 1 WHERE $where");

			return 1;

		} else {

			return 0;
		}
	}

	function deja_envoye_mail ($id_defieur, $id_defie, $_key) {

		$num = requete_sql("SELECT deja_envoye FROM defieurs WHERE (id_defieur = $id_defieur) and (id_defie = $id_defie) and (_key = '$_key')");

		$ret = $num[1]['deja_envoye'];

		return $ret;
	}	

	function message_forum_existant ($id_forum, $forum_id) {
	
		$table = 'IPBposts'; 
		$where = "(author_id = $id_forum) and (forum_id = $forum_id)";

		$ret   = (nb_results($table,$where) > 0);

		return $ret;
	}
	
	function message_du_webdesigner_forum ($id_forum, $forum_id) {
	
		$table = 'IPBposts'; 
		$where = "(author_id = $id_forum) and (forum_id = $forum_id) and (edit_time != post_date)";

		$ret   = (nb_results($table,$where) > 0);

		return $ret;
	}
	
	function webdesigner_existant ($id) {
	
		$table = 'webdesigners'; 
		$where = "(id = '$id')";
	
		return (nb_results($table,$where) > 0);
	}
	
	function webdesigner_forum_existant ($id) {
	
		$table = 'webdesigners'; 
		$where = "(id_forum = '$id')";
	
		return (nb_results($table,$where) > 0);
	}
	
	function webdesigner_dispo_existant () {
	
		$table = 'webdesigners, membres'; 
		$where = "(membres.id = webdesigners.id) and (disponible = 1)";
	
		return (nb_results($table,$where) > 0);
	}

	function is_peut_transfert($id, $id_cible) {

		if ((webdesigner_existant ($id_cible)) or (webdesigner_existant ($id))) {

			$table 	= 'membres'; 

			$where 	= "(nb_transferts < 10) and (id = $id_cible)";
	
			$ok 	= (nb_results($table,$where) > 0);

			$where 	= "(nb_transferts < 10) and (id = $id)";;

			$ok 	= $ok && (nb_results($table,$where) > 0);

			return $ok;		

		} else {

			$table = 'membres'; 

			$where 	= "(nb_transferts < 3) and (id = $id_cible)";
	
			$ok 	= (nb_results($table,$where) > 0);

			$where 	= "(nb_transferts < 3) and (id = $id)";;

			$ok 	= $ok && (nb_results($table,$where) > 0);

			return $ok;
		}
	}
	
	function allopass_existant ($id) {
	
		$table = 'partenaires_allopass';
		$where = "(id = '$id')";
	
		return (nb_results($table,$where) > 0);
	}
	
	function site_existant ($id) {
	
		$table = 'membres'; 
		$where = "(id = '$id') and (url != 'http://')";
	
		return (nb_results($table,$where) > 0);
	}
	
	function visiteur_existant ($idem, $id_annonceur) {
	
		$table = 'visiter_pour_25pts, membres';
		$where = "(id_visiteur = membres.id) and (membres.idem = '$idem') and (id_annonceur='$id_annonceur')";
	
		return (nb_results($table,$where) > 0);
	}
	
	function annonce_existante ($id) {
	
		$table = 'annonceurs'; 
		$where = "(id = '$id')";
	
		return (nb_results($table,$where) > 0);
	}

	function defi_existant ($id_defieur, $id_defie) {

		$table = 'defieurs';
		$where = "(id_defieur = '$id_defieur') and (id_defie = '$id_defie')";

		$ret   = (nb_results($table,$where) > 0);

		return $ret;
	}

	function defi_key_existant ($id_defieur, $id_defie, $key_defi) {

		$table = 'defieurs';
		$where = "(id_defieur = '$id_defieur') and (id_defie = '$id_defie') and (_key = '$key_defi')";
	
		return (nb_results($table,$where) > 0);
	}

	function membre_actif_existant ($id) {
	
		$table = 'le_plus_actif';
		$where = "(id = '$id')";
	
		return (nb_results($table,$where) > 0);
	}
	
	function miseur_auto_existant ($id) {
	
		$table = 'miseur_automatique';
		$where = "(id = '$id')";
	
		return (nb_results($table,$where) > 0);
	}

	function programmeur_affichages_existant ($id) {
	
		$table = 'affichages_sur_le_web';
		$where = "(id = '$id')";
	
		return (nb_results($table,$where) > 0);
	}

	function retourner_champ_table ($id, $champ, $table) {

		$where 	 	= "(id_membre = '$id')";

		$requete 	= "SELECT $champ FROM $table WHERE $where LIMIT 1";
		$num	 	= requete_sql($requete);
	
		$x 		= $num[1][$champ];

		if (!isset($x)) { $x = 0; }
	
		return $x;
	}

	function retourner_mise_defieur ($id_defieur, $id_defie) {

		$table		= "defieurs";
		$where 	 	= "(id_defieur = '$id_defieur') and (id_defie = '$id_defie')";

		$requete 	= "SELECT mise_defieur FROM $table WHERE $where LIMIT 1";

		$num	 	= requete_sql($requete);
		$x 		= $num[1]['mise_defieur'];

		if (!isset($x)) { $x = 0; }
	
		return $x;
	}
	
	function retourner_champ_membre ($id, $champ) {
	
		$table 	 = 'membres'; 

		return retourner_champ_table ($id, $champ, $table);
	}
	
	function retourner_champ_webdesigner ($id, $champ) {
	
		$table 	 = 'webdesigners'; 

		return retourner_champ_table ($id, $champ, $table);
	}
	
	function retourner_champ_allopass ($id, $champ) {
	
		$table 	 = 'partenaires_allopass'; 

		return retourner_champ_table ($id, $champ, $table);
	}
	
	function retourner_champ_annonce ($id, $champ) {
	
		$table 	 	= 'annonceurs';

		return retourner_champ_table ($id, $champ, $table);
	}
	
	function retourner_pts_membre_actif ($id) {
	
		$table 	 	= 'le_plus_actif'; 

		$champ		= 'pts';

		return retourner_champ_table ($id, $champ, $table);
	}

	function retourner_position_membre_actif ($id) {
	
		if (membre_actif_existant($id)) {
	
			$where 		= "(membres.id = '$id') and (membres.id = le_plus_actif.id)";
			$table 		= 'le_plus_actif, membres';
	
			$requete 	= "SELECT le_plus_actif.pts,porno FROM $table WHERE $where";
			$num 		= requete_sql ($requete);
	
			$pts		= $num[1]['pts'];
			$porno		= $num[1]['porno'];
	
			if (!$porno) 	{ $str_porno = "and (porno = 0)"; } 
			else		{ $str_porno = ""; }
	
			$where 		= "(le_plus_actif.pts > $pts) || ((le_plus_actif.pts = $pts) and (membres.id < $id))";
			$where 		= "($where) {$str_porno} and (membres.id = le_plus_actif.id)";
	
			$table 		= 'le_plus_actif, membres';
	
			$position	= nb_results($table,$where) + 1;
			$nb_sites 	= nb_results('le_plus_actif','');

			$str_pos 	= str_replace(","," ",number_format($position));
			$str_sites	= str_replace(","," ",number_format($nb_sites));
	
			return $str_pos."/".$str_sites;
	
		} else { return "(non classé)"; }
	}
	
	function retourner_position_membre_categorie ($id) {
	
		$table 		= "membres";
	
		$requete 	= "SELECT pts,porno,categorie FROM $table WHERE id=$id";
		$num 		= requete_sql ($requete);
	
		$pts		= $num[1]['pts'];
		$porno		= $num[1]['porno'];
	
		if (!$porno) 	{ $str_porno = "and (porno = 0)"; } 
		else		{ $str_porno = ""; }
	
		$categorie	= addslashes($num[1]['categorie']);
	
		$where 		= "(confirmation=1) $str_porno and (url != 'http://') and (categorie like '$categorie%')";
	
		$nb_sites	= nb_results($table,$where);
	
		$where 		= "(pts > $pts) || ((pts = $pts) and (id < $id))";
		$where 		= "($where) $str_porno and (confirmation=1) and (url != 'http://') and (categorie like '$categorie%')";
	
		$position	= nb_results($table,$where) + 1;

		$str_pos 	= str_replace(","," ",number_format($position));
		$str_sites	= str_replace(","," ",number_format($nb_sites));
	
		if ($position > $nb_sites) 	{ return "(non classé)"; }
		else 				{ return $str_pos."/".$str_sites; }
	}
	
	function retourner_position_membre ($id) {
	
		$table 		= "membres";
	
		$requete 	= "SELECT pts,porno FROM $table WHERE id=$id";
		$num 		= requete_sql ($requete);
	
		$pts		= $num[1]['pts'];
		$porno		= $num[1]['porno'];
	
		if (!$porno) 	{ $str_porno = "and (porno = 0)"; } 
		else		{ $str_porno = ""; }
	
		$where 		= "(confirmation = 1) $str_porno and (url != 'http://')";
	
		$nb_sites	= nb_results($table,$where);
	
		$where 		= "(pts > $pts) || ((pts = $pts) and (id < $id))";
		$where 		= "($where) $str_porno and (confirmation=1) and (url != 'http://')";
	
		$position	= nb_results($table,$where) + 1;

		$str_pos 	= str_replace(","," ",number_format($position));
		$str_sites	= str_replace(","," ",number_format($nb_sites));
	
		if ($position > $nb_sites) 	{ return "(non classé)"; }
		else 				{ return $str_pos."/".$str_sites; }
	}
	
	function retourner_nb_destinataires ($id) {
	
		$table 		= "membres";
	
		$requete 	= "SELECT email,porno FROM $table WHERE id=$id";
		$num 		= requete_sql ($requete);
	
		$email		= $num[1]['email'];
		$porno		= $num[1]['porno'];
	
		if ($porno) 	{ $str_porno = "and (visite_porno = 1)"; } 
		else		{ $str_porno = ""; }
	
		$where 		= "(confirmation = 1) and (email != '$email') and (i_annonces < nb_annonces) $str_porno";

		$nb_dest	= round((nb_results($table,$where) * 9) / 10);

		return $nb_dest;
	}
	
	function retourner_tabmail_destinataires ($id, $nb_visiteurs) {
	
		$table 		= "membres";
	
		$requete 	= "SELECT email,porno FROM $table WHERE id=$id";
		$num 		= requete_sql ($requete);
	
		$email		= $num[1]['email'];
		$porno		= $num[1]['porno'];
			
		if ($porno) 	{ $str_porno = "and (visite_porno = 1)"; } 
		else		{ $str_porno = ""; }
	
		$where 		= "(confirmation = 1) and (email != '$email') and (i_annonces < nb_annonces) $str_porno";
	
		$num		= requete_sql("SELECT email FROM $table WHERE $where ORDER by nb_annonces DESC, pts DESC, id LIMIT $nb_visiteurs");
	
		return $num;
	}
	
	function retourner_id_membre ($login) {
	
		$table 	 = 'membres'; 
		$where 	 = "(login like '$login')";
	
		$requete = "SELECT id FROM $table WHERE $where";
		$num	 = requete_sql($requete);
	
		$x = $num[1]['id'];
	
		return $x;
	}
	
	function retourner_id_membre_par_email ($email) {
	
		$table 	 = 'membres'; 
		$where 	 = "(email like '$email')";
	
		$requete = "SELECT id FROM $table WHERE $where";
		$num	 = requete_sql($requete);
	
		$x = $num[1]['id'];
	
		return $x;
	}
	
	function assez_pts_gratuits_membre ($id, $pts) {

		$table = 'membres'; 
		$where = "(id = '$id') and ((pts - pts_achetes) >= $pts)";
	
		if (nb_results($table, $where) > 0) { 
			
			return 1;

		} else { 

			return 0;
		}	
	}

	function assez_pts_membre ($id, $pts) {

		return assez_pts_gratuits_membre ($id, $pts);
	}

	function assez_pts_achetes_membre ($id, $pts) {

		$table = 'membres'; 
		$where = "(id = '$id') and (pts_achetes >= $pts)";
	
		if (nb_results($table, $where) > 0) { 
			
			return 1;

		} else { 

			return 0;
		}	
	}

	//-- DEBUT - Traitement des POINTS RETIRES -->


	function retirer_pts_cumul ($id, $pts) {

		$table 	 = 'membres';
		$where 	 = "(id = '$id')";
	
		$requete = "UPDATE $table SET pts_cumul = pts_cumul - $pts WHERE $where";
		$num 	 = requete_sql($requete);

	}

	function retirer_pts_bonus_membre ($id, $pts) {

		retirer_pts_cumul ($id, $pts);

		$table 	 = 'membres';
		$where 	 = "(id = $id) and (pts >= $pts)";
		
		$requete = "UPDATE $table SET pts = pts - $pts WHERE $where";
		$num 	 = requete_sql($requete);
	
		return 1;
	}

		
	function new_retirer_pts_achetes_membre ($id, $pts) {

		retirer_pts_cumul ($id, $pts);
		
		$table 	 = 'membres';

		$where 	 = "(id = $id) and (pts_achetes >= $pts)";
	
		$requete = "UPDATE $table SET pts_achetes = pts_achetes - $pts, pts = pts - $pts WHERE $where";

		$num 	 = requete_sql($requete);
	
		return 1;
	}


	function retirer_pts_achetes_membre ($id, $pts) {

		return new_retirer_pts_achetes_membre ($id, $pts);
	}

	function retirer_pts_membre ($id, $pts) {
			
		retirer_pts_bonus_membre ($id, $pts);
	}


	//-- FIN - Traitement des POINTS RETIRES -->




	//-- DEBUT- Traitement des POINTS AJOUTES -->

	function ajouter_pts_cumul ($id, $pts) {

		$table 	 = 'membres';
		$where 	 = "(id = '$id')";
	
		$requete = "UPDATE $table SET pts_cumul = pts_cumul + $pts WHERE $where";
		$num 	 = requete_sql($requete);

	}

	function ajouter_pts_bonus_membre ($id, $pts) {

		ajouter_pts_cumul ($id, $pts);

		$table 	 = 'membres';
		$where 	 = "(id = '$id')";
	
		$requete = "UPDATE $table SET pts = pts + $pts WHERE $where";
		$num 	 = requete_sql($requete);
	
		return 1;
	}

	function ajouter_pts_direct_achetes_membre ($id, $pts) {

		ajouter_pts_cumul ($id, $pts);

		$table 	 = 'membres';

		$where 	 = "(id = $id)";

		$requete = "UPDATE $table SET pts_achetes = pts_achetes + $pts, pts = pts + $pts WHERE $where";
 
		$num 	 =  requete_sql($requete);

		return 1;
	}

	function ajouter_pts_achetes_membre ($id, $pts) {

		ajouter_pts_direct_achetes_membre ($id, $pts);
	}

	function ajouter_pts_membre ($id, $pts) {
			
		ajouter_pts_bonus_membre ($id, $pts);
	}

	function ajouter_pts_direct_membre ($id, $pts) {
			
		ajouter_pts_direct_achetes_membre ($id, $pts);
	}

	//-- FIN - Traitement des POINTS AJOUTES -->



	function ajouter_pts_affichages ($id, $pts) {
	
		$table 	= 'membres';
		$where 	= "(id = '$id')";
	
		$num 	= requete_sql("UPDATE $table SET pts_affichages_restants = pts_affichages_restants + $pts WHERE $where");

	return 1;
	}


	function ajouter_pts_all_palettes() {
	global $SITE;
	
		$table 	 			= 'membres';
		$where 	 			= "(nbannieres_palette != 0) || (nboutons_palette != 0)";

		$requete 			= "SELECT id, nbannieres_palette, nboutons_palette, pts_palette_jour FROM $table WHERE $where";
		$num 	 			= requete_sql($requete);

		$i	 			= 1;

		while ($i <= $num['count']) {

			$id	 		= $num[$i]['id'];

			$nboutons		= $num[$i]['nboutons_palette'];
			$nbannieres		= $num[$i]['nbannieres_palette'];

			$pts_palette		= $num[$i]['pts_palette_jour'];

			$pts_requis		= round($nboutons / $SITE['ratio_pts_bouton']) + round($nbannieres / $SITE['ratio_pts_banniere']) - $pts_palette;

			$pts_test		= $pts_palette + $pts_requis;

			if ($pts_test > 250)	{

				$ecart 		= $pts_test - 250;

				$pts_ajout	= $pts_requis - $ecart;

				$pts_test	= 250;

			} else {

				$pts_ajout	= $pts_requis;
			}

			if (($pts_ajout != 0) and ($pts_palette < 250)) {

				ajouter_pts_membre($id, $pts_ajout);

				requete_sql("UPDATE membres SET pts_palette_jour = $pts_test WHERE id = $id");
			}

			$i++;
		}

		return 1;
	}	

	function soustraire_pts_affichages($id, $rep, $inc_nb) {
	global $SITE;
	
		$table 	 				= 'membres';
		$where 	 				= "(id = $id)";

		$requete 				= "SELECT pts_affichages_restants FROM $table WHERE $where";
		$num 	 				= requete_sql($requete);

		$pts_affichages_restants		= $num[1]['pts_affichages_restants'];

		if ($rep == 'nboutons') {

			$ratio				= $SITE['ratio_pts_bouton'];

		} else {

			$ratio				= $SITE['ratio_pts_banniere'];
		}

		$pts_test				= $pts_affichages_restants - round($inc_nb / $ratio);

		if ($pts_test < 0) {

			$pts_test		= 0;
		}

		requete_sql("UPDATE membres SET pts_affichages_restants = $pts_test WHERE id = $id");

		return 1;
	}	
	
	function convert_file_to_mysql($id, $rep, $nb) {

		$table 				= 'membres';
		$where 				= "(id = '$id')";

		$ok_traite_envoi		= 0;

		if ($rep == 'nboutons') {

			$ok_envoi		= retourner_champ_membre($id, 'pts_affichages_restants');

			$old_nboutons		= retourner_champ_membre($id, $rep);

			$inc_nb			= $nb - $old_nboutons;
			
			soustraire_pts_affichages($id, $rep, $inc_nb);

			$reste_pts		= retourner_champ_membre($id, 'pts_affichages_restants');

			$ok_traite_envoi	= ($ok_envoi && !$reste_pts);

			$requete 		= "UPDATE $table SET nboutons 		= $nb 	WHERE $where";

		} else if ($rep == 'nbannieres') {

			$ok_envoi		= retourner_champ_membre($id, 'pts_affichages_restants');

			$old_nbannieres		= retourner_champ_membre($id, $rep);

			$inc_nb			= $nb - $old_nbannieres;
			
			soustraire_pts_affichages($id, $rep, $inc_nb);

			$reste_pts		= retourner_champ_membre($id, 'pts_affichages_restants');

			$ok_traite_envoi	= ($ok_envoi && !$reste_pts);

			$requete 		= "UPDATE $table SET nbannieres 	= $nb 	WHERE $where";

		} else if ($rep == 'palettes/nbannieres') {

			$requete 		= "UPDATE $table SET nbannieres_palette = $nb	WHERE $where";

		} else if ($rep == 'palettes/nboutons') {

			$requete 		= "UPDATE $table SET nboutons_palette	= $nb	WHERE $where";
		}

		$num				= requete_sql($requete);

		if ($ok_traite_envoi) {
			
			traiter_envoyer_mail_affichages($id);
		}

		return 1;
	}

	function login_present ($login) { 
	
		$requete = "SELECT confirmation FROM membres WHERE (login like '$login') and ((confirmation = 1) or (confirmation = 3))";
		$num  	 = requete_sql($requete);
	
		if ($num) 	{ return 1; } 
		else 		{ return 0; } 
	}

	function login_dactylo_present ($filtre_id, $login) { 
	
		$requete = "SELECT id FROM membres WHERE (login_dactylo like '$login') and (id != $filtre_id)";
		$num  	 = requete_sql($requete);

		$id 	= $num[1]['id'];
	
		if ($num) 	{ return $id; }
		else 		{ return 0; } 
	}

	function bon_membre_dactylo($login, $pass) {

		$url = "http://www.zonedactylo.com/partners/populassite.php?member=$login&password=$pass";

		$ok = return_url($url);

		if (!$ok) {

			$ok = 0;

		} else {

			$ok = 1;
		}

		return $ok;
	}
	
	function email_present ($email) { 
	
		$requete = "SELECT confirmation FROM membres WHERE (email like '$email') and ((confirmation = 1) or (confirmation = 3))";
		$num	 = requete_sql($requete);
	
		if ($num) 	{ return 1; }
		else 		{ return 0; }
	}
	
	function verifier_deja_url($url) {
	global $SITE, $INSCRIPTION;
	
		$requete = "SELECT nom,prenom,email,url,confirmation FROM membres WHERE url LIKE '$url' ORDER BY confirmation";
		$num 	 = requete_sql($requete);
	
		if ($num) {
	
			$INSCRIPTION['email'] 		= $num[1]['email'];
			$INSCRIPTION['prenom'] 		= $num[1]['prenom'];
			$INSCRIPTION['nom'] 		= $num[1]['nom'];
	
			$confirmation 			= $num[1]['confirmation'];
	
			if (($confirmation == 1) || ($confirmation == 3)) { $tst = 1; } 
	
			else {
	
				$tst = 0;
				requete_sql("DELETE FROM membres WHERE (url LIKE '$url') and (confirmation != 1) and (confirmation != 3)"); 
			}
	
		} else $tst = 0;
	
		return $tst;
	}
	
	function tester_visiter_10pts($id) {
		
		$idem  = retourner_champ_membre($id, 'idem');
	
		$table = 'ip_et_id'; 
		$where = "(id = $idem) and (ip = 'plus.10.visits')";
	
		if (nb_results($table, $where) > 0) { return 1; } 
		else 				    { return 0; }
	}
	
	function inserer_visiter_10pts($id) {

		$idem  = retourner_champ_membre($id, 'idem');
	
		requete_sql("INSERT INTO ip_et_id (id,ip) VALUES ('$idem','plus.10.visits')");
		return 1;
	}

	function inserer_newsletter_envoyee ($titre_annonce, $texte_annonce, $where_sql_newsletter) {
	
		requete_sql ("INSERT INTO newsletter (sujet,message,where_sql) VALUES ('".addslashes($titre_annonce)."','".addslashes($texte_annonce)."','".addslashes($where_sql_newsletter)."')");
	
		return 1; 
	}
	
	function tester_code_allopass($code) {
	
		$table = 'codeallopass';
		$where = "code = '$code'";
	
		if (nb_results($table,$where) > 0) 	{ return 1; }
		else					{ return 0; }
	}
	
	function inserer_code_allopass($code) {
	
		requete_sql("INSERT INTO codeallopass (code) VALUES ('$code')");
		return 1;
	}
	
	function retourner_url_visiter_site($id) {
		$url = 0;
	
		if (site_existant($id)) {
	
			$ip 	= $_SERVER['REMOTE_ADDR'];
			
			$url 	= retourner_champ_membre($id, 'url');
	
			$table	= "ip_et_id";

			$where	= "(id = '$id') and (ip = '$ip')";
	
			if (!nb_results($table, $where)) {
	
				requete_sql("INSERT INTO $table (id,ip) VALUES ('$id','$ip')");
	
				requete_sql("UPDATE annonceurs SET visiteurs = visiteurs + 1 WHERE id = $id");
				requete_sql("UPDATE membres SET visiteurs = visiteurs + 1 WHERE id = $id");
			}
		}		
	
		return $url;
	}
	
	function tester_annonce_perimee($id) {
		
		$num 			= requete_sql("SELECT id, pcentage, visiteurs, visiteurs_garantis, visiteurs_potentiels, pts, pts_offerts, date FROM annonceurs WHERE id = $id");
	
		if ($num) {
	
			$date 			= $num[1]['date'];
			$id			= $num[1]['id'];
			$pcentage		= $num[1]['pcentage'];
			$visiteurs		= $num[1]['visiteurs'];
			$visiteurs_garantis	= $num[1]['visiteurs_garantis'];
			$visiteurs_potentiels	= $num[1]['visiteurs_potentiels'];
			$pts_annonce		= $num[1]['pts'];
			$pts_offerts		= $num[1]['pts_offerts'];
		
			$id_annonceur		= $id;
			
			$todaydate 		= date_courante_sql();
			
			$date_annonce		= str_replace('-', '/', $date);
		
			$ecart_jour		= nb_jours($todaydate, $date);
		
			if (($visiteurs >= $visiteurs_potentiels) || ($ecart_jour >= 7)) {
		
				requete_sql("DELETE FROM annonceurs WHERE id = $id_annonceur");
		
				envoi_mail_annonce_terminee($id_annonceur, $pcentage, $pts_annonce, $pts_offerts, $date_annonce, $visiteurs, $visiteurs_garantis, $visiteurs_potentiels);
					
				requete_sql("DELETE FROM visiter_pour_25pts WHERE id_annonceur = $id");
		
				return 1;
				
			} else 	{ return 0; }
	
		} else 		{ return 0; }
	}
	
	function peut_annoncer($id) {
	
		if (!annonce_existante($id)) { 
			
			return 1;
		
		} else {
			
			$bool = tester_annonce_perimee($id);
		
			return $bool;
		}
	}

	function ajouter_defi($id_defieur, $id_defie, $mise_defieur) {
	global $key_defi;

		$tdate 		= date_courante_sql();

		$key_defi	= clef_defi($tdate, $id_defieur, $id_defie);

		retirer_pts_membre ($id_defieur, $mise_defieur);
	
		requete_sql("INSERT INTO defieurs (id_defieur, id_defie, mise_defieur, mise_defie, date, _key) VALUES ('$id_defieur', '$id_defie', '$mise_defieur', '0', '$tdate','$key_defi')");

		return 1; 
	}
	
	function ajouter_annonce($id, $pts_requis, $pts_annonce, $pcentage, $texte_annonce, $tabmail, $visiteurs_garantis, $visiteurs_potentiels, $gratuit) {
	
		$tdate 	= date_courante_sql();

		if ($gratuit) {

			retirer_pts_bonus_membre ($id, $pts_requis);		
			
			$pcentage = 0;

		} else {
		
			new_retirer_pts_achetes_membre ($id, $pts_requis);
			
			$pcentage = 1;
		}
		
		requete_sql("INSERT INTO annonceurs (id, date, pcentage, pts, visiteurs, visiteurs_garantis, visiteurs_potentiels, texte_annonce) VALUES ('$id', '$tdate', '$pcentage', '$pts_annonce', '0', '$visiteurs_garantis', '$visiteurs_potentiels', '".addslashes($texte_annonce)."')");
		requete_sql("INSERT INTO visiter_pour_25pts (id_annonceur, id_visiteur) VALUES ('$id', '$id')");
	
		$where = '';
	
		for ($i = 1; $i <= $visiteurs_garantis; $i++) {
				
			if ($i < $visiteurs_garantis) 	{ $where .= "(email = '".$tabmail[$i]['email']."') or "; }
			else 				{ $where .= "(email = '".$tabmail[$i]['email']."')"; }
		}
	
		requete_sql("UPDATE membres SET i_annonces = i_annonces + 1 WHERE $where");
	
		return 1; 
	}
	
	function envoyer_annonce() {
	global $SITE, $envoi_annonce_ok, $annoncer_ok, $tabmail;
	global $id, $pts_annonce, $texte_annonce, $visiteurs_garantis, $visiteurs_potentiels, $nomail;
	
		if ($envoi_annonce_ok) {
	
			$nb_groupe 	= floor($visiteurs_garantis / $SITE['max_mails_bcc']);
	
			$reste 		= $visiteurs_garantis - ($nb_groupe * $SITE['max_mails_bcc']);
	
			$position	= 0;
	
			if ($nb_groupe) {
	
				for ($i = 1; $i <= $nb_groupe; $i++) {
	
					$bcc 	= '';
	
					for ($j = 1; $j <= $SITE['max_mails_bcc']; $j++) {
	
						$position++;
	
						if ($j < $SITE['max_mails_bcc']) 	{ $bcc .= $tabmail[$position]['email'].","; }
						else 					{ $bcc .= $tabmail[$position]['email']; }
					}
	
					envoi_mail_annonceur($id, $pts_annonce, $texte_annonce, $bcc, $nomail);
				}
			}
		
			if ($reste) {
	
				$bcc 		= '';
				$debut		= ($visiteurs_garantis - $reste) + 1;
	
				for ($i = $debut; $i <= $visiteurs_garantis; $i++) {
	
					if ($i < $visiteurs_garantis) 	{ $bcc .= $tabmail[$i]['email'].","; }
					else 				{ $bcc .= $tabmail[$i]['email']; }
				}
		
				envoi_mail_annonceur($id, $pts_annonce, $texte_annonce, $bcc, $nomail);
			}
	
			envoi_mail_confirmation_annonceur($id, $pts_annonce, $texte_annonce, $visiteurs_garantis, $visiteurs_potentiels);	
		}
	}
	
	function envoyer_newsletter() {
	global $SITE, $tabmail, $base_email_pvirgule;
	global $where_sql_newsletter, $titre_annonce, $texte_annonce, $tout_le_monde, $test_newsletter;
	
		$sujet 					= stripslashes($titre_annonce);
		$mess 					= stripslashes($texte_annonce);
		
		$where_sql_newsletter 			= stripslashes($where_sql_newsletter);
	
		if (envoi_annoncernewsletter_ok) {
	
			if ($tout_le_monde) 	{ requete_sql("UPDATE membres SET mail_recu = 0"); }
	
			if (!$test_newsletter)	{
			
				if ($tout_le_monde) { 
	
					inserer_newsletter_envoyee ($titre_annonce, $texte_annonce, $where_sql_newsletter);
				}
	
				$generique 	= 'robot@populassite.com';
			
			} else { $generique 	= 'undavid@wanadoo.fr'; }
	
			$numletter		= nb_results('membres', $where_sql_newsletter);
	
			$nb_destinataires	= $numletter;
	
			$num 			= requete_sql ("SELECT DISTINCT idem, email, id FROM membres where {$where_sql_newsletter}");
	
			$i 			= 1;
	
			while ($i <= $num['count']) {
	
				$tabmail[$i] 	= $num[$i]['email'];
				$tabid[$i] 	= $num[$i]['id'];
	
				if ($i > 1) 	{ $base_email_pvirgule .= "\r\n".$tabmail[$i]; }
				else 		{ $base_email_pvirgule  = $tabmail[1]; }
	
				$i++;
			}
	
			if ($test_newsletter) {
				
				$numletter 	= 1;
				$tabmail[1] 	= 'undavid@wanadoo.fr';
			}
	
			$nb_groupe 		= floor($numletter / $SITE['max_mails_bcc']);
	
			$reste 			= $numletter - ($nb_groupe * $SITE['max_mails_bcc']);
	
			$position		= 0;
	
			if ($nb_groupe) {
	
				for ($i = 1; $i <= $nb_groupe; $i++) {
	
					$bcc 	= '';
	
					for ($j = 1; $j <= $SITE['max_mails_bcc']; $j++) {
	
						$position++;
	
						if ($j < $SITE['max_mails_bcc']) 	{ $bcc	.= $tabmail[$position].","; }
						else 					{ $bcc 	.= $tabmail[$position]; }
	
					}
	
					envoi_mail_newsletter($nb_destinataires, $base_email_pvirgule, $generique, $mess, $bcc, $sujet);
				}
			}
		
			if ($reste > 0) {
	
				$bcc 		= '';
	
				$debut		= ($numletter - $reste) + 1;
	
				for ($i = $debut; $i <= $numletter; $i++) {
	
					if ($i < $numletter) 				{ $bcc 	.= $tabmail[$i].","; }
					else 						{ $bcc 	.= $tabmail[$i]; }
	
				}
		
				envoi_mail_newsletter($nb_destinataires, $base_email_pvirgule, $generique, $mess, $bcc, $sujet);
			}
	
			if (!$test_newsletter) { 
	
				for ($i = 1; $i <= $numletter; $i++) {
	
					if ($i < $numletter) 			{ $les_where_id .= "(id = {$tabid[$i]}) or "; }
					else 					{ $les_where_id .= "(id = {$tabid[$i]})"; }
				}
	
				if (!$numletter)				{ $les_where_id = 1; }
		
				requete_sql("UPDATE membres SET mail_recu = 1 WHERE (".$les_where_id.")");
			}			
		}
	}
	
	function where_id_visiteur_du_groupe_idem ($idem) {
	
		$sql_idem	= '';
	
		$num		= requete_sql("SELECT DISTINCT id_visiteur FROM membres, visiter_pour_25pts WHERE (id_visiteur = membres.id) and (idem = $idem)");
	
		for ($i = 1; $i <= $num['count']; $i++) {
			
			if ($i < $num['count']) { $sql_idem .= "(id_visiteur = ".$num[$i]['id_visiteur'].") or "; }
			else			{ $sql_idem .= "(id_visiteur = ".$num[$i]['id_visiteur'].")"; }
		}
		
		if ($num['count'] == 0) { $sql_idem = "(0)"; }
		
		return $sql_idem;
	}
	
	function where_annonceurs_deja_visites ($idem, $majeur) {
	
		if (!$majeur)	{ $sql_porno = "and (porno = 0)"; }
		else		{ $sql_porno = ''; }
	
		$sql_idem	= where_id_visiteur_du_groupe_idem ($idem);
	
		$num		= requete_sql("SELECT id_annonceur FROM membres, annonceurs, visiter_pour_25pts WHERE (annonceurs.id = membres.id) and (url != 'http://') and (annonceurs.visiteurs < visiteurs_potentiels) and ({$sql_idem}) and (id_annonceur = annonceurs.id) {$sql_porno} ORDER BY annonceurs.pts DESC, annonceurs.id");
	
		$sql_filtrer	= '';
	
		for ($i = 1; $i <= $num['count']; $i++) {
			
			if ($i < $num['count']) { $sql_filtrer .= "(membres.id != ".$num[$i]['id_annonceur'].") and "; }
			else			{ $sql_filtrer .= "(membres.id != ".$num[$i]['id_annonceur'].")"; }
		}
		
		if ($num['count'] == 0)		{ $sql_filtrer  = "(1)"; }
		
		return $sql_filtrer;
	}
	
	function id_annonceur_precedent($url_annonceur) { 
	
		if (isset($_COOKIE['visiteurs_25pts'])) {
			$id_annonceur 	= $_COOKIE['visiteurs_25pts'];
	
		} else {
		
			$num 		= requete_sql("SELECT id FROM membres WHERE url = '$url_annonceur'");
			$id_annonceur 	= $num[1]['id'];
		}
	
		return ($id_annonceur);
	} 
	
	function tester_id_timeout($idem, $id_annonceur) { 
	
		$time_courant = time();
	
		$num = requete_sql("SELECT time, id_annonceur FROM visiter_pour_25pts, membres WHERE (id_visiteur = membres.id) and (membres.idem = '$idem') and (id_annonceur != '$id_annonceur') ORDER BY time DESC");
	
		if ($num) {
	
			$timeoute 	= 0;
			$i		= 0;
	
			while ((!$timeoute) and ($i < $num['count'])) {
				
				$i++;
	
				$time 		= $num[$i]['time'];
				$id_annonceur 	= $num[$i]['id_annonceur'];
	
				$num2		= requete_sql("SELECT pts FROM annonceurs WHERE id = '$id_annonceur'");
	
				$pts_annonce	= $num2[1]['pts'];
	
	       			$ecart_sec	= (60 * ($pts_annonce/10)) - ($time_courant - $time);
	
				if ($ecart_sec >= 0) { $timeoute = 1; }
			}
	
			if (($timeoute) || (isset($_COOKIE['visiteurs_25pts']))) 	{ return ($ecart_sec); } 
			else 								{ return (-$pts_annonce); }
	
		} else 									{ return 0; }
	} 
	
	function retirer_points_annonceur($id_annonceur) {
	
		$num = requete_sql("SELECT pts, visiteurs, visiteurs_garantis, visiteurs_potentiels FROM annonceurs WHERE id = '$id_annonceur'");
	
		$visiteurs_annonc 	= $num[1]['visiteurs']; 
		$visiteurs_garantis 	= $num[1]['visiteurs_garantis']; 
		$visiteurs_potentiels 	= $num[1]['visiteurs_potentiels']; 
		$pts_annonce 		= $num[1]['pts']; 
		
		if (($visiteurs_annonc >= $visiteurs_garantis) and ($visiteurs_potentiels > $visiteurs_garantis)) {
	
			retirer_pts_membre ($id_annonceur, $pts_annonce / 10);
	
			requete_sql ("UPDATE annonceurs SET pts_offerts = pts_offerts + $pts_annonce WHERE id = '$id_annonceur'");
		}
	
		return 1;
	}
	
	function depasser_potentiel($id_annonceur) {
	
		$num = requete_sql("SELECT visiteurs, visiteurs_potentiels FROM annonceurs WHERE id = '$id_annonceur'");
	
		if ($num) {
	
			$visiteurs_annonc 	= $num[1]['visiteurs'];
			$visiteurs_potentiels 	= $num[1]['visiteurs_potentiels'];
			
			if ($visiteurs_annonc > $visiteurs_potentiels) {
			
				return $visiteurs_potentiels;
	
			} else { return 0; }
		
		} else { return 0; }
	} 
	
	function tester_id_timeout2($id_annonceur) {
	
		$num 		= requete_sql("SELECT pts FROM annonceurs WHERE id = '$id_annonceur'");
	
		$pts_annonce 	= $num[1]['pts'];
	
		return ($pts_annonce);
	} 
	
	function inserer_id($id_visiteur, $id_annonceur, $ip) {
	
		$time_courant 	= time();
	
		requete_sql("INSERT INTO visiter_pour_25pts (id_annonceur, id_visiteur, time, ip) VALUES ('$id_annonceur', '$id_visiteur', '$time_courant', '".$ip."')");
		
		return 1;
	}

	function ajouter_pts_membre_actif ($id, $pts) {
	
		$table 	= 'le_plus_actif';
		$where 	= "(id = '$id')";
	
		if (!membre_actif_existant($id)) { $requete = "INSERT INTO $table (id, pts) VALUES ('$id','$pts')"; }
		else 				 { $requete = "UPDATE $table SET pts = pts + $pts WHERE $where"; }
	
		$num	= requete_sql($requete);
	
		return 1;
	}
	
	function ajouter_pts_parrain($id_parrain, $duree) {
	
		ajouter_pts_membre ($id_parrain, $duree);
	
		requete_sql("UPDATE membres SET pts_filleuls_du_jour = pts_filleuls_du_jour + $duree WHERE id = '$id_parrain'");  
	
		return 1;
	}
	
	function rappeler_email($id, $id_annonceur, $key) {
	
		$num = requete_sql("SELECT date FROM annonceurs WHERE id = '$id_annonceur'"); 
	
		if ($num) {
	
			$date_annonceur = $num[1]['date'];
	
			$key_tst 	= clef_annonceur($date_annonceur, $id_annonceur);
	
			if ($key_tst == $key) {
	
				$last_date_visit = date_courante_sql();
	
				requete_sql("UPDATE membres SET last_date_visit = '$last_date_visit' WHERE id = '$id'"); 
				
				return 1;
	
			} else { return 0; }
	
		} else { return 0; }
	}
	
	function ajouter_25_pts($id, $pts_annonce) {
	
		ajouter_pts_membre ($id, $pts_annonce);
		ajouter_pts_membre_actif ($id, $pts_annonce);
	
		requete_sql("UPDATE membres SET participation = participation + $pts_annonce WHERE id = '$id'");  
	
		$id_parrain_tst = retourner_champ_membre($id, 'id_parrain');
	
		if ($id_parrain_tst != 0) {
	
			ajouter_pts_parrain($id_parrain_tst, ceil($pts_annonce / 10)); 
		}
	
		return 1;
	}
	
	function piquer1_url_annonceur($id_annonceur) { 
	
		$url = retourner_champ_membre($id_annonceur, 'url');
	
		return ($url);
	} 
	
	function piquer_url_annonceur($idem, $id_annonceur) {
	
		$time_courant 	= time();
		$str 		= "";
	
		if (isset($_COOKIE['visiteurs_25pts'])) {
	
			$id_annonceur 	= $_COOKIE['visiteurs_25pts'];
	
			$requete 	= "SELECT time, id_annonceur FROM visiter_pour_25pts WHERE (id_annonceur = '$id_annonceur') ORDER BY time";
	
		} else { $requete 	= "SELECT DISTINCT time, id_annonceur FROM visiter_pour_25pts, membres WHERE (id_visiteur = membres.id) and (membres.idem = '$idem') and (id_annonceur != '$id_annonceur') ORDER BY time DESC"; }
	
		$num = requete_sql($requete);
	
		if ($num) {
			
			$timeoute 	= 0;
			$i		= 0;
	
			while ((!$timeoute) and ($i < $num['count'])) {
				
				$i++;
				
				$time 		= $num[$i]['time'];
				$id_annonceur 	= $num[$i]['id_annonceur'];
	
				$num2 		= requete_sql("SELECT pts FROM annonceurs WHERE id = '$id_annonceur'");
	
				$pts_annonce 	= $num2[1]['pts'];
	
				if (($time_courant - $time) <= (60 * $pts_annonce)) {
				
					$timeoute 	= 1;
					$str 		= piquer1_url_annonceur($id_annonceur);
				}
			}
		}
	
		return $str;
	} 

	function is_purge_en_cours() {

		$num 		= requete_sql("SELECT purge_en_cours FROM purger_semaine ORDER BY id DESC LIMIT 1");
	
		$purge_en_cours	= $num[1]['purge_en_cours'];

		if ($purge_en_cours) {
			
			return 1;

		} else {

			return 0;
		}
	}

	function is_transition_masters_results_en_cours() {

		$num 		= requete_sql("SELECT status FROM partenaire_zonedactylo ORDER BY pk DESC LIMIT 1");
	
		$old_status	= $num[1]['status'];

		$status		= return_url("http://www.zonedactylo.com/partners/populassite.php");

		if ($old_status == 'master' && $status == 'results') {
			
			$ok = 1;

		} else {

			$ok = 0;
		}

		if (($status == 'results') || ($status == 'master')) {

			requete_sql("UPDATE partenaire_zonedactylo SET status = '$status'");
		}

		return $ok;
	}

	function is_post_purge_en_cours() {

		$num 		= requete_sql("SELECT post_purge_en_cours FROM purger_semaine ORDER BY id DESC LIMIT 1");
	
		$post_purge_en_cours	= $num[1]['post_purge_en_cours'];

		if ($post_purge_en_cours) {
			
			return 1;

		} else {

			return 0;
		}
	}

	function actualiser_mail_purge($nb_mails, $nb_file_delete) {

		if (!is_purge_en_cours() && is_post_purge_en_cours()) {

			$time_courant	= time();
 
			requete_sql("UPDATE purger_semaine SET nb_mails = nb_mails + $nb_mails, nb_file_delete = nb_file_delete + $nb_file_delete, last_duree_post_purge = $time_courant - last_time_minuit ORDER BY id DESC LIMIT 1");
		}

		return 1;
	}

	function pas_assez_points($id_annonceur) {
	
		$pts_compte 	= retourner_champ_membre ($id_annonceur, 'pts');
	
		$num		= requete_sql("SELECT pts, visiteurs, visiteurs_garantis, visiteurs_potentiels FROM annonceurs WHERE id = '$id_annonceur'");
	
		if ($num) {
	
			$visiteurs_annonc 	= $num[1]['visiteurs'];
			$pts_annonce 		= $num[1]['pts'];
			$visiteurs_garantis 	= $num[1]['visiteurs_garantis'];
			$visiteurs_potentiels 	= $num[1]['visiteurs_potentiels'];
			
			if (($visiteurs_annonc < $visiteurs_potentiels)	and ($visiteurs_annonc >= $visiteurs_garantis)
			and ($pts_annonce > $pts_compte)) 	{ return 1; }
	
			else 					{ return 0; }
		
		} else { return 0; }
	}

	function creer_plateau_texte () {
	global $mise_defieur, $mise_defie, $liste_txt, $creation_plateau, $vect_bool;

		$tab_frac					    = rapport_irreductible_den_max($mise_defieur,$mise_defie,250);
	
		$num						    = $tab_frac['n1'];
		$den						    = $tab_frac['n2'];

		$total						    = $num + $den;

		for ($k = 1; $k <= $num; $k++) {
	
			$pif 				    	    = rand(1,$total);

			if ($vect_bool[$pif]) {

				$k--;

			} else {				

				$vect_bool[$pif]	    	    = true;

				$tab_txt[$pif]		  	    = 1;
			}
		}
	
		for ($k = 1; $k <= $den; $k++) {

			$pif 				    	    = rand(1,$total);

			if ($vect_bool[$pif]) {

				$k--;

			} else {

				$vect_bool[$pif]	    	    = true;

				$tab_txt[$pif]		  	    = 2;
			}
		}

		$liste_txt	 				    = '';
		
		for ($i = 1; $i <= count($tab_txt); $i++) {

			$liste_txt     				   .= $tab_txt[$i];
		}

		return $liste_txt;
	}


	function enregistrer_tab_txt_to_sql ($id_defieur, $id_defie, $_key) {
	global $liste_txt;

		if (accepter_le_defi ($id_defieur, $id_defie, $_key)) {

			$liste_txt				    = creer_plateau_texte();

			requete_sql("UPDATE defieurs SET plateau = '$liste_txt' WHERE (id_defieur = $id_defieur) AND (id_defie = $id_defie) AND (_key = '$_key')");

			return 1;

		} else {

			return 0;
		}
	}	

	function ouvrir_tab_txt_from_sql ($id_defieur, $id_defie, $_key) {

		$num 		= requete_sql("SELECT plateau AS liste_txt FROM defieurs WHERE (id_defieur = $id_defieur) AND (id_defie = $id_defie) AND (_key = '$_key')");

		$liste_txt 	= $num[1]['liste_txt'];
			
		return $liste_txt;
	}	

	function enregistrer_variables_mise_auto($position, $limite, $marge, $enabled, $id) {
	
		if (!miseur_auto_existant($id)) {
	
			requete_sql("INSERT INTO miseur_automatique (id,position,limite,marge,enabled) VALUES ('$id','$position','$limite','$marge','$enabled')");
	
		} else {
		
			requete_sql("UPDATE miseur_automatique SET position = $position, limite = $limite, marge = $marge, enabled = $enabled WHERE id = $id");
		}
	}

	function enregistrer_variables_affichages_sur_le_web($id, $tab_jours, $tab_heures, $verif_total) {
	global $SITE;

		if (!programmeur_affichages_existant($id)) {

			$sql_jours = '';

			for ($i = 1; $i <= 7; $i++) {

				if (!$tab_jours[$i]) { 

					$tab_jours[$i] = 0;
				}

				$sql_jours .= ", '{$tab_jours[$i]}'";
			}

			$sql_heures = '';

			for ($i = 1; $i <= 24; $i++) {

				if (!$tab_heures[$i]) { 

					$tab_heures[$i] = 0;
				}

				$sql_heures .= ", '{$tab_heures[$i]}'";
			}

			$champs_heures = '';

			for ($j = 0; $j <= 23; $j++) {

				$k = $j + 1;

				if ($k == 24) {

					$k = 0;
				}

				$champs_heures .= ", {$j}h_{$k}h";
			}

			if ($verif_total < 31) {
	
				requete_sql("INSERT INTO affichages_sur_le_web (id, lundi, mardi, mercredi, jeudi, vendredi, samedi, dimanche{$champs_heures}) VALUES ('$id'{$sql_jours}{$sql_heures})");
			}
	
		} else {

			$tab_semaine[1] = 'lundi'; $tab_semaine[2] = 'mardi'; 	 $tab_semaine[3] = 'mercredi';
			$tab_semaine[4] = 'jeudi'; $tab_semaine[5] = 'vendredi'; $tab_semaine[6] = 'samedi';
			$tab_semaine[7] = 'dimanche';

			$sql_jours = '';

			for ($i = 1; $i <= 7; $i++) {

				if (!$tab_jours[$i]) {

					$tab_jours[$i] = 0;
				}

				if ($i == 1) {

					$sql_jours = "{$tab_semaine[1]} = $tab_jours[$i]";

				} else {

					$sql_jours .= ", {$tab_semaine[$i]} = $tab_jours[$i]";
				}
			}

			$sql_heures = '';

			for ($i = 1; $i <= 24; $i++) {

				$bi = $i - 1;

				if ($i == 24) { 

					$bs = 0;
				
				} else {
					
					$bs = $i;
				}

				if (!$tab_heures[$i]) {

					$tab_heures[$i] = 0;
				}

				if ($i == 1) {

					$sql_heures = "{$bi}h_{$bs}h = $tab_heures[$i]";

				} else {

					$sql_heures .= ", {$bi}h_{$bs}h = $tab_heures[$i]";
				}
			}

			if ($verif_total < 31) {
	
				requete_sql("UPDATE affichages_sur_le_web SET {$sql_jours}, {$sql_heures} WHERE id = $id");

			} else {

				requete_sql("DELETE FROM affichages_sur_le_web WHERE id = $id");
			}
		}
	}

	function traiter_envoyer_mail_affichages($id) {
	global $SITE, $root;

		include $root."lib/register_globals.php";
		include $root."lib/tri_palette.php";

		$nboutons 		= lire_bouton($id, 'nboutons');
		$nbannieres 		= lire_bouton($id, 'nbannieres');

		$ok_envoi		= retourner_champ_membre($id, 'recevoir_aff_par_email');

		if ($ok_envoi) {

			envoyer_mail_affichages($id, $nboutons, $nbannieres);
		}

		requete_sql("UPDATE membres SET nboutons = 0, nbannieres = 0 WHERE id = $id");

		@unlink($root."cache/nboutons/$id.dat");
		@unlink($root."cache/nbannieres/$id.dat");

		return 1;
	}
}
?>