diff --git a/studs.php b/studs.php index 93e8ac0d52be31c8fd6c0419ee50ec1639cc04a2..43badfec6c90e86c9e2c4b65ee4cf907a25bb758 100644 --- a/studs.php +++ b/studs.php @@ -51,23 +51,23 @@ include_once('fonctions.php'); $numsondage = false; //On récupère le numéro de sondage par le lien web. -if(isset($_GET["sondage"])) { - $numsondage=$_GET["sondage"]; +if(issetAndNoEmpty('sondage', $_GET) === true) { + $numsondage = $_GET["sondage"]; $_SESSION["numsondage"] = $numsondage; } -if(isset($_POST["sondage"])) { - $numsondage=$_POST["sondage"]; +if(issetAndNoEmpty('sondage') === true) { + $numsondage = $_POST["sondage"]; $_SESSION["numsondage"] = $numsondage; -} elseif(isset($_COOKIE["sondage"])) { - $numsondage=$_COOKIE["sondage"]; -} elseif(isset($_SESSION["numsondage"])) { - $numsondage=$_SESSION["numsondage"]; +} elseif(issetAndNoEmpty('sondage', $_COOKIE) === true) { + $numsondage = $_COOKIE["sondage"]; +} elseif(issetAndNoEmpty('numsondage', $_SESSION) === true) { + $numsondage = $_SESSION["numsondage"]; } -if ($numsondage) { +if ($numsondage !== false) { $dsondage = get_sondage_from_id($numsondage); - if(!$dsondage) { + if($dsondage === false) { $err |= NO_POLL; } } else { @@ -75,12 +75,12 @@ if ($numsondage) { } //output a CSV and die() -if(isset($_GET['export']) && $Sondage) { +if(issetAndNoEmpty('export', $_GET) && $dsondage !== false) { if($_GET['export'] == 'csv') { require_once('exportcsv.php'); } - if($_GET['export'] == 'ics' && $Sondage->is_date) { + if($_GET['export'] == 'ics' && $dsondage->is_date) { require_once('exportics.php'); } @@ -88,28 +88,36 @@ if(isset($_GET['export']) && $Sondage) { } // quand on ajoute un commentaire utilisateur -if(isset($_POST['ajoutcomment'])) { - if (isset($_SERVER['REMOTE_USER'])) { - $comment_user = $_SESSION['nom']; - } elseif(isset($_POST["commentuser"]) && ! empty($_POST["commentuser"])) { - $comment_user = $connect->qstr(strip_tags($_POST["commentuser"])); +if(isset($_POST['ajoutcomment']) || isset($_POST['ajoutcomment_x'])) { + if (isset($_SESSION['nom'])) { + // Si le nom vient de la session, on le de-htmlentities + $comment_user = html_entity_decode($_SESSION['nom'], ENT_QUOTES, 'UTF-8'); + } elseif(issetAndNoEmpty('commentuser')) { + $comment_user = $_POST["commentuser"]; } elseif(isset($_POST["commentuser"])) { $err |= COMMENT_USER_EMPTY; } else { $comment_user = _('anonyme'); } - if(empty($_POST["comment"])) { + if(issetAndNoEmpty('comment') === false) { $err |= COMMENT_EMPTY; } - if (isset($_POST["comment"]) && ! is_error(COMMENT_EMPTY) && ! is_error(NO_POLL) && ! is_error(COMMENT_USER_EMPTY)) { - if( ! $connect->Execute('INSERT INTO comments ' . - '(id_sondage, comment, usercomment) VALUES ("'. - $numsondage . '","'. - $connect->qstr(strip_tags($_POST['comment'])). - '","' . - $comment_user .'")') ) { + if (isset($_POST["comment"]) && !is_error(COMMENT_EMPTY) && !is_error(NO_POLL) && !is_error(COMMENT_USER_EMPTY)) { + // protection contre les XSS : htmlentities + $comment = htmlentities($_POST['comment'], ENT_QUOTES, 'UTF-8'); + $comment_user = htmlentities($comment_user, ENT_QUOTES, 'UTF-8'); + + $sql = 'INSERT INTO comments (id_sondage, comment, usercomment) VALUES ('. + $connect->Param('id_sondage').','. + $connect->Param('comment').','. + $connect->Param('comment_user').')'; + + $sql = $connect->Prepare($sql); + $comments = $connect->Execute($sql, array($numsondage, $comment, $comment_user)); + + if ($comments === false) { $err |= COMMENT_INSERT_FAILED; } } @@ -117,15 +125,19 @@ if(isset($_POST['ajoutcomment'])) { // Action quand on clique le bouton participer -$user_studs=$connect->Execute("SELECT * FROM user_studs WHERE id_sondage='$numsondage' ORDER BY id_users"); -$nbcolonnes=substr_count($dsondage->sujet,',')+1; -if (!is_error(NO_POLL) && isset($_POST["boutonp"])) { +$sql = 'SELECT * FROM user_studs WHERE id_sondage='.$connect->Param('numsondage').' ORDER BY id_users'; +$sql = $connect->Prepare($sql); +$user_studs = $connect->Execute($sql, array($numsondage)); + +$nbcolonnes = substr_count($dsondage->sujet, ',') + 1; +if (!is_error(NO_POLL) && (isset($_POST["boutonp"]) || isset($_POST["boutonp_x"]))) { //Si le nom est bien entré - if (! isset($_POST["nom"]) || empty($_POST['nom'])) { + if (issetAndNoEmpty('nom') === false) { $err |= NAME_EMPTY; } - if(!is_error(NAME_EMPTY) && (!isset($_SERVER['REMOTE_USER']) || $_POST["nom"] == $_SESSION["nom"])) { + if(!is_error(NAME_EMPTY) && (!isset($_SERVER['REMOTE_USER']) || $_POST["nom"] == $_SESSION["nom"])) { + $nouveauchoix = ''; for ($i=0;$i<$nbcolonnes;$i++) { // Si la checkbox est enclenchée alors la valeur est 1 if (isset($_POST["choix$i"]) && $_POST["choix$i"] == '1') { @@ -134,21 +146,29 @@ if (!is_error(NO_POLL) && isset($_POST["boutonp"])) { $nouveauchoix.="0"; } } + + $nom=substr($_POST["nom"],0,64); + + // protection contre les XSS : htmlentities + $nom = htmlentities($nom, ENT_QUOTES, 'UTF-8'); while($user = $user_studs->FetchNextObject(false)) { - if ($_POST["nom"]==$user->nom) { + if ($nom == $user->nom) { $err |= NAME_TAKEN; } } // Ecriture des choix de l'utilisateur dans la base - if (!is_error(NAME_TAKEN) && ! is_error(NAME_EMPTY)) { - // TODO: throw error - $nom=substr($_POST["nom"],0,64); - $connect->Execute('INSERT INTO user_studs (nom,id_sondage,reponses) VALUES ("' . - $nom . '","' . - $numsondage . '","' . - $nouveauchoix . '")'); + if (!is_error(NAME_TAKEN) && !is_error(NAME_EMPTY)) { + + $sql = 'INSERT INTO user_studs (nom,id_sondage,reponses) VALUES ('. + $connect->Param('nom').', '. + $connect->Param('numsondage').', '. + $connect->Param('nouveauchoix').')'; + $sql = $connect->Prepare($sql); + + // Todo : Il faudrait lever une erreur en cas d'erreur d'insertion + $connect->Execute($sql, array($nom, $numsondage, $nouveauchoix)); if ($dsondage->mailsonde || /* compatibility for non boolean DB */ $dsondage->mailsonde=="yes" || $dsondage->mailsonde=="true") { $headers="From: ".NOMAPPLICATION." <".ADRESSEMAILADMIN.">\r\nContent-Type: text/plain; charset=\"UTF-8\"\nContent-Transfer-Encoding: 8bit"; @@ -156,7 +176,7 @@ if (!is_error(NO_POLL) && isset($_POST["boutonp"])) { "[".NOMAPPLICATION."] "._("Poll's participation")." : $dsondage->titre", "\"$nom\" ". _("has filled a line.\nYou can find your poll at the link") . " :\n\n". - get_server_name()."/studs.php?sondage=$numsondage \n\n" . + getUrlSondage($numsondage)." \n\n" . _("Thanks for your confidence.") . "\n". NOMAPPLICATION, $headers); } @@ -223,7 +243,7 @@ echo _("Initiator of the poll") .' : '.$dsondage->nom_admin.'<br><br>'."\n"; //affichage des commentaires du sondage if ($dsondage->commentaires) { echo _("Comments") .' :<br>'."\n"; - $commentaires=$dsondage->commentaires; + $commentaires = $dsondage->commentaires; $commentaires=str_replace("\\","",$commentaires); echo $commentaires; echo '<br>'."\n"; @@ -234,6 +254,7 @@ echo '</div>'."\n"; echo '<form name="formulaire" action="studs.php"'.'#bas" method="POST" onkeypress="javascript:process_keypress(event)">'."\n"; echo '<input type="hidden" name="sondage" value="' . $numsondage . '"/>'; +// Todo : add CSRF protection echo '<div class="cadre"> '."\n"; echo _("If you want to vote in this poll, you have to give your name, choose the values that fit best for you<br>(without paying attention to the choices of the other voters) and validate with the plus button at the end of the line.") ."\n"; echo '<br><br>'."\n"; @@ -242,25 +263,26 @@ echo '<br><br>'."\n"; echo '<table class="resultats">'."\n"; //On récupere les données et les sujets du sondage -$nblignes=$user_studs->RecordCount(); +$nblignes = $user_studs->RecordCount(); //on teste pour voir si une ligne doit etre modifiée $testmodifier = false; $ligneamodifier = -1; for ($i=0;$i<$nblignes;$i++) { if (isset($_POST["modifierligne$i"]) || isset($_POST['modifierligne'.$i.'_x'])) { - $ligneamodifier=$i; + $ligneamodifier = $i; } //test pour voir si une ligne est a modifier - if ($_POST['validermodifier'.$i.'_x']) { - $modifier=$i; - $testmodifier=true; + if (isset($_POST['validermodifier'.$i]) || isset($_POST['validermodifier'.$i.'_x'])) { + $modifier = $i; + $testmodifier = true; } } //si le test est valide alors on affiche des checkbox pour entrer de nouvelles valeurs if ($testmodifier) { + $nouveauchoix = ''; for ($i=0;$i<$nbcolonnes;$i++) { //recuperation des nouveaux choix de l'utilisateur if (isset($_POST["choix$i"]) && $_POST["choix$i"] == 1) { @@ -271,13 +293,16 @@ if ($testmodifier) { } $compteur=0; - while ($data=$user_studs->FetchNextObject(false) ) { + while ($data = $user_studs->FetchNextObject(false) ) { //mise a jour des données de l'utilisateur dans la base SQL - if ($compteur==$modifier) { - $connect->Execute("UPDATE user_studs SET reponses='$nouveauchoix' WHERE nom='$data->nom' AND id_users='$data->id_users'"); + if ($compteur == $modifier) { + $sql = 'UPDATE user_studs SET reponses='.$connect->Param('nouveauchoix').' WHERE nom='.$connect->Param('nom').' AND id_users='.$connect->Param('id_users'); + $sql = $connect->Prepare($sql); + $connect->Execute($sql, array($nouveauchoix, $data->nom, $data->id_users)); + if ($dsondage->mailsonde=="yes") { $headers="From: ".NOMAPPLICATION." <".ADRESSEMAILADMIN.">\r\nContent-Type: text/plain; charset=\"UTF-8\"\nContent-Transfer-Encoding: 8bit"; - mail ("$dsondage->mail_admin", "[".NOMAPPLICATION."] " . _("Poll's participation") . " : $dsondage->titre", "\"$data->nom\""."" . _("has filled a line.\nYou can find your poll at the link") . " :\n\n".get_server_name()."/studs.php?sondage=$numsondage \n\n" . _("Thanks for your confidence.") . "\n".NOMAPPLICATION,$headers); + mail ("$dsondage->mail_admin", "[".NOMAPPLICATION."] " . _("Poll's participation") . " : $dsondage->titre", "\"$data->nom\""."" . _("has filled a line.\nYou can find your poll at the link") . " :\n\n".getUrlSondage($numsondage)." \n\n" . _("Thanks for your confidence.") . "\n".NOMAPPLICATION,$headers); } } @@ -286,10 +311,12 @@ if ($testmodifier) { } //recuperation des utilisateurs du sondage -$user_studs=$connect->Execute("SELECT * FROM user_studs WHERE id_sondage='$numsondage' ORDER BY id_users"); +$sql = 'SELECT * FROM user_studs WHERE id_sondage='.$connect->Param('numsondage').' ORDER BY id_users'; +$sql = $connect->Prepare($sql); +$user_studs = $connect->Execute($sql, array($numsondage)); //reformatage des données des sujets du sondage -$toutsujet=explode(",",$dsondage->sujet); +$toutsujet = explode(",",$dsondage->sujet); //si le sondage est un sondage de date if ($dsondage->format=="D"||$dsondage->format=="D+") { @@ -300,7 +327,7 @@ if ($dsondage->format=="D"||$dsondage->format=="D+") { //affichage des années $colspan=1; for ($i=0;$i<count($toutsujet);$i++) { - if (date('Y', intval($toutsujet[$i])) == date('Y', intval($toutsujet[$i+1]))) { + if (isset($toutsujet[$i+1]) && date('Y', intval($toutsujet[$i])) == date('Y', intval($toutsujet[$i+1]))) { $colspan++; } else { echo '<td colspan='.$colspan.' class="annee">'.date('Y', intval($toutsujet[$i])).'</td>'."\n"; @@ -317,8 +344,13 @@ if ($dsondage->format=="D"||$dsondage->format=="D+") { for ($i=0;$i<count($toutsujet);$i++) { // intval() est utiliser pour supprimer le suffixe @* qui déplaît logiquement à strftime() $cur = intval($toutsujet[$i]); - $next = intval($toutsujet[$i+1]); - if (strftime("%B", $cur) == strftime("%B", $next)&&date('Y',$cur)==date('Y',$next)) { + if (isset($toutsujet[$i+1]) === false) { + $next = false; + } else { + $next = intval($toutsujet[$i+1]); + } + + if ($next && strftime("%B", $cur) == strftime("%B", $next) && date('Y', $cur) == date('Y', $next)) { $colspan++; } else { if ($_SESSION["langue"]=="EN") { // because strftime doesn't support english suffix (like st,nd,rd,th) @@ -338,8 +370,12 @@ if ($dsondage->format=="D"||$dsondage->format=="D+") { $colspan=1; for ($i=0;$i<count($toutsujet);$i++) { $cur = intval($toutsujet[$i]); - $next = intval($toutsujet[$i+1]); - if (strftime("%a %e",$cur)==strftime("%a %e",$next)&&strftime("%B",$cur)==strftime("%B",$next)) { + if (isset($toutsujet[$i+1]) === false) { + $next = false; + } else { + $next = intval($toutsujet[$i+1]); + } + if ($next && strftime("%a %e", $cur) == strftime("%a %e", $next) && strftime("%B", $cur) == strftime("%B", $next)) { $colspan++; } else { if ($_SESSION["langue"]=="EN") { @@ -359,9 +395,13 @@ if ($dsondage->format=="D"||$dsondage->format=="D+") { echo '<tr>'."\n"; echo '<td></td>'."\n"; - for ($i=0;$toutsujet[$i];$i++) { + for ($i=0; isset($toutsujet[$i]); $i++) { $heures=explode("@",$toutsujet[$i]); - echo '<td class="heure">'.$heures[1].'</td>'."\n"; + if (isset($heures[1]) === true) { + echo '<td class="heure">'.$heures[1].'</td>'."\n"; + } else { + echo '<td class="heure"></td>'."\n"; + } } echo '</tr>'."\n"; @@ -373,7 +413,7 @@ if ($dsondage->format=="D"||$dsondage->format=="D+") { echo '<tr>'."\n"; echo '<td></td>'."\n"; - for ($i=0;$toutsujet[$i];$i++) { + for ($i=0; isset($toutsujet[$i]); $i++) { echo '<td class="sujet">'.$toutsujet[$i].'</td>'."\n"; } @@ -381,13 +421,13 @@ if ($dsondage->format=="D"||$dsondage->format=="D+") { } //Usager pré-authentifié dans la liste? -$user_mod = FALSE; +$user_mod = false; //affichage des resultats actuels $somme = array(); $compteur = 0; -while ($data=$user_studs->FetchNextObject(false)) { +while ($data = $user_studs->FetchNextObject(false)) { echo '<tr>'."\n"; echo '<td class="nom">'; @@ -396,7 +436,7 @@ while ($data=$user_studs->FetchNextObject(false)) { echo $nombase.'</td>'."\n"; // Les réponses qu'il a choisit - $ensemblereponses=$data->reponses; + $ensemblereponses = $data->reponses; // ligne d'un usager pré-authentifié $mod_ok = !isset($_SERVER['REMOTE_USER']) || ($nombase == $_SESSION['nom']); @@ -413,9 +453,12 @@ while ($data=$user_studs->FetchNextObject(false)) { echo ' /></td>'."\n"; } else { - $car=substr($ensemblereponses,$k,1); - if ($car=="1") { + $car = substr($ensemblereponses, $k, 1); + if ($car == "1") { echo '<td class="ok">OK</td>'."\n"; + if (isset($somme[$k]) === false) { + $somme[$k] = 0; + } $somme[$k]++; } else { echo '<td class="non"></td>'."\n"; @@ -430,8 +473,8 @@ while ($data=$user_studs->FetchNextObject(false)) { //demande de confirmation pour modification de ligne for ($i=0;$i<$nblignes;$i++) { - if (isset($_POST["modifierligne$i"])||isset($_POST['modifierligne'.$i.'_x'])) { - if ($compteur==$i) { + if (isset($_POST["modifierligne$i"]) || isset($_POST['modifierligne'.$i.'_x'])) { + if ($compteur == $i) { echo '<td class="casevide"><input type="image" name="validermodifier'.$compteur.'" value="Valider la modification" src="images/accept.png" ></td>'."\n"; } } @@ -445,9 +488,8 @@ while ($data=$user_studs->FetchNextObject(false)) { if (!isset($_SERVER['REMOTE_USER']) || !$user_mod) { echo '<tr>'."\n"; echo '<td class="nom">'."\n"; - if (isset($_SERVER['REMOTE_USER'])) { - echo '<input type=hidden name="nom" value="'. - $_SESSION['nom'].'">'.$_SESSION['nom']."\n"; + if (isset($_SESSION['nom'])) { + echo '<input type=hidden name="nom" value="'.$_SESSION['nom'].'">'.$_SESSION['nom']."\n"; } else { echo '<input type=text name="nom" maxlength="64">'."\n"; } @@ -471,13 +513,15 @@ if (!isset($_SERVER['REMOTE_USER']) || !$user_mod) { //determination de la meilleure date // On cherche la meilleure colonne -for ($i=0;$i<$nbcolonnes;$i++) { - if ($i=="0") { - $meilleurecolonne=$somme[$i]; - } - - if ($somme[$i]>$meilleurecolonne) { - $meilleurecolonne=$somme[$i]; +for ($i=0; $i < $nbcolonnes; $i++) { + if (isset($somme[$i]) === true) { + if ($i == "0") { + $meilleurecolonne = $somme[$i]; + } + + if (isset($meilleurecolonne) === false || $somme[$i] > $meilleurecolonne) { + $meilleurecolonne = $somme[$i]; + } } } @@ -485,10 +529,15 @@ for ($i=0;$i<$nbcolonnes;$i++) { echo '<tr>'."\n"; echo '<td align="right">'. _("Addition") .'</td>'."\n"; -for ($i=0;$i<$nbcolonnes;$i++) { - $affichesomme=$somme[$i]; - if ($affichesomme=="") { - $affichesomme="0"; +for ($i=0; $i < $nbcolonnes; $i++) { + if (isset($somme[$i]) === true) { + $affichesomme = $somme[$i]; + + if ($affichesomme == "") { + $affichesomme = '0'; + } + } else { + $affichesomme = '0'; } echo '<td class="somme">'.$affichesomme.'</td>'."\n"; @@ -497,8 +546,9 @@ for ($i=0;$i<$nbcolonnes;$i++) { echo '</tr>'."\n"; echo '<tr>'."\n"; echo '<td class="somme"></td>'."\n"; -for ($i=0;$i<$nbcolonnes;$i++) { - if ($somme[$i]==$meilleurecolonne&&$somme[$i]) { + +for ($i=0; $i < $nbcolonnes; $i++) { + if (isset($somme[$i]) && isset($meilleurecolonne) && $somme[$i] == $meilleurecolonne) { echo '<td class="somme"><img src="images/medaille.png" alt="' . _('Best choice') . '"></td>'."\n"; } else { echo '<td class="somme"></td>'."\n"; @@ -516,11 +566,13 @@ $toutsujet=str_replace("°","'",$toutsujet); // On compare le nombre de résultat avec le meilleur et si le résultat est égal // on concatene le resultat dans $meilleursujet $compteursujet=0; -for ($i=0;$i<$nbcolonnes;$i++) { - if ($somme[$i]==$meilleurecolonne) { +$meilleursujet = ''; + +for ($i = 0; $i < $nbcolonnes; $i++) { + if (isset($somme[$i]) && isset($meilleurecolonne) && $somme[$i] == $meilleurecolonne) { $meilleursujet.=", "; if ($dsondage->format=="D"||$dsondage->format=="D+") { - $meilleursujetexport=$toutsujet[$i]; + $meilleursujetexport = $toutsujet[$i]; if (strpos($toutsujet[$i],'@') !== false) { $toutsujetdate=explode("@",$toutsujet[$i]); if ($_SESSION["langue"]=="EN") { @@ -536,36 +588,39 @@ for ($i=0;$i<$nbcolonnes;$i++) { } } } else { - $meilleursujet.=$toutsujet[$i]; + $meilleursujet .= $toutsujet[$i]; } $compteursujet++; } } -$meilleursujet=substr("$meilleursujet",1); +$meilleursujet=substr("$meilleursujet", 1); $vote_str = _('vote'); -if ($meilleurecolonne > 1) { +if (isset($meilleurecolonne) && $meilleurecolonne > 1) { $vote_str = _('votes'); } echo '<p class="affichageresultats">'."\n"; // Affichage du meilleur choix -if ($compteursujet=="1"&&$meilleurecolonne) { +if ($compteursujet == "1" && isset($meilleurecolonne)) { print '<img src="images/medaille.png" alt="Meilleur choix"> ' . _('The best choice at this time is:') . "<b>$meilleursujet</b> " . _('with') . " <b>$meilleurecolonne </b>" . $vote_str . ".\n"; -} elseif ($meilleurecolonne) { +} elseif (isset($meilleurecolonne)) { print '<img src="images/medaille.png" alt="Meilleur choix"> ' . _('The bests choices at this time are:') . " <b>$meilleursujet</b> " . _('with') . " <b>$meilleurecolonne </b>" . $vote_str . ".\n"; } echo '</p>'; //affichage des commentaires des utilisateurs existants -$comment_user=$connect->Execute("select * from comments where id_sondage='$numsondage' order by id_comment"); +$sql = 'select * from comments where id_sondage='.$connect->Param('numsondage').' order by id_comment'; +$sql = $connect->Prepare($sql); +$comment_user=$connect->Execute($sql, array($numsondage)); + if ($comment_user->RecordCount() != 0) { print "<br><b>" . _("Comments of polled people") . " :</b><br>\n"; - while( $dcomment=$comment_user->FetchNextObject(false)) { + while($dcomment = $comment_user->FetchNextObject(false)) { print $dcomment->usercomment . ' : ' . $dcomment->comment . '<br />'; } } @@ -573,10 +628,10 @@ if ($comment_user->RecordCount() != 0) { //affichage de la case permettant de rajouter un commentaire par les utilisateurs print '<div class="addcomment">' .'<p>' ._("Add a comment in the poll:") . '</p>' . "\n"; -if (isset($_SERVER['REMOTE_USER']) || isset($_SESSION['nom'])) { +if (isset($_SESSION['nom']) === false) { echo _("Name") .' : '; echo '<input type="text" name="commentuser" maxlength="64" /><br>'."\n"; -} +} echo '<textarea name="comment" rows="2" cols="40"></textarea>'."\n"; echo '<input type="image" name="ajoutcomment" value="Ajouter un commentaire" src="images/accept.png" alt="Valider"><br>'."\n";