forked from SimpleMachines/SMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSI.php
2528 lines (2199 loc) · 95.2 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 https://www.simplemachines.org
* @copyright 2024 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1.4
*/
// Don't do anything if SMF is already loaded.
if (defined('SMF'))
return true;
define('SMF', 'SSI');
define('SMF_VERSION', '2.1.4');
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
define('SMF_SOFTWARE_YEAR', '2024');
define('JQUERY_VERSION', '3.6.3');
define('POSTGRE_TITLE', 'PostgreSQL');
define('MYSQL_TITLE', 'MySQL');
define('SMF_USER_AGENT', 'Mozilla/5.0 (' . php_uname('s') . ' ' . php_uname('m') . ') AppleWebKit/605.1.15 (KHTML, like Gecko) SMF/' . strtr(SMF_VERSION, ' ', '.'));
// Just being safe... Do this before defining globals as otherwise it unsets the global.
foreach (array('db_character_set', 'cachedir') as $variable)
unset($GLOBALS[$variable]);
// We're going to want a few globals... these are all set later.
global $maintenance, $msubject, $mmessage, $mbname, $language;
global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename, $db_character_set;
global $db_type, $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error, $db_show_debug;
global $db_connection, $db_port, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cache_enable, $cachedir;
global $auth_secret, $cache_accelerator, $cache_memcached;
if (!defined('TIME_START'))
define('TIME_START', microtime(true));
// Get the forum's settings for database and file paths.
require_once(dirname(__FILE__) . '/Settings.php');
// Make absolutely sure the cache directory is defined and writable.
if (empty($cachedir) || !is_dir($cachedir) || !is_writable($cachedir))
{
if (is_dir($boarddir . '/cache') && is_writable($boarddir . '/cache'))
$cachedir = $boarddir . '/cache';
else
{
$cachedir = sys_get_temp_dir() . '/smf_cache_' . md5($boarddir);
@mkdir($cachedir, 0750);
}
}
$ssi_error_reporting = error_reporting(!empty($db_show_debug) ? E_ALL : E_ALL & ~E_DEPRECATED);
/* 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');
// Ensure we don't trip over disabled internal functions
if (version_compare(PHP_VERSION, '8.0.0', '>='))
require_once($sourcedir . '/Subs-Compat.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();
/**
* An autoloader for certain classes.
*
* @param string $class The fully-qualified class name.
*/
spl_autoload_register(function($class) use ($sourcedir)
{
$classMap = array(
'ReCaptcha\\' => 'ReCaptcha/',
'MatthiasMullie\\Minify\\' => 'minify/src/',
'MatthiasMullie\\PathConverter\\' => 'minify/path-converter/src/',
'SMF\\Cache\\' => 'Cache/',
);
// Do any third-party scripts want in on the fun?
call_integration_hook('integrate_autoload', array(&$classMap));
foreach ($classMap as $prefix => $dirName)
{
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0)
{
continue;
}
// get the relative class name
$relativeClass = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$fileName = $dirName . strtr($relativeClass, '\\', '/') . '.php';
// if the file exists, require it
if (file_exists($fileName = $sourcedir . '/' . $fileName))
{
require_once $fileName;
return;
}
}
});
// 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($smcFunc['random_int']() . session_id() . $smcFunc['random_int']()), 0, rand(7, 12));
$_SESSION['session_value'] = md5(session_id() . $smcFunc['random_int']());
}
$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);
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();
}
/**
* Show the SMF version.
*
* @param string $output_method If 'echo', displays the version, otherwise returns it
* @return void|string Returns nothing if output_method is 'echo', otherwise returns the version
*/
function ssi_version($output_method = 'echo')
{
if ($output_method == 'echo')
echo SMF_VERSION;
else
return SMF_VERSION;
}
/**
* Show the full SMF version string.
*
* @param string $output_method If 'echo', displays the full version string, otherwise returns it
* @return void|string Returns nothing if output_method is 'echo', otherwise returns the version string
*/
function ssi_full_version($output_method = 'echo')
{
if ($output_method == 'echo')
echo SMF_FULL_VERSION;
else
return SMF_FULL_VERSION;
}
/**
* Show the SMF software year.
*
* @param string $output_method If 'echo', displays the software year, otherwise returns it
* @return void|string Returns nothing if output_method is 'echo', otherwise returns the software year
*/
function ssi_software_year($output_method = 'echo')
{
if ($output_method == 'echo')
echo SMF_SOFTWARE_YEAR;
else
return SMF_SOFTWARE_YEAR;
}
/**
* Show the forum copyright. Only used in our ssi_examples files.
*
* @param string $output_method If 'echo', displays the forum copyright, otherwise returns it
* @return void|string Returns nothing if output_method is 'echo', otherwise returns the copyright string
*/
function ssi_copyright($output_method = 'echo')
{
global $forum_copyright, $scripturl;
if ($output_method == 'echo')
printf($forum_copyright, SMF_FULL_VERSION, SMF_SOFTWARE_YEAR, $scripturl);
else
return sprintf($forum_copyright, SMF_FULL_VERSION, SMF_SOFTWARE_YEAR, $scripturl);
}
/**
* 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'], $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']) && !empty($modSettings['recycle_board']))
$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,
COALESCE(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
COALESCE(lt.id_msg, lmr.id_msg, 0) >= m.id_msg_modified AS is_read,
COALESCE(lt.id_msg, 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)' . ($modSettings['postmod_active'] ? '
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)' : '') . '
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}
AND t.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' => $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 something 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" class="new_posts">' . $txt['new'] . '</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
ml.poster_time, mf.subject, mf.id_topic, ml.id_member, ml.id_msg, t.num_replies, t.num_views, mg.online_color, t.id_last_msg,
COALESCE(mem.real_name, ml.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
COALESCE(lt.id_msg, lmr.id_msg, 0) >= ml.id_msg_modified AS is_read,
COALESCE(lt.id_msg, lmr.id_msg, -1) + 1 AS new_from') . ', SUBSTRING(ml.body, 1, 384) AS body, ml.smileys_enabled, ml.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_first_msg)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ml.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})
ORDER BY t.id_last_msg DESC',
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'] == $recycle_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';
elseif (!isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = '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' => $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 something 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" class="new_posts">' . $txt['new'] . '</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 something 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' : '
(COALESCE(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']) && !empty($modSettings['recycle_board']) ? '
AND b.id_board != {int:recycle_board}' : '') . '
ORDER BY b.num_posts DESC
LIMIT ' . $num_top,
array(
'current_member' => $user_info['id'],
'recycle_board' => !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : null,
)
);
$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 something 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'] . '" class="new_posts">' . $txt['new'] . '</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']) && !empty($modSettings['recycle_board']) ? '
AND b.id_board != {int:recycle_board}' : '') . '
ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
LIMIT {int:limit}',
array(
'topic_list' => $topic_ids,
'is_approved' => 1,
'recycle_board' => !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : null,
'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 something 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.