ssi_showPoll & ssi_pollVote Issue

Started by Darkness7148, January 31, 2007, 05:10:14 PM

Previous topic - Next topic

Darkness7148

I seem to be having a problem with ssi_showPoll. I've got it setup on my website's homepage but when I or anybody tries to vote, it goes to a blank page: e.g. http://www.mysite.comforum/SSI.php?ssi_function=pollVote

The vote doesn't get recorded and the person doesn't get redirected to the topic.

I changed the code in ssi_showPoll slightly but even if I use the orginal code, it still does the same thing. This is what my ssi_showPoll and ssi_pollVote look like in SSI.php:

function ssi_showPoll($topic = null, $output_method = 'echo')
{
global $db_prefix, $txt, $ID_MEMBER, $settings, $boardurl, $sc, $user_info;
global $context;

$boardsAllowed = boardsAllowedTo('poll_view');

if (empty($boardsAllowed))
return array();

if ($topic === null && isset($_REQUEST['ssi_topic']))
$topic = (int) $_REQUEST['ssi_topic'];
else
$topic = (int) $topic;

$request = db_query("
SELECT
p.ID_POLL, p.question, p.votingLocked, p.hideResults, p.expireTime, p.maxVotes
FROM ({$db_prefix}topics AS t, {$db_prefix}polls AS p, {$db_prefix}boards AS b)
WHERE p.ID_POLL = t.ID_POLL
AND t.ID_TOPIC = $topic
AND b.ID_BOARD = t.ID_BOARD
AND $user_info[query_see_board]" . (!in_array(0, $boardsAllowed) ? "
AND b.ID_BOARD IN (" . implode(', ', $boardsAllowed) . ")" : '') . "
LIMIT 1", __FILE__, __LINE__);

// Either this topic has no poll, or the user cannot view it.
if (mysql_num_rows($request) == 0)
return array();

$row = mysql_fetch_assoc($request);
mysql_free_result($request);

// Check if they can vote.
if (!empty($row['expireTime']) && $row['expireTime'] < time())
$allow_vote = false;
elseif ($user_info['is_guest'] || !empty($row['votingLocked']) || !allowedTo('poll_vote'))
$allow_vote = false;
else
{
$request = db_query("
SELECT ID_MEMBER
FROM {$db_prefix}log_polls
WHERE ID_POLL = $row[ID_POLL]
AND ID_MEMBER = $ID_MEMBER
LIMIT 1", __FILE__, __LINE__);
$allow_vote = mysql_num_rows($request) == 0;
mysql_free_result($request);
}

$request = db_query("
SELECT COUNT(DISTINCT ID_MEMBER)
FROM {$db_prefix}log_polls
WHERE ID_POLL = $row[ID_POLL]", __FILE__, __LINE__);
list ($total) = mysql_fetch_row($request);
mysql_free_result($request);

$request = db_query("
SELECT ID_CHOICE, label, votes
FROM {$db_prefix}poll_choices
WHERE ID_POLL = $row[ID_POLL]", __FILE__, __LINE__);
$options = array();
$total_votes = 0;
while ($rowChoice = mysql_fetch_assoc($request))
{
censorText($rowChoice['label']);

$options[$rowChoice['ID_CHOICE']] = array($rowChoice['label'], $rowChoice['votes']);
$total_votes += $rowChoice['votes'];
}
mysql_free_result($request);

$return = array(
'id' => $row['ID_POLL'],
'image' => empty($pollinfo['votingLocked']) ? 'poll' : 'locked_poll',
'question' => $row['question'],
'total_votes' => $total,
'is_locked' => !empty($pollinfo['votingLocked']),
'allow_vote' => $allow_vote,
'topic' => $topic
);

// Calculate the percentages and bar lengths...
$divisor = $total_votes == 0 ? 1 : $total_votes;
foreach ($options as $i => $option)
{
$bar = floor(($option[1] * 100) / $divisor);
$barWide = $bar == 0 ? 1 : floor(($bar * 1) / 3);
$return['options'][$i] = array(
'id' => 'options-' . $i,
'percent' => $bar,
'votes' => $option[1],
'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_left.gif" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.gif" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_right.gif" alt="" /></span>',
'option' => parse_bbc($option[0]),
'vote_button' => '<input type="' . ($row['maxVotes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="check" />'
);
}

$return['allowed_warning'] = $row['maxVotes'] > 1 ? sprintf($txt['poll_options6'], $row['maxVotes']) : '';

if ($output_method != 'echo')
return $return;

if ($context['user']['is_guest']){

echo '
<table border="0" cellspacing="0" cellpadding="0" class="ssi_table text1">
<tr>
<td>', $return['question'], '</td>
</tr>
<tr>
<td>', $return['allowed_warning'], '</td>
</tr>';
foreach ($return['options'] as $option)
echo '
<tr>
<td align="left"><label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label></td>
</tr>';
echo '
<tr>
<td><div style="margin-top: 5px; margin-bottom: 5px;" class="center">[Login to Vote]</div></td>
</tr>
</table>';

}

elseif ($return['allow_vote'])
{
echo '
<form action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
<input type="hidden" name="poll" value="', $return['id'], '" />
<table border="0" cellspacing="0" cellpadding="0" class="ssi_table text1">
<tr>
<td>', $return['question'], '</td>
</tr>
<tr>
<td>', $return['allowed_warning'], '</td>
</tr>';
foreach ($return['options'] as $option)
echo '
<tr>
<td align="left"><label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label></td>
</tr>';
echo '
<tr>
<td><div style="margin-top: 5px;"><input type="submit" value="Vote" /></div></td>
</tr>
</table>
<input type="hidden" name="sc" value="', $sc, '" />
</form>';
}

else
{
echo '
<table border="0" cellspacing="0" cellpadding="0" class="ssi_table text1">
<tr>
<td colspan="2">', $return['question'], '</td>
</tr>';
foreach ($return['options'] as $option)
echo '
<tr>
<td align="left" valign="top">', $option['option'], '
<br />', $option['bar'], ' ', $option['votes'], ' (', $option['percent'], '%)</td>
</tr>';
echo '
<tr>
<td colspan="2"><div style="margin-top: 5px;">', $txt['smf24'], ': ', $return['total_votes'], '</div>
</td>
</tr>
</table>';
}
}

// Takes care of voting - don't worry, this is done automatically.
function ssi_pollVote()
{
global $db_prefix, $ID_MEMBER, $user_info, $sc;

if (!isset($_POST['sc']) || $_POST['sc'] != $sc || empty($_POST['options']) || !isset($_POST['poll']))
{
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[
history.go(-1);
// ]]></script>
</head>
<body>&laquo;</body>
</html>';
return;
}

// This can cause weird errors! (ie. copyright missing.)
checkSession();

$_POST['poll'] = (int) $_POST['poll'];

// Check if they have already voted, or voting is locked.
$request = db_query("
SELECT IFNULL(lp.ID_CHOICE, -1) AS selected, p.votingLocked, p.expireTime, p.maxVotes, t.ID_TOPIC
FROM ({$db_prefix}polls AS p, {$db_prefix}topics AS t, {$db_prefix}boards AS b)
LEFT JOIN {$db_prefix}log_polls AS lp ON (lp.ID_POLL = p.ID_POLL AND lp.ID_MEMBER = $ID_MEMBER)
WHERE p.ID_POLL = $_POST[poll]
AND t.ID_POLL = $_POST[poll]
AND b.ID_BOARD = t.ID_BOARD
AND $user_info[query_see_board]
LIMIT 1", __FILE__, __LINE__);
if (mysql_num_rows($request) == 0)
die;
$row = mysql_fetch_assoc($request);
mysql_free_result($request);

if (!empty($row['votingLocked']) || $row['selected'] != -1 || (!empty($row['expireTime']) && time() > $row['expireTime']))
redirectexit('topic=' . $row['ID_TOPIC'] . '.0');

// Too many options checked?
if (count($_REQUEST['options']) > $row['maxVotes'])
redirectexit('topic=' . $row['ID_TOPIC'] . '.0');

$options = array();
$setString = '';
foreach ($_REQUEST['options'] as $id)
{
$id = (int) $id;

$options[] = $id;
$setString .= "
($_POST[poll], $ID_MEMBER, $id),";
}
$setString = substr($setString, 0, -1);

// Add their vote in to the tally.
db_query("
INSERT INTO {$db_prefix}log_polls
(ID_POLL, ID_MEMBER, ID_CHOICE)
VALUES $setString", __FILE__, __LINE__);
db_query("
UPDATE {$db_prefix}poll_choices
SET votes = votes + 1
WHERE ID_POLL = $_POST[poll]
AND ID_CHOICE IN (" . implode(', ', $options) . ")
LIMIT " . count($options), __FILE__, __LINE__);

redirectexit('topic=' . $row['ID_TOPIC'] . '.0');
}


I'm running SMF 1.1.1.

Darkness7148


Advertisement: