-
Notifications
You must be signed in to change notification settings - Fork 3
/
Class-Karma.php
328 lines (276 loc) · 9.09 KB
/
Class-Karma.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* Karma
*
* This is the main file, handles the hooks, the actions, permissions, load needed files, etc.
* @package Karma mod
* @version 1.0 Alpha
* @author John Rayes <live627@gmail.com>
* @copyright Copyright (c) 2014, John Rayes
* @license http://opensource.org/licenses/MIT MIT
*/
if (!defined('SMF'))
die('Hacking attempt...');
class KarmaIntegration
{
public static function load_theme()
{
loadLanguage('Karma+ManageKarma');
}
public static function actions(&$action_array)
{
$action_array['karma'] = array('Class-Karma.php', 'Karma::init#');
}
public static function modify_features(&$subActions)
{
global $sourcedir;
require_once($sourcedir . '/ManageKarma.php');
$subActions['karma'] = 'ModifyKarmaSettings';
}
public static function admin_areas(&$admin_areas)
{
global $txt;
$admin_areas['config']['areas']['featuresettings']['subsections']['karma'] = array($txt['mods_cat_karma']);
}
public static function admin_search(&$language_files, &$include_files, &$settings_search)
{
$language_files[] = 'ManageKarma';
$include_files[] = 'ManageKarma';
$settings_search[] = array('ModifyKarmaSettings', 'area=featuresettings;sa=karma');
}
public static function load_permissions(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions)
{
global $modSettings;
$permissionList['membergroup']['karma_edit'] = array(false, 'general');
if (empty($modSettings['karmaMode']))
$hiddenPermissions[] = 'karma_edit';
}
public static function load_permission_levels(&$groupLevels, &$boardLevels)
{
$groupLevels['global']['standard'][] = 'karma_edit';
}
public static function illegal_guest_permissions()
{
global $context;
$context['non_guest_permissions'][] = 'karma_edit';
}
public static function reports_groupperm(&$disabled_permissions)
{
global $modSettings;
if (empty($modSettings['karmaMode']))
$disabled_permissions[] = 'karma_edit';
}
public static function member_context(&$memData, $memID, $display_custom_fields)
{
global $context, $modSettings, $memberContext, $scripturl, $txt;
static $karma;
if (!empty($modSettings['karmaMode']) && $display_custom_fields)
{
if (empty($karma[$memID]))
$karma = loadMemberCustomFields(array($memID), array('karma_good', 'karma_bad'));
if (empty($karma[$memID]['karma_good']['value']))
$karma[$memID]['karma_good']['value'] = 0;
if (empty($karma[$memID]['karma_bad']['value']))
$karma[$memID]['karma_bad']['value'] = 0;
// Total or +/-?
if ($modSettings['karmaMode'] == 1)
$value = $karma[$memID]['karma_good']['value'] - $karma[$memID]['karma_bad']['value'];
elseif ($modSettings['karmaMode'] == 2)
$value = '+' . $karma[$memID]['karma_good']['value'] . ' / -' . $karma[$memID]['karma_bad']['value'];
$memberContext[$memID]['custom_fields'][] = array(
'title' => $txt['karma'],
'col_name' => 'karma',
'value' => $value,
'placement' => 6,
);
$memberContext[$memID]['custom_fields'][] = array(
'title' => '',
'col_name' => 'karma_labels',
'value' => '
<a href = "' . $scripturl . '?action=karma;sa=applaud;uid=' . $memID . ';' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['karmaApplaudLabel'] . '</a>
<a href = "' . $scripturl . '?action=karma;sa=smite;uid=' . $memID . ';' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['karmaSmiteLabel'] . '</a>',
'placement' => 6,
);
}
}
}
class Karma
{
/**
* Modify a user's karma.
* It redirects back to the referrer afterward, whether by javascript or the passed parameters.
* Requires the karma_edit permission, and that the user isn't a guest.
* It depends on the karmaMode, karmaWaitTime, and karmaTimeRestrictAdmins settings.
* It is accessed via ?action=karma.
*
* @return void
*/
public function init()
{
global $modSettings, $txt, $user_info, $topic, $smcFunc, $context, $sourcedir;
// If the mod is disabled, show an error.
if (empty($modSettings['karmaMode']))
fatal_lang_error('feature_disabled', true);
// If you're a guest or can't do this, blow you off...
is_not_guest();
isAllowedTo('karma_edit');
checkSession('get');
// If you don't have enough posts, tough luck.
// @todo Should this be dropped in favor of post group permissions?
// Should this apply to the member you are smiting/applauding?
if (!$user_info['is_admin'] && $user_info['posts'] < $modSettings['karmaMinPosts'])
fatal_lang_error('not_enough_posts_karma', true, array($modSettings['karmaMinPosts']));
// And you can't modify your own, punk! (use the profile if you need to.)
if (empty($_REQUEST['uid']) || (int) $_REQUEST['uid'] == $user_info['id'])
fatal_lang_error('cant_change_own_karma', false);
// The user ID _must_ be a number, no matter what.
$memID = (int) $_REQUEST['uid'];
// Applauding or smiting?
$dir = $_REQUEST['sa'] != 'applaud' ? -1 : 1;
if (($dir == 1 && empty($txt['karmaApplaudLabel'])) || ($dir == -1 && empty($txt['karmaSmiteLabel'])))
fatal_lang_error('feature_disabled', false);
// Delete any older items from the log. (karmaWaitTime is by hour.)
$smcFunc['db_query'](
'',
'
DELETE FROM {db_prefix}log_karma
WHERE {int:current_time} - log_time > {int:wait_time}',
array(
'wait_time' => (int) ($modSettings['karmaWaitTime'] * 3600),
'current_time' => time(),
)
);
// Start off with no change in karma.
$action = 0;
// Not an administrator... or one who is restricted as well.
if (!empty($modSettings['karmaTimeRestrictAdmins']) || !allowedTo('moderate_forum'))
{
// Find out if this user has done this recently...
$request = $smcFunc['db_query'](
'',
'
SELECT action
FROM {db_prefix}log_karma
WHERE id_target = {int:id_target}
AND id_executor = {int:current_member}
LIMIT 1',
array(
'current_member' => $user_info['id'],
'id_target' => $memID,
)
);
if ($smcFunc['db_num_rows']($request) > 0)
list ($action) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
$karma = loadMemberCustomFields(array($memID), array('karma_good', 'karma_bad'));
if (empty($karma[$memID]['karma_good']['value']))
$karma[$memID]['karma_good']['value'] = 0;
if (empty($karma[$memID]['karma_bad']['value']))
$karma[$memID]['karma_bad']['value'] = 0;
$changes = array();
$log_changes = array();
$col_name = $dir == 1 ? 'karma_good' : 'karma_bad';
$value = $karma[$memID][$col_name]['value'] + 1;
// They haven't, not before now, anyhow.
if (empty($action) || empty($modSettings['karmaWaitTime']))
{
// Put it in the log.
$smcFunc['db_insert'](
'replace',
'{db_prefix}log_karma',
array('action' => 'int', 'id_target' => 'int', 'id_executor' => 'int', 'log_time' => 'int'),
array($dir, $memID, $user_info['id'], time()),
array('id_target', 'id_executor')
);
// Change by one.
$log_changes[] = array(
'action' => $col_name,
'log_type' => 'user',
'extra' => array(
'value' => $value,
'applicator' => $user_info['id'],
'member_affected' => $memID,
),
);
$changes[] = array(1, $col_name, $value, $memID);
}
else
{
// If you are gonna try to repeat.... don't allow it.
if ($action == $dir)
fatal_lang_error(
'karma_wait_time',
false,
array(
$modSettings['karmaWaitTime'],
($modSettings['karmaWaitTime'] == 1 ? strtolower($txt['hour']) : $txt['hours']),
)
);
// You decided to go back on your previous choice?
$smcFunc['db_query'](
'',
'
UPDATE {db_prefix}log_karma
SET action = {int:action}, log_time = {int:current_time}
WHERE id_target = {int:id_target}
AND id_executor = {int:current_member}',
array(
'current_member' => $user_info['id'],
'action' => $dir,
'current_time' => time(),
'id_target' => $memID,
)
);
// It was recently changed the OTHER way... so... reverse it!
$log_changes[] = array(
'action' => $col_name,
'log_type' => 'user',
'extra' => array(
'value' => $value,
'applicator' => $user_info['id'],
'member_affected' => $memID,
),
);
$changes[] = array(1, $col_name, $value, $memID);
$changes[] = array(
1,
$dir == 1 ? 'karma_good' : 'karma_bad',
$karma[$memID][$dir == 1 ? 'karma_good' : 'karma_bad']['value'] - 1,
$memID,
);
}
// Make those changes!
if (!empty($changes))
{
$smcFunc['db_insert'](
'replace',
'{db_prefix}themes',
array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534', 'id_member' => 'int'),
$changes,
array('id_theme', 'variable', 'id_member')
);
if (!empty($log_changes) && !empty($modSettings['modlog_enabled']))
{
require_once($sourcedir . '/Logging.php');
logActions($log_changes);
}
}
if (true)
redirectexit($_SERVER['HTTP_REFERER']);
else
{
echo '<!DOCTYPE html>
<html', $context['right_to_left'] ? ' dir="rtl"' : '', '>
<head>
<title>...</title>
<script><!-- // --><![CDATA[
history.go(-1);
// ]]></script>
</head>
<body>«</body>
</html>';
obExit(false);
}
}
}