Location: PHPKode > projects > Pnyx : des lections virtuelles! > pnyx1.0.1/lib/fonctions.php
<?PHP
	#########################################################################################
	# FFonctions pour base de données de Pnyx												#
	# Auteur : V. Blais																		#
	# Création : 2006-04-05																	#
	# Modification : 2008-09-03																#
	# 																						#
	# Pnyx - une application Web (PHP/MySQL) d'élections virtuelles conçue pour	des	élèves	#
	# du primaire et du secondaire, dans lequel ils sont amenés à faire des	propositions	#
	# en ligne et voter pour leur favorite, sous la supervision d'un enseignant.			#
	#																						#
    # Copyright (C) <2008 - 2009>															#
	# <Service national du RÉCIT de l'univers social de la Commission scolaire de la		#
	# Pointe-de-l'Île>																		#
	#																						#
	# 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.	#
	# 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 as the file LICENSE.txt; if not, please see 									#
	# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.									#
	# 																						#
	# Service national du RÉCIT de l'univers social											#
	# http://www.recitus.qc.ca																#
	# Contact e-mail : steve-hide@address.com												#
	#########################################################################################


########### FONCTIONS DE CONNECTION ET DE REQUETES ##################################

function bd_connexion() {
	//declarer une variable globale pour la connexion 
	global $lbd, $HOTE, $BD, $USAGER, $MOTPASSE;
	
	$lbd = mysql_connect($HOTE,$USAGER, $MOTPASSE)
	#$lbd = mysql_connect("localhost","root","")
	or die("D&eacute;sol&eacute, impossible de se connecter au serveur de donn&eacute;es.");
	
	mysql_select_db($BD)
	or die("D&eacute;sol&eacute, impossible de se connecter &agrave; la base de donn&eacute;es $BD.<br>");
	

}

function bd_executer_sql($requete_sql){
	//	$requete_protegee = mysql_escape_string($requete_sql);
	$reponse_sql = mysql_query($requete_sql)
  		or die("Sorry, the following query is invalid : <br>" 
		. $requete_sql
		. "<br>Erreur num&eacute;ro : " . mysql_errno()
		. "<br>Message d'erreur : " . mysql_error());

	if (strtoupper(substr($requete_sql,0,1)) == "S") {
		return $reponse_sql;
	} else {
	 	$nenregistrements = mysql_affected_rows();
	   return $nenregistrements;
	}
}

############## FONCTIONS DE SÉCURITÉ #############################################
function kill_session(){
	
	$HTTP_SESSION_VARS['SESSION']['niveau'] = "general";

  		if (isset($_COOKIE[session_name()])) {
   			setcookie(session_name(), '', time()-42000, '/');
		}
		@session_destroy(); 
	
 		session_unregister("SESSION");
}


function verifier_session() {
	global $HTTP_SESSION_VARS, $SESSION, $USAGER, $MOTPASSE;
	
	if (! session_is_registered("SESSION")) {
		session_register("SESSION");
		
		$HTTP_SESSION_VARS['SESSION']["message"] = "";
		$HTTP_SESSION_VARS['SESSION']["action"] = "";
		$HTTP_SESSION_VARS['SESSION']["onglet"] = 0;
		$HTTP_SESSION_VARS['SESSION']["script_dem"] = "";
		$HTTP_SESSION_VARS['SESSION']['app'] = array();
		$HTTP_SESSION_VARS['SESSION']["projet_admin"] = 0;
		
		if (!isset($SESSION["niveau"])) {
			
			$HTTP_SESSION_VARS['SESSION']["niveau"] = "general";
			
			$requete_sql = "SELECT projet_id, periode
    						FROM projet
    						WHERE statut = \"actif\"
  							";
			$reponse_sql = bd_executer_sql($requete_sql);
			$nb_projet = mysql_num_rows($reponse_sql);
			
			if ($nb_projet == 0){
				$HTTP_SESSION_VARS['SESSION']["projet"] = "0";
			} else {
				$ligne_projet = mysql_fetch_array($reponse_sql);
			
				$HTTP_SESSION_VARS['SESSION']["projet"] = $ligne_projet['projet_id'];
				$HTTP_SESSION_VARS['SESSION']["periode"] = $ligne_projet['periode'];
			}
			
		}
	}
}

function acces_admin($ce_script) {
	# controle l'acces aux scripts de saisie
	global $HTTP_SESSION_VARS, $SESSION;
	if ($HTTP_SESSION_VARS['SESSION']["niveau"] != "admin") {
		$HTTP_SESSION_VARS['SESSION']["script_dem"] = $ce_script;
		echo "<html><head>";
		echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=index.php\">";
		echo "</head></body>";
		die();
	}

}

function acces_resp($ce_script) {
	# controle l'acces aux scripts de saisie
	global $HTTP_SESSION_VARS, $SESSION;
	$HTTP_SESSION_VARS['SESSION']["script_dem"] = $ce_script;
	
	if ($HTTP_SESSION_VARS['SESSION']["niveau"] != "admin" && 
		$HTTP_SESSION_VARS['SESSION']["niveau"] != "resp") {
		
		echo "<html><head>";
		echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=index.php\">";
		echo "</head></body>";
		die();
	}

}

function acces_eleve($ce_script) {
	# controle l'acces aux scripts de saisie
	global $HTTP_SESSION_VARS, $SESSION;
	$HTTP_SESSION_VARS['SESSION']["script_dem"] = $ce_script;
	
	if ($HTTP_SESSION_VARS['SESSION']["niveau"] != "eleve") {
		
		echo "<html><head>";
		echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=index.php\">";
		echo "</head></body>";
		die();
	}

}

function acces_eleveC($ce_script) {
	# controle l'acces aux scripts de saisie
	global $HTTP_SESSION_VARS, $SESSION;
	$HTTP_SESSION_VARS['SESSION']["script_dem"] = $ce_script;
	
	if ($HTTP_SESSION_VARS['SESSION']["niveau"] != "eleve") {
		
		echo "<html><head>";
		echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=index.php\">";
		echo "</head></body>";
		die();
	} else {
		$ide = $HTTP_SESSION_VARS['SESSION']["usager_id"];
		
		$requete_sql = "SELECT candidat
    					FROM eleve
    					WHERE eleve_id = $ide
  						";
		$reponse_sql = bd_executer_sql($requete_sql);
		$eleve = mysql_fetch_array($reponse_sql);
		
		if ($eleve['candidat'] == 0){
			echo "<html><head>";
			echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=index.php\">";
			echo "</head></body>";
			die();
		}
	}
}

function valider($login,$mdp) {
	# verifier si le nom d'usager et le mot de passe sont corrects
	global $HTTP_SESSION_VARS, $SESSION, $USAGER, $MOTPASSE;
	
 	$requete_sql = "SELECT *
    				FROM usager
    				WHERE login = \"".addslashes($login)."\"
    				AND password = '" . sha1($mdp) . "'
  					";
	$reponse_sql = bd_executer_sql($requete_sql);
	$nb_res = mysql_num_rows($reponse_sql);
	
	if ($nb_res != 0) {
		
		# si le login est valide
		$ligne = mysql_fetch_array($reponse_sql);
		
		# modifier les variables session
		if ($ligne["type"] == "admin") {
#			gen_variables_session();
			$HTTP_SESSION_VARS['SESSION']["niveau"] = "admin";
			$HTTP_SESSION_VARS['SESSION']["projet_admin"] = 0;
			
			if(isset($HTTP_SESSION_VARS['SESSION']["script_dem"])){
				if ($HTTP_SESSION_VARS['SESSION']["script_dem"] != ""){
					$page = $HTTP_SESSION_VARS['SESSION']['script_dem'];
					
					$HTTP_SESSION_VARS['SESSION']['script_dem'] ="";
		
					header("Location: $page");
				} else {
					header("Location: adm_projet.php");
				}
			}
			
		} elseif ($ligne["type"] == "resp") {
			
			$HTTP_SESSION_VARS['SESSION']["niveau"] = "resp";
			
			$requete_sql = "SELECT nom, prenom, resp_id
    				FROM responsable
    				WHERE usager_id = \"$ligne[usager_id]\"
  					";
			$reponse_sql = bd_executer_sql($requete_sql);
			$ligne_resp = mysql_fetch_array($reponse_sql);
#			echo $requete_sql;
				
			$HTTP_SESSION_VARS['SESSION']["nom_complet"] = $ligne_resp['prenom']." ".$ligne_resp['nom'];
			$HTTP_SESSION_VARS['SESSION']["usager_id"] = $ligne_resp['resp_id'];
			
			if ($HTTP_SESSION_VARS['SESSION']["script_dem"] != ""){
				$page = $HTTP_SESSION_VARS['SESSION']['script_dem'];
				$HTTP_SESSION_VARS['SESSION']['script_dem'] ="";
	
				header("Location: $page");
			} else {
				header("Location: rsp_compte.php");
			}
						
		} elseif ($ligne["type"] == "eleve") {
			
			$requete_sql = "SELECT nom, prenom, eleve_id, groupe_id, projet_id
    				FROM eleve
    				WHERE usager_id = \"$ligne[usager_id]\"
  					";
			$reponse_sql = bd_executer_sql($requete_sql);
			$ligne_eleve = mysql_fetch_array($reponse_sql);
			
			if ($ligne_eleve['projet_id'] != $HTTP_SESSION_VARS['SESSION']["projet"]){
			
				echo "<script type=\"text/javascript\" src=\"lib/fonctions.js\"></script>
				<script language=\"JavaScript\">
					no_log_eleve();
				</script>";
			} else {
			
			$HTTP_SESSION_VARS['SESSION']["niveau"] = "eleve";
			$HTTP_SESSION_VARS['SESSION']["nom_complet"] = $ligne_eleve['prenom']." ".$ligne_eleve['nom'];
			$HTTP_SESSION_VARS['SESSION']["usager_id"] = $ligne_eleve['eleve_id'];
			$HTTP_SESSION_VARS['SESSION']["groupe"] = $ligne_eleve['groupe_id'];
			
				if ($HTTP_SESSION_VARS['SESSION']["script_dem"] != ""){
					$page = $HTTP_SESSION_VARS['SESSION']['script_dem'];
					$HTTP_SESSION_VARS['SESSION']['script_dem'] ="";
	
					header("Location: $page");
				} else {
					header("Location: ele_compte.php");
				}
			}	
		}else {
			$HTTP_SESSION_VARS['SESSION']["niveau"] = "general";
			header("Location: index.php");
		}


	}  	else {
	
		echo "<script type=\"text/javascript\" src=\"lib/fonctions.js\"></script>
		<script language=\"JavaScript\">
			no_log();
			window.location=\"".$_SERVER['PHP_SELF']."\";
		</script>";
	}

}
######### FONCTIONS POUR ECOLES.XML #####################################################
/*
function ecole_xml() {
	
	global $HTTP_SESSION_VARS, $SESSION;
	$idp = $HTTP_SESSION_VARS['SESSION']["projet"];
		
		$requete_sql = "SELECT DISTINCT EC.*
						FROM ((((((proposition AS P LEFT JOIN fiche AS F ON P.eleve_id = F.eleve_id)
									LEFT JOIN appreciation AS A ON P.prop_id = A.prop_id)
									LEFT JOIN eleve AS E ON P.eleve_id = E.eleve_id)
									LEFT JOIN usager AS U ON E.usager_id = U.usager_id)
									LEFT JOIN groupe AS G ON E.groupe_id = G.groupe_id)
									LEFT JOIN responsable AS R ON G.resp_id = R.resp_id)
									LEFT JOIN ecole AS EC ON R.ecole_id = EC.ecole_id
						WHERE E.projet_id = $idp
						AND F.valide = 1 
						";
				$reponse_ec = bd_executer_sql($requete_sql);
				$nb_prop = mysql_num_rows($reponse_ec);

		$inputString = "<markers>";
		
	while ($ligne_ec = mysql_fetch_array($reponse_ec)){
	
	$inputString .= "<marker lat=\"".$ligne_ec[latitude]."\" lng=\"".$ligne_ec[longitude]."\" html='&lt;table width=\"100\" border=\"0\"
		 cellspacing=\"5\" cellpadding=\"0\" align=\"left\"&gt;
  		&lt;tr&gt;
    	&lt;td align=\"center\"&gt;&lt;img src=\"images/image13_p.jpg\" width=100 height=103&gt;
		&lt;/td&gt;
 		&lt;/tr&gt;
		&lt;/table&gt;
 		&lt;b&gt;".$ligne_ec[nom]."&lt;/b&gt;&lt;br&gt;
		&lt;font size=\"2\"&gt; - Circonscription électorale&lt;br&gt;provinciale : &lt;b&gt;".$ligne_ec[circonscription]."
		&lt;/b&gt; - &lt;br&gt;
 		Voir les &lt;a href=\"rech_prop2.php?id_ecole=".$ligne_ec[ecole_id]."\"&gt;propositions&lt;/a&gt;&lt;br&gt;
 		créées par les élèves de&lt;br&gt;cette école.&lt;/font&gt;'/>";
	}
	$inputString .= "</markers>";
	$fp = fopen( "ecoles.xml" , "w" );
		if(!$fp)
		{
    		echo "Couldn't open the data file. Try again later.";
    		exit;
		}
fwrite( $fp, $inputString );
fclose( $fp );

}
*/
######### FONCTIONS DE DATES #####################################################

function date_fr($date_complete) {
	$jour = intval(substr($date_complete,8,2));
	$mois = mois_fr(intval(substr($date_complete,5,2)));
	$annee = intval(substr($date_complete,0,4));
	$renvoi = $jour . " " . $mois . " " . $annee;
	return $renvoi;
}

function date_bdd($jour, $mois, $annee) {
	if ($jour < 10) {
		$jour = "0" . $jour;
	}
	if ($mois < 10) {
		$mois = "0" . $mois;
	}
	$renvoi = $annee."-".$mois."-".$jour;
	return $renvoi;
}

function duree($dateD, $dateF){

	$jourD = intval(substr($dateD,8,2));
	$moisD = intval(substr($dateD,5,2));
	$anneeD = intval(substr($dateD,0,4));
	
	$jourF = intval(substr($dateF,8,2));
	$moisF = intval(substr($dateF,5,2));
	$anneeF = intval(substr($dateF,0,4));

	$tFar = mktime (0,0,0,$moisF, $jourF, $anneeF);
  	$tClose = mktime (0,0,0,$moisD, $jourD, $anneeD);
  	$tDistance = $tFar - $tClose;
  	return round ($tDistance/(24*60*60));


}

function mois_fr($moisn) {
	if ($moisn == 1) {
		return "janvier";
	} elseif ($moisn == 2) {
		return "f&eacute;vrier";
	} elseif ($moisn == 3) {
		return "mars";
	} elseif ($moisn == 4) {
		return "avril";
	} elseif ($moisn == 5) {
		return "mai";
	} elseif ($moisn == 6) {
		return "juin";
	} elseif ($moisn == 7) {
		return "juillet";
	} elseif ($moisn == 8) {
		return "ao&ucirc;t";
	} elseif ($moisn == 9) {
		return "septembre";
	} elseif ($moisn == 10) {
		return "octobre";
	} elseif ($moisn == 11) {
		return "novembre";
	} elseif ($moisn == 12) {
		return "d&eacute;cembre";

	}
}

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

    return $string;
}

######### FONCTION AFFICHAGE DES VARIABLES ####################################################

function gen_variables_get() {
	// affiche les variables get
	echo "<p>Liste des variables get</p>";
	global $HTTP_GET_VARS;
	foreach ($HTTP_GET_VARS as $key => $value) {
		echo "$key = $value<br>";
	}	
}


function gen_variables_post() {
	// affiche les variables post
	echo "<p>Liste des variables de post</p>";
	global $HTTP_POST_VARS;
	foreach ($HTTP_POST_VARS as $key => $value) {
		echo "$key = $value<br>";
	}	

}

function gen_variables_postF() {
	// affiche les variables post
	echo "<p>Liste des variables de post</p>";
	global $HTTP_POST_FILES;
	foreach ($HTTP_POST_FILES as $key => $value) {
		echo "$key = $value<br>";
	}	

}

function gen_variables_session() {
	// affiche les variables de session
	echo "<p>Liste des variables de session</p>";
	global $HTTP_SESSION_VARS;
	foreach ($HTTP_SESSION_VARS as $sess) {
		echo "variable de session : $sess<br>";
		foreach ($sess as $key=>$value) {
			echo "$key = $value<br>";
		}
	}	

}

function gen_variables_cookie() {
	// affiche les variables cookie
	echo "<p>Liste des variables cookie</p>";
	global $HTTP_COOKIE_VARS;
	foreach ($HTTP_COOKIE_VARS as $key => $value) {
		echo "$key = $value<br>";
	}	
}

function gen_variables_globales() {
	// affiche les variables globales
	foreach ($GLOBALS as $key => $value) {
		echo "$key = $value<br>";
	}	
}


?>
Return current item: Pnyx : des lections virtuelles!