Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.75 KB | None | 0 0
  1. // On line 82 of .htaccess (i.e., at the bottom of the "Misc" section), I added the following
  2.  
  3. RewriteRule ^cast_vote/([0-9]+)$ action.php?action=cast_vote&id=$1 [L]
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. // On line 354 of post.php (in the new topic/not editing section, just above "if ($namefag != '') {", I added code to validate poll options:
  16.  
  17.                 // Is this a valid poll?
  18.                 if(!empty($_POST['option'][0]) && !empty($_POST['option'][1])) {
  19.                     if(count($_POST['option']) > 10) {
  20.                         $_POST['option'] = array_slice($_POST['option'], 0, 9);
  21.                     }
  22.                    
  23.                     foreach($_POST['option'] as $id => $text) {
  24.                         if(empty($text)) {
  25.                             unset($_POST['option'][$id]);
  26.                         }
  27.                         else if(strlen($text) > 80) {
  28.                             $id = $id +1;
  29.                             add_error('Poll option ' . $id . ' exceeded 80 characters.');
  30.                         }
  31.                     }
  32.                     $poll = 1;
  33.                 }
  34.                 else {
  35.                     $poll = 0;
  36.                 }
  37.  
  38.  
  39.  
  40.  
  41. // One (what was now) line 342 of post.php, I changed the new topic creation query to include two new fields:
  42.  
  43.                     $stmt = $link->db_exec('INSERT INTO topics (author, author_ip, headline, body, last_post, time, namefag, tripfag, sticky, locked, poll) VALUES (%1, %2, %3, %4, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), %5, %6, %7, %8, %9)', $author, $_SERVER['REMOTE_ADDR'], $headline, $body, $namefag[0], $namefag[1], $sticky, $locked, $poll);
  44.  
  45.  
  46. // Immediately below the line that follows that query ("$inserted_id = $link->insert_id();"), I added:
  47.  
  48.                     if($poll) {
  49.                         foreach($_POST['option'] as $option) {
  50.                             $stmt = $link->db_exec('INSERT INTO poll_options (`parent_id`, `option`) VALUES (%1, %2)', $inserted_id, $option);
  51.                         }
  52.                     }
  53.  
  54.  
  55. // After "echo "<p>To post as a Wiseguy, just enter your name and tripcode. Your name will always have a Wiseguy link, unless you use no name at all.</p>"; }, I added
  56.  
  57.         if(!$reply) {
  58.         ?>
  59.        
  60.         <p style="display:none;" id="attach_poll"><a href="javascript:void(0);" onclick="$('#attach_poll').hide(); $('#poll_fields').show()" >+ Attach poll</a></p>
  61.        
  62.         <div id="poll_fields">
  63.             <p>To attach a poll to this thread, fill in at least <em>two</em> of the options below.</p>
  64.            
  65.             <div class="row">
  66.                 <label>Option #1</label>
  67.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][0]) ?>" class="inline" />
  68.             </div>
  69.            
  70.             <div class="row">
  71.                 <label>Option #2</label>
  72.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][1]) ?>" class="inline" />
  73.             </div>
  74.            
  75.             <div class="row">
  76.                 <label>Option #3</label>
  77.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][2]) ?>" class="inline" />
  78.             </div>
  79.            
  80.             <div class="row">
  81.                 <label>Option #4</label>
  82.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][3]) ?>" class="inline" />
  83.             </div>
  84.            
  85.             <div class="row">
  86.                 <label>Option #5</label>
  87.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][4]) ?>" class="inline" />
  88.             </div>
  89.            
  90.             <div class="row">
  91.                 <label>Option #6</label>
  92.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][5]) ?>" class="inline" />
  93.             </div>
  94.            
  95.             <div class="row">
  96.                 <label>Option #7</label>
  97.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][6]) ?>" class="inline" />
  98.             </div>
  99.            
  100.             <div class="row">
  101.                 <label>Option #8</label>
  102.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][7]) ?>" class="inline" />
  103.             </div>
  104.            
  105.             <div class="row">
  106.                 <label>Option #9</label>
  107.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][8]) ?>" class="inline" />
  108.             </div>
  109.            
  110.             <div class="row">
  111.                 <label>Option #10</label>
  112.                 <input type="text" size="80" maxlength="80" name="option[]" value="<?php if($_POST['form_sent']) echo htmlspecialchars($_POST['option'][9]) ?>" class="inline" />
  113.             </div>
  114.         </div>
  115.        
  116.         <?php
  117.             if(empty($_POST['option'][0])) {
  118.                 echo "<script>$('#attach_poll').show(); $('#poll_fields').hide()</script>";
  119.             }
  120.         }
  121.        
  122.         ?>
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. // I added the following to action.php
  140.  
  141.     case 'cast_vote':
  142.         if( ! ctype_digit($_GET['id'])) {
  143.             add_error('Invalid topic ID.', true);
  144.         }
  145.        
  146.         $id = $_GET['id'];
  147.         $page_title = 'Cast vote';
  148.        
  149.         if(ctype_digit($_POST['option_id'])) {
  150.             check_token();
  151.            
  152.             $check_votes = $link->db_exec('SELECT 1 FROM poll_votes WHERE (ip = %1 OR uid = %2) AND parent_id = %3', $_SERVER['REMOTE_ADDR'], $_SESSION['UID'], $id);
  153.             if($link->num_rows() == 0) {
  154.                 $record = $link->db_exec('INSERT INTO poll_votes (uid, ip, parent_id, option_id) VALUES (%1, %2, %3, %4)', $_SESSION['UID'], $_SERVER['REMOTE_ADDR'], $id, $_POST['option_id']);
  155.                 $increment_option = $link->db_exec('UPDATE poll_options SET votes = votes + 1 WHERE id = %1', $_POST['option_id']);
  156.                 $increment_poll = $link->db_exec('UPDATE topics SET poll_votes = poll_votes + 1 WHERE id = %1', $id);
  157.             }
  158.             else {
  159.                 add_error('You\'ve already voted in this poll.', true);
  160.             }
  161.             redirect('Thanks for voting.', 'topic/' . $id);
  162.         }
  163.         else {
  164.             redirect('You need to select an option.', 'topic/' . $id);
  165.         }
  166.        
  167.     break;
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. // In topic.php, I updated the statements near the start to include the two new fields.
  192.  
  193. if (ALLOW_IMAGES) {
  194.     $stmt = $link->db_exec('SELECT topics.time, topics.author, topics.visits, topics.replies, topics.headline, topics.body, topics.edit_time, topics.edit_mod, images.file_name, topics.namefag, topics.tripfag, topics.sticky, topics.locked, topics.poll, topics.poll_votes FROM topics LEFT OUTER JOIN images ON topics.id = images.topic_id WHERE topics.id = %1', $_GET['id']);
  195. } else {
  196.     $stmt = $link->db_exec('SELECT time, author, visits, replies, headline, body, edit_time, edit_mod, namefag, tripfag, sticky, locked, poll, poll_votes FROM topics WHERE id = %1', $_GET['id']);
  197. }
  198.  
  199. //...and...
  200.  
  201. if (ALLOW_IMAGES) {
  202.     list($topic_time, $topic_author, $topic_visits, $topic_replies, $topic_headline, $topic_body, $topic_edit_time, $topic_edit_mod, $topic_image_name, $opnamefag, $optripfag, $sticky, $locked, $poll, $poll_votes) = $link->fetch_row($stmt);
  203. } else {
  204.     list($topic_time, $topic_author, $topic_visits, $topic_replies, $topic_headline, $topic_body, $topic_edit_time, $topic_edit_mod, $opnamefag, $optripfag, $sticky, $locked, $poll, $poll_votes) = $link->fetch_row($stmt);
  205. }
  206.  
  207.  
  208.  
  209. // Immediately after the OP's post is output (line 125), I added the following to output the poll:
  210.  
  211. // Output poll.
  212. if($poll) {
  213.     $check_votes = $link->db_exec('SELECT option_id FROM poll_votes WHERE uid = %1 AND parent_id = %2', $_SESSION['UID'], $_GET['id']);
  214.     list($voted) = $link->fetch_row($check_votes);
  215.  
  216.     if(!$voted) {
  217.         echo '<form action="' . DOMAIN . 'cast_vote/' . $_GET['id'] . '" method="POST">';
  218.         csrf_token();
  219.     }
  220.    
  221.     $table = new table();
  222.    
  223.     $columns = array
  224.     (
  225.         'Poll option',
  226.         'Votes',
  227.         'Percentage',
  228.         'Graph'
  229.     );
  230.     $table->define_columns($columns, 'Poll option');
  231.  
  232.     $options = $link->db_exec('SELECT poll_options.id, poll_options.option, poll_options.votes FROM poll_options WHERE poll_options.parent_id = %1', $_GET['id']);
  233.     while(list($option_id, $option_text, $option_votes) = $link->fetch_row($options)) {
  234.         if($poll_votes == 0) {
  235.             $percent = 0;
  236.         }
  237.         else {
  238.             $percent = round(100 * $option_votes / $poll_votes);
  239.         }
  240.    
  241.         $values = array
  242.         (
  243.             htmlspecialchars($option_text),
  244.             format_number($option_votes),
  245.             $percent . '%',
  246.             '<div class="bar_container help" style="width: 130px; padding:1px; border:1px solid #555" title=" ' . $option_votes . ' of ' . $poll_votes . ' "><div class="bar" style="width: ' . $percent . '%; height:.9em; background-color:#990000;"></div></div>'
  247.         );
  248.        
  249.         if(!$voted) {
  250.             $values[0] = '<input name="option_id" class="inline" value="' . $option_id . '" id="option_' . $option_id . '" type="radio" /><label for="option_' . $option_id . '" class="inline">' . $values[0] . '</label>';
  251.         }
  252.         else if($voted == $option_id) {
  253.             $values[0] = '<strong title="You voted for this." class="help">' . $values[0] . '</strong>';
  254.         }
  255.        
  256.         $table->row($values);
  257.     }
  258.    
  259.     echo $table->output('options');
  260.     if(!$voted) {
  261.         echo '<div class="row"><input type="submit" name="cast_vote" value="Cast your vote" /></div></form>';
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement