Advertisement
Guest User

Untitled

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