<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* NPDS Copyright (c) 2002-2007 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, $HTTP_COOKIE_VARS,$_COOKIE, $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 = $NPDS_Prefix.$timeStamp;
        if (($HTTP_COOKIE_VARS["$cookieName"]=="1") or ($_COOKIE["$cookieName"]=="1")) {
           $voteValid = "0";
        } else {
           setcookie("$cookieName","1",time()+86400);
        }

        global $user;
        if ($user) {
           global $cookie;
           $user_req="or al_uid='$cookie[0]'";
        } else {
           $user_req="";
        }
        if ($setCookies=="1") {
           $ip=getip();
           if ($dns_verif)
              $hostname="or al_hostname='".@gethostbyaddr($ip)."' ";
           $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);
           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;
     global $user, $cookie;

     $result = sql_query("SELECT pollID, pollTitle, timeStamp, voters FROM ".$NPDS_Prefix."poll_desc ORDER BY timeStamp");
     $counter = 0;
     echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
     while($object = sql_fetch_object($result)) {
        $resultArray[$counter] = array($object->pollID, $object->pollTitle, $object->timeStamp, $object->voters);
        $counter++;
     }
     for ($count = 0; $count < count($resultArray); $count++) {
        $id = $resultArray[$count][0];
        $pollTitle = $resultArray[$count][1];
        $voters = $resultArray[$count][3];
        $result2 = sql_query("SELECT SUM(optionCount) AS SUM FROM ".$NPDS_Prefix."poll_data WHERE pollID='$id'");
        $result = sql_query("SELECT pollType FROM ".$NPDS_Prefix."poll_data WHERE (pollID='$id')");
        list($pollType)=sql_fetch_row($result);
        $pollType = $pollType%128;
        if ( (($pollType) and (isset($user))) or (!$pollType)) {
           $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&amp;mode=$cookie[4]&amp;order=$cookie[5]&amp;thold=$cookie[6]\" 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 />";
     sql_free_result($result);
     $result = sql_query("SELECT SUM(optionCount) AS SUM FROM ".$NPDS_Prefix."poll_data WHERE pollID='$pollID'");
     list($sum) = sql_fetch_row($result);
     sql_free_result($result);
     echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
     for ($i = 1; $i <= $maxOptions; $i++) {
        $result = sql_query("SELECT pollID, optionText, optionCount, voteID FROM ".$NPDS_Prefix."poll_data WHERE (pollID='$pollID') AND (voteID='$i')");
        $object = sql_fetch_object($result);
        if (is_object($object)) {
           $optionText = $object->optionText;
           $optionCount = $object->optionCount;
           if ($optionText!= "") {
              if ($sum) {
                 $percent = 100*$optionCount/$sum;
                 $percentInt = (int)$percent;
              } else {
                 $percentInt = 1;
              }
              $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>";
              echo "</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;
   global $cookie;

   if (!isset($pollID))
      $pollID = 1;
   if (!isset($url))
      $url = sprintf("pollBooth.php?op=results&amp;pollID=%d", $pollID);
   $boxContent = "<br />
     <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\" border=\"0\">&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&amp;mode=$cookie[4]&amp;order=$cookie[5]&amp;thold=$cookie[6]\"><img src=\"$imgtmp\" 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\" align=\"center\" border=\"0\">";
   }
   $boxContent .= translate("Past Surveys")."</a></b></p>";
   echo $boxContent;
}

if (!is_numeric($pollID)) {
   include ('header.php');
   pollList();
   include ('footer.php');
}
if (isset($forwarder)) {
    pollCollector($pollID, $voteID, $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><td valign=\"top\" width=\"15%\">";
      if (!$pollClose) {
         $block_title="";
         pollboxbooth($pollID,$pollClose);
         echo "</td><td valign=\"top\">";
      } else {
         PollMain_aff($pollID);
      }
      pollResults($pollID);
      echo "</td></tr></table>";
      if (($pollcomm) AND ($mode!="nocomments")) {
         if (file_exists("modules/comments/pollBoth.conf.php")) {
            include ("modules/comments/pollBoth.conf.php");
            include ("modules/comments/comments.php");
         }
      }
      include ("footer.php");
   } else {
      Header("Location: $forwarder");
   }
}
?>