Changeset 357

Show
Ignore:
Timestamp:
2009-08-09 19:28:20 (3 years ago)
Author:
hannes
Message:

don't allow voting on closed topics (ticket #122)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/redirectors/pollvote.php

    r291 r357  
    127127        if (Member::checkRights('Pollvote', 'w') && isset($_SESSION['memberid']) && $_SESSION['memberid'] > 0) { 
    128128            // permission granted 
    129             $q = $C->prepare('SELECT * FROM ' . $SETTINGS['dbtableprefix'] . 'polls WHERE pollid=:id'); 
     129            $q = $C->prepare('SELECT t.closed, p.pollid, p.pollchoices, p.pollvotes, p.pollvoters FROM ' . $SETTINGS['dbtableprefix'] . 'polls AS p INNER JOIN ' . $SETTINGS['dbtableprefix'] . 'topics AS t ON t.poll = p.pollid WHERE p.pollid = :id'); 
    130130            $q->bindParam(':id', $INPUT['poll'], PDO::PARAM_INT, 12); 
    131131            if ($q->execute()) { 
    132132                $row = $q->fetchObject(); 
    133133                $q = NULL; 
    134                 // check if logged in member already voted 
    135                 $pollvoters = split("\n", $row->pollvoters); 
    136                 $i = 0; $voted = 0; 
    137                 while ($pollvoters[$i] != '') { 
    138                     if ($_SESSION['memberid'] == $pollvoters[$i]) { 
    139                         $voted = 1; 
     134                // check if topic is closed 
     135                if ($row->closed == 0) { 
     136                    // check if logged in member already voted 
     137                    $pollvoters = split("\n", $row->pollvoters); 
     138                    $i = 0; $voted = 0; 
     139                    while ($pollvoters[$i] != '') { 
     140                        if ($_SESSION['memberid'] == $pollvoters[$i]) { 
     141                            $voted = 1; 
     142                        } 
     143                        $i++; 
    140144                    } 
    141                     $i++; 
    142                 } 
    143                 if ($voted === 0) { 
    144                     // not voted yet -> add vote 
    145                     $pollvotes = split("\n", $row->pollvotes); 
    146                     $pollvotes[$INPUT['vote']]++; 
    147                     $row->pollvotes = implode("\n", $pollvotes); 
    148                     $row->pollvoters .= $_SESSION['memberid'] . "\n"; 
    149                     // update database 
    150                     $q = $C->prepare('UPDATE ' . $SETTINGS['dbtableprefix'] . 'polls SET pollvotes=:votes, pollvoters=:voters WHERE pollid=:id'); 
    151                     $q->bindParam(':votes', $row->pollvotes, PDO::PARAM_STR); 
    152                     $q->bindParam(':voters', $row->pollvoters, PDO::PARAM_STR); 
    153                     $q->bindParam(':id', $INPUT['poll'], PDO::PARAM_INT, 12); 
    154                     $q->execute(); 
    155                     $q = NULL; 
    156                     // close database connection 
    157                     $C = NULL; 
    158                     session_write_close(); 
    159                     session_start(); 
    160                     // refresh topic 
    161                     header('Location: ' . $SETTINGS['webpath'] . $F->link('topic') . 'id=' . $INPUT['topic'] . '&page=' . $INPUT['page'] . '&' . strip_tags(SID)); 
    162                     // some message in case redirecting failed 
    163                     $div = $html->body->addChild('div', str_replace(Array('%topic%', '%page%'), Array($INPUT['topic'], $INPUT['page']), $LANG['voting_successful'])); 
    164                     $div->addAttribute('class', 'message'); 
     145                    if ($voted === 0) { 
     146                        // not voted yet -> add vote 
     147                        $pollvotes = split("\n", $row->pollvotes); 
     148                        $pollvotes[$INPUT['vote']]++; 
     149                        $row->pollvotes = implode("\n", $pollvotes); 
     150                        $row->pollvoters .= $_SESSION['memberid'] . "\n"; 
     151                        // update database 
     152                        $q = $C->prepare('UPDATE ' . $SETTINGS['dbtableprefix'] . 'polls SET pollvotes=:votes, pollvoters=:voters WHERE pollid=:id'); 
     153                        $q->bindParam(':votes', $row->pollvotes, PDO::PARAM_STR); 
     154                        $q->bindParam(':voters', $row->pollvoters, PDO::PARAM_STR); 
     155                        $q->bindParam(':id', $INPUT['poll'], PDO::PARAM_INT, 12); 
     156                        $q->execute(); 
     157                        $q = NULL; 
     158                        // close database connection 
     159                        $C = NULL; 
     160                        session_write_close(); 
     161                        session_start(); 
     162                        // refresh topic 
     163                        header('Location: ' . $SETTINGS['webpath'] . $F->link('topic') . 'id=' . $INPUT['topic'] . '&page=' . $INPUT['page'] . '&' . strip_tags(SID)); 
     164                        // some message in case redirecting failed 
     165                        $div = $html->body->addChild('div', str_replace(Array('%topic%', '%page%'), Array($INPUT['topic'], $INPUT['page']), $LANG['voting_successful'])); 
     166                        $div->addAttribute('class', 'message'); 
     167                    } else { 
     168                        // someone trying to vote more than once 
     169                        $div = $html->body->addChild('div', $LANG['error_already_voted']); 
     170                        $div->addAttribute('class', 'message'); 
     171                    } 
    165172                } else { 
    166                     // someone trying to vote more than once 
    167                     $div = $html->body->addChild('div', $LANG['error_already_voted']); 
     173                    // no voting on closed topics 
     174                    $div = $html->body->addChild('div', $LANG['error_topic_closed']); 
    168175                    $div->addAttribute('class', 'message'); 
    169176                }