[ Index ]

PHP Cross Reference of MyBB 1.4.13

title

Body

[close]

/ -> polls.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.4
   4   * Copyright © 2008 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybboard.net
   7   * License: http://www.mybboard.net/about/license
   8   *
   9   * $Id: polls.php 4304 2009-01-02 01:11:56Z chris $
  10   */
  11  
  12  define("IN_MYBB", 1);
  13  define('THIS_SCRIPT', 'polls.php');
  14  
  15  $templatelist = "poll_newpoll,redirect_pollposted,redirect_pollupdated,redirect_votethanks";
  16  require_once  "./global.php";
  17  require_once  MYBB_ROOT."inc/functions_post.php";
  18  require_once  MYBB_ROOT."inc/class_parser.php";
  19  $parser = new postParser;
  20  
  21  // Load global language phrases
  22  $lang->load("polls");
  23  
  24  if($mybb->user['uid'] != 0)
  25  {
  26      eval("\$loginbox = \"".$templates->get("changeuserbox")."\";");
  27  }
  28  else
  29  {
  30      eval("\$loginbox = \"".$templates->get("loginbox")."\";");
  31  }
  32  
  33  if($mybb->input['preview'] || $mybb->input['updateoptions'])
  34  {
  35      if($mybb->input['action'] == "do_editpoll")
  36      {
  37          $mybb->input['action'] = "editpoll";
  38      }
  39      else
  40      {
  41          $mybb->input['action'] = "newpoll";
  42      }
  43  }
  44  if($mybb->input['action'] == "newpoll")
  45  {
  46      // Form for new poll
  47      $tid = intval($mybb->input['tid']);
  48  
  49      $plugins->run_hooks("polls_newpoll_start");
  50  
  51      $query = $db->simple_select("threads", "*", "tid='".intval($mybb->input['tid'])."'");
  52      $thread = $db->fetch_array($query);
  53      $fid = $thread['fid'];
  54      $forumpermissions = forum_permissions($fid);
  55  
  56      if(!$thread['tid'])
  57      {
  58          error($lang->error_invalidthread);
  59      }
  60      // Make navigation
  61      build_forum_breadcrumb($fid);
  62      add_breadcrumb(htmlspecialchars_uni($thread['subject']), get_thread_link($thread['tid']));
  63      add_breadcrumb($lang->nav_postpoll);
  64  
  65      // No permission if: Not thread author; not moderator; no forum perms to view, post threads, post polls
  66      if(($thread['uid'] != $mybb->user['uid'] && !is_moderator($fid)) || ($forumpermissions['canview'] == 0 || $forumpermissions['canpostthreads'] == 0 || $forumpermissions['canpostpolls'] == 0))
  67      {
  68          error_no_permission();
  69      }
  70  
  71      if($thread['poll'])
  72      {
  73          error($lang->error_pollalready);
  74      }
  75  
  76      // Sanitize number of poll options
  77      if($mybb->input['numpolloptions'] > 0)
  78      {
  79          $mybb->input['polloptions'] = $mybb->input['numpolloptions'];
  80      }
  81      if($mybb->settings['maxpolloptions'] && $mybb->input['polloptions'] > $mybb->settings['maxpolloptions'])
  82      {    // Too big
  83          $polloptions = $mybb->settings['maxpolloptions'];
  84      }
  85      elseif($mybb->input['polloptions'] < 2)
  86      {    // Too small
  87          $polloptions = 2;
  88      }
  89      else
  90      {    // Just right
  91          $polloptions = intval($mybb->input['polloptions']);
  92      }
  93  
  94      $question = htmlspecialchars_uni($mybb->input['question']);
  95  
  96      $postoptions = $mybb->input['postoptions'];
  97      if($postoptions['multiple'] == 1)
  98      {
  99          $postoptionschecked['multiple'] = 'checked="checked"';
 100      }
 101      if($postoptions['public'] == 1)
 102      {
 103          $postoptionschecked['public'] = 'checked="checked"';
 104      }
 105  
 106      $options = $mybb->input['options'];
 107      $optionbits = '';
 108      for($i = 1; $i <= $polloptions; ++$i)
 109      {
 110          $option = $options[$i];
 111          $option = htmlspecialchars_uni($option);
 112          eval("\$optionbits .= \"".$templates->get("polls_newpoll_option")."\";");
 113          $option = "";
 114      }
 115  
 116      if($mybb->input['timeout'] > 0)
 117      {
 118          $timeout = intval($mybb->input['timeout']);
 119      }
 120      else
 121      {
 122          $timeout = 0;
 123      }
 124  
 125      $plugins->run_hooks("polls_newpoll_end");
 126  
 127      eval("\$newpoll = \"".$templates->get("polls_newpoll")."\";");
 128      output_page($newpoll);
 129  }
 130  if($mybb->input['action'] == "do_newpoll" && $mybb->request_method == "post")
 131  {
 132      // Verify incoming POST request
 133      verify_post_check($mybb->input['my_post_key']);
 134  
 135      $plugins->run_hooks("polls_do_newpoll_start");
 136  
 137      $query = $db->simple_select("threads", "*", "tid='".intval($mybb->input['tid'])."'");
 138      $thread = $db->fetch_array($query);
 139      $fid = $thread['fid'];
 140      $forumpermissions = forum_permissions($fid);
 141  
 142      if(!$thread['tid'])
 143      {
 144          error($lang->error_invalidthread);
 145      }
 146  
 147      // No permission if: Not thread author; not moderator; no forum perms to view, post threads, post polls
 148      if(($thread['uid'] != $mybb->user['uid'] && !is_moderator($fid)) || ($forumpermissions['canview'] == 0 || $forumpermissions['canpostthreads'] == 0 || $forumpermissions['canpostpolls'] == 0))
 149      {
 150          error_no_permission();
 151      }
 152  
 153      if($thread['poll'])
 154      {
 155          error($lang->error_pollalready);
 156      }
 157  
 158      $polloptions = $mybb->input['polloptions'];
 159      if($mybb->settings['maxpolloptions'] && $polloptions > $mybb->settings['maxpolloptions'])
 160      {
 161          $polloptions = $mybb->settings['maxpolloptions'];
 162      }
 163  
 164      $postoptions = $mybb->input['postoptions'];
 165      if($postoptions['multiple'] != '1')
 166      {
 167          $postoptions['multiple'] = 0;
 168      }
 169  
 170      if($postoptions['public'] != '1')
 171      {
 172          $postoptions['public'] = 0;
 173      }
 174      
 175      if($polloptions < 2)
 176      {
 177          $polloptions = "2";
 178      }
 179      $optioncount = "0";
 180      $options = $mybb->input['options'];
 181      
 182      for($i = 1; $i <= $polloptions; ++$i)
 183      {
 184          if(trim($options[$i]) != "")
 185          {
 186              $optioncount++;
 187          }
 188          
 189          if(my_strlen($options[$i]) > $mybb->settings['polloptionlimit'] && $mybb->settings['polloptionlimit'] != 0)
 190          {
 191              $lengtherror = 1;
 192              break;
 193          }
 194      }
 195      
 196      if($lengtherror)
 197      {
 198          error($lang->error_polloptiontoolong);
 199      }
 200      
 201      if(empty($mybb->input['question']) || $optioncount < 2)
 202      {
 203          error($lang->error_noquestionoptions);
 204      }
 205      
 206      $optionslist = '';
 207      $voteslist = '';
 208      for($i = 1; $i <= $optioncount; ++$i)
 209      {
 210          if(trim($options[$i]) != '')
 211          {
 212              if($i > 1)
 213              {
 214                  $optionslist .= '||~|~||';
 215                  $voteslist .= '||~|~||';
 216              }
 217              $optionslist .= $options[$i];
 218              $voteslist .= '0';
 219          }
 220      }
 221      
 222      if($mybb->input['timeout'] > 0)
 223      {
 224          $timeout = intval($mybb->input['timeout']);
 225      }
 226      else
 227      {
 228          $timeout = 0;
 229      }
 230      
 231      $newpoll = array(
 232          "tid" => $thread['tid'],
 233          "question" => $db->escape_string($mybb->input['question']),
 234          "dateline" => TIME_NOW,
 235          "options" => $db->escape_string($optionslist),
 236          "votes" => $db->escape_string($voteslist),
 237          "numoptions" => intval($optioncount),
 238          "numvotes" => 0,
 239          "timeout" => $timeout,
 240          "closed" => 0,
 241          "multiple" => $postoptions['multiple'],
 242          "public" => $postoptions['public']
 243      );
 244  
 245      $plugins->run_hooks("polls_do_newpoll_process");
 246  
 247      $pid = $db->insert_query("polls", $newpoll);
 248  
 249      $db->update_query("threads", array('poll' => $pid), "tid='".$thread['tid']."'");
 250  
 251      $plugins->run_hooks("polls_do_newpoll_end");
 252  
 253      if($thread['visible'] == 1)
 254      {
 255          redirect(get_thread_link($thread['tid']), $lang->redirect_pollposted);
 256      }
 257      else
 258      {
 259          redirect(get_forum_link($forum['fid']), $lang->redirect_pollpostedmoderated);
 260      }
 261  }
 262  
 263  if($mybb->input['action'] == "editpoll")
 264  {
 265      $pid = intval($mybb->input['pid']);
 266  
 267      $plugins->run_hooks("polls_editpoll_start");
 268  
 269      $query = $db->simple_select("polls", "*", "pid='$pid'");
 270      $poll = $db->fetch_array($query);
 271  
 272      $query = $db->simple_select("threads", "*", "poll='$pid'");
 273      $thread = $db->fetch_array($query);
 274      $tid = $thread['tid'];
 275      $fid = $thread['fid'];
 276  
 277      // Make navigation
 278      build_forum_breadcrumb($fid);
 279      add_breadcrumb(htmlspecialchars_uni($thread['subject']), get_thread_link($thread['tid']));
 280      add_breadcrumb($lang->nav_editpoll);
 281  
 282  
 283      $forumpermissions = forum_permissions($fid);
 284  
 285      $query = $db->simple_select("forums", "*", "fid='$fid'");
 286      $forum = $db->fetch_array($query);
 287  
 288  
 289      if($thread['visible'] == "0" || !$tid)
 290      {
 291          error($lang->error_invalidthread);
 292      }
 293      
 294      if(!is_moderator($fid, "caneditposts"))
 295      {
 296          error_no_permission();
 297      }
 298      
 299      $polldate = my_date($mybb->settings['dateformat'], $poll['dateline']);
 300      if(!$mybb->input['preview'] && !$mybb->input['updateoptions'])
 301      {
 302          if($poll['closed'] == 1)
 303          {
 304              $postoptionschecked['closed'] = 'checked="checked"';
 305          }
 306          
 307          if($poll['multiple'] == 1)
 308          {
 309              $postoptionschecked['multiple'] = 'checked="checked"';
 310          }
 311          
 312          if($poll['public'] == 1)
 313          {
 314              $postoptionschecked['public'] = 'checked="checked"';
 315          }
 316  
 317          $optionsarray = explode("||~|~||", $poll['options']);
 318          $votesarray = explode("||~|~||", $poll['votes']);
 319  
 320  
 321          for($i = 1; $i <= $poll['numoptions']; ++$i)
 322          {
 323              $poll['totvotes'] = $poll['totvotes'] + $votesarray[$i-1];
 324          }
 325          
 326          $question = htmlspecialchars_uni($poll['question']);
 327          $numoptions = $poll['numoptions'];
 328          $optionbits = "";
 329          for($i = 0; $i < $numoptions; ++$i)
 330          {
 331              $counter = $i + 1;
 332              $option = $optionsarray[$i];
 333              $option = htmlspecialchars_uni($option);
 334              $optionvotes = intval($votesarray[$i]);
 335              
 336              if(!$optionvotes)
 337              {
 338                  $optionvotes = 0;
 339              }
 340              
 341              eval("\$optionbits .= \"".$templates->get("polls_editpoll_option")."\";");
 342              $option = "";
 343              $optionvotes = "";
 344          }
 345          
 346          if(!$poll['timeout'])
 347          {
 348              $timeout = 0;
 349          }
 350          else
 351          {
 352              $timeout = $poll['timeout'];
 353          }
 354      }
 355      else
 356      {
 357          if($mybb->settings['maxpolloptions'] && $mybb->input['numoptions'] > $mybb->settings['maxpolloptions'])
 358          {
 359              $numoptions = $mybb->settings['maxpolloptions'];
 360          }
 361          elseif($mybb->input['numoptions'] < 2)
 362          {
 363              $numoptions = "2";
 364          }
 365          else
 366          {
 367              $numoptions = $mybb->input['numoptions'];
 368          }
 369          $question = htmlspecialchars_uni($mybb->input['question']);
 370  
 371          $postoptions = $mybb->input['postoptions'];
 372          if($postoptions['multiple'] == 1)
 373          {
 374              $postoptionschecked['multiple'] = 'checked="checked"';
 375          }
 376          
 377          if($postoptions['public'] == 1)
 378          {
 379              $postoptionschecked['public'] = 'checked="checked"';
 380          }
 381          
 382          if($postoptions['closed'] == 1)
 383          {
 384              $postoptionschecked['closed'] = 'checked="checked"';
 385          }
 386  
 387          $options = $mybb->input['options'];
 388          $votes = $mybb->input['votes'];
 389          $optionbits = '';
 390          for($i = 1; $i <= $numoptions; ++$i)
 391          {
 392              $counter = $i;
 393              $option = $options[$i];
 394              $option = htmlspecialchars_uni($option);
 395              $optionvotes = $votes[$i];
 396              
 397              if(!$optionvotes)
 398              {
 399                  $optionvotes = 0;
 400              }
 401              
 402              eval("\$optionbits .= \"".$templates->get("polls_editpoll_option")."\";");
 403              $option = "";
 404          }
 405  
 406          if($mybb->input['timeout'] > 0)
 407          {
 408              $timeout = $mybb->input['timeout'];
 409          }
 410          else
 411          {
 412              $timeout = 0;
 413          }
 414      }
 415  
 416      $plugins->run_hooks("polls_editpoll_end");
 417  
 418      eval("\$editpoll = \"".$templates->get("polls_editpoll")."\";");
 419      output_page($editpoll);
 420  }
 421  
 422  if($mybb->input['action'] == "do_editpoll" && $mybb->request_method == "post")
 423  {
 424      // Verify incoming POST request
 425      verify_post_check($mybb->input['my_post_key']);
 426  
 427      $plugins->run_hooks("polls_do_editpoll_start");
 428  
 429      $query = $db->simple_select("polls", "*", "pid='".intval($mybb->input['pid'])."'");
 430      $poll = $db->fetch_array($query);
 431  
 432      $query = $db->simple_select("threads", "*", "poll='".intval($mybb->input['pid'])."'");
 433      $thread = $db->fetch_array($query);
 434  
 435      $forumpermissions = forum_permissions($thread['fid']);
 436  
 437      $query = $db->simple_select("forums", "*", "fid='".$thread['fid']."'");
 438      $forum = $db->fetch_array($query);
 439  
 440      if($thread['visible'] == 0 || !$thread['tid'])
 441      {
 442          error($lang->error_invalidthread);
 443      }
 444      
 445      if(!is_moderator($thread['fid'], "caneditposts"))
 446      {
 447          error_no_permission();
 448      }
 449  
 450      if($mybb->settings['maxpolloptions'] && $mybb->input['numoptions'] > $mybb->settings['maxpolloptions'])
 451      {
 452          $numoptions = $mybb->settings['maxpolloptions'];
 453      }
 454      elseif(!$mybb->input['numoptions'])
 455      {
 456          $numoptions = 2;
 457      }
 458      else
 459      {
 460          $numoptions = $mybb->input['numoptions'];
 461      }
 462  
 463      $postoptions = $mybb->input['postoptions'];
 464      if($postoptions['multiple'] != '1')
 465      {
 466          $postoptions['multiple'] = 0;
 467      }
 468      
 469      if($postoptions['public'] != '1')
 470      {
 471          $postoptions['public'] = 0;
 472      }
 473      
 474      if($postoptions['closed'] != '1')
 475      {
 476          $postoptions['closed'] = 0;
 477      }
 478      $optioncount = "0";
 479      $options = $mybb->input['options'];
 480  
 481      for($i = 1; $i <= $numoptions; ++$i)
 482      {
 483          if(trim($options[$i]) != '')
 484          {
 485              $optioncount++;
 486          }
 487          
 488          if(my_strlen($options[$i]) > $mybb->settings['polloptionlimit'] && $mybb->settings['polloptionlimit'] != 0)
 489          {
 490              $lengtherror = 1;
 491              break;
 492          }
 493      }
 494      
 495      if($lengtherror)
 496      {
 497          error($lang->error_polloptiontoolong);
 498      }
 499  
 500      if(trim($mybb->input['question']) == '' || $optioncount < 2)
 501      {
 502          error($lang->error_noquestionoptions);
 503      }
 504      
 505      $optionslist = '';
 506      $voteslist = '';
 507      $numvotes = '';
 508      $votes = $mybb->input['votes'];
 509      for($i = 1; $i <= $optioncount; ++$i)
 510      {
 511          if(trim($options[$i]) != '')
 512          {
 513              if($i > 1)
 514              {
 515                  $optionslist .= "||~|~||";
 516                  $voteslist .= "||~|~||";
 517              }
 518              
 519              $optionslist .= $options[$i];
 520              if(intval($votes[$i]) <= 0)
 521              {
 522                  $votes[$i] = "0";
 523              }
 524              $voteslist .= $votes[$i];
 525              $numvotes = $numvotes + $votes[$i];
 526          }
 527      }
 528      
 529      if($mybb->input['timeout'] > 0)
 530      {
 531          $timeout = intval($mybb->input['timeout']);
 532      }
 533      else
 534      {
 535          $timeout = 0;
 536      }
 537      
 538      $updatedpoll = array(
 539          "question" => $db->escape_string($mybb->input['question']),
 540          "options" => $db->escape_string($optionslist),
 541          "votes" => $db->escape_string($voteslist),
 542          "numoptions" => intval($numoptions),
 543          "numvotes" => $numvotes,
 544          "timeout" => $timeout,
 545          "closed" => $postoptions['closed'],
 546          "multiple" => $postoptions['multiple'],
 547          "public" => $postoptions['public']
 548      );
 549  
 550      $plugins->run_hooks("polls_do_editpoll_process");
 551  
 552      $db->update_query("polls", $updatedpoll, "pid='".intval($mybb->input['pid'])."'");
 553  
 554      $plugins->run_hooks("polls_do_editpoll_end");
 555      
 556      $modlogdata['fid'] = $thread['fid'];
 557      $modlogdata['tid'] = $thread['tid'];
 558      log_moderator_action($modlogdata, $lang->poll_edited);
 559  
 560      redirect(get_thread_link($thread['tid']), $lang->redirect_pollupdated);
 561  }
 562  
 563  if($mybb->input['action'] == "showresults")
 564  {
 565      $query = $db->simple_select("polls", "*", "pid='".intval($mybb->input['pid'])."'");
 566      $poll = $db->fetch_array($query);
 567      $tid = $poll['tid'];
 568      $query = $db->simple_select("threads", "*", "tid='$tid'");
 569      $thread = $db->fetch_array($query);
 570      $fid = $thread['fid'];
 571  
 572      // Get forum info
 573      $forum = get_forum($fid);
 574      if(!$forum)
 575      {
 576          error($lang->error_invalidforum);
 577      }
 578  
 579      $forumpermissions = forum_permissions($forum['fid']);
 580  
 581      $plugins->run_hooks("polls_showresults_start");
 582  
 583      if($forumpermissions['canviewthreads'] == 0 || $forumpermissions['canview'] == 0)
 584      {
 585          error($lang->error_pollpermissions);
 586      }
 587      
 588      if(!$poll['pid'])
 589      {
 590          error($lang->error_invalidpoll);
 591      }
 592      
 593      if(!$thread['tid'])
 594      {
 595          error($lang->error_invalidthread);
 596      }
 597  
 598      // Make navigation
 599      build_forum_breadcrumb($fid);
 600      add_breadcrumb(htmlspecialchars_uni($thread['subject']), get_thread_link($thread['tid']));
 601      add_breadcrumb($lang->nav_pollresults);
 602  
 603      $voters = array();
 604  
 605      // Calculate votes
 606      $query = $db->query("
 607          SELECT v.*, u.username 
 608          FROM ".TABLE_PREFIX."pollvotes v 
 609          LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=v.uid) 
 610          WHERE v.pid='{$poll['pid']}' 
 611          ORDER BY u.username
 612      ");
 613      while($voter = $db->fetch_array($query))
 614      {
 615          // Mark for current user's vote
 616          if($mybb->user['uid'] == $voter['uid'] && $mybb->user['uid'])
 617          {
 618              $votedfor[$voter['voteoption']] = 1;
 619          }
 620  
 621          // Count number of guests and users without a username (assumes they've been deleted)
 622          if($voter['uid'] == 0 || $voter['username'] == '')
 623          {
 624              // Add one to the number of voters for guests
 625              ++$guest_voters[$voter['voteoption']];
 626          }
 627          else
 628          {
 629              $voters[$voter['voteoption']][$voter['uid']] = $voter['username'];
 630          }
 631      }
 632      
 633      $optionsarray = explode("||~|~||", $poll['options']);
 634      $votesarray = explode("||~|~||", $poll['votes']);
 635      for($i = 1; $i <= $poll['numoptions']; ++$i)
 636      {
 637          $poll['totvotes'] = $poll['totvotes'] + $votesarray[$i-1];
 638      }
 639      
 640      $polloptions = '';
 641      for($i = 1; $i <= $poll['numoptions']; ++$i)
 642      {
 643          $parser_options = array(
 644              "allow_html" => $forum['allowhtml'],
 645              "allow_mycode" => $forum['allowmycode'],
 646              "allow_smilies" => $forum['allowsmilies'],
 647              "allow_imgcode" => $forum['allowimgcode'],
 648              "filter_badwords" => 1
 649          );
 650          $option = $parser->parse_message($optionsarray[$i-1], $parser_options);
 651  
 652          $votes = $votesarray[$i-1];
 653          $number = $i;
 654          // Make the mark for current user's voted option
 655          if($votedfor[$number])
 656          {
 657              $optionbg = 'trow2';
 658              $votestar = '*';
 659          }
 660          else
 661          {
 662              $optionbg = 'trow1';
 663              $votestar = '';
 664          }
 665          
 666          if($votes == '0')
 667          {
 668              $percent = '0';
 669          }
 670          else
 671          {
 672              $percent = number_format($votes / $poll['totvotes'] * 100, 2);
 673          }
 674          
 675          $imagewidth = round($percent/3) * 5;
 676          $comma = '';
 677          $guest_comma = '';
 678          $userlist = '';
 679          $guest_count = 0;
 680          if($poll['public'] == 1 || is_moderator($fid))
 681          {
 682              if(is_array($voters[$number]))
 683              {
 684                  foreach($voters[$number] as $uid => $username)
 685                  {
 686                      $userlist .= $comma.build_profile_link($username, $uid);
 687                      $comma = $guest_comma = ', ';
 688                  }
 689              }
 690  
 691              if($guest_voters[$number] > 0)
 692              {
 693                  if($guest_voters[$number] == 1)
 694                  {
 695                      $userlist .= $guest_comma.$lang->guest_count;
 696                  }
 697                  else
 698                  {
 699                      $userlist .= $guest_comma.$lang->sprintf($lang->guest_count_multiple, $guest_voters[$number]);
 700                  }
 701              }
 702          }
 703          eval("\$polloptions .= \"".$templates->get("polls_showresults_resultbit")."\";");
 704      }
 705      
 706      if($poll['totvotes'])
 707      {
 708          $totpercent = '100%';
 709      }
 710      else
 711      {
 712          $totpercent = '0%';
 713      }
 714  
 715      $plugins->run_hooks("polls_showresults_end");
 716  
 717      $poll['question'] = htmlspecialchars_uni($poll['question']);
 718      eval("\$showresults = \"".$templates->get("polls_showresults")."\";");
 719      output_page($showresults);
 720  }
 721  if($mybb->input['action'] == "vote")
 722  {
 723      $query = $db->simple_select("polls", "*", "pid='".intval($mybb->input['pid'])."'");
 724      $poll = $db->fetch_array($query);
 725      $poll['timeout'] = $poll['timeout']*60*60*24;
 726  
 727      $plugins->run_hooks("polls_vote_start");
 728  
 729      if(!$poll['pid'])
 730      {
 731          error($lang->error_invalidpoll);
 732      }
 733  
 734      $query = $db->simple_select("threads", "*", "poll='".$poll['pid']."'");
 735      $thread = $db->fetch_array($query);
 736  
 737      if(!$thread['tid'])
 738      {
 739          error($lang->error_invalidthread);
 740      }
 741      
 742      $fid = $thread['fid'];
 743      $forumpermissions = forum_permissions($fid);
 744      if($forumpermissions['canvotepolls'] == 0)
 745      {
 746          error_no_permission();
 747      }
 748  
 749      $expiretime = $poll['dateline'] + $poll['timeout'];
 750      $now = TIME_NOW;
 751      if($poll['closed'] == 1 || $thread['closed'] == 1 || ($expiretime < $now && $poll['timeout']))
 752      {
 753          error($lang->error_pollclosed);
 754      }
 755      
 756      if(!isset($mybb->input['option']))
 757      {
 758          error($lang->error_nopolloptions);
 759      }
 760      
 761      // Check if the user has voted before...
 762      if($mybb->user['uid'])
 763      {
 764          $query = $db->simple_select("pollvotes", "*", "uid='".$mybb->user['uid']."' AND pid='".$poll['pid']."'");
 765          $votecheck = $db->fetch_array($query);
 766      }
 767      
 768      if($votecheck['vid'] || $mybb->cookies['pollvotes'][$poll['pid']])
 769      {
 770          error($lang->error_alreadyvoted);
 771      }
 772      elseif(!$mybb->user['uid'])
 773      {
 774          // Give a cookie to guests to inhibit revotes
 775          my_setcookie("pollvotes[{$poll['pid']}]", '1');
 776      }
 777      
 778      $votesql = '';
 779      $now = TIME_NOW;
 780      $votesarray = explode("||~|~||", $poll['votes']);
 781      $option = $mybb->input['option'];
 782      $numvotes = $poll['numvotes'];
 783      if($poll['multiple'] == 1)
 784      {
 785          foreach($option as $voteoption => $vote)
 786          {
 787              if($vote == 1 && isset($votesarray[$voteoption-1]))
 788              {
 789                  if($votesql)
 790                  {
 791                      $votesql .= ",";
 792                  }
 793                  $votesql .= "('".$poll['pid']."','".$mybb->user['uid']."','".$db->escape_string($voteoption)."','$now')";
 794                  $votesarray[$voteoption-1]++;
 795                  $numvotes = $numvotes+1;
 796              }
 797          }
 798      }
 799      else
 800      {
 801          if(!isset($votesarray[$option-1]))
 802          {
 803              error($lang->error_nopolloptions);
 804          }
 805          $votesql = "('".$poll['pid']."','".$mybb->user['uid']."','".$db->escape_string($option)."','$now')";
 806          $votesarray[$option-1]++;
 807          $numvotes = $numvotes+1;
 808      }
 809  
 810      $db->write_query("
 811          INSERT INTO 
 812          ".TABLE_PREFIX."pollvotes (pid,uid,voteoption,dateline) 
 813          VALUES $votesql
 814      ");
 815      $voteslist = '';
 816      for($i = 1; $i <= $poll['numoptions']; ++$i)
 817      {
 818          if($i > 1)
 819          {
 820              $voteslist .= "||~|~||";
 821          }
 822          $voteslist .= $votesarray[$i-1];
 823      }
 824      $updatedpoll = array(
 825          "votes" => $db->escape_string($voteslist),
 826          "numvotes" => intval($numvotes),
 827      );
 828  
 829      $plugins->run_hooks("polls_vote_process");
 830  
 831      $db->update_query("polls", $updatedpoll, "pid='".$poll['pid']."'");
 832  
 833      $plugins->run_hooks("polls_vote_end");
 834  
 835      redirect(get_thread_link($poll['tid']), $lang->redirect_votethanks);
 836  }
 837  
 838  ?>


Generated: Mon Apr 19 19:52:21 2010 Cross-referenced by PHPXref 0.7