forked from SimpleMachines/SMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSI.php
2361 lines (2045 loc) · 91.4 KB
/
SSI.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines http://www.simplemachines.org
* @copyright 2017 Simple Machines and individual contributors
* @license http://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1 Beta 3
*/
// Don't do anything if SMF is already loaded.
if (defined('SMF'))
return true;
define('SMF', 'SSI');
// We're going to want a few globals... these are all set later.
global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename;
global $db_type, $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
global $db_connection, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cachedir;
// Remember the current configuration so it can be set back.
$ssi_magic_quotes_runtime = function_exists('get_magic_quotes_gpc') && get_magic_quotes_runtime();
if (function_exists('set_magic_quotes_runtime'))
@set_magic_quotes_runtime(0);
$time_start = microtime();
// Just being safe...
foreach (array('db_character_set', 'cachedir') as $variable)
if (isset($GLOBALS[$variable]))
unset($GLOBALS[$variable]);
// Get the forum's settings for database and file paths.
require_once(dirname(__FILE__) . '/Settings.php');
// Make absolutely sure the cache directory is defined.
if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
$cachedir = $boarddir . '/cache';
$ssi_error_reporting = error_reporting(defined('E_STRICT') ? E_ALL | E_STRICT : E_ALL);
/* Set this to one of three values depending on what you want to happen in the case of a fatal error.
false: Default, will just load the error sub template and die - not putting any theme layers around it.
true: Will load the error sub template AND put the SMF layers around it (Not useful if on total custom pages).
string: Name of a callback function to call in the event of an error to allow you to define your own methods. Will die after function returns.
*/
$ssi_on_error_method = false;
// Don't do john didley if the forum's been shut down completely.
if ($maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
die($mmessage);
// Fix for using the current directory as a path.
if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
$sourcedir = dirname(__FILE__) . substr($sourcedir, 1);
// Load the important includes.
require_once($sourcedir . '/QueryString.php');
require_once($sourcedir . '/Session.php');
require_once($sourcedir . '/Subs.php');
require_once($sourcedir . '/Errors.php');
require_once($sourcedir . '/Logging.php');
require_once($sourcedir . '/Load.php');
require_once($sourcedir . '/Security.php');
require_once($sourcedir . '/Class-BrowserDetect.php');
require_once($sourcedir . '/Subs-Auth.php');
// Create a variable to store some SMF specific functions in.
$smcFunc = array();
// Initiate the database connection and define some database functions to use.
loadDatabase();
// Load installed 'Mods' settings.
reloadSettings();
// Clean the request variables.
cleanRequest();
// Seed the random generator?
if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
smf_seed_generator();
// Check on any hacking attempts.
if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
die('No direct access...');
elseif (isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] == (int) $ssi_theme)
die('No direct access...');
elseif (isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] == (int) $ssi_theme)
die('No direct access...');
elseif (isset($_REQUEST['ssi_layers'], $ssi_layers) && (@get_magic_quotes_gpc() ? stripslashes($_REQUEST['ssi_layers']) : $_REQUEST['ssi_layers']) == $ssi_layers)
die('No direct access...');
if (isset($_REQUEST['context']))
die('No direct access...');
// Gzip output? (because it must be boolean and true, this can't be hacked.)
if (isset($ssi_gzip) && $ssi_gzip === true && ini_get('zlib.output_compression') != '1' && ini_get('output_handler') != 'ob_gzhandler' && version_compare(PHP_VERSION, '4.2.0', '>='))
ob_start('ob_gzhandler');
else
$modSettings['enableCompressedOutput'] = '0';
// Primarily, this is to fix the URLs...
ob_start('ob_sessrewrite');
// Start the session... known to scramble SSI includes in cases...
if (!headers_sent())
loadSession();
else
{
if (isset($_COOKIE[session_name()]) || isset($_REQUEST[session_name()]))
{
// Make a stab at it, but ignore the E_WARNINGs generated because we can't send headers.
$temp = error_reporting(error_reporting() & !E_WARNING);
loadSession();
error_reporting($temp);
}
if (!isset($_SESSION['session_value']))
{
$_SESSION['session_var'] = substr(md5(mt_rand() . session_id() . mt_rand()), 0, rand(7, 12));
$_SESSION['session_value'] = md5(session_id() . mt_rand());
}
$sc = $_SESSION['session_value'];
}
// Get rid of $board and $topic... do stuff loadBoard would do.
unset($board, $topic);
$user_info['is_mod'] = false;
$context['user']['is_mod'] = &$user_info['is_mod'];
$context['linktree'] = array();
// Load the user and their cookie, as well as their settings.
loadUserSettings();
// Load the current user's permissions....
loadPermissions();
// Load the current or SSI theme. (just use $ssi_theme = id_theme;)
loadTheme(isset($ssi_theme) ? (int) $ssi_theme : 0);
// @todo: probably not the best place, but somewhere it should be set...
if (!headers_sent())
header('Content-Type: text/html; charset=' . (empty($modSettings['global_character_set']) ? (empty($txt['lang_character_set']) ? 'ISO-8859-1' : $txt['lang_character_set']) : $modSettings['global_character_set']));
// Take care of any banning that needs to be done.
if (isset($_REQUEST['ssi_ban']) || (isset($ssi_ban) && $ssi_ban === true))
is_not_banned();
// Do we allow guests in here?
if (empty($ssi_guest_access) && empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && basename($_SERVER['PHP_SELF']) != 'SSI.php')
{
require_once($sourcedir . '/Subs-Auth.php');
KickGuest();
obExit(null, true);
}
// Load the stuff like the menu bar, etc.
if (isset($ssi_layers))
{
$context['template_layers'] = $ssi_layers;
template_header();
}
else
setupThemeContext();
// Make sure they didn't muss around with the settings... but only if it's not cli.
if (isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['is_cli']) && session_id() == '')
trigger_error($txt['ssi_session_broken'], E_USER_NOTICE);
// Without visiting the forum this session variable might not be set on submit.
if (!isset($_SESSION['USER_AGENT']) && (!isset($_GET['ssi_function']) || $_GET['ssi_function'] !== 'pollVote'))
$_SESSION['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
// Have the ability to easily add functions to SSI.
call_integration_hook('integrate_SSI');
// Ignore a call to ssi_* functions if we are not accessing SSI.php directly.
if (basename($_SERVER['PHP_SELF']) == 'SSI.php')
{
// You shouldn't just access SSI.php directly by URL!!
if (!isset($_GET['ssi_function']))
die(sprintf($txt['ssi_not_direct'], $user_info['is_admin'] ? '\'' . addslashes(__FILE__) . '\'' : '\'SSI.php\''));
// Call a function passed by GET.
if (function_exists('ssi_' . $_GET['ssi_function']) && (!empty($modSettings['allow_guestAccess']) || !$user_info['is_guest']))
call_user_func('ssi_' . $_GET['ssi_function']);
exit;
}
// To avoid side effects later on.
unset($_GET['ssi_function']);
error_reporting($ssi_error_reporting);
if (function_exists('set_magic_quotes_runtime'))
@set_magic_quotes_runtime($ssi_magic_quotes_runtime);
return true;
/**
* This shuts down the SSI and shows the footer.
* @return void
*/
function ssi_shutdown()
{
if (!isset($_GET['ssi_function']) || $_GET['ssi_function'] != 'shutdown')
template_footer();
}
/**
* Display a welcome message, like: Hey, User, you have 0 messages, 0 are new.
* @param string $output_method The output method. If 'echo', will display everything. Otherwise returns an array of user info.
* @return void|array Displays a welcome message or returns an array of user data depending on output_method.
*/
function ssi_welcome($output_method = 'echo')
{
global $context, $txt, $scripturl;
if ($output_method == 'echo')
{
if ($context['user']['is_guest'])
echo sprintf($txt[$context['can_register'] ? 'welcome_guest_register' : 'welcome_guest'], $txt['guest_title'], $context['forum_name_html_safe'], $scripturl . '?action=login', 'return reqOverlayDiv(this.href, ' . JavaScriptEscape($txt['login']) . ');', $scripturl . '?action=signup');
else
echo $txt['hello_member'], ' <strong>', $context['user']['name'], '</strong>', allowedTo('pm_read') ? ', ' . (empty($context['user']['messages']) ? $txt['msg_alert_no_messages'] : (($context['user']['messages'] == 1 ? sprintf($txt['msg_alert_one_message'], $scripturl . '?action=pm') : sprintf($txt['msg_alert_many_message'], $scripturl . '?action=pm', $context['user']['messages'])) . ', ' . ($context['user']['unread_messages'] == 1 ? $txt['msg_alert_one_new'] : sprintf($txt['msg_alert_many_new'], $context['user']['unread_messages'])))) : '';
}
// Don't echo... then do what?!
else
return $context['user'];
}
/**
* Display a menu bar, like is displayed at the top of the forum.
* @param string $output_method The output method. If 'echo', will display the menu, otherwise returns an array of menu data.
* @return void|array Displays the menu or returns an array of menu data depending on output_method.
*/
function ssi_menubar($output_method = 'echo')
{
global $context;
if ($output_method == 'echo')
template_menu();
// What else could this do?
else
return $context['menu_buttons'];
}
/**
* Show a logout link.
* @param string $redirect_to A URL to redirect the user to after they log out.
* @param string $output_method The output method. If 'echo', shows a logout link, otherwise returns the HTML for it.
* @return void|string Displays a logout link or returns its HTML depending on output_method.
*/
function ssi_logout($redirect_to = '', $output_method = 'echo')
{
global $context, $txt, $scripturl;
if ($redirect_to != '')
$_SESSION['logout_url'] = $redirect_to;
// Guests can't log out.
if ($context['user']['is_guest'])
return false;
$link = '<a href="' . $scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['logout'] . '</a>';
if ($output_method == 'echo')
echo $link;
else
return $link;
}
/**
* Recent post list: [board] Subject by Poster Date
* @param int $num_recent How many recent posts to display
* @param null|array $exclude_boards If set, doesn't show posts from the specified boards
* @param null|array $include_boards If set, only includes posts from the specified boards
* @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of information about them.
* @param bool $limit_body Whether or not to only show the first 384 characters of each post
* @return void|array Displays a list of recent posts or returns an array of information about them depending on output_method.
*/
function ssi_recentPosts($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo', $limit_body = true)
{
global $modSettings, $context;
// Excluding certain boards...
if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
$exclude_boards = array($modSettings['recycle_board']);
else
$exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
// What about including certain boards - note we do some protection here as pre-2.0 didn't have this parameter.
if (is_array($include_boards) || (int) $include_boards === $include_boards)
{
$include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
}
elseif ($include_boards != null)
{
$include_boards = array();
}
// Let's restrict the query boys (and girls)
$query_where = '
m.id_msg >= {int:min_message_id}
' . (empty($exclude_boards) ? '' : '
AND b.id_board NOT IN ({array_int:exclude_boards})') . '
' . ($include_boards === null ? '' : '
AND b.id_board IN ({array_int:include_boards})') . '
AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
AND m.approved = {int:is_approved}' : '');
$query_where_params = array(
'is_approved' => 1,
'include_boards' => $include_boards === null ? '' : $include_boards,
'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
'min_message_id' => $modSettings['maxMsgID'] - (!empty($context['min_message_posts']) ? $context['min_message_posts'] : 25) * min($num_recent, 5),
);
// Past to this simpleton of a function...
return ssi_queryPosts($query_where, $query_where_params, $num_recent, 'm.id_msg DESC', $output_method, $limit_body);
}
/**
* Fetches one or more posts by ID.
* @param array $post_ids An array containing the IDs of the posts to show
* @param bool $override_permissions Whether to ignore permissions. If true, will show posts even if the user doesn't have permission to see them.
* @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of info about them
* @return void|array Displays the specified posts or returns an array of info about them, depending on output_method.
*/
function ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo')
{
global $modSettings;
if (empty($post_ids))
return;
// Allow the user to request more than one - why not?
$post_ids = is_array($post_ids) ? $post_ids : array($post_ids);
// Restrict the posts required...
$query_where = '
m.id_msg IN ({array_int:message_list})' . ($override_permissions ? '' : '
AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
AND m.approved = {int:is_approved}' : '');
$query_where_params = array(
'message_list' => $post_ids,
'is_approved' => 1,
);
// Then make the query and dump the data.
return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg DESC', $output_method, false, $override_permissions);
}
/**
* This handles actually pulling post info. Called from other functions to eliminate duplication.
* @param string $query_where The WHERE clause for the query
* @param array $query_where_params An array of parameters for the WHERE clause
* @param int $query_limit The maximum number of rows to return
* @param string $query_order The ORDER BY clause for the query
* @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of info about them.
* @param bool $limit_body If true, will only show the first 384 characters of the post rather than all of it
* @param bool|false $override_permissions Whether or not to ignore permissions. If true, will show all posts regardless of whether the user can actually see them
* @return void|array Displays the posts or returns an array of info about them, depending on output_method
*/
function ssi_queryPosts($query_where = '', $query_where_params = array(), $query_limit = 10, $query_order = 'm.id_msg DESC', $output_method = 'echo', $limit_body = false, $override_permissions = false)
{
global $scripturl, $txt, $user_info;
global $modSettings, $smcFunc, $context;
if (!empty($modSettings['enable_likes']))
$context['can_like'] = allowedTo('likes_like');
// Find all the posts. Newer ones will have higher IDs.
$request = $smcFunc['db_query']('substring', '
SELECT
m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, m.id_board, m.likes, b.name AS board_name,
IFNULL(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . ', ' . ($limit_body ? 'SUBSTRING(m.body, 1, 384) AS body' : 'm.body') . ', m.smileys_enabled
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
WHERE 1=1 ' . ($override_permissions ? '' : '
AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
AND m.approved = {int:is_approved}' : '') . '
' . (empty($query_where) ? '' : 'AND ' . $query_where) . '
ORDER BY ' . $query_order . '
' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
array_merge($query_where_params, array(
'current_member' => $user_info['id'],
'is_approved' => 1,
))
);
$posts = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
// Censor it!
censorText($row['subject']);
censorText($row['body']);
$preview = strip_tags(strtr($row['body'], array('<br>' => ' ')));
// Build the array.
$posts[$row['id_msg']] = array(
'id' => $row['id_msg'],
'board' => array(
'id' => $row['id_board'],
'name' => $row['board_name'],
'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
),
'topic' => $row['id_topic'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'short_subject' => shorten_subject($row['subject'], 25),
'preview' => $smcFunc['strlen']($preview) > 128 ? $smcFunc['substr']($preview, 0, 128) . '...' : $preview,
'body' => $row['body'],
'time' => timeformat($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
'new' => !empty($row['is_read']),
'is_new' => empty($row['is_read']),
'new_from' => $row['new_from'],
);
// Get the likes for each message.
if (!empty($modSettings['enable_likes']))
$posts[$row['id_msg']]['likes'] = array(
'count' => $row['likes'],
'you' => in_array($row['id_msg'], prepareLikesContext($row['id_topic'])),
'can_like' => !$context['user']['is_guest'] && $row['id_member'] != $context['user']['id'] && !empty($context['can_like']),
);
}
$smcFunc['db_free_result']($request);
// If mods want to do somthing with this list of posts, let them do that now.
call_integration_hook('integrate_ssi_queryPosts', array(&$posts));
// Just return it.
if ($output_method != 'echo' || empty($posts))
return $posts;
echo '
<table style="border: none" class="ssi_table">';
foreach ($posts as $post)
echo '
<tr>
<td style="text-align: right; vertical-align: top; white-space: nowrap">
[', $post['board']['link'], ']
</td>
<td style="vertical-align: top">
<a href="', $post['href'], '">', $post['subject'], '</a>
', $txt['by'], ' ', $post['poster']['link'], '
', $post['is_new'] ? '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><span class="new_posts">' . $txt['new'] . '</span></a>' : '', '
</td>
<td style="text-align: right; white-space: nowrap">
', $post['time'], '
</td>
</tr>';
echo '
</table>';
}
/**
* Recent topic list: [board] Subject by Poster Date
* @param int $num_recent How many recent topics to show
* @param null|array $exclude_boards If set, exclude topics from the specified board(s)
* @param null|array $include_boards If set, only include topics from the specified board(s)
* @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
* @return void|array Either displays a list of topics or returns an array of info about them, depending on output_method.
*/
function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo')
{
global $settings, $scripturl, $txt, $user_info;
global $modSettings, $smcFunc, $context;
if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
$exclude_boards = array($modSettings['recycle_board']);
else
$exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));
// Only some boards?.
if (is_array($include_boards) || (int) $include_boards === $include_boards)
{
$include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
}
elseif ($include_boards != null)
{
$output_method = $include_boards;
$include_boards = array();
}
$icon_sources = array();
foreach ($context['stable_icons'] as $icon)
$icon_sources[$icon] = 'images_url';
// Find all the posts in distinct topics. Newer ones will have higher IDs.
$request = $smcFunc['db_query']('substring', '
SELECT
t.id_topic, b.id_board, b.name AS board_name
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
WHERE t.id_last_msg >= {int:min_message_id}' . (empty($exclude_boards) ? '' : '
AND b.id_board NOT IN ({array_int:exclude_boards})') . '' . (empty($include_boards) ? '' : '
AND b.id_board IN ({array_int:include_boards})') . '
AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
AND t.approved = {int:is_approved}
AND ml.approved = {int:is_approved}' : '') . '
ORDER BY t.id_last_msg DESC
LIMIT ' . $num_recent,
array(
'include_boards' => empty($include_boards) ? '' : $include_boards,
'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
'min_message_id' => $modSettings['maxMsgID'] - (!empty($context['min_message_topics']) ? $context['min_message_topics'] : 35) * min($num_recent, 5),
'is_approved' => 1,
)
);
$topics = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$topics[$row['id_topic']] = $row;
$smcFunc['db_free_result']($request);
// Did we find anything? If not, bail.
if (empty($topics))
return array();
$recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
// Find all the posts in distinct topics. Newer ones will have higher IDs.
$request = $smcFunc['db_query']('substring', '
SELECT
mf.poster_time, mf.subject, ml.id_topic, mf.id_member, ml.id_msg, t.num_replies, t.num_views, mg.online_color,
IFNULL(mem.real_name, mf.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= ml.id_msg_modified AS is_read,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . ', SUBSTRING(mf.body, 1, 384) AS body, mf.smileys_enabled, mf.icon
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_last_msg)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mf.id_member)' . (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})' : '') . '
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
WHERE t.id_topic IN ({array_int:topic_list})',
array(
'current_member' => $user_info['id'],
'topic_list' => array_keys($topics),
)
);
$posts = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br>' => ' ')));
if ($smcFunc['strlen']($row['body']) > 128)
$row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
// Censor the subject.
censorText($row['subject']);
censorText($row['body']);
// Recycled icon
if (!empty($recycle_board) && $topics[$row['id_topic']]['id_board'])
$row['icon'] = 'recycled';
if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
// Build the array.
$posts[] = array(
'board' => array(
'id' => $topics[$row['id_topic']]['id_board'],
'name' => $topics[$row['id_topic']]['board_name'],
'href' => $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0">' . $topics[$row['id_topic']]['board_name'] . '</a>',
),
'topic' => $row['id_topic'],
'poster' => array(
'id' => $row['id_member'],
'name' => $row['poster_name'],
'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'replies' => $row['num_replies'],
'views' => $row['num_views'],
'short_subject' => shorten_subject($row['subject'], 25),
'preview' => $row['body'],
'time' => timeformat($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
// Retained for compatibility - is technically incorrect!
'new' => !empty($row['is_read']),
'is_new' => empty($row['is_read']),
'new_from' => $row['new_from'],
'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" style="vertical-align:middle;" alt="' . $row['icon'] . '">',
);
}
$smcFunc['db_free_result']($request);
// If mods want to do somthing with this list of topics, let them do that now.
call_integration_hook('integrate_ssi_recentTopics', array(&$posts));
// Just return it.
if ($output_method != 'echo' || empty($posts))
return $posts;
echo '
<table style="border: none" class="ssi_table">';
foreach ($posts as $post)
echo '
<tr>
<td style="text-align: right; vertical-align: top; white-space: nowrap">
[', $post['board']['link'], ']
</td>
<td style="vertical-align: top">
<a href="', $post['href'], '">', $post['subject'], '</a>
', $txt['by'], ' ', $post['poster']['link'], '
', !$post['is_new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow"><span class="new_posts">' . $txt['new'] . '</span></a>', '
</td>
<td style="text-align: right; white-space: nowrap">
', $post['time'], '
</td>
</tr>';
echo '
</table>';
}
/**
* Shows a list of top posters
* @param int $topNumber How many top posters to list
* @param string $output_method The output method. If 'echo', will display a list of users, otherwise returns an array of info about them.
* @return void|array Either displays a list of users or returns an array of info about them, depending on output_method.
*/
function ssi_topPoster($topNumber = 1, $output_method = 'echo')
{
global $scripturl, $smcFunc;
// Find the latest poster.
$request = $smcFunc['db_query']('', '
SELECT id_member, real_name, posts
FROM {db_prefix}members
ORDER BY posts DESC
LIMIT ' . $topNumber,
array(
)
);
$return = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$return[] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
'posts' => $row['posts']
);
$smcFunc['db_free_result']($request);
// If mods want to do somthing with this list of members, let them do that now.
call_integration_hook('integrate_ssi_topPoster', array(&$return));
// Just return all the top posters.
if ($output_method != 'echo')
return $return;
// Make a quick array to list the links in.
$temp_array = array();
foreach ($return as $member)
$temp_array[] = $member['link'];
echo implode(', ', $temp_array);
}
/**
* Shows a list of top boards based on activity
* @param int $num_top How many boards to display
* @param string $output_method The output method. If 'echo', displays a list of boards, otherwise returns an array of info about them.
* @return void|array Displays a list of the top boards or returns an array of info about them, depending on output_method.
*/
function ssi_topBoards($num_top = 10, $output_method = 'echo')
{
global $txt, $scripturl, $user_info, $modSettings, $smcFunc;
// Find boards with lots of posts.
$request = $smcFunc['db_query']('', '
SELECT
b.name, b.num_topics, b.num_posts, b.id_board,' . (!$user_info['is_guest'] ? ' 1 AS is_read' : '
(IFNULL(lb.id_msg, 0) >= b.id_last_msg) AS is_read') . '
FROM {db_prefix}boards AS b
LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
AND b.id_board != {int:recycle_board}' : '') . '
ORDER BY b.num_posts DESC
LIMIT ' . $num_top,
array(
'current_member' => $user_info['id'],
'recycle_board' => (int) $modSettings['recycle_board'],
)
);
$boards = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$boards[] = array(
'id' => $row['id_board'],
'num_posts' => $row['num_posts'],
'num_topics' => $row['num_topics'],
'name' => $row['name'],
'new' => empty($row['is_read']),
'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'
);
$smcFunc['db_free_result']($request);
// If mods want to do somthing with this list of boards, let them do that now.
call_integration_hook('integrate_ssi_topBoards', array(&$boards));
// If we shouldn't output or have nothing to output, just jump out.
if ($output_method != 'echo' || empty($boards))
return $boards;
echo '
<table class="ssi_table">
<tr>
<th style="text-align: left">', $txt['board'], '</th>
<th style="text-align: left">', $txt['board_topics'], '</th>
<th style="text-align: left">', $txt['posts'], '</th>
</tr>';
foreach ($boards as $sBoard)
echo '
<tr>
<td>', $sBoard['link'], $sBoard['new'] ? ' <a href="' . $sBoard['href'] . '"><span class="new_posts">' . $txt['new'] . '</span></a>' : '', '</td>
<td style="text-align: right">', comma_format($sBoard['num_topics']), '</td>
<td style="text-align: right">', comma_format($sBoard['num_posts']), '</td>
</tr>';
echo '
</table>';
}
// Shows the top topics.
/**
* Shows a list of top topics based on views or replies
* @param string $type Can be either replies or views
* @param int $num_topics How many topics to display
* @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them.
* @return void|array Either displays a list of topics or returns an array of info about them, depending on output_method.
*/
function ssi_topTopics($type = 'replies', $num_topics = 10, $output_method = 'echo')
{
global $txt, $scripturl, $modSettings, $smcFunc;
if ($modSettings['totalMessages'] > 100000)
{
// @todo Why don't we use {query(_wanna)_see_board}?
$request = $smcFunc['db_query']('', '
SELECT id_topic
FROM {db_prefix}topics
WHERE num_' . ($type != 'replies' ? 'views' : 'replies') . ' != 0' . ($modSettings['postmod_active'] ? '
AND approved = {int:is_approved}' : '') . '
ORDER BY num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
LIMIT {int:limit}',
array(
'is_approved' => 1,
'limit' => $num_topics > 100 ? ($num_topics + ($num_topics / 2)) : 100,
)
);
$topic_ids = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$topic_ids[] = $row['id_topic'];
$smcFunc['db_free_result']($request);
}
else
$topic_ids = array();
$request = $smcFunc['db_query']('', '
SELECT m.subject, m.id_topic, t.num_views, t.num_replies
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
WHERE {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
AND t.approved = {int:is_approved}' : '') . (!empty($topic_ids) ? '
AND t.id_topic IN ({array_int:topic_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
AND b.id_board != {int:recycle_enable}' : '') . '
ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
LIMIT {int:limit}',
array(
'topic_list' => $topic_ids,
'is_approved' => 1,
'recycle_enable' => $modSettings['recycle_board'],
'limit' => $num_topics,
)
);
$topics = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
censorText($row['subject']);
$topics[] = array(
'id' => $row['id_topic'],
'subject' => $row['subject'],
'num_replies' => $row['num_replies'],
'num_views' => $row['num_views'],
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
);
}
$smcFunc['db_free_result']($request);
// If mods want to do somthing with this list of topics, let them do that now.
call_integration_hook('integrate_ssi_topTopics', array(&$topics, $type));
if ($output_method != 'echo' || empty($topics))
return $topics;
echo '
<table class="ssi_table">
<tr>
<th style="text-align: left"></th>
<th style="text-align: left">', $txt['views'], '</th>
<th style="text-align: left">', $txt['replies'], '</th>
</tr>';
foreach ($topics as $sTopic)
echo '
<tr>
<td style="text-align: left">
', $sTopic['link'], '
</td>
<td style="text-align: right">', comma_format($sTopic['num_views']), '</td>
<td style="text-align: right">', comma_format($sTopic['num_replies']), '</td>
</tr>';
echo '
</table>';
}
/**
* Top topics based on replies
* @param int $num_topics How many topics to show
* @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
* @return void|array Either displays a list of top topics or returns an array of info about them, depending on output_method.
*/
function ssi_topTopicsReplies($num_topics = 10, $output_method = 'echo')
{
return ssi_topTopics('replies', $num_topics, $output_method);
}
/**
* Top topics based on views
* @param int $num_topics How many topics to show
* @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
* @return void|array Either displays a list of top topics or returns an array of info about them, depending on output_method.
*/
function ssi_topTopicsViews($num_topics = 10, $output_method = 'echo')
{
return ssi_topTopics('views', $num_topics, $output_method);
}
/**
* Show a link to the latest member: Please welcome, Someone, our latest member.
* @param string $output_method The output method. If 'echo', returns a string with a link to the latest member's profile, otherwise returns an array of info about them.
* @return void|array Displays a "welcome" message for the latest member or returns an array of info about them, depending on output_method.
*/
function ssi_latestMember($output_method = 'echo')
{
global $txt, $context;
if ($output_method == 'echo')
echo '
', sprintf($txt['welcome_newest_member'], $context['common_stats']['latest_member']['link']), '<br>';
else
return $context['common_stats']['latest_member'];
}
/**
* Fetches a random member.
* @param string $random_type If 'day', only fetches a new random member once a day.
* @param string $output_method The output method. If 'echo', displays a link to the member's profile, otherwise returns an array of info about them.
* @return void|array Displays a link to a random member's profile or returns an array of info about them depending on output_method.
*/
function ssi_randomMember($random_type = '', $output_method = 'echo')
{
global $modSettings;
// If we're looking for something to stay the same each day then seed the generator.
if ($random_type == 'day')
{
// Set the seed to change only once per day.
mt_srand(floor(time() / 86400));
}
// Get the lowest ID we're interested in.
$member_id = mt_rand(1, $modSettings['latestMember']);
$where_query = '
id_member >= {int:selected_member}
AND is_activated = {int:is_activated}';
$query_where_params = array(
'selected_member' => $member_id,
'is_activated' => 1,
);
$result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member ASC', $output_method);
// If we got nothing do the reverse - in case of unactivated members.
if (empty($result))
{
$where_query = '
id_member <= {int:selected_member}
AND is_activated = {int:is_activated}';
$query_where_params = array(
'selected_member' => $member_id,
'is_activated' => 1,
);
$result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member DESC', $output_method);
}
// Just to be sure put the random generator back to something... random.
if ($random_type != '')
mt_srand(time());
return $result;
}
/**
* Fetch specific members
* @param array $member_ids The IDs of the members to fetch
* @param string $output_method The output method. If 'echo', displays a list of links to the members' profiles, otherwise returns an array of info about them.
* @return void|array Displays links to the specified members' profiles or returns an array of info about them, depending on output_method.
*/
function ssi_fetchMember($member_ids = array(), $output_method = 'echo')
{
if (empty($member_ids))
return;
// Can have more than one member if you really want...
$member_ids = is_array($member_ids) ? $member_ids : array($member_ids);
// Restrict it right!
$query_where = '
id_member IN ({array_int:member_list})';
$query_where_params = array(
'member_list' => $member_ids,
);
// Then make the query and dump the data.
return ssi_queryMembers($query_where, $query_where_params, '', 'id_member', $output_method);
}
/**
* Get al members in the specified group
* @param int $group_id The ID of the group to get members from
* @param string $output_method The output method. If 'echo', returns a list of group members, otherwise returns an array of info about them.
* @return void|array Displays a list of group members or returns an array of info about them, depending on output_method.
*/
function ssi_fetchGroupMembers($group_id = null, $output_method = 'echo')
{
if ($group_id === null)
return;
$query_where = '
id_group = {int:id_group}
OR id_post_group = {int:id_group}
OR FIND_IN_SET({int:id_group}, additional_groups) != 0';
$query_where_params = array(
'id_group' => $group_id,
);
return ssi_queryMembers($query_where, $query_where_params, '', 'real_name', $output_method);
}
/**
* Pulls info about members based on the specified parameters. Used by other functions to eliminate duplication.
* @param string $query_where The info for the WHERE clause of the query
* @param array $query_where_params The parameters for the WHERE clause
* @param string|int $query_limit The number of rows to return or an empty string to return all
* @param string $query_order The info for the ORDER BY clause of the query
* @param string $output_method The output method. If 'echo', displays a list of members, otherwise returns an array of info about them
* @return void|array Displays a list of members or returns an array of info about them, depending on output_method.
*/
function ssi_queryMembers($query_where = null, $query_where_params = array(), $query_limit = '', $query_order = 'id_member DESC', $output_method = 'echo')
{
global $smcFunc, $memberContext;
if ($query_where === null)
return;
// Fetch the members in question.
$request = $smcFunc['db_query']('', '