<?php
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* NPDS Copyright (c) 2002-2013 by Philippe Brunier                     */
/*                                                                      */
/* 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; either version 2 of the License.       */
/************************************************************************/
/* 2003 by snipe / vote unique, implémentation de la table appli_log    */
/************************************************************************/
if (!function_exists("Mysql_Connexion")) {
   include ("mainfile.php");
}
// ----------------------------------------------------------------------------
// Specified the index and the name of the application for the table appli_log
$al_id = 1;
$al_nom = "Poll";
// ----------------------------------------------------------------------------
function pollCollector($pollID, $voteID, $forwarder) {
     global $NPDS_Prefix;

     if ($voteID) {
        global $setCookies, $al_id, $al_nom, $dns_verif;

        $voteValid="1";
        $result = sql_query("SELECT timeStamp FROM ".$NPDS_Prefix."poll_desc WHERE pollID='$pollID'");
        list($timeStamp) = sql_fetch_row($result);
        $cookieName = "poll".$NPDS_Prefix.$timeStamp;
        global $$cookieName;
        if ($$cookieName=="1") {
           $voteValid="0";
        } else {
           setcookie("$cookieName","1",time()+86400);
        }

        global $user;
        if ($user) {
           global $cookie;
           $user_req="or al_uid='$cookie[0]'";
        } else {
           $cookie[0]="1";
           $user_req="";
        }
        if ($setCookies=="1") {
           $ip=getip();
           if ($dns_verif)
              $hostname="or al_hostname='".@gethostbyaddr($ip)."' ";
           else
              $hostname="";
           $sql="select al_id from ".$NPDS_Prefix."appli_log where al_id='$al_id' and al_subid='$pollID' and (al_ip='$ip' ".$hostname.$user_req.")";
           if ($result = sql_fetch_row(sql_query($sql))) {
              $voteValid="0";
           }
        }
        if ($voteValid=="1") {
           $ip=getip();
           if ($dns_verif)
              $hostname=@gethostbyaddr($ip);
           else
              $hostname="";
           sql_query("INSERT INTO ".$NPDS_Prefix."appli_log (al_id, al_name, al_subid, al_date, al_uid, al_data, al_ip, al_hostname) VALUES ('$al_id', '$al_nom', '$pollID', now(), '$cookie[0]', '$voteID', '$ip', '$hostname')");
           sql_query("UPDATE ".$NPDS_Prefix."poll_data SET optionCount=optionCount+1 WHERE (pollID='$pollID') AND (voteID='$voteID')");
           sql_query("UPDATE ".$NPDS_Prefix."poll_desc SET voters=voters+1 WHERE pollID='$pollID'");
        }
     }
     Header("Location: $forwarder");
}

function pollList() {
     global $NPDS_Prefix;

     echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
     $result = sql_query("SELECT pollID, pollTitle, voters FROM ".$NPDS_Prefix."poll_desc ORDER BY timeStamp");
     while($object = sql_fetch_assoc($result)) {
        $id = $object['pollID'];
        $pollTitle = $object['pollTitle'];
        $voters = $object['voters'];
        $result2 = sql_query("SELECT SUM(optionCount) AS SUM FROM ".$NPDS_Prefix."poll_data WHERE pollID='$id'");
        $rowcolor = tablos();
        list ($sum) = sql_fetch_row($result2);
        echo("<tr $rowcolor><td> ".aff_langue($pollTitle)."</td><td>");
        echo("(<a href=\"pollBooth.php?op=results&amp;pollID=$id\" class=\"noir\">".translate("Results")."</a> - $sum ".translate("votes").")</td></tr>\n");
     }
     echo "</table>";
}

function pollResults($pollID) {
     global $NPDS_Prefix;
     global $maxOptions, $setCookies;

     if (!isset($pollID) OR empty($pollID)) $pollID = 1;
     $result = sql_query("SELECT pollID, pollTitle, timeStamp FROM ".$NPDS_Prefix."poll_desc WHERE pollID='$pollID'");
     $holdtitle = sql_fetch_row($result);
     echo "<span class=\"titrea\">".aff_langue($holdtitle[1])."</span><br /><br />";
     $result = sql_query("SELECT SUM(optionCount) AS SUM FROM ".$NPDS_Prefix."poll_data WHERE pollID='$pollID'");
     list($sum) = sql_fetch_row($result);
     echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
     for ($i = 1; $i <= $maxOptions; $i++) {
        $result = sql_query("SELECT optionText, optionCount, voteID FROM ".$NPDS_Prefix."poll_data WHERE (pollID='$pollID') AND (voteID='$i')");
        $object = sql_fetch_assoc($result);
        $optionText = $object['optionText'];
        $optionCount = $object['optionCount'];
        if ($optionText!= "") {
           if ($sum) {
              $percent = 100*$optionCount/$sum;
              $percentInt = (int)$percent;
           } else {
              $percentInt = 0;
           }
           $rowcolor = tablos();
           echo "<tr $rowcolor>";
           echo "<td>".aff_langue($optionText)."</td>"
           ."<td width=\"40%\"><div class=\"graph\"><strong class=\"bar\" style=\"width: $percentInt%;\">$percentInt%</strong></div></td><td align=\"left\">(".wrh($optionCount).")</td>\n";
           echo "</tr>";
        }
     }
     echo "</table><br />";
     echo "<p align=\"center\"><b>".translate("Total Votes: ")."$sum</b></p><br />";
     if ($setCookies>0) {
        echo translate("We allow just one vote per poll.")."<br /><br />";
     }
}

#autodoc pollboxbooth($pollID,$pollClose) : Construit le blocs sondages / code du mainfile avec autre présentation
function pollboxbooth($pollID,$pollClose) {
   global $NPDS_Prefix;
   global $maxOptions, $boxTitle, $boxContent, $userimg, $language, $pollcomm;

   if (!isset($pollID))
      $pollID = 1;
   if (!isset($url))
      $url = sprintf("pollBooth.php?op=results&amp;pollID=%d", $pollID);
   $boxContent = "<form action=\"pollBooth.php\" method=\"post\">\n
   <input type=\"hidden\" name=\"pollID\" value=\"".$pollID."\" />\n
   <input type=\"hidden\" name=\"forwarder\" value=\"".$url."\" />\n";
   $result = sql_query("SELECT pollTitle, voters FROM ".$NPDS_Prefix."poll_desc WHERE pollID='$pollID'");
   list($pollTitle, $voters) = sql_fetch_row($result);
   global $block_title;
   if ($block_title=="")
      $boxTitle=translate("Survey");
   else
      $boxTitle=$block_title;
   $boxContent .= "<div class=\"titboxcont\">".aff_langue($pollTitle)."</div>\n";

   $result = sql_query("SELECT pollID, optionText, optionCount, voteID FROM ".$NPDS_Prefix."poll_data WHERE (pollID='$pollID' and optionText<>'') order by voteID");
   $sum = 0;
   if (!$pollClose) {
      while($object=sql_fetch_assoc($result)) {
         $boxContent .= "<input type=\"radio\" name=\"voteID\" value=\"".$object['voteID']."\" />&nbsp;".aff_langue($object['optionText'])."<br />\n";
         $sum = $sum + $object['optionCount'];
      }
   } else {
      while($object=sql_fetch_assoc($result)) {
         $boxContent .= "&nbsp;".aff_langue($object['optionText'])."<br />\n";
         $sum = $sum + $object['optionCount'];
      }
   }
   if (!$pollClose) {
      if ($ibid=theme_image("box/vote.gif")) {$imgtmp=$ibid;} elseif ($ibid=theme_image("box/$language/vote.gif")) {$imgtmp=$ibid;}
     else {$imgtmp="$userimg$language/vote.gif";}
      $inputvote = "<input type=\"image\" src=\"$imgtmp\" title=\"Vote\" alt=\"Vote\" style=\"border:0px;\" />&nbsp;&nbsp;";
   }
    if ($ibid=theme_image("box/result.gif")) {$imgtmp=$ibid;} elseif ($ibid=theme_image("box/$language/result.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/menu/$language/result.gif";}
   $boxContent .= "<p align=\"center\">\n".$inputvote
                 ."<a href=\"pollBooth.php?op=results&amp;pollID=$pollID\"><img src=\"$imgtmp\" title=\"R&eacute;sultats, Results\" alt=\"Results, R&eacute;sultats\" border=\"0\" /></a>\n</p>\n";
   $boxContent .= "</form>";
   $boxContent .= "<ul>\n<li><a href=\"pollBooth.php\">".translate("Past Surveys")."</a></li>\n</ul>\n";
   if ($pollcomm) {
      if (file_exists("modules/comments/pollBoth.conf.php")) {
         include ("modules/comments/pollBoth.conf.php");
      }
      list($numcom) = sql_fetch_row(sql_query("select count(*) from ".$NPDS_Prefix."posts where forum_id='$forum' and topic_id='$pollID' and post_aff='1'"));
      $boxContent .= "<br />\n<ul>\n<li>".translate("Votes: ")." ".$sum."</li>\n<li>".translate("comments:")." ".$numcom."</li>\n</ul>\n";
   } else {
      $boxContent .= "<br />\n<ul>\n<li>".translate("Votes: ")." ".$sum."</li>\n</ul>\n";
   }
   echo "<div id=\"box_poll\">
         <div class=\"bloc_title1\">
         <h3>".$boxTitle."</h3></div>
         <div class=\"bloc_content1\">
         ".$boxContent."
         </div>
         <div class=\"bloc_content2\"></div>
         </div>";
}

function PollMain_aff($pollID) {
   $boxContent = "<p align=\"center\"><b><a href=\"pollBooth.php\">";
   if ($ibid=theme_image("fle_b.gif")) {$imgtmp=$ibid;} else {$imgtmp=false;}
   if ($imgtmp) {
      $boxContent .= "<img alt=\"\" src=\"$imgtmp\" border=\"0\" style=\"vertical-align: middle;\" />";
   }
   $boxContent .= translate("Past Surveys")."</a></b></p>";
   echo $boxContent;
}

if (!isset($pollID)) {
   include ('header.php');
   pollList();
   include ('footer.php');
}
settype($pollID,'integer');
if (isset($forwarder)) {
   if (isset($voteID))
      pollCollector($pollID, $voteID, $forwarder);
   else
      Header("Location: $forwarder");
} elseif ($op=="results") {
   list($ibid,$pollClose)=pollSecur($pollID);
   if ($pollID==$ibid) {
      include ("header.php");
      echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr>";
      if (!$pollClose) {
         $block_title="";
         echo "<td valign=\"top\" width=\"25%\">";
         pollboxbooth($pollID,$pollClose);
         echo "</td><td valign=\"top\">";
      } else {
         echo "<td valign=\"top\" width=\"10%\">ddd</td><td valign=\"top\">";
         PollMain_aff($pollID);
      }
      pollResults($pollID);
      echo "</td></tr></table>";
      if ($pollcomm) {
         if (file_exists("modules/comments/pollBoth.conf.php")) {
            include ("modules/comments/pollBoth.conf.php");
            if ($pollClose==99)
               $anonpost=0;
            include ("modules/comments/comments.php");
         }
      }
      include ("footer.php");
   } else {
      Header("Location: $forwarder");
   }
}
?>