forked from mybbgroup/Thank-you-like-system
/
thankyoulike.php
473 lines (433 loc) · 15.3 KB
/
thankyoulike.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*
* $Id: thankyoulike.php 53 2011-10-26 08:17:45Z - G33K - $
*/
define("IN_MYBB", 1);
define("NO_ONLINE", 1);
define('THIS_SCRIPT', 'thankyoulike.php');
$prefix = "g33k_thankyoulike_";
$templatelist = "thankyoulike_users,thankyoulike,thankyoulike_button_add,thankyoulike_button_del";
require_once "./global.php";
// Load global language phrases
$lang->load("thankyoulike");
if($mybb->user['uid'] == 0)
{
error_no_permission();
}
// Access to this file only if plugin is existant and active
if (!$mybb->settings[$prefix.'enabled'])
{
error($lang->sprintf($lang->tyl_error_disabled, "This"));
}
if ($mybb->settings[$prefix.'thankslike'] == "like")
{
$pre = $lang->tyl_like;
$pre1 = $lang->tyl_liked;
}
else
{
$pre = $lang->tyl_thankyou;
$pre1 = $lang->tyl_thanked;
}
if($mybb->settings[$prefix.'enabled'] != "1")
{
error($lang->sprintf($lang->tyl_error_disabled, $pre));
}
// Exit if no regular action.
if($mybb->input['action'] != "add" && $mybb->input['action'] != "del")
{
error($lang->tyl_error_invalid_action);
}
// Verify post key
verify_post_check($mybb->input['my_post_key']);
// Get the pid and tid
$pid = intval($mybb->input['pid']);
$options = array(
"limit" => 1
);
$query_post = $db->simple_select("posts", "*", "pid='".$pid."'", $options);
$post = $db->fetch_array($query_post);
if(!$post['pid'])
{
error($lang->error_invalidpost);
}
$tid = $post['tid'];
// Set up $thread and $forum.
$options = array(
"limit" => 1
);
$query_thread = $db->simple_select("threads", "*", "tid='".$tid."'", $options);
$thread = $db->fetch_array($query_thread);
$fid = $thread['fid'];
// Get forum info
$forum = get_forum($fid);
if(!$forum)
{
error($lang->error_invalidforum);
}
$forumpermissions = forum_permissions($fid);
// See if everything is valid up to here.
if(isset($post) && (($post['visible'] == 0 && !is_moderator($fid)) || $post['visible'] == 0))
{
error($lang->error_invalidpost);
}
if(isset($thread) && (($thread['visible'] == 0 && !is_moderator($fid)) || $thread['visible'] < 0))
{
error($lang->error_invalidthread);
}
if($forum['open'] == 0 || $forum['type'] != "f")
{
error($lang->error_closedinvalidforum);
}
if($forumpermissions['canview'] == 0 || $forumpermissions['canpostreplys'] == 0 || $mybb->user['suspendposting'] == 1)
{
error_no_permission();
}
// Check if this forum is password protected and we have a valid password
check_forum_password($forum['fid']);
// Check to see if the thread is closed, and if the user is a mod.
if(!is_moderator($fid, "caneditposts"))
{
if($thread['closed'] == 1 && $mybb->settings[$prefix.'closedthreads'] != "1")
{
error($lang->sprintf($lang->tyl_error_threadclosed, $pre));
}
}
// Check if setting is first post or not and if it is, whether its the first post
if($mybb->settings[$prefix.'firstall'] == "first" && $thread['firstpost'] != $post['pid'])
{
error($lang->tyl_error_not_allowed);
}
// Check if the post is in a forum that has been excluded
// Check first if this post is in an exclued forum, if it is end right here.
$exc_forums = explode(",", $mybb->settings[$prefix.'exclude']);
$excluded = false;
foreach($exc_forums as $exc_forum)
{
if (trim($exc_forum) == $fid)
{
$excluded = true;
}
}
if ($excluded)
{
error($lang->tyl_error_excluded);
}
if($mybb->input['action'] == "add")
{
// Can't thank/like own post
if($post['uid'] == $mybb->user['uid'])
{
error($lang->sprintf($lang->tyl_error_own_post, $pre));
}
// Check if user has already thanked/liked this post.
$options = array(
"limit" => 1
);
$query_check = $db->simple_select($prefix."thankyoulike", "*", "pid='".$pid."' AND uid='".$mybb->user['uid']."'", $options);
$utyl = $db->fetch_array($query_check);
if(isset($utyl['tlid']))
{
error($lang->sprintf($lang->tyl_error_already_tyled, $pre1));
}
// Add ty/l to db
$tyl_data = array(
"pid" => intval($post['pid']),
"uid" => intval($mybb->user['uid']),
"puid" => intval($post['uid']),
"dateline" => TIME_NOW
);
$tlid = $db->insert_query($prefix."thankyoulike", $tyl_data);
// Verify if myalerts exists and if compatible with 1.8.x then add alert type
include_once('inc/plugins/thankyoulike.php');
if(function_exists("myalerts_info")){
// Load myalerts info into an array
$my_alerts_info = myalerts_info();
// Set version info to a new var
$verify = $my_alerts_info['version'];
// If MyAlerts 2.0 or better then do this !!!
if($verify >= "2.0.0"){
global $cache;
// Load cache data and compare if version is the same or don't
$myalerts_plugins = $cache->read('mybbstuff_myalerts_alert_types');
if($myalerts_plugins['tyl']['code'] == 'tyl'){
tyl_recordAlertThankyou();
}
}
}
if($tlid)
{
// Update tyl count in posts and threads and users and total
if($post['tyl_pnumtyls'] == 0)
{
// Post thanks were previously 0, so add this post to user's thanked posts
$db->write_query("UPDATE ".TABLE_PREFIX."users SET tyl_unumptyls=tyl_unumptyls+1 WHERE uid='".intval($post['uid'])."'");
}
$db->write_query("UPDATE ".TABLE_PREFIX."posts SET tyl_pnumtyls=tyl_pnumtyls+1 WHERE pid='".intval($pid)."'");
$db->write_query("UPDATE ".TABLE_PREFIX."threads SET tyl_tnumtyls=tyl_tnumtyls+1 WHERE tid='".intval($tid)."'");
$db->write_query("UPDATE ".TABLE_PREFIX."users SET tyl_unumtyls=tyl_unumtyls+1 WHERE uid='".intval($mybb->user['uid'])."'");
$db->write_query("UPDATE ".TABLE_PREFIX."users SET tyl_unumrcvtyls=tyl_unumrcvtyls+1 WHERE uid='".intval($post['uid'])."'");
$db->write_query("UPDATE ".TABLE_PREFIX.$prefix."stats SET value=value+1 WHERE title='total'");
if($mybb->input['ajax'])
{
// Do nothing here
}
else
{
// Go back to the post
$url = get_post_link($pid, $tid)."#pid{$pid}";
redirect($url, $lang->sprintf($lang->tyl_redirect_tyled, $pre).$lang->tyl_redirect_back);
exit;
}
}
else
{
error($lang->sprintf($lang->tyl_error_unknown, $pre));
}
}
if($mybb->input['action'] == "del")
{
if($mybb->settings[$prefix.'removing'] != "1")
{
error($lang->sprintf($lang->tyl_error_removal_disabled, $pre));
}
// Check tyl owner and tyl exists
$options = array(
"limit" => 1
);
$query_r = $db->simple_select($prefix."thankyoulike", "*", "pid='".$pid."' AND uid='".$mybb->user['uid']."'", $options);
$tyl_r = $db->fetch_array($query_r);
if(isset($tyl_r['tlid']))
{
if($tyl_r['uid'] == $mybb->user['uid'])
{
// process delete
$db->delete_query($prefix."thankyoulike", "tlid='".$tyl_r['tlid']."'", "1");
// if alert of user was added and unread then review if delete thanks and delete alert too.
if((function_exists('myalerts_is_activated') && myalerts_is_activated()) && $mybb->user['uid']){
$db->query("DELETE FROM ".TABLE_PREFIX."alerts WHERE from_user_id={$mybb->user['uid']} AND object_id='{$pid}' AND unread=1 LIMIT 1");
}
// Update counts
if($post['tyl_pnumtyls'] == 1)
{
// This was the last thanks in the post, so remove this post from user's thanked posts
$db->write_query("UPDATE ".TABLE_PREFIX."users SET tyl_unumptyls=tyl_unumptyls-1 WHERE uid='".intval($post['uid'])."'");
}
$db->write_query("UPDATE ".TABLE_PREFIX."posts SET tyl_pnumtyls=tyl_pnumtyls-1 WHERE pid='".intval($pid)."'");
$db->write_query("UPDATE ".TABLE_PREFIX."threads SET tyl_tnumtyls=tyl_tnumtyls-1 WHERE tid='".intval($tid)."'");
$db->write_query("UPDATE ".TABLE_PREFIX."users SET tyl_unumtyls=tyl_unumtyls-1 WHERE uid='".intval($mybb->user['uid'])."'");
$db->write_query("UPDATE ".TABLE_PREFIX."users SET tyl_unumrcvtyls=tyl_unumrcvtyls-1 WHERE uid='".intval($post['uid'])."'");
$db->write_query("UPDATE ".TABLE_PREFIX.$prefix."stats SET value=value-1 WHERE title='total'");
if($mybb->input['ajax'])
{
// Do nothing here
}
else
{
$url = get_post_link($pid, $tid)."#pid{$pid}";
redirect($url, $lang->sprintf($lang->tyl_redirect_deleted, $pre).$lang->tyl_redirect_back);
}
}
else
{
error($lang->sprintf($lang->tyl_error_own_delete, $pre));
}
}
else
{
error($lang->sprintf($lang->tyl_error_not_found, $pre));
}
}
if($mybb->input['ajax'])
{
// Send headers.
header("Content-type: application/json; charset={$charset}");
// Get all the thanks/likes for this post
switch($mybb->settings[$prefix.'sortorder'])
{
case "userdesc":
$order = " ORDER BY username DESC";
break;
case "dtasc":
$order = " ORDER BY dateline ASC";
break;
case "dtdesc":
$order = " ORDER BY dateline DESC";
break;
case "userasc":
default:
$order = " ORDER BY username ASC";
break;
}
$query1 = $db->query("
SELECT tyl.*, u.username, u.usergroup, u.displaygroup
FROM ".TABLE_PREFIX.$prefix."thankyoulike tyl
LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=tyl.uid)
WHERE tyl.pid='".$post['pid']."'
".$order."
");
$tyls = '';
$comma = '';
$tyled = 0;
$count = 0;
while($tyl = $db->fetch_array($query1))
{
$profile_link = get_profile_link($tyl['uid']);
// Format username... or not
$tyl_list = $mybb->settings[$prefix.'unameformat'] == "1" ? format_name($tyl['username'], $tyl['usergroup'], $tyl['displaygroup']) : $tyl['username'];
$datedisplay_next = $mybb->settings[$prefix.'showdt'] == "nexttoname" ? "<span class='smalltext'> (".my_date($mybb->settings[$prefix.'dtformat'], $tyl['dateline']).")</span>" : "";
$datedisplay_title = $mybb->settings[$prefix.'showdt'] == "astitle" ? "title='".my_date($mybb->settings[$prefix.'dtformat'], $tyl['dateline'])."'" : "";
eval("\$thankyoulike_users = \"".$templates->get("thankyoulike_users", 1, 0)."\";");
$tyls .= trim($thankyoulike_users);
$comma = ', ';
// Has this user tyled?
if($tyl['uid'] == $mybb->user['uid'])
{
$tyled = 1;
}
$count++;
}
// Are we using thanks or like? Setup titles
if($count == 1)
{
$tyl_user = $lang->tyl_user;
$tyl_say = $lang->tyl_says;
$tyl_like = $lang->tyl_likes;
}
else
{
$tyl_user = $lang->tyl_users;
$tyl_say = $lang->tyl_say;
$tyl_like = $lang->tyl_like;
}
if ($mybb->settings[$prefix.'thankslike'] == "like")
{
$pre = "l";
$lang->add_tyl = $lang->add_l;
$lang->del_tyl = $lang->del_l;
$tyl_thankslikes = $lang->tyl_likes;
$tyl_data = get_user($post['uid']);
if($mybb->settings[$prefix.'unameformat'] == "1"){
$tyl_data['username'] = format_name($tyl_data['username'], $tyl_data['usergroup'], $tyl_data['displaygroup']);
$tyl_profilelink = build_profile_link($tyl_data['username'], $tyl_data['uid']);
}else{
$tyl_profilelink = htmlspecialchars_uni($tyl_data['username']);
}
$lang->tyl_title = $lang->sprintf($lang->tyl_title_l, $count, $tyl_user, $tyl_like, $tyl_profilelink);
$lang->tyl_title_collapsed = $lang->sprintf($lang->tyl_title_collapsed_l, $count, $tyl_user, $tyl_like, $tyl_profilelink);
}
else if ($mybb->settings[$prefix.'thankslike'] == "thanks")
{
$pre = "ty";
$lang->add_tyl = $lang->add_ty;
$lang->del_tyl = $lang->del_ty;
$tyl_thankslikes = $lang->tyl_thanks;
$tyl_data = get_user($post['uid']);
if($mybb->settings[$prefix.'unameformat'] == "1"){
$tyl_data['username'] = format_name($tyl_data['username'], $tyl_data['usergroup'], $tyl_data['displaygroup']);
$tyl_profilelink = build_profile_link($tyl_data['username'], $tyl_data['uid']);
}else{
$tyl_profilelink = htmlspecialchars_uni($tyl_data['username']);
}
$lang->tyl_title = $lang->sprintf($lang->tyl_title_ty, $count, $tyl_user, $tyl_say, $tyl_profilelink);
$lang->tyl_title_collapsed = $lang->sprintf($lang->tyl_title_collapsed_ty, $count, $tyl_user, $tyl_say, $tyl_profilelink);
}
// Setup the collapsible elements
if ($mybb->settings[$prefix.'collapsible'] == "1" && $mybb->settings[$prefix.'colldefault'] == "closed")
{
$tyl_title_display = "display: none;";
$tyl_title_display_collapsed = "";
$tyl_data_display = "display: none;";
$tyl_expcolimg = "collapse_collapsed.png";
eval("\$tyl_expcol = \"".$templates->get("thankyoulike_expcollapse", 1, 0)."\";");
}
else if ($mybb->settings[$prefix.'collapsible'] == "1" && $mybb->settings[$prefix.'colldefault'] == "open")
{
$tyl_title_display = "";
$tyl_title_display_collapsed = "display: none;";
$tyl_data_display = "";
$tyl_expcolimg = "collapse.png";
eval("\$tyl_expcol = \"".$templates->get("thankyoulike_expcollapse", 1, 0)."\";");
}
else
{
$tyl_title_display = "";
$tyl_title_display_collapsed = "display: none;";
$tyl_data_display = "";
$tyl_expcolimg = "";
$tyl_expcol = "";
$lang->tyl_title_collapsed = "";
}
$button_tyl = '';
if(($tyled && $mybb->settings[$prefix.'removing'] != "1") || (!is_moderator($post['fid'], "caneditposts") && $thread['closed'] == 1 && $mybb->settings[$prefix.'closedthreads'] != "1") || $post['uid'] == $mybb->user['uid'] || is_member($mybb->settings[$prefix.'hideforgroups']) || $mybb->settings[$prefix.'hideforgroups'] == "-1")
{
// Show no button for poster or user who has already thanked/liked or removing is disabled.
$button_tyl = '';
}
else if($tyled && $mybb->settings[$prefix.'removing'] == "1" && (($mybb->settings[$prefix.'firstall'] == "first" && $thread['firstpost'] == $post['pid']) || $mybb->settings[$prefix.'firstall'] == "all"))
{
// Show remove button if removing already thanked/liked and removing enabled and is either the first post in thread if setting is for first or setting is all
eval("\$button_tyl = \"".$templates->get("thankyoulike_button_del")."\";");
}
else if(($mybb->settings[$prefix.'firstall'] == "first" && $thread['firstpost'] == $post['pid']) || $mybb->settings[$prefix.'firstall'] == "all")
{
if ((my_strpos($mybb->settings[$prefix.'firstalloverwrite'], $post['fid']) !== false || $mybb->settings[$prefix.'firstalloverwrite'] == "-1") && $thread['firstpost'] != $post['pid'])
{
eval("\$button_tyl = \"".$templates->get("thankyoulike_button_add")."\";");
}
else
{
// Same as above but show add button
eval("\$button_tyl = \"".$templates->get("thankyoulike_button_add")."\";");
}
}
// Cleanup for JSON
$button_tyl = thankyoulike_cleanup_json($button_tyl);
if($count>0 && (($mybb->settings[$prefix.'firstall'] == "first" && $thread['firstpost'] == $post['pid']) || $mybb->settings[$prefix.'firstall'] == "all"))
{
// We have thanks/likes to show
$post['thankyoulike'] = $tyls;
$post['tyl_display'] = "";
if($mybb->settings['postlayout'] == "classic")
{
eval("\$thankyoulike = \"".$templates->get("thankyoulike_classic")."\";");
}
else
{
eval("\$thankyoulike = \"".$templates->get("thankyoulike")."\";");
}
// Cleanup for JSON
$thankyoulike = thankyoulike_cleanup_json($thankyoulike);
echo '{';
echo '"tylButton":"'.$button_tyl.'",';
echo '"tylData":"'.$thankyoulike.'"';
echo '}';
}
else
{
// Nothing to show, return blank data with buttons.
echo '{';
echo '"tylButton":"'.$button_tyl.'",';
echo '"tylData":""';
echo '}';
}
exit;
}
function thankyoulike_cleanup_json($data)
{
return addcslashes($data, "\\\/\"\n\r\t/".chr(0).chr(8).chr(12));
}