· 9 years ago · Jan 11, 2017, 05:20 PM
1<?php
2/* by Tomasz 'Devilshakerz' Mlynski [devilshakerz.com]; Copyright (C) 2014
3 released under Creative Commons BY-NC-SA 3.0 license: http://creativecommons.org/licenses/by-nc-sa/3.0/ */
4
5$plugins->add_hook('global_end', 'dvz_shoutbox_global_end'); // load language file, catch archive page
6$plugins->add_hook('xmlhttp', 'dvz_shoutbox_xmlhttp'); // xmlhttp.php listening
7$plugins->add_hook('index_end', 'dvz_shoutbox'); // load Shoutbox window to {$dvz_shoutbox} variable
8
9// MyBB handling
10function dvz_shoutbox_info () {
11 return array(
12 'name' => 'DVZ Shoutbox',
13 'description' => 'Lightweight AJAX chat.',
14 'website' => 'http://devilshakerz.com/',
15 'author' => 'Tomasz \'Devilshakerz\' Mlynski',
16 'authorsite' => 'http://devilshakerz.com/',
17 'version' => '2.0',
18 'guid' => 'a54d9c66ae174f090b6345ce19e7a063',
19 'compatibility' => '16*',
20 );
21}
22function dvz_shoutbox_install () {
23 global $db;
24
25 // table
26 $db->write_query("
27 CREATE TABLE IF NOT EXISTS `".TABLE_PREFIX."dvz_shoutbox` (
28 `id` int(11) NOT NULL auto_increment,
29 `uid` int(11) NOT NULL,
30 `text` text NOT NULL,
31 `date` int(11) NOT NULL,
32 `ip` varchar(15) NOT NULL,
33 PRIMARY KEY (`id`)
34 ) ENGINE=MyISAM ".$db->build_create_table_collation()."
35 ");
36
37 // example shout
38 $db->write_query("INSERT INTO ".TABLE_PREFIX."dvz_shoutbox VALUES (NULL, 1, 'DVZ Shoutbox!', ".time().", '127.0.0.1')");
39
40 // settings
41 $db->write_query("INSERT INTO `".TABLE_PREFIX."settinggroups` VALUES (NULL, 'dvz_shoutbox', 'DVZ Shoutbox', 'Settings for DVZ Shoutbox.', 1, 0)");
42 $sgID = $db->insert_id();
43
44 $db->write_query("INSERT INTO `".TABLE_PREFIX."settings` VALUES
45 (NULL, 'dvz_sb_num', 'Shouts to display', 'Number of shouts displayed in the Shoutbox window.', 'text', '20', 1, $sgID, 0),
46 (NULL, 'dvz_sb_num_archive', 'Shouts to display on archive', 'Number of shouts to display per page on archive view.', 'text', '15', 2, $sgID, 0),
47 (NULL, 'dvz_sb_reversed', 'Reversed order', 'Reverse the order of displaying shouts in the Shoutbox window so that new ones appear on the bottom. You might also want to move the <b>{\$panel}</b> variable below window in the <i>dvz_shoutbox</i> template.', 'yesno', '0', 3, $sgID, 0),
48 (NULL, 'dvz_sb_height', 'Shoutbox height', 'Height of the Shoutbox window in pixels.', 'text', '160', 4, $sgID, 0),
49 (NULL, 'dvz_sb_dateformat', 'Date format', 'Format of the date displayed. This format uses the PHP <a href=\"http://php.net/manual/en/function.date.php\">date()</a> function.', 'text', 'd M H:i', 5, $sgID, 0),
50
51 (NULL, 'dvz_sb_mycode', 'Parse MyCode', '', 'yesno', '1', 6, $sgID, 0),
52 (NULL, 'dvz_sb_smilies', 'Parse smilies', '', 'yesno', '1', 7, $sgID, 0),
53 (NULL, 'dvz_sb_interval', 'Refresh interval', 'Number of seconds before new posted shouts are displayed in the window (lower values provide better synchronization but cause higher server load). Set 0 to disable the auto-refreshing feature.', 'text', '5', 8, $sgID, 0),
54 (NULL, 'dvz_sb_away', 'Away mode', 'Number of seconds after last user action (e.g. click) after which shoutbox will be minimized to prevent unnecessary usage of server resources. Set 0 to disable this feature.', 'text', '600', 9, $sgID, 0),
55 (NULL, 'dvz_sb_antiflood', 'Anti-flood interval', 'Minimum number of seconds before user can post next shout (this does not apply to Shoutbox moderators).', 'text', '5', 10, $sgID, 0),
56 (NULL, 'dvz_sb_lazyload', 'Lazy load', 'Start loading data only when the Shoutbox window is actually being displayed on the screen (the page is scrolled to the Shoutbox position).', 'select
57off=Disabled
58start=Check if on display to start
59always=Always check if on display to refresh', 'off', 11, $sgID, 0),
60 (NULL, 'dvz_sb_status', 'Shoutbox default status', 'Choose whether Shoutbox window should be expanded or collapsed by default.', 'onoff', '1', 12, $sgID, 0),
61
62 (NULL, 'dvz_sb_minposts', 'Minimum posts required to shout', 'Set 0 to allow everyone.', 'text', '0', 13, $sgID, 0),
63
64 (NULL, 'dvz_sb_groups_view', 'Group permissions: View', 'Comma-separated list of user groups that can view Shoutbox. Leave empty to let everyone view (including guests).', 'text', '', 14, $sgID, 0),
65 (NULL, 'dvz_sb_groups_shout', 'Group permissions: Shout', 'Comma-separated list of user groups that can post shouts in Shoutbox. Leave empty to let everyone post (that does not include guests).', 'text', '', 15, $sgID, 0),
66 (NULL, 'dvz_sb_groups_refresh', 'Group permissions: Auto-refresh', 'Comma-separated list of user groups that shoutbox will be refreshing for. Leave empty to let Shoutbox refresh for everyone.', 'text', '', 16, $sgID, 0),
67 (NULL, 'dvz_sb_groups_mod', 'Group permissions: Moderate', 'Comma-separated list of users groups that can moderate the Shoutbox (edit and delete shouts).', 'text', '', 17, $sgID, 0),
68 (NULL, 'dvz_sb_groups_mod_own', 'Group permissions: Moderate own shouts', 'Comma-separated list of users groups that can edit and delete own shouts.', 'text', '', 18, $sgID, 0),
69
70 (NULL, 'dvz_sb_supermods', 'Super moderators are Shoutbox moderators', 'Automatically allow forum super moderators to moderate Shoutbox as well.', 'yesno', '1', 19, $sgID, 0),
71
72
73 (NULL, 'dvz_sb_blocked_users', 'Banned users', 'Comma-separated list of user IDs that are banned from posting messages.', 'textarea', '', 20, $sgID, 0)
74 ");
75
76 rebuild_settings();
77
78 // templates
79 $template_panel = '
80<div class="panel">
81<form>
82<input type="text" class="text" placeholder="{$lang->dvz_sb_default}" autocomplete="off" />
83<input type="submit" style="display:none" />
84</form>
85</div>';
86
87 $template_shoutbox = '
88<div id="shoutbox" class="front{$classes}">
89
90<div class="thead">
91{$lang->dvz_sb_shoutbox}
92<p class="right"><a href="{$mybb->settings[\'bburl\']}/index.php?action=shoutbox_archive">« {$lang->dvz_sb_archivelink}</a></p>
93</div>
94
95<div class="body">
96
97{$panel}
98
99<div class="window" style="height:{$mybb->settings[\'dvz_sb_height\']}px">
100<div class="data"></div>
101</div>
102
103</div>
104
105<script type="text/javascript" src="{$mybb->settings[\'bburl\']}/jscripts/dvz_shoutbox.js"></script>
106{$javascript}
107
108</div>';
109
110 $template_archive = '<html>
111<head>
112<title>{$lang->dvz_sb_archive}</title>
113{$headerinclude}
114</head>
115<body>
116{$header}
117
118<script type="text/javascript" src="{$mybb->settings[\'bburl\']}/jscripts/dvz_shoutbox.js"></script>
119{$javascript}
120
121{$multipage}
122
123<br />
124
125<div id="shoutbox">
126
127{$modoptions}
128
129<div class="thead">{$lang->dvz_sb_archive}</div>
130
131<div class="data">
132{$archive}
133</div>
134</div>
135
136<br />
137
138{$multipage}
139
140{$footer}
141</body>
142</html>';
143
144 $template_archive_modoptions = '<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
145<tr><td class="thead" colspan="2"><strong>{$lang->dvz_sb_mod}</strong></td></tr>
146<tr><td class="tcat">{$lang->dvz_sb_mod_banlist}</td><td class="tcat">{$lang->dvz_sb_mod_clear}</td></tr>
147<tr>
148<td class="trow1">
149<form action="" method="post">
150<input type="text" class="textbox" style="width:80%" name="banlist" value="{$blocked_users}"></textarea>
151<input type="hidden" name="postkey" value="{$mybb->post_code}" />
152<input type="submit" class="button" value="{$lang->dvz_sb_mod_banlist_button}" />
153</form>
154</td>
155<td class="trow1">
156<form action="" method="post">
157<select name="days">
158<option value="2">2 {$lang->days}</option>
159<option value="7">7 {$lang->days}</option>
160<option value="30">30 {$lang->days}</option>
161<option value="90">90 {$lang->days}</option>
162<option value="all">* {$lang->dvz_sb_mod_clear_all} *</option>
163</select>
164<input type="hidden" name="postkey" value="{$mybb->post_code}" />
165<input type="submit" class="button" value="{$lang->dvz_sb_mod_clear_button}" />
166</form>
167</td>
168</tr>
169</table>
170<br />';
171
172 $db->write_query("INSERT INTO `".TABLE_PREFIX."templates` VALUES (NULL, 'dvz_shoutbox_panel', '".$db->escape_string($template_panel)."', '-1', '1', '', '".time()."')");
173 $db->write_query("INSERT INTO `".TABLE_PREFIX."templates` VALUES (NULL, 'dvz_shoutbox', '".$db->escape_string($template_shoutbox)."', '-1', '1', '', '".time()."')");
174 $db->write_query("INSERT INTO `".TABLE_PREFIX."templates` VALUES (NULL, 'dvz_shoutbox_archive', '".$db->escape_string($template_archive)."', '-1', '1', '', '".time()."')");
175 $db->write_query("INSERT INTO `".TABLE_PREFIX."templates` VALUES (NULL, 'dvz_shoutbox_archive_modoptions', '".$db->escape_string($template_archive_modoptions)."', '-1', '1', '', '".time()."')");
176
177}
178function dvz_shoutbox_uninstall () {
179 global $db;
180
181 $groupID = $db->fetch_field(
182 $db->simple_select('settinggroups', 'gid', "name='dvz_shoutbox'"),
183 'gid'
184 );
185
186 // delete settings
187 $db->delete_query('settinggroups', "name='dvz_shoutbox'");
188 $db->delete_query('settings', 'gid='.$groupID);
189
190 // delete templates
191 $db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title IN('dvz_shoutbox', 'dvz_shoutbox_panel', 'dvz_shoutbox_archive', 'dvz_shoutbox_archive_modoptions')");
192
193 // delete data
194 $db->query("DROP TABLE ".TABLE_PREFIX.'dvz_shoutbox');
195}
196function dvz_shoutbox_is_installed () {
197 global $db;
198 $query = $db->simple_select('settinggroups', '*', "name='dvz_shoutbox'");
199 return $db->num_rows($query);
200}
201function dvz_shoutbox_activate () {
202}
203function dvz_shoutbox_deactivate () {
204}
205
206// hooks
207function dvz_shoutbox_global_end () {
208 global $mybb, $lang;
209
210 $lang->load('dvz_shoutbox');
211
212 if ($mybb->input['action'] == 'shoutbox_archive') {
213 return dvz_shoutbox::show_archive();
214 }
215}
216function dvz_shoutbox_xmlhttp () {
217 global $mybb, $db, $lang, $charset;
218
219 switch ($mybb->input['action']) {
220
221 case 'dvz_sb_get_shouts':
222
223 $permissions = (
224 (dvz_shoutbox::access_view() && !isset($mybb->input['from']) ) ||
225 dvz_shoutbox::access_refresh()
226 );
227
228 $handler = function() use ($mybb, $db) {
229 $data = dvz_shoutbox::get_multiple("WHERE s.id > " . (int)$mybb->input['from'] . " ORDER BY s.id DESC LIMIT " . (int)$mybb->settings['dvz_sb_num']);
230
231 $html = null; // JS-handled empty response
232 $lastId = 0;
233
234 while ($row = $db->fetch_array($data)) {
235 if ($lastId == 0) {
236 $lastId = $row['id'];
237 }
238 $shout = dvz_shoutbox::render_shout($row);
239 $html = $mybb->settings['dvz_sb_reversed']
240 ? $shout . $html
241 : $html . $shout
242 ;
243 }
244
245 if ($html != null) {
246 echo json_encode(array(
247 'html' => $html,
248 'last' => $lastId,
249 ));
250 }
251 };
252
253 break;
254 case 'dvz_sb_shout':
255
256 $permissions = (
257 dvz_shoutbox::access_shout() &&
258 verify_post_check($mybb->input['key'], true)
259 );
260
261 $handler = function() use ($mybb) {
262 if (!dvz_shoutbox::antiflood_pass() && !dvz_shoutbox::access_mod()) die('A'); // JS-handled error (Anti-flood)
263
264 dvz_shoutbox::shout(array(
265 'uid' => $mybb->user['uid'],
266 'text' => $mybb->input['text'],
267 'ip' => get_ip(),
268 ));
269 };
270
271 break;
272 case 'dvz_sb_get':
273
274 $data = dvz_shoutbox::get($mybb->input['id']);
275
276 $permissions = (
277 (
278 dvz_shoutbox::access_mod() ||
279 (dvz_shoutbox::access_mod_own() && $data['uid'] == $mybb->user['uid'] && dvz_shoutbox::access_shout())
280 ) &&
281 verify_post_check($mybb->input['key'], true)
282 );
283
284 $handler = function() use ($data) {
285 echo json_encode(array(
286 'text' => $data['text'],
287 ));
288 };
289
290 break;
291 case 'dvz_sb_update':
292
293 $permissions = (
294 dvz_shoutbox::can_mod($mybb->input['id']) &&
295 verify_post_check($mybb->input['key'], true)
296 );
297
298 $handler = function() use ($mybb) {
299 dvz_shoutbox::update($mybb->input['id'], $mybb->input['text']);
300 echo dvz_shoutbox::parse($mybb->input['text'], dvz_shoutbox::get_username($mybb->input['id']));
301
302 };
303
304 break;
305 case 'dvz_sb_delete':
306
307 $permissions = (
308 dvz_shoutbox::can_mod($mybb->input['id']) &&
309 verify_post_check($mybb->input['key'], true)
310 );
311
312 $handler = function() use ($mybb) {
313 dvz_shoutbox::delete($mybb->input['id']);
314 };
315
316 break;
317
318 }
319
320 if (isset($permissions)) {
321
322 if ($permissions == false) {
323 echo 'P'; // JS-handled error (Permissions)
324 } else {
325
326 $lang->load("dvz_shoutbox");
327 header('Content-type: text/plain; charset='.$charset);
328 header('Cache-Control: no-store'); // Chrome request caching issue
329 $handler();
330
331 }
332 }
333
334}
335function dvz_shoutbox () {
336 return dvz_shoutbox::load_window();
337}
338
339class dvz_shoutbox {
340
341 // internal cache
342 static $userGroups = false;
343
344 // immediate output
345 static function load_window () {
346 global $templates, $dvz_shoutbox, $lang, $mybb, $theme;
347
348 // MyBB template
349 $dvz_shoutbox = null;
350
351 // dvz_shoutbox template
352 $javascript = null;
353 $panel = null;
354 $classes = null;
355
356 if (dvz_shoutbox::access_view()) {
357
358 if (dvz_shoutbox::is_user()) {
359
360 // message: blocked
361 if (dvz_shoutbox::is_blocked()) {
362 $panel = '<div class="panel blocked"><p>' . $lang->dvz_sb_user_blocked . '</p></div>';
363 }
364 // message: minimum posts
365 else if (!dvz_shoutbox::access_minposts() && !dvz_shoutbox::access_mod()) {
366 $panel = '<div class="panel minposts"><p>' . str_replace('{MINPOSTS}', $mybb->settings['dvz_sb_minposts'], $lang->dvz_sb_minposts) . '</p></div>';
367 }
368 // shout form
369 else if (dvz_shoutbox::access_shout()) {
370 eval('$panel = "' . $templates->get('dvz_shoutbox_panel') . '";');
371 }
372
373 }
374
375 $js = null;
376
377 // configuration
378 $js .= 'dvz_shoutbox.interval = ' . (dvz_shoutbox::access_refresh() ? (float)$mybb->settings['dvz_sb_interval'] : 0) . ';' . PHP_EOL;
379 $js .= 'dvz_shoutbox.antiflood = ' . (dvz_shoutbox::access_mod() ? 0 : (float)$mybb->settings['dvz_sb_antiflood']) . ';' . PHP_EOL;
380 $js .= 'dvz_shoutbox.maxShouts = ' . (int)$mybb->settings['dvz_sb_num'] . ';' . PHP_EOL;
381 $js .= 'dvz_shoutbox.awayTime = ' . (float)$mybb->settings['dvz_sb_away'] . '*1000;' . PHP_EOL;
382 $js .= 'dvz_shoutbox.lang = [\'' . $lang->dvz_sb_delete_confirm . '\', \'' . str_replace('{ANTIFLOOD}', $mybb->settings['dvz_sb_antiflood'], $lang->dvz_sb_antiflood) . '\', \''.$lang->dvz_sb_permissions.'\'];' . PHP_EOL;
383
384 // reversed order
385 if ($mybb->settings['dvz_sb_reversed']) {
386 $js .= 'dvz_shoutbox.reversed = true;' . PHP_EOL;
387 }
388
389 // lazyload
390 if ($mybb->settings['dvz_sb_lazyload']) {
391 $js .= 'dvz_shoutbox.lazyMode = \'' . $mybb->settings['dvz_sb_lazyload'] . '\';' . PHP_EOL;
392 $js .= 'jQuery(window).bind(\'scroll resize\', dvz_shoutbox.checkVisibility);' . PHP_EOL;
393 }
394
395 // away mode
396 if ($mybb->settings['dvz_sb_away']) {
397 $js .= 'jQuery(window).on(\'mousemove click dblclick keydown scroll\', dvz_shoutbox.updateActivity);' . PHP_EOL;
398 }
399
400 // shoutbox status
401 $status = isset($_COOKIE['dvz_sb_status'])
402 ? (bool)$_COOKIE['dvz_sb_status']
403 : (bool)$mybb->settings['dvz_sb_status']
404 ;
405 $js .= 'dvz_shoutbox.status = ' . (int)$status . ';' . PHP_EOL;
406
407 if ($status == false) {
408 $classes .= ' collapsed';
409 }
410
411 $javascript = '
412<script>
413' . $js . '
414dvz_shoutbox.updateActivity();
415dvz_shoutbox.loop();
416</script>';
417
418 eval('$dvz_shoutbox = "' . $templates->get('dvz_shoutbox') . '";');
419
420 }
421
422 }
423 static function show_archive () {
424 global $db, $mybb, $templates, $lang, $theme, $footer, $headerinclude, $header, $charset;
425
426 if (!dvz_shoutbox::access_view()) return false;
427
428 header('Content-type: text/html; charset='.$charset);
429
430 add_breadcrumb($lang->dvz_sb_shoutbox, "index.php?action=shoutbox_archive");
431
432 // moderation panel
433 if (dvz_shoutbox::access_mod()) {
434
435 if (isset($mybb->input['banlist']) && verify_post_check($mybb->input['postkey'])) {
436 dvz_shoutbox::banlist_update($mybb->input['banlist']);
437 }
438
439 if (isset($mybb->input['days']) && verify_post_check($mybb->input['postkey'])) {
440 if ($mybb->input['days'] == 'all') {
441 dvz_shoutbox::clear();
442 } else {
443 $allowed = array(2, 7, 30, 90);
444 if (in_array($mybb->input['days'], $allowed)) {
445 dvz_shoutbox::clear($mybb->input['days']);
446 }
447 }
448 }
449
450 $blocked_users = htmlspecialchars($mybb->settings['dvz_sb_blocked_users']);
451 eval('$modoptions = "'.$templates->get("dvz_shoutbox_archive_modoptions").'";');
452
453 } else {
454 $modoptions = null;
455 }
456
457 // pagination
458 $shoutsTotal = dvz_shoutbox::count();
459 $pageNum = (int)$mybb->input['page'];
460 $perPage = (int)$mybb->settings['dvz_sb_num_archive'];
461 $pages = ceil($shoutsTotal / $perPage);
462
463 if (!$pageNum || $pageNum < 1 || $pageNum > $pages) $pageNum = 1;
464
465 $start = ($pageNum - 1) * $perPage;
466
467 if ($shoutsTotal > $perPage) {
468 $multipage = multipage($shoutsTotal, $perPage, $pageNum, 'index.php?action=shoutbox_archive');
469 }
470
471 $data = dvz_shoutbox::get_multiple("ORDER by s.id DESC LIMIT $start,$perPage");
472
473 $archive = null;
474
475 while ($row = $db->fetch_array($data)) {
476 $archive .= dvz_shoutbox::render_shout($row, true);
477 }
478
479 $javascript = '
480<script>
481dvz_shoutbox.lang = [\'' . $lang->dvz_sb_delete_confirm . '\', \'' . str_replace('{ANTIFLOOD}', $mybb->settings['dvz_sb_antiflood'], $lang->dvz_sb_antiflood) . '\', \''.$lang->dvz_sb_permissions.'\'];
482</script>';
483
484 eval('$content = "'.$templates->get("dvz_shoutbox_archive").'";');
485
486 output_page($content);
487
488 exit;
489
490 }
491 static function render_shout ($data, $static = false) {
492 global $mybb, $lang;
493
494 $id = $data['id'];
495 $text = dvz_shoutbox::parse($data['text'], $data['username']);
496 $date = my_date($mybb->settings['dvz_sb_dateformat'], $data['date']);
497
498 $avatar = '<img src="' . (empty($data['avatar']) ? 'images/default_avatar.gif' : $data['avatar']) . '" alt="avatar" />';
499 $user = '<a href="member.php?action=profile&uid=' . (int)$data['uid'] . '">' . format_name($data['username'], $data['usergroup'], $data['displaygroup']) . '</a>';
500
501
502 $notes = null;
503 $attributes = null;
504
505 $own = $data['uid'] == $mybb->user['uid'];
506
507 if ($static) {
508 if (dvz_shoutbox::access_mod()) {
509 $notes .= '<span class="ip">'.$data['ip'].'</span>';
510 }
511
512 if (
513 dvz_shoutbox::access_mod() ||
514 (dvz_shoutbox::access_mod_own() && $own)
515 ) {
516 $notes .= '<a href="" class="mod edit">E</a><a href="" class="mod del">X</a>';
517 }
518 }
519
520 if (
521 dvz_shoutbox::access_mod() ||
522 (dvz_shoutbox::access_mod_own() && $own)
523 ) {
524 $attributes .= ' data-mod';
525 }
526
527 if ($own) {
528 $attributes .= ' data-own';
529 }
530
531 return '
532<div class="entry" data-id="'.$id.'" data-username="'.$data['username'].'"'.$attributes.'>
533 <div class="avatar">'.$avatar.'</div>
534 <div class="user">'.$user.':</div>
535 <div class="text">'.$text.'</div>
536 <div class="info"><span class="date">'.$date.'</span>'.$notes.'</div>
537</div>';
538
539 }
540
541 // data manipulation
542 static function get ($id) {
543 global $db;
544 return $db->fetch_array( $db->simple_select('dvz_shoutbox', '*', 'id=' . (int)$id) );
545 }
546 static function get_multiple ($clauses) {
547 global $db;
548 return $db->query("
549 SELECT
550 s.*, u.username, u.usergroup, u.displaygroup, u.avatar
551 FROM
552 ".TABLE_PREFIX."dvz_shoutbox s
553 LEFT JOIN ".TABLE_PREFIX."users u ON u.uid = s.uid
554 ".$clauses."
555 ");
556 }
557 static function get_username ($id) {
558 global $db;
559 return $db->fetch_field( $db->query("SELECT username FROM ".TABLE_PREFIX."users u, ".TABLE_PREFIX."dvz_shoutbox s WHERE u.uid=s.uid AND s.id=" . (int)$id), 'username');
560 }
561 static function user_last_shout_time ($uid) {
562 global $db;
563 return $db->fetch_field(
564 $db->simple_select('dvz_shoutbox', 'date', 'uid=' . (int)$uid, array(
565 'order_by' => 'date',
566 'order_dir' => 'desc',
567 'limit' => 1
568 )), 'date');
569 }
570 static function count () {
571 global $db;
572 return $db->fetch_field(
573 $db->simple_select('dvz_shoutbox', 'COUNT(*) as n'),
574 'n'
575 );
576 }
577 static function shout ($data) {
578 global $db;
579
580 foreach ($data as &$item) {
581 $item = $db->escape_string($item);
582 }
583
584 $data['date'] = time();
585
586 return $db->insert_query('dvz_shoutbox', $data);
587 }
588 static function update ($id, $text) {
589 global $db;
590 return $db->update_query('dvz_shoutbox', array('text' => $db->escape_string($text)), 'id=' . (int)$id);
591 }
592 static function banlist_update ($new) {
593 global $db;
594 $db->update_query('settings', array('value' => $db->escape_string($new)), "name='dvz_sb_blocked_users'");
595 rebuild_settings();
596 }
597 static function delete ($id) {
598 global $db;
599 return $db->delete_query('dvz_shoutbox', 'id=' . (int)$id);
600 }
601 static function clear ($days = false) {
602 global $db;
603 if ($days) {
604 $where = 'date < '.( time()-((int)$days*86400) );
605 } else {
606 $where = false;
607 }
608 return $db->delete_query('dvz_shoutbox', $where);
609 }
610
611 // permissions
612 static function is_user () {
613 global $mybb;
614 return !($mybb->user['usergroup'] == 1 && $mybb->user['uid'] < 1);
615 }
616 static function is_blocked () {
617 global $mybb;
618 $array = dvz_shoutbox::settings_get_csv('blocked_users');
619 return in_array($mybb->user['uid'], $array);
620 }
621 static function access_view () {
622 global $mybb;
623
624 $array = dvz_shoutbox::settings_get_csv('groups_view');
625
626 return (
627 empty($array) ||
628 dvz_shoutbox::member_of($array)
629 );
630 }
631 static function access_refresh () {
632 global $mybb;
633
634 $array = dvz_shoutbox::settings_get_csv('groups_refresh');
635
636 return (
637 empty($array) ||
638 dvz_shoutbox::member_of($array)
639 );
640 }
641 static function access_shout () {
642 global $mybb;
643
644 $array = dvz_shoutbox::settings_get_csv('groups_shout');
645
646 return (
647 dvz_shoutbox::is_user() &&
648 !dvz_shoutbox::is_blocked() &&
649 (
650 dvz_shoutbox::access_mod() ||
651 (
652 dvz_shoutbox::access_view() &&
653 dvz_shoutbox::access_minposts() &&
654 (
655 empty($array) ||
656 dvz_shoutbox::member_of($array)
657 )
658 )
659 )
660 );
661 }
662 static function access_mod () {
663 global $mybb;
664
665 $array = dvz_shoutbox::settings_get_csv('groups_mod');
666 return (
667 dvz_shoutbox::member_of($array) ||
668 ($mybb->settings['dvz_sb_supermods'] && $mybb->usergroup['issupermod'])
669 );
670 }
671 static function access_mod_own () {
672 global $mybb;
673
674 if ($mybb->settings['dvz_sb_groups_mod_own']) {
675 $array = dvz_shoutbox::settings_get_csv('groups_mod_own');
676 return dvz_shoutbox::member_of($array);
677 } else {
678 return false;
679 }
680 }
681 static function access_minposts () {
682 global $mybb;
683 return $mybb->user['postnum'] >= $mybb->settings['dvz_sb_minposts'];
684 }
685 static function can_mod ($shoutId) {
686 global $mybb;
687
688 if (dvz_shoutbox::access_mod()) {
689 return true;
690 } else if (dvz_shoutbox::access_mod_own() && dvz_shoutbox::access_shout()) {
691
692 $data = dvz_shoutbox::get($shoutId);
693
694 if ($data['uid'] == $mybb->user['uid']) {
695 return true;
696 }
697
698 }
699
700 return false;
701
702 }
703
704 // core
705 static function parse ($message, $me_username) {
706 global $mybb;
707
708 require_once MYBB_ROOT.'inc/class_parser.php';
709
710 $parser = new postParser;
711 $options = array(
712 'allow_mycode' => $mybb->settings['dvz_sb_mycode'],
713 'allow_smilies' => $mybb->settings['dvz_sb_smilies'],
714 'allow_imgcode' => 0,
715 'me_username' => $me_username,
716 );
717
718 return $parser->parse_message($message, $options);
719
720 }
721 static function antiflood_pass () {
722 global $mybb;
723
724 return (
725 !$mybb->settings['dvz_sb_antiflood'] ||
726 ( time() - dvz_shoutbox::user_last_shout_time($mybb->user['uid']) ) > $mybb->settings['dvz_sb_antiflood']
727 );
728
729 }
730 static function member_of ($groupsArray) {
731 global $mybb;
732
733 if (dvz_shoutbox::$userGroups == false) {
734 dvz_shoutbox::$userGroups = explode(',', $mybb->user['additionalgroups']);
735 dvz_shoutbox::$userGroups[] = $mybb->user['usergroup'];
736 }
737
738 return array_intersect(dvz_shoutbox::$userGroups, $groupsArray);
739 }
740 static function settings_get_csv ($name) {
741 global $mybb;
742
743 $items = explode(',', $mybb->settings['dvz_sb_'.$name]);
744
745 if (count($items) == 1 && $items[0] == '') {
746 return array();
747 } else
748
749 return $items;
750 }
751
752}
753
754?>