<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* Original Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)   */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2005 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 off the application for the table appli_log
$al_id = 1;
$al_nom = "Poll";
// ----------------------------------------------------------------------------
function pollCollector($pollID, $voteID, $forwarder) {
     if ($voteID) {
        global $setCookies, $NPDS_Prefix, $HTTP_COOKIE_VARS,$_COOKIE, $al_id, $al_nom, $dns_verif;
        $voteValid="1";
        $result = mysql_query("SELECT timeStamp FROM poll_desc WHERE pollID='$pollID'");
        $object = mysql_fetch_object($result);
        $timeStamp = $object->timeStamp;
        //$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 = mysql_fetch_array(mysql_query($sql))) {
              $voteValid="0";
           }
        }
        if ($voteValid=="1") {
           $ip= getip();
           if ($dns_verif)
              $hostname=@gethostbyaddr($ip);
           @mysql_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')");
           @mysql_query("UPDATE poll_data SET optionCount=optionCount+1 WHERE (pollID='$pollID') AND (voteID='$voteID')");
           @mysql_query("UPDATE poll_desc SET voters=voters+1 WHERE pollID='$pollID'");
        }
     }
     Header("Location: $forwarder");
}

function pollList() {
     global $user, $cookie;
     $result = mysql_query("SELECT pollID, pollTitle, timeStamp, voters FROM poll_desc ORDER BY timeStamp");
     $counter = 0;
     echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
     while($object = mysql_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 = mysql_query("SELECT SUM(optionCount) AS SUM FROM poll_data WHERE pollID='$id'");
        $result = mysql_query("SELECT pollType FROM poll_data WHERE (pollID='$id')");
        list($pollType)=mysql_fetch_row($result);
        $pollType = $pollType%128;
        if ( (($pollType) and (isset($user))) or (!$pollType)) {
           $rowcolor = tablos();
           $sum = (int)mysql_result($result2, 0, "SUM");
           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 $maxOptions, $BarScale, $setCookies;
     global $bargif;
     if (($bargif=="") or (!isset($bargif))) {
        $Lbargif="images/leftbar.gif";
        $Mbargif="images/mainbar.gif";
        $Rbargif="images/rightbar.gif";
     } else {
        $Lbargif=$bargif;
        $Mbargif=$bargif;
        $Rbargif=$bargif;
     }

     if (!isset($pollID) OR empty($pollID)) $pollID = 1;
     $result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM poll_desc WHERE pollID='$pollID'");
     $holdtitle = mysql_fetch_row($result);
     echo "<span class=\"TITREA\">".aff_langue($holdtitle[1])."</span><br /><br />";
     mysql_free_result($result);
     $result = mysql_query("SELECT SUM(optionCount) AS SUM FROM poll_data WHERE pollID='$pollID'");
     $sum = (int)mysql_result($result, 0, "SUM");
     mysql_free_result($result);
     echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
     for ($i = 1; $i <= $maxOptions; $i++) {
        $result = mysql_query("SELECT pollID, optionText, optionCount, voteID FROM poll_data WHERE (pollID='$pollID') AND (voteID='$i')");
        $object = mysql_fetch_object($result);
        if (is_object($object)) {
           $optionText = $object->optionText;
           $optionCount = $object->optionCount;
           if ($optionText!= "") {
              if ($sum) {
                 $percent = 100*$optionCount/$sum;
                 $percentInt = (int)$percent * 3 * $BarScale;
                 $percent2 = (int)$percent;
              } else {
                 $percentInt = 1;
              }
              $rowcolor = tablos();
              echo "<TR $rowcolor>";
              echo "<td>".aff_langue($optionText)."</td>"
              ."<td>"
              ."<img src=\"$Lbargif\" height=\"10\" width=\"7\">"
              ."<img src=\"$Mbargif\" height=\"10\" width=\"$percentInt\">"
              ."<img src=\"$Rbargif\" height=\"10\" width=\"7\">";
              printf(" %.2f %% (%d)", $percent, $optionCount);
              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 $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 = mysql_query("SELECT pollTitle, voters FROM poll_desc WHERE pollID='$pollID'");
   list($pollTitle, $voters) = mysql_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 = mysql_query("SELECT pollID, optionText, optionCount, voteID FROM poll_data WHERE (pollID='$pollID' and optionText<>'') order by voteID");
   $sum = 0;
   if (!$pollClose) {
      while($object=mysql_fetch_array($result)) {
         $boxContent .= "<input type=\"radio\" name=\"voteID\" value=\"".$object[voteID]."\">&nbsp;".aff_langue($object[optionText])."<br />\n";
         $sum = $sum + $object[optionCount];
      }
   } else {
      while($object=mysql_fetch_array($result)) {
         $boxContent .= aff_langue($object[optionText])."\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="$userimg$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) {
      list($numcom) = mysql_fetch_row(mysql_query("select count(*) from pollcomments where pollID='$pollID'"));
      $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\"></div>
         <h3>".$boxTitle."</h3>
         <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=\"middle\" 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") {
    global $bgcolor5;
    if (($bgcolor5=="") or (!isset($bgcolor5))) {
       $bgcolor5="FFFFFF";
    } else {
       $bgcolor5=substr($bgcolor5,1);
    }
    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 ($anonpost==1 OR $admin OR $user) {
             if ($ibid=theme_image("box/comment.gif")) {$imgtmp=$ibid;} elseif ($ibid=theme_image("box/$language/comment.gif")) {$imgtmp=$ibid;} else {$imgtmp="$userimg$language/comment.gif";}
             echo "<p align=\"center\"><a href=\"pollcomments.php?op=Reply&amp;pid=0&amp;pollID=$pollID\"><img src=\"$imgtmp\" border=\"0\" ALT=\"\"></a></p><br />";
          }
          include("pollcomments.php");
       }
       include ("footer.php");
    } else {
       Header("Location: $forwarder");
    }
}
?>