ratethread.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * MyBB 1.8
  4. * Copyright 2014 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://www.mybb.com
  7. * License: http://www.mybb.com/about/license
  8. *
  9. */
  10. define("IN_MYBB", 1);
  11. define('THIS_SCRIPT', 'ratethread.php');
  12. $templatelist = 'forumdisplay_password_wrongpass,forumdisplay_password';
  13. require_once "./global.php";
  14. // Verify incoming POST request
  15. verify_post_check($mybb->get_input('my_post_key'));
  16. $lang->load("ratethread");
  17. $tid = $mybb->get_input('tid');
  18. $thread = get_thread($tid);
  19. if(!$thread)
  20. {
  21. error($lang->error_invalidthread);
  22. }
  23. // Is the currently logged in user a moderator of this forum?
  24. $ismod = is_moderator($thread['fid']);
  25. // Make sure we are looking at a real thread here.
  26. if(($thread['visible'] != 1 && $ismod == false) || ($thread['visible'] > 1 && $ismod == true))
  27. {
  28. error($lang->error_invalidthread);
  29. }
  30. if($thread['uid'] == $mybb->user['uid'])
  31. {
  32. error($lang->error_cannotrateownthread);
  33. }
  34. $forumpermissions = forum_permissions($thread['fid']);
  35. if($forumpermissions['canview'] == 0 || $forumpermissions['canratethreads'] == 0 || $mybb->usergroup['canratethreads'] == 0 || $mybb->settings['allowthreadratings'] == 0 || (isset($forumpermissions['canonlyviewownthreads']) && $forumpermissions['canonlyviewownthreads'] != 0))
  36. {
  37. error_no_permission();
  38. }
  39. // Get forum info
  40. $fid = $thread['fid'];
  41. $forum = get_forum($fid);
  42. if(!$forum)
  43. {
  44. error($lang->error_invalidforum);
  45. }
  46. else
  47. {
  48. // Is our forum closed?
  49. if($forum['open'] == 0)
  50. {
  51. // Doesn't look like it is
  52. error($lang->error_closedinvalidforum);
  53. }
  54. }
  55. // Check if this forum is password protected and we have a valid password
  56. check_forum_password($forum['fid']);
  57. if($forum['allowtratings'] == 0)
  58. {
  59. error_no_permission();
  60. }
  61. $mybb->input['rating'] = $mybb->get_input('rating', MyBB::INPUT_INT);
  62. if($mybb->input['rating'] < 1 || $mybb->input['rating'] > 5)
  63. {
  64. error($lang->error_invalidrating);
  65. }
  66. $plugins->run_hooks("ratethread_start");
  67. if($mybb->user['uid'] != 0)
  68. {
  69. $whereclause = "uid='{$mybb->user['uid']}'";
  70. }
  71. else
  72. {
  73. $whereclause = "ipaddress=".$db->escape_binary($session->packedip);
  74. }
  75. $query = $db->simple_select("threadratings", "*", "{$whereclause} AND tid='{$tid}'");
  76. $ratecheck = $db->fetch_array($query);
  77. if($ratecheck['rid'] || isset($mybb->cookies['mybbratethread'][$tid]))
  78. {
  79. error($lang->error_alreadyratedthread);
  80. }
  81. else
  82. {
  83. $plugins->run_hooks("ratethread_process");
  84. $db->write_query("
  85. UPDATE ".TABLE_PREFIX."threads
  86. SET numratings=numratings+1, totalratings=totalratings+'{$mybb->input['rating']}'
  87. WHERE tid='{$tid}'
  88. ");
  89. if($mybb->user['uid'] != 0)
  90. {
  91. $insertarray = array(
  92. 'tid' => $tid,
  93. 'uid' => $mybb->user['uid'],
  94. 'rating' => $mybb->input['rating'],
  95. 'ipaddress' => $db->escape_binary($session->packedip)
  96. );
  97. $db->insert_query("threadratings", $insertarray);
  98. }
  99. else
  100. {
  101. $insertarray = array(
  102. 'tid' => $tid,
  103. 'rating' => $mybb->input['rating'],
  104. 'ipaddress' => $db->escape_binary($session->packedip)
  105. );
  106. $db->insert_query("threadratings", $insertarray);
  107. $time = TIME_NOW;
  108. my_setcookie("mybbratethread[{$tid}]", $mybb->input['rating']);
  109. }
  110. }
  111. $plugins->run_hooks("ratethread_end");
  112. if(!empty($mybb->input['ajax']))
  113. {
  114. $json = array("success" => $lang->rating_added);
  115. $query = $db->simple_select("threads", "totalratings, numratings", "tid='$tid'", array('limit' => 1));
  116. $fetch = $db->fetch_array($query);
  117. $width = 0;
  118. if($fetch['numratings'] >= 0)
  119. {
  120. $averagerating = (float)round($fetch['totalratings']/$fetch['numratings'], 2);
  121. $width = (int)round($averagerating)*20;
  122. $fetch['numratings'] = (int)$fetch['numratings'];
  123. $ratingvotesav = $lang->sprintf($lang->rating_votes_average, $fetch['numratings'], $averagerating);
  124. $json = $json + array("average" => $ratingvotesav);
  125. }
  126. $json = $json + array("width" => $width);
  127. @header("Content-type: application/json; charset={$lang->settings['charset']}");
  128. echo json_encode($json);
  129. exit;
  130. }
  131. redirect(get_thread_link($thread['tid']), $lang->redirect_threadrated);