Karma.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * This file contains one humble function, which applauds or smites a user.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2014 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * Modify a user's karma.
  18. * It redirects back to the referrer afterward, whether by javascript or the passed parameters.
  19. * Requires the karma_edit permission, and that the user isn't a guest.
  20. * It depends on the karmaMode, karmaWaitTime, and karmaTimeRestrictAdmins settings.
  21. * It is accessed via ?action=modifykarma.
  22. */
  23. function ModifyKarma()
  24. {
  25. global $modSettings, $txt, $user_info, $topic, $smcFunc, $context;
  26. // If the mod is disabled, show an error.
  27. if (empty($modSettings['karmaMode']))
  28. fatal_lang_error('feature_disabled', true);
  29. // If you're a guest or can't do this, blow you off...
  30. is_not_guest();
  31. isAllowedTo('karma_edit');
  32. checkSession('get');
  33. // If you don't have enough posts, tough luck.
  34. // @todo Should this be dropped in favor of post group permissions?
  35. // Should this apply to the member you are smiting/applauding?
  36. if (!$user_info['is_admin'] && $user_info['posts'] < $modSettings['karmaMinPosts'])
  37. fatal_lang_error('not_enough_posts_karma', true, array($modSettings['karmaMinPosts']));
  38. // And you can't modify your own, punk! (use the profile if you need to.)
  39. if (empty($_REQUEST['uid']) || (int) $_REQUEST['uid'] == $user_info['id'])
  40. fatal_lang_error('cant_change_own_karma', false);
  41. // The user ID _must_ be a number, no matter what.
  42. $_REQUEST['uid'] = (int) $_REQUEST['uid'];
  43. // Applauding or smiting?
  44. $dir = $_REQUEST['sa'] != 'applaud' ? -1 : 1;
  45. if (($dir == 1 && empty($modSettings['karmaApplaudLabel'])) || ($dir == -1 && empty($modSettings['karmaSmiteLabel'])))
  46. fatal_lang_error('feature_disabled', false);
  47. // Delete any older items from the log. (karmaWaitTime is by hour.)
  48. $smcFunc['db_query']('', '
  49. DELETE FROM {db_prefix}log_karma
  50. WHERE {int:current_time} - log_time > {int:wait_time}',
  51. array(
  52. 'wait_time' => (int) ($modSettings['karmaWaitTime'] * 3600),
  53. 'current_time' => time(),
  54. )
  55. );
  56. // Start off with no change in karma.
  57. $action = 0;
  58. // Not an administrator... or one who is restricted as well.
  59. if (!empty($modSettings['karmaTimeRestrictAdmins']) || !allowedTo('moderate_forum'))
  60. {
  61. // Find out if this user has done this recently...
  62. $request = $smcFunc['db_query']('', '
  63. SELECT action
  64. FROM {db_prefix}log_karma
  65. WHERE id_target = {int:id_target}
  66. AND id_executor = {int:current_member}
  67. LIMIT 1',
  68. array(
  69. 'current_member' => $user_info['id'],
  70. 'id_target' => $_REQUEST['uid'],
  71. )
  72. );
  73. if ($smcFunc['db_num_rows']($request) > 0)
  74. list ($action) = $smcFunc['db_fetch_row']($request);
  75. $smcFunc['db_free_result']($request);
  76. }
  77. // They haven't, not before now, anyhow.
  78. if (empty($action) || empty($modSettings['karmaWaitTime']))
  79. {
  80. // Put it in the log.
  81. $smcFunc['db_insert']('replace',
  82. '{db_prefix}log_karma',
  83. array('action' => 'int', 'id_target' => 'int', 'id_executor' => 'int', 'log_time' => 'int'),
  84. array($dir, $_REQUEST['uid'], $user_info['id'], time()),
  85. array('id_target', 'id_executor')
  86. );
  87. // Change by one.
  88. updateMemberData($_REQUEST['uid'], array($dir == 1 ? 'karma_good' : 'karma_bad' => '+'));
  89. }
  90. else
  91. {
  92. // If you are gonna try to repeat.... don't allow it.
  93. if ($action == $dir)
  94. fatal_lang_error('karma_wait_time', false, array($modSettings['karmaWaitTime'], ($modSettings['karmaWaitTime'] == 1 ? strtolower($txt['hour']) : $txt['hours'])));
  95. // You decided to go back on your previous choice?
  96. $smcFunc['db_query']('', '
  97. UPDATE {db_prefix}log_karma
  98. SET action = {int:action}, log_time = {int:current_time}
  99. WHERE id_target = {int:id_target}
  100. AND id_executor = {int:current_member}',
  101. array(
  102. 'current_member' => $user_info['id'],
  103. 'action' => $dir,
  104. 'current_time' => time(),
  105. 'id_target' => $_REQUEST['uid'],
  106. )
  107. );
  108. // It was recently changed the OTHER way... so... reverse it!
  109. if ($dir == 1)
  110. updateMemberData($_REQUEST['uid'], array('karma_good' => '+', 'karma_bad' => '-'));
  111. else
  112. updateMemberData($_REQUEST['uid'], array('karma_bad' => '+', 'karma_good' => '-'));
  113. }
  114. // Figure out where to go back to.... the topic?
  115. if (!empty($topic))
  116. redirectexit('topic=' . $topic . '.' . $_REQUEST['start'] . '#msg' . (int) $_REQUEST['m']);
  117. // Hrm... maybe a personal message?
  118. elseif (isset($_REQUEST['f']))
  119. redirectexit('action=pm;f=' . $_REQUEST['f'] . ';start=' . $_REQUEST['start'] . (isset($_REQUEST['l']) ? ';l=' . (int) $_REQUEST['l'] : '') . (isset($_REQUEST['pm']) ? '#' . (int) $_REQUEST['pm'] : ''));
  120. // JavaScript as a last resort.
  121. else
  122. {
  123. echo '<!DOCTYPE html>
  124. <html', $context['right_to_left'] ? ' dir="rtl"' : '', '>
  125. <head>
  126. <title>...</title>
  127. <script><!-- // --><![CDATA[
  128. history.go(-1);
  129. // ]]></script>
  130. </head>
  131. <body>&laquo;</body>
  132. </html>';
  133. obExit(false);
  134. }
  135. }
  136. ?>