Location: PHPKode > projects > PSlash > pslash-0.70/html/pollBooth.php
<?

###############################################################################
# Copyright (C) 2000  Derek Leung
#
# 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, or
# (at your option) any later version.
#
# You may modify your copy or copies of this Program or any portion of it,
# but you must cause the modified files to carry prominent notices stating 
# that you changed the files and the date of any change.  And you are required 
# to keep a copy of this License along with this Program.
#
# You are not required to accept this License, since you have not signed it. 
# However, nothing else grants you permission to modify or distribute this 
# Program or its derivative works. These actions are prohibited by law if 
# you do not accept this License. Therefore, by modifying or distributing 
# this Program (or any work based on this Program), you indicate your 
# acceptance of this License to do so, and all its terms and conditions 
# for copying, distributing or modifying this Program or works based on it.
#
# 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.  
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# See the GNU General Public License for more details.
# http://www.opensource.org/licenses/gpl-license.html
###############################################################################


require("mainfile.php");
global $config;

function pollCollector($pollID, $voteID, $forwarder) {
// checks vote validity and records

	global $config,$myTopic;
	
	// anonymous ?  can we vote?
	$userArray = getCookie($config[user_cookie_name]);
	if (sizeof($userArray) <=0 && $config[anon_users] == 0) {
		errorPage("You have to login before you can vote!",$myTopic);
	}

	// in case $forwarder did not set, we will go back to pollBooth.php after we vote
	if ($forwarder == "") {
		$forwarder = "pollBooth.php?op=results&pollID=$pollID&myTopic=$myTopic";
	}

	// If someone did set the forwarder, we still want to set a defaultLoc if any error occur
	$defaultLoc = "pollBooth.php?op=results&pollID=$pollID&myTopic=$myTopic";

	// get IP 
	$IPaddress = getenv("REMOTE_ADDR");

	// find the topic of the pollID
	$result = mysql_query("select topic from ps_poll_desc where pollID = $pollID");
	if ($result) {
		list($pollTopic) = mysql_fetch_row($result);
	} else {
		// vote on something not exist
		$tmp = "&invalid=true";
		header("Location: $defaultLoc$tmp");
	}

	// find the latest poll of this topic
	$currentPollID = pollLatest($pollTopic);

	// kick out anyone trying to vote on old poll
	if ($pollID != $currentPollID) {
		header("Location: $forwarder&oldPoll=true&myTopic=$myTopic");
		exit;
	}

	// delete vote records which is not the current vote
	$result = mysql_query("delete from ps_poll_log where topic='$pollTopic' and pollID <> $currentPollID");
	if (!$result) { 
		echo mysql_errno(). ": ".mysql_error(). "<br>"; 
		exit;	
	}

	// make the select statement and count if we voted already, and prepare the sql statement to insert to log
	$sql = "SELECT count(*) from ps_poll_log WHERE ";
	$sql2 = "INSERT INTO ps_poll_log VALUES ('$pollID', now(), ";
	if(sizeof($userArray) > 0) {
		$sql .= "((uid='$userArray[userid]') and (pollID=$pollID))";
		$sql2 .= "'$userArray[userid]', ";
	} else {
		$sql .= "(IPaddr='$IPaddress') and (pollID='$pollID')";
		$sql2 .= "'', ";
	}
	$sql2 .= "'$IPaddress','$pollTopic')";
	list($times) = mysql_fetch_row(mysql_query($sql));

	// if we did not vote on this poll before, let do the work
	if(($times == 0) && ($voteID != "")) {
		$result = mysql_query("UPDATE ps_poll_data SET optionCount=optionCount+1 WHERE (pollID=$pollID) AND (voteID=$voteID)");
		if (!$result) { echo mysql_errno(). ": ".mysql_error(). "<br>";	exit(); }
		$result = mysql_query("UPDATE ps_poll_desc SET voters=voters+1 WHERE pollID=$pollID");
		if (!$result) { echo mysql_errno(). ": ".mysql_error(). "<br>";	exit(); }
		$result = mysql_query($sql2);
		if (!$result) { echo mysql_errno(). ": ".mysql_error(). "<br>"; }
		header("Location: $forwarder");

	} elseif ($times > 0) {		
		
		// voted more than 1 time
		$tmp = $config[warnCheaters]>0 ? "&cheated=true" : "";
		header("Location: $forwarder$tmp");
		
	} elseif ($voteID == "") {

		// no voitID?  go to the the defaultLoc
		$tmp = "&invalid=true";
		header("Location: $defaultLoc$tmp");
	} else {
		
		// you shouldn't come into here...but just in case
		$tmp = "&invalid=true";
		header("Location: $defaultLoc$tmp");
	}
}


###########################################################3
// Main start here

	if(!isset($pollID)) {

		// No pollID input, we list the polls
		ps_header("");
		$result	= mysql_query("SELECT pollID, pollTitle, timeStamp FROM ps_poll_desc ORDER BY timeStamp desc");
		$counter = 0;
		while($object = mysql_fetch_object($result)) {
			$resultArray[$counter] = array($object->pollID, $object->pollTitle, $object->timeStamp);
			$counter++;
		}
		if ($result) {
			mysql_free_result($result);
		}

		// show the list of old poll
		echo "<b>" . translate("Please choose a poll from the list below.") . "</b>\n";
		echo("<table width=100% border=0>\n");
		for ($count = 0; $count < count($resultArray); $count++) {
			$id = $resultArray[$count][0];
			$pollTitle = $resultArray[$count][1];
			$timeStamp = $resultArray[$count][2];
			$pollSID = $resultArray[$count][3];
			echo("<tr><td>$pollTitle ");
			echo("(<a href=\"pollBooth.php?op=results&pollID=$id&myTopic=$myTopic\">".translate("results")."</a>");
			if ($pollSID) { 
				echo("|<a href=\"article.php?sid=$pollSID\">".translate("article")."</a>)</td>\n");
			} else {
				echo ")</td>\n";
			}
			echo "</td><td>" . strftime($datestring,$timeStamp) . "</td></tr>\n";
		}
		echo "</table>\n";
		ps_footer("");
		
	} elseif($op == "voteNow" && isset($pollID) ) {
		// we got input, lets vote 
		pollCollector($pollID, $voteID, $forwarder);

	} elseif($op == "results" && $pollID > 0) {
		// display poll results
		ps_header("");
		echo "<center>";
		
		// check if there is any error
		if (!empty($cheated)) { echo errorMsg("Already voted") . "\n"; }
		if (!empty($invalid)) { echo errorMsg("Invalid poll") . "\n"; }
		if (!empty($oldPoll)) { echo errorMsg("No voting on old poll") . "\n"; }

		// find the result
		$result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM ps_poll_desc WHERE pollID=$pollID");
		$holdtitle = mysql_fetch_row($result);
		if ($result) {
			mysql_free_result($result);
		}
		$myPollID = $holdtitle[0];
		echo "<b>$holdtitle[1]</b><br>";
		$result = mysql_query("SELECT SUM(optionCount) AS SUM FROM ps_poll_data WHERE pollID=$pollID");
		$sum = (int)mysql_result($result, 0, "SUM"); 
		mysql_free_result($result);
		echo "<table bgcolor=\"$resultTableBgColor\">";

		// cycle through all options	
		for($i = 1; $i <= $config[maxOptions]; $i++) {
			// select next vote option
			$result = mysql_query("SELECT pollID, optionText, optionCount, voteID FROM ps_poll_data WHERE (pollID=$pollID) AND (voteID=$i)");
			$object = mysql_fetch_object($result);

			if(is_object($object)) {
				$optionText = $object->optionText;
				$optionCount = $object->optionCount;
				echo "<tr>\n";

				if($optionText != "") {
					echo "<td>";
					echo "$optionText";
					echo "</td>";

					if($sum) {
						$percent = 100 * $optionCount * $config[BarScale] / $sum;
					} else {
						$percent = 0;
					}
					echo "<td>";

					if ($percent > 0) {
						$percentInt = (int)$percent * 3;
						echo "<img src=\"imgs/mainpollbar.gif\" height=20 width=$percentInt>";
					}

					printf(" %.2f %% (%d)", $percent, $optionCount);
					echo "</td>\n";
				}
			}
			echo "</tr>\n";
		}

		echo "<tr>\n<td>\n";
		echo translate("Total votes").": $sum";
		echo "\n";
		echo "</td>\n</tr>\n</table>\n";

		// make the rest of the links
		$booth = pollLatest($myTopic);
		echo("[<a href=\"pollBooth.php?pollID=$myPollID&myTopic=$myTopic\">".translate("Voting Booth")."</a> | ");
		echo("<a href=\"pollBooth.php?myTopic=$myTopic\">".translate("Old Polls")."</a> | ");
		echo("<a href=\"index.php\">".translate("Home")."</a>]\n");
		echo "</center>";
		ps_footer("");

	} else {
		// anything else, show the poll form
		ps_header("");
		echo "<center>";
		pollNewest($myTopic);
		echo "</center>";
		ps_footer("");
	}

?>
Return current item: PSlash